Skip to content

Commit

Permalink
replace feature flag api call with launch darkly
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaed Parkar committed Oct 26, 2023
1 parent 3e3bd32 commit 5c4f5ea
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
using AdminWebsite.Configuration;
using AdminWebsite.Contracts.Enums;
using AdminWebsite.Extensions;
using AdminWebsite.Mappers;
using AdminWebsite.Models;
using AdminWebsite.Security;
using AdminWebsite.Services;
using AdminWebsite.UnitTests.Helper;
using BookingsApi.Client;
using BookingsApi.Contract.V1.Configuration;
using BookingsApi.Contract.V1.Requests;
using BookingsApi.Contract.V1.Responses;
using BookingsApi.Contract.V2.Enums;
Expand Down Expand Up @@ -97,7 +95,7 @@ public void Setup()
_participantGroupLogger = new Mock<ILogger<HearingsService>>();
_hearingsService = new HearingsService(_bookingsApiClient.Object, _participantGroupLogger.Object, _featureToggle.Object);

_bookingsApiClient.Setup(x => x.GetFeatureFlagAsync(It.Is<string>(f => f == nameof(FeatureFlags.EJudFeature)))).ReturnsAsync(true);
_featureToggle.Setup(x => x.EJudEnabled()).Returns(true);

_controller = new AdminWebsite.Controllers.HearingsController(_bookingsApiClient.Object,
_userIdentity.Object,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using AdminWebsite.Contracts.Responses;
using BookingsApi.Client;
using Autofac.Extras.Moq;
using BookingsApi.Contract.V1.Configuration;
using BookingsApi.Contract.V1.Requests;
using BookingsApi.Contract.V1.Requests.Enums;
using VideoApi.Contract.Responses;
Expand Down Expand Up @@ -61,7 +60,7 @@ public void Setup()
},
CaseTypeName = "Generic"
});
_mocker.Mock<IBookingsApiClient>().Setup(x => x.GetFeatureFlagAsync(It.Is<string>(f => f == nameof(FeatureFlags.EJudFeature)))).ReturnsAsync(true);
_mocker.Mock<IFeatureToggles>().Setup(x => x.EJudEnabled()).Returns(true);
_mocker.Mock<IFeatureToggles>().Setup(e => e.BookAndConfirmToggle()).Returns(true);
_controller = _mocker.Create<AdminWebsite.Controllers.HearingsController>();
}
Expand Down
32 changes: 13 additions & 19 deletions AdminWebsite/AdminWebsite.UnitTests/Services/HearingServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AdminWebsite.Configuration;
using AdminWebsite.Contracts.Enums;
Expand All @@ -9,7 +8,6 @@
using AdminWebsite.Services;
using Autofac.Extras.Moq;
using BookingsApi.Client;
using BookingsApi.Contract.V1.Configuration;
using BookingsApi.Contract.V1.Requests;
using BookingsApi.Contract.V1.Requests.Enums;
using BookingsApi.Contract.V1.Responses;
Expand All @@ -20,7 +18,6 @@
using NUnit.Framework;
using VideoApi.Contract.Responses;
using CaseResponse = BookingsApi.Contract.V1.Responses.CaseResponse;
using EndpointResponse = BookingsApi.Contract.V1.Responses.EndpointResponse;

namespace AdminWebsite.UnitTests.Services
{
Expand All @@ -36,7 +33,7 @@ public class HearingServiceTests
public void Setup()
{
_mocker = AutoMock.GetLoose();
_mocker.Mock<IOptions<KinlyConfiguration>>().Setup(opt => opt.Value).Returns(new KinlyConfiguration()
_mocker.Mock<IOptions<KinlyConfiguration>>().Setup(opt => opt.Value).Returns(new KinlyConfiguration
{
ConferencePhoneNumber = ExpectedTeleConferencePhoneNumber
});
Expand All @@ -58,8 +55,7 @@ public void Setup()
_mocker.Mock<IBookingsApiClient>()
.Setup(c => c.GetHearingsByGroupIdAsync(It.IsAny<Guid>()))
.ReturnsAsync(new List<HearingDetailsResponse> { _hearing });
_mocker.Mock<IBookingsApiClient>()
.Setup(x => x.GetFeatureFlagAsync(It.Is<string>(f => f == nameof(FeatureFlags.EJudFeature)))).ReturnsAsync(true);
_mocker.Mock<IFeatureToggles>().Setup(x => x.EJudEnabled()).Returns(true);
_mocker.Mock<IFeatureToggles>()
.Setup(x => x.BookAndConfirmToggle()).Returns(true);
_service = _mocker.Create<HearingsService>();
Expand Down Expand Up @@ -110,7 +106,7 @@ public void Should_have_one_added_participant()
public async Task Should_process_participants()
{
var existingParticipants = new List<UpdateParticipantRequest>();
var newParticipants = new List<BookingsApi.Contract.V1.Requests.ParticipantRequest>();
var newParticipants = new List<ParticipantRequest>();
var removedParticipantIds = new List<Guid>();
var linkedParticipants = new List<LinkedParticipantRequest>();

Expand All @@ -134,7 +130,7 @@ public async Task Should_process_participants()
public async Task Should_process_new_joh_participant_EJudFeature_Is_ON(string hearingRole)
{
// Arrange
var participant = new EditParticipantRequest()
var participant = new EditParticipantRequest
{
Id = Guid.NewGuid(),
HearingRoleName = hearingRole,
Expand All @@ -155,15 +151,14 @@ public async Task Should_process_new_joh_participant_EJudFeature_Is_ON(string he
public async Task Should_process_new_joh_participant_EJudFeature_Is_OFF(string hearingRole)
{
// Arrange
var participant = new EditParticipantRequest()
var participant = new EditParticipantRequest
{
Id = Guid.NewGuid(),
HearingRoleName = hearingRole,
ContactEmail = "[email protected]"
};
var removedParticipantIds = new List<Guid>();
_mocker.Mock<IBookingsApiClient>()
.Setup(x => x.GetFeatureFlagAsync(It.Is<string>(f => f == nameof(FeatureFlags.EJudFeature)))).ReturnsAsync(false);
_mocker.Mock<IFeatureToggles>().Setup(x => x.EJudEnabled()).Returns(false);

// Act
var newParticipant = await _service.ProcessNewParticipant(_hearing.Id, participant, removedParticipantIds, _hearing.Map());
Expand All @@ -177,15 +172,14 @@ public async Task Should_process_new_joh_participant_EJudFeature_Is_OFF(string h
public async Task Should_process_new_judge_participant_EJudFeature_Is_OFF()
{
// Arrange
var participant = new EditParticipantRequest()
var participant = new EditParticipantRequest
{
Id = Guid.NewGuid(),
CaseRoleName = "Judge",
ContactEmail = "[email protected]"
};
var removedParticipantIds = new List<Guid>();
_mocker.Mock<IBookingsApiClient>()
.Setup(x => x.GetFeatureFlagAsync(It.Is<string>(f => f == nameof(FeatureFlags.EJudFeature)))).ReturnsAsync(false);
_mocker.Mock<IFeatureToggles>().Setup(x => x.EJudEnabled()).Returns(false);

// Act
var newParticipant = await _service.ProcessNewParticipant(_hearing.Id, participant, removedParticipantIds, _hearing.Map());
Expand All @@ -199,14 +193,14 @@ public async Task Should_process_new_judge_participant_EJudFeature_Is_OFF()
public async Task Should_NOT_process_new_joh_participant_when_participant_is_in_list_and_NOT_removed()
{
// Arrange
var participant = new EditParticipantRequest()
var participant = new EditParticipantRequest
{
Id = Guid.NewGuid(),
HearingRoleName = "Panel Member",
ContactEmail = "[email protected]"
};

_hearing.Participants.Add(new ParticipantResponse()
_hearing.Participants.Add(new ParticipantResponse
{
Id = participant.Id.Value,
Username = participant.ContactEmail,
Expand All @@ -226,14 +220,14 @@ public async Task Should_NOT_process_new_joh_participant_when_participant_is_in_
public async Task Should_process_new_joh_participant_when_participant_is_in_list_and_is_removed()
{
// Arrange
var participant = new EditParticipantRequest()
var participant = new EditParticipantRequest
{
Id = Guid.NewGuid(),
CaseRoleName = "Judge",
ContactEmail = "[email protected]"
};

_hearing.Participants.Add(new ParticipantResponse()
_hearing.Participants.Add(new ParticipantResponse
{
Id = participant.Id.Value,
Username = participant.ContactEmail,
Expand All @@ -257,7 +251,7 @@ public async Task Should_process_new_joh_participant_when_participant_is_in_list
public async Task Should_process_non_joh_participant()
{
// Arrange
var participant = new EditParticipantRequest()
var participant = new EditParticipantRequest
{
Id = Guid.NewGuid(),
CaseRoleName = "NOT JUDGE",
Expand Down
28 changes: 15 additions & 13 deletions AdminWebsite/AdminWebsite/Configuration/FeatureToggles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public interface IFeatureToggles
public bool BookAndConfirmToggle();
public bool Dom1Enabled();
public bool ReferenceDataToggle();
public bool EJudEnabled();
}

public class FeatureToggles : IFeatureToggles
Expand All @@ -21,6 +22,7 @@ public class FeatureToggles : IFeatureToggles
private const string BookAndConfirmToggleKey = "Book_and_Confirm";
private const string Dom1EnabledToggleKey = "dom1";
private const string ReferenceDataToggleKey = "reference-data";
private const string EJudFeatureToggleKey = "ejud-feature";

public FeatureToggles(string sdkKey, string environmentName)
{
Expand All @@ -32,32 +34,32 @@ public FeatureToggles(string sdkKey, string environmentName)

public bool BookAndConfirmToggle()
{
if (!_ldClient.Initialized)
{
throw new InvalidOperationException("LaunchDarkly client not initialized");
}

return _ldClient.BoolVariation(BookAndConfirmToggleKey, _context);
return GetBoolValueWithKey(BookAndConfirmToggleKey);
}

public bool Dom1Enabled()
{
if (!_ldClient.Initialized)
{
throw new InvalidOperationException("LaunchDarkly client not initialized");
}

return _ldClient.BoolVariation(Dom1EnabledToggleKey, _context);
return GetBoolValueWithKey(Dom1EnabledToggleKey);
}

public bool ReferenceDataToggle()
{
return GetBoolValueWithKey(ReferenceDataToggleKey);
}

public bool EJudEnabled()
{
return GetBoolValueWithKey(EJudFeatureToggleKey);
}

private bool GetBoolValueWithKey(string key)
{
if (!_ldClient.Initialized)
{
throw new InvalidOperationException("LaunchDarkly client not initialized");
}

return _ldClient.BoolVariation(ReferenceDataToggleKey, _context);
return _ldClient.BoolVariation(key, _context);
}
}
}
13 changes: 7 additions & 6 deletions AdminWebsite/AdminWebsite/Services/HearingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using AdminWebsite.Configuration;
using AdminWebsite.Contracts.Responses;
using BookingsApi.Contract.Interfaces.Requests;
using BookingsApi.Contract.V1.Configuration;
using BookingsApi.Contract.V1.Requests;
using BookingsApi.Contract.V2.Requests;

Expand All @@ -30,12 +29,14 @@ public interface IHearingsService
public class HearingsService : IHearingsService
{
private readonly IBookingsApiClient _bookingsApiClient;
private readonly IFeatureToggles _featureToggles;
private readonly ILogger<HearingsService> _logger;
#pragma warning disable S107
public HearingsService(IBookingsApiClient bookingsApiClient, ILogger<HearingsService> logger, IFeatureToggles featureFlag)
public HearingsService(IBookingsApiClient bookingsApiClient, ILogger<HearingsService> logger, IFeatureToggles featureToggles)
{
_bookingsApiClient = bookingsApiClient;
_logger = logger;
_featureToggles = featureToggles;
}

public void AssignEndpointDefenceAdvocates(List<Contracts.Requests.EndpointRequest> endpointsWithDa, IReadOnlyCollection<Contracts.Requests.ParticipantRequest> participants)
Expand Down Expand Up @@ -107,7 +108,7 @@ public async Task<ParticipantRequest> ProcessNewParticipant(
return (ParticipantRequest)await ProcessNewParticipant(hearingId, participant, newParticipant, removedParticipantIds, hearing);
}

public async Task<IParticipantRequest> ProcessNewParticipant(
public Task<IParticipantRequest> ProcessNewParticipant(
Guid hearingId,
EditParticipantRequest participant,
IParticipantRequest newParticipant,
Expand All @@ -116,7 +117,7 @@ public async Task<IParticipantRequest> ProcessNewParticipant(
{
// Add a new participant
// Map the request except the username
var ejudFeatureFlag = await _bookingsApiClient.GetFeatureFlagAsync(nameof(FeatureFlags.EJudFeature));
var ejudFeatureFlag = _featureToggles.EJudEnabled();

if ((ejudFeatureFlag && (participant.CaseRoleName == RoleNames.Judge
|| participant.HearingRoleName == RoleNames.PanelMember
Expand All @@ -127,7 +128,7 @@ public async Task<IParticipantRequest> ProcessNewParticipant(
hearing.Participants.Exists(p => p.ContactEmail.Equals(participant.ContactEmail) && removedParticipantIds.TrueForAll(removedParticipantId => removedParticipantId != p.Id)))
{
//If the judge already exists in the database, there is no need to add again.
return null;
return Task.FromResult<IParticipantRequest>(null);
}

if (newParticipant is ParticipantRequest v1Request)
Expand All @@ -138,7 +139,7 @@ public async Task<IParticipantRequest> ProcessNewParticipant(

_logger.LogDebug("Adding participant {Participant} to hearing {Hearing}",
newParticipant.DisplayName, hearingId);
return newParticipant;
return Task.FromResult(newParticipant);
}

public async Task ProcessEndpoints(Guid hearingId, EditHearingRequest request, HearingDetailsResponse hearing,
Expand Down

0 comments on commit 5c4f5ea

Please sign in to comment.