using FluentAssertions; using Microsoft.EntityFrameworkCore; using Moq; using NUnit.Framework; using Strata.ContinuousImprovement.Api.Controllers; using Strata.ContinuousImprovement.Biz.Configuration; using Strata.ContinuousImprovement.Biz.DbContexts; using Strata.ContinuousImprovement.JazzEntityFrameworkStub; using Strata.Schema.Client; using Strata.Schema.Client.Dtos.Info; 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 ConfigurationsControllerTests { public JazzDbContext DbContext { get; set; } public IEnumerable Configurations { get; set; } public ISchemaServiceClient SchemaService { get; set; } public IConfigurationService ConfigurationService { get; set; } [OneTimeSetUp] public async Task ConfigurationsControllerTestSetup() { var jeff = new JazzEntityFrameworkFactory(); DbContext = await jeff.CreateDbContextAsync(nameof(ConfigurationsControllerTests), CancellationToken.None); Configurations = await DbContext.Configurations.ToListAsync(); var configuration = Configurations.First(); var entities = new HashSet(configuration.ConfiguredEntityIds); var expected = entities.Select(e => new Member { Id = e, Name = $"Entity {e}" }); var dimensionInfo = new ScoreDimensionInfoDto { FriendlyName = "Entity", DefaultHierarchyGuid = Guid.NewGuid(), DimensionGuid = Guid.NewGuid(), ParentDimensionGuid = Guid.NewGuid() }; ConfigurationService = Mock.Of(); Mock.Get(ConfigurationService) .Setup(s => s.GetConfigurationsAsync(It.IsAny())) .Returns(Task.FromResult(Configurations.AsEnumerable())); Mock.Get(ConfigurationService) .Setup(s => s.GetConfigurationAsync(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(configuration)); SchemaService = Mock.Of(); Mock.Get(SchemaService) .Setup(s => s.GetDimensionMembersByGlobalIdAsync(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(expected)); Mock.Get(SchemaService) .Setup(s => s.GetDimensionByGlobalIdAsync(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(dimensionInfo)); Mock.Get(SchemaService) .Setup(s => s.GetDimensionByIdAsync(It.IsAny(), It.IsAny())) .Returns(Task.FromResult(dimensionInfo)); } [Test()] public void ConfigurationsControllerTest() { var controller = new ConfigurationsController(ConfigurationService, SchemaService); controller.Should().NotBeNull(); } [Test()] public async Task GetAllConfigurationsTest() { var controller = new ConfigurationsController(ConfigurationService, SchemaService); controller.Should().NotBeNull(); var result = await controller.GetAllConfigurations(CancellationToken.None); result.Should().BeEquivalentTo(Configurations); } [Test()] public async Task GetConfigurationEntitiesTest() { var configuration = Configurations.First(); var entities = new HashSet(configuration.ConfiguredEntityIds); var expected = entities.Select(e => new Member { Id = e, Name = $"Entity {e}" }); var controller = new ConfigurationsController(ConfigurationService, SchemaService); var result = await controller.GetConfigurationEntities(configuration.ConfigurationGuid, CancellationToken.None); result.Should().BeEquivalentTo(expected); } [Test()] public async Task GetConfigurationEntityInfoTest() { var configuration = Configurations.First(); var entities = new HashSet(configuration.ConfiguredEntityIds); var controller = new ConfigurationsController(ConfigurationService, SchemaService); var result = await controller.GetConfigurationEntityInfo(configuration.ConfigurationGuid, CancellationToken.None); result.Should().NotBeNull() .And.BeOfType(); result.EntityTitle.Should().NotBeNullOrEmpty(); result.EntityTitle.Should().Be("Entities"); result.EntityText.Should().Be("Entity"); result.DepartmentRollupGlobalId.Should().Be("Entity"); result.HierarchicalGuid.Should().NotBeEmpty(); } [Test()] public async Task GetConfigurationServiceLineInfoTest() { var configuration = Configurations.First(); var controller = new ConfigurationsController(ConfigurationService, SchemaService); var result = await controller.GetConfigurationServiceLineInfo(configuration.ConfigurationGuid, CancellationToken.None); result.Should().NotBeNull() .And.BeOfType(); result.ServiceLineName.Should().NotBeNullOrEmpty(); result.ServiceLineName.Should().Be("Entity"); result.ServiceLineTitle.Should().Be("Entities"); } } }