using FluentAssertions; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Moq; using NUnit.Framework; using Strata.ContinuousImprovement.Api.Controllers; using Strata.ContinuousImprovement.Biz.Configuration; using Strata.ContinuousImprovement.Biz.DbContexts; using Strata.ContinuousImprovement.Biz.Exploration; using Strata.ContinuousImprovement.Biz.Exploration.CaseTypeFamilies; using Strata.ContinuousImprovement.Biz.Exploration.Filters; using Strata.ContinuousImprovement.JazzEntityFrameworkStub; using Strata.FeatureFlags.Client; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace Strata.ContinuousImprovement.Api.Test.Unit.Controllers { [TestFixture, Category("Unit"), Category("Controller")] public class ExplorationControllerTests { public JazzEntityFrameworkFactory JazzEntityFrameworkFactory { get; set; } public JazzDbContext JazzDbContext { get; set; } public ExplorationController ExplorationController { get; set; } public IConfigurationService ConfigurationService { get; set; } public IExplorationService ExplorationService { get; set; } public IFeatureFlagServiceClient FeatureFlagServiceClient { get; set; } [OneTimeSetUp] public async Task ClientNetworkOpportunityLinkControllerTestsSetup() { // Arrange JazzEntityFrameworkFactory = new JazzEntityFrameworkFactory(); JazzDbContext = await JazzEntityFrameworkFactory.CreateDbContextAsync(nameof(ConfigurationsControllerTests), CancellationToken.None); ConfigurationService = Mock.Of(); ExplorationService = Mock.Of(); FeatureFlagServiceClient = Mock.Of(); // Act } [SetUp] public async Task Setup() { // Arrange ExplorationController = new ExplorationController(ExplorationService, ConfigurationService, FeatureFlagServiceClient, Mock.Of>()); // Act } [Test] public async Task ExplorationControllerTest() { // Assert ExplorationController.Should().NotBeNull(); } [TestCaseSource(nameof(GetCaseTypeFamilySummariesTestCases))] public async Task GetCaseTypeFamilySummariesTest(Guid configurationGuid) { // Arrange var configuration = JazzDbContext.Configurations.First(); var explorationInfo = new ExplorationInfo { ConfigurationGuid = JazzEntityFrameworkFactory.CurrentConfigurationGuid, ExclusionCriteria = new Biz.Exploration.Filters.ExclusionCriteria(), Filters = new Biz.Exploration.Filters.Filter { DischargeDateStart = new System.DateTime(2024, 1, 1), DischargeDateEnd = new System.DateTime(2024, 12, 31), FilterChipItems = new List() } }; var result = new List(); if (configuration.ConfigurationGuid == configurationGuid) { Mock.Get(ConfigurationService) .Setup(s => s.GetById(It.IsAny(), It.IsAny())) .Returns( (configurationGuid, token) => Task.FromResult(JazzDbContext.Configurations.First())); Mock.Get(ConfigurationService) .Setup(s => s.Contains(It.IsAny(), It.IsAny())) .Returns( (configuration, token) => Task.FromResult(true)); Mock.Get(ConfigurationService) .Setup(s => s.IsConfigurationValid(It.IsAny(), It.IsAny())) .Returns( (configurationGuid, token) => Task.FromResult(true)); } else { Mock.Get(ConfigurationService) .Setup(s => s.GetById(It.IsAny(), It.IsAny())) .Returns( (configurationGuid, token) => Task.FromResult(default(Strata.ContinuousImprovement.Biz.Configuration.Configuration))); Mock.Get(ConfigurationService) .Setup(s => s.Contains(It.IsAny(), It.IsAny())) .Returns( (configuration, token) => Task.FromResult(false)); Mock.Get(ConfigurationService) .Setup(s => s.IsConfigurationValid(It.IsAny(), It.IsAny())) .Returns( (configurationGuid, token) => Task.FromResult(false)); } Mock.Get(ExplorationService) .Setup(s => s.GetCaseTypeFamilySummariesAsync(It.IsAny(), It.IsAny())) .Returns( (explorationInfo, token) => Task.FromResult(result.AsEnumerable())); // Act var controllerResponse = await ExplorationController.GetCaseTypeFamilySummaries(explorationInfo, CancellationToken.None); // Assert controllerResponse.Should().NotBeNull(); if (controllerResponse.Result is OkObjectResult okResult) { okResult.Should().NotBeNull(); var value = okResult.Value as IEnumerable; value.Should().NotBeNull(); } else { controllerResponse.Result.Should().BeOfType(); } } [TestCaseSource(nameof(GetCaseTypeFamilyDetailsTestCases))] public async Task GetCaseTypeFamilyDetailsTest(Guid configurationGuid) { // Arrange var configuration = JazzDbContext.Configurations.First(); var explorationInfo = new ExplorationDetailInfo { ConfigurationGuid = JazzEntityFrameworkFactory.CurrentConfigurationGuid, CaseTypeFamilyId = 0, ExclusionCriteria = new Biz.Exploration.Filters.ExclusionCriteria(), Filters = new Biz.Exploration.Filters.Filter { DischargeDateStart = new System.DateTime(2024, 1, 1), DischargeDateEnd = new System.DateTime(2024, 12, 31), FilterChipItems = new List() } }; var result = new CaseTypeFamilyDetailInfo { Metrics = new CaseTypeFamilyMetric(), CaseTypeFamilyDetails = Enumerable.Empty() }; if (configuration.ConfigurationGuid == configurationGuid) { Mock.Get(ConfigurationService) .Setup(s => s.GetById(It.IsAny(), It.IsAny())) .Returns( (configurationGuid, token) => Task.FromResult(JazzDbContext.Configurations.First())); Mock.Get(ConfigurationService) .Setup(s => s.Contains(It.IsAny(), It.IsAny())) .Returns( (configuration, token) => Task.FromResult(true)); Mock.Get(ConfigurationService) .Setup(s => s.IsConfigurationValid(It.IsAny(), It.IsAny())) .Returns( (configurationGuid, token) => Task.FromResult(true)); } else { Mock.Get(ConfigurationService) .Setup(s => s.GetById(It.IsAny(), It.IsAny())) .Returns( (configurationGuid, token) => Task.FromResult(default(Strata.ContinuousImprovement.Biz.Configuration.Configuration))); Mock.Get(ConfigurationService) .Setup(s => s.Contains(It.IsAny(), It.IsAny())) .Returns( (configuration, token) => Task.FromResult(false)); Mock.Get(ConfigurationService) .Setup(s => s.IsConfigurationValid(It.IsAny(), It.IsAny())) .Returns( (configurationGuid, token) => Task.FromResult(false)); } Mock.Get(ExplorationService) .Setup(s => s.GetCaseTypeFamilyDetailsAsync(It.IsAny(), It.IsAny())) .Returns( (exploration, token) => Task.FromResult(result)); // Act var controllerResponse = await ExplorationController.GetCaseTypeFamilyDetails(explorationInfo, CancellationToken.None); // Assert controllerResponse.Should().NotBeNull(); if (controllerResponse.Result is OkObjectResult okResult) { okResult.Should().NotBeNull(); okResult.Value.Should().BeOfType(); configuration.ConfigurationGuid.Should().Be(configurationGuid); } else if (controllerResponse.Result is BadRequestResult badRequestResult) { badRequestResult.Should().NotBeNull(); } } public static IEnumerable GetCaseTypeFamilySummariesTestCases() { var configurationGuid = JazzEntityFrameworkFactory.CurrentConfigurationGuid; yield return new TestCaseData(configurationGuid) .SetName("summaries valid configuration"); yield return new TestCaseData(Guid.NewGuid()) .SetName("summaries invalid configuration"); } public static IEnumerable GetCaseTypeFamilyDetailsTestCases() { var configurationGuid = JazzEntityFrameworkFactory.CurrentConfigurationGuid; yield return new TestCaseData(configurationGuid) .SetName("details valid configuration"); yield return new TestCaseData(Guid.NewGuid()) .SetName("details invalid configuration"); } } }