public void GetCalculationSummariesForSpecification_WhenGivenASpecificationIdInValidFormat_ShouldReturnResult()
        {
            // Arrange
            List <Common.ApiClient.Calcs.Models.CalculationSummary> summaryModels = new List <Common.ApiClient.Calcs.Models.CalculationSummary>()
            {
                new Common.ApiClient.Calcs.Models.CalculationSummary()
                {
                    Name            = "TestCalc",
                    CalculationType = Common.ApiClient.Calcs.Models.CalculationType.Template,
                    Id = "CalcId"
                }
            };

            ICalculationsApiClient mockApi = Substitute.For <ICalculationsApiClient>();

            mockApi
            .GetCalculationSummariesForSpecification(Arg.Any <string>())
            .Returns(new ApiResponse <IEnumerable <Common.ApiClient.Calcs.Models.CalculationSummary> >(HttpStatusCode.OK, summaryModels));

            CalculationsRepository calculationsRepository = new CalculationsRepository(mockApi, CreateMapper());

            // Act
            var configuredTaskAwaiter = calculationsRepository.GetCalculationSummariesForSpecification("Test").ConfigureAwait(false).GetAwaiter();
            List <CalculationSummaryModel> calculationSummaryModels = configuredTaskAwaiter.GetResult().ToList();

            // Assert
            calculationSummaryModels.Should().NotBeNull();
            calculationSummaryModels.Should().BeEquivalentTo(summaryModels);
            mockApi.Received(1).GetCalculationSummariesForSpecification(Arg.Any <string>());
        }
        public void GetBuildProjectBySpecificationId_WhenSpeficationIdIsInValidFormat_ShouldReturnResult()
        {
            // Arrange
            ICalculationsApiClient mockApi = Substitute.For <ICalculationsApiClient>();

            mockApi
            .GetBuildProjectBySpecificationId(Arg.Any <string>())
            .Returns(new ApiResponse <Common.ApiClient.Calcs.Models.BuildProject>(HttpStatusCode.OK, new Common.ApiClient.Calcs.Models.BuildProject()));

            CalculationsRepository calculationsRepository = new CalculationsRepository(mockApi, CreateMapper());

            // Act
            var          configuredTaskAwaiter = calculationsRepository.GetBuildProjectBySpecificationId("Test").ConfigureAwait(false).GetAwaiter();
            BuildProject buildProject          = configuredTaskAwaiter.GetResult();

            // Assert
            buildProject.Should().NotBeNull();
            mockApi.Received(1).GetBuildProjectBySpecificationId(Arg.Any <string>());
        }
        public async Task OnGet_WhenCalculationExistsThenCalculationReturned()
        {
            // Arrange
            ICalculationsApiClient calcsClient = Substitute.For <ICalculationsApiClient>();
            ISpecsApiClient        specsClient = Substitute.For <ISpecsApiClient>();
            IMapper mapper = MappingHelper.CreateFrontEndMapper();

            const string calculationId     = "5";
            const string specificationId   = "specId";
            const string specificationName = "Spec Name";

            Calculation calcsCalculation = new Calculation()
            {
                Id = calculationId,
                CalculationSpecification = new Reference(calculationId, "Test Calculation Specification"),
                SpecificationId          = specificationId,
                SourceCode = "Test Source Code"
            };

            Clients.SpecsClient.Models.CalculationCurrentVersion specsCalculation = new Clients.SpecsClient.Models.CalculationCurrentVersion()
            {
                Id          = calculationId,
                Name        = "Specs Calculation",
                Description = "Spec Description",
            };

            calcsClient
            .GetCalculationById(calculationId)
            .Returns(new ApiResponse <Calculation>(System.Net.HttpStatusCode.OK, calcsCalculation));

            specsClient
            .GetCalculationById(calcsCalculation.SpecificationId, calculationId)
            .Returns(new ApiResponse <Clients.SpecsClient.Models.CalculationCurrentVersion>(System.Net.HttpStatusCode.OK, specsCalculation));

            Clients.SpecsClient.Models.SpecificationSummary specificationSummary = new Clients.SpecsClient.Models.SpecificationSummary()
            {
                Id   = specificationId,
                Name = specificationName
            };

            specsClient
            .GetSpecificationSummary(Arg.Is(specificationId))
            .Returns(new ApiResponse <Clients.SpecsClient.Models.SpecificationSummary>(System.Net.HttpStatusCode.OK, specificationSummary));

            EditCalculationPageModel pageModel = CreatePageModel(specsClient: specsClient, calcsClient: calcsClient, mapper: mapper);

            // Act
            IActionResult result = await pageModel.OnGet(calculationId);

            // Assert
            result.Should().NotBeNull();
            result.Should().BeOfType <PageResult>();

            pageModel.Calculation.Should().NotBeNull();
            pageModel.Calculation.Name.Should().Be(calcsCalculation.Name);
            pageModel.Calculation.Description.Should().Be(specsCalculation.Description);
            pageModel.SpecificationId.Should().Be(calcsCalculation.SpecificationId);
            pageModel.EditModel.SourceCode.Should().Be(calcsCalculation.SourceCode);
            pageModel.SpecificationName.Should().Be(specificationName);

            await calcsClient
            .Received(1)
            .GetCalculationById(Arg.Is(calculationId));

            await specsClient
            .Received(1)
            .GetSpecificationSummary(Arg.Is(specificationId));

            await specsClient
            .Received(1)
            .GetCalculationById(Arg.Is(specificationId), Arg.Is(calculationId));
        }