Beispiel #1
0
        public void MapModels(AppRegistrationModel?appRegistrationModel, LegacyPathModel?legacyPathModel)
        {
            _ = appRegistrationModel ?? throw new ArgumentNullException(nameof(appRegistrationModel));
            _ = legacyPathModel ?? throw new ArgumentNullException(nameof(legacyPathModel));

            mapper.Map(legacyPathModel, appRegistrationModel);
        }
Beispiel #2
0
        public async Task UpdateHashCodesAsync(AppRegistrationModel?appRegistrationModel, string?cdnLocation)
        {
            _ = appRegistrationModel ?? throw new ArgumentNullException(nameof(appRegistrationModel));

            if (string.IsNullOrWhiteSpace(cdnLocation))
            {
                throw new ArgumentNullException(nameof(cdnLocation));
            }

            logger.LogInformation($"Attempting to update JavaScript hash codes in app registration for: {appRegistrationModel.Path}");

            var updatedHashcodeCount = await RefreshHashcodesAsync(appRegistrationModel, cdnLocation).ConfigureAwait(false);

            if (updatedHashcodeCount > 0)
            {
                appRegistrationModel.LastModifiedDate = DateTime.UtcNow;

                var statusCode = await documentService.UpsertAsync(appRegistrationModel).ConfigureAwait(false);

                if (statusCode == HttpStatusCode.OK)
                {
                    logger.LogInformation($"Updated ({updatedHashcodeCount}) app registration JavaScript hash codes for: {appRegistrationModel.Path}");
                }
                else
                {
                    logger.LogError($"Error updating app registration JavaScript hash codes for: {appRegistrationModel.Path}: Status code {statusCode}");
                }
            }
            else
            {
                logger.LogInformation($"No app registration JavaScript hash codes updated for: {appRegistrationModel.Path}");
            }
        }
Beispiel #3
0
        public void ValidateModelReturnsExceptionForNullAppRegistrationModel()
        {
            // Arrange
            AppRegistrationModel?appRegistrationModel = null;

            // Act
            var exceptionResult = Assert.Throws <ArgumentNullException>(() => modelValidationService.ValidateModel(appRegistrationModel));

            // assert
            Assert.Equal("Value cannot be null. (Parameter 'appRegistrationModel')", exceptionResult.Message);
        }
Beispiel #4
0
        public async Task <int> RefreshHashcodesAsync(AppRegistrationModel?appRegistrationModel, string?cdnLocation)
        {
            _ = appRegistrationModel ?? throw new ArgumentNullException(nameof(appRegistrationModel));

            if (string.IsNullOrWhiteSpace(cdnLocation))
            {
                throw new ArgumentNullException(nameof(cdnLocation));
            }

            int updatedHashcodeCount = 0;

            foreach (var dict in new List <Dictionary <string, string?> > {
                appRegistrationModel.CssScriptNames !, appRegistrationModel.JavaScriptNames !
            })
        public async Task UpdateAppRegistrationAsyncReturnsExceptionForNullAppRegistrationModel()
        {
            // Arrange
            AppRegistrationModel?dummyAppRegistrationModel = null;

            // Act
            var exceptionResult = await Assert.ThrowsAsync <ArgumentNullException>(async() => await legacyDataLoadService.UpdateAppRegistrationAsync(dummyAppRegistrationModel).ConfigureAwait(false)).ConfigureAwait(false);

            // assert
            A.CallTo(() => fakeModelValidationService.ValidateModel(A <AppRegistrationModel> .Ignored)).MustNotHaveHappened();
            A.CallTo(() => fakeDocumentService.UpsertAsync(A <AppRegistrationModel> .Ignored)).MustNotHaveHappened();

            Assert.Equal("Value cannot be null. (Parameter 'appRegistrationModel')", exceptionResult.Message);
        }
Beispiel #6
0
        public void MapModelsPairReturnsExceptionForNullAppRegistrationModel()
        {
            // Arrange
            AppRegistrationModel?appRegistrationModel = null;
            var legacyPathModel = ModelBuilders.ValidLegacyPathModel(ModelBuilders.PathName);

            // Act
            var exceptionResult = Assert.Throws <ArgumentNullException>(() => modelMappingService.MapModels(appRegistrationModel, legacyPathModel));

            // assert
            A.CallTo(() => fakeMapper.Map(legacyPathModel, appRegistrationModel)).MustNotHaveHappened();

            Assert.Equal("Value cannot be null. (Parameter 'appRegistrationModel')", exceptionResult.Message);
        }
        public async Task RefreshHashcodesAsyncReturnsExceptionWhenNoAppRegistrations()
        {
            // arrange
            AppRegistrationModel?nullAppRegistrationModel = null;

            var service = BuildServiceToTest();

            // act
            var exceptionResult = await Assert.ThrowsAsync <ArgumentNullException>(async() => await service.RefreshHashcodesAsync(nullAppRegistrationModel, DefaultCdnLocation).ConfigureAwait(false)).ConfigureAwait(false);

            // assert
            A.CallTo(() => fakeDocumentService.UpsertAsync(A <AppRegistrationModel> .Ignored)).MustNotHaveHappened();

            Assert.Equal("Value cannot be null. (Parameter 'appRegistrationModel')", exceptionResult.Message);
        }
Beispiel #8
0
        public async Task PagesDataLoadServiceLoadAsyncNullRegistrationDocumentNoUpdate()
        {
            // Arrange
            AppRegistrationModel?model = null;
            var serviceToTest          = new PagesDataLoadService(logger, fakeHttpClient, pagesClientOptions, fakeApiDataService, fakeDataLoadService);

            A.CallTo(() => fakeDataLoadService.GetAppRegistrationByPathAsync(A <string> .Ignored)).Returns(model);

            // Act
            await serviceToTest.LoadAsync().ConfigureAwait(false);

            // Assert
            A.CallTo(() => fakeDataLoadService.GetAppRegistrationByPathAsync(A <string> .Ignored)).MustHaveHappenedOnceExactly();
            A.CallTo(() => fakeDataLoadService.UpdateAppRegistrationAsync(A <AppRegistrationModel> .Ignored)).MustNotHaveHappened();
            A.CallTo(() => fakeApiDataService.GetAsync <PageLocationModel>(A <HttpClient> .Ignored, A <Uri> .Ignored)).MustNotHaveHappened();
        }
Beispiel #9
0
        public void MapModels(AppRegistrationModel?appRegistrationModel, LegacyPathModel?legacyPathModel, IList <LegacyRegionModel>?legacyRegionModels)
        {
            _ = appRegistrationModel ?? throw new ArgumentNullException(nameof(appRegistrationModel));
            _ = legacyPathModel ?? throw new ArgumentNullException(nameof(legacyPathModel));

            MapModels(appRegistrationModel, legacyPathModel);

            if (legacyRegionModels != null)
            {
                appRegistrationModel.Regions = mapper.Map <List <RegionModel> >(legacyRegionModels);
            }
            else
            {
                appRegistrationModel.Regions = null;
            }
        }
        public bool ValidateModel(AppRegistrationModel?appRegistrationModel)
        {
            _ = appRegistrationModel ?? throw new ArgumentNullException(nameof(appRegistrationModel));

            var validationContext = new System.ComponentModel.DataAnnotations.ValidationContext(appRegistrationModel, null, null);
            var validationResults = new List <ValidationResult>();
            var isValid           = Validator.TryValidateObject(appRegistrationModel, validationContext, validationResults, true);

            if (!isValid && validationResults.Any())
            {
                foreach (var validationResult in validationResults)
                {
                    logger.LogError($"Error validating {appRegistrationModel.Path}: {string.Join(",", validationResult.MemberNames)} - {validationResult.ErrorMessage}");
                }
            }

            return(isValid);
        }
Beispiel #11
0
        public async Task PagesDataLoadServiceRemoveAsyncNullRegistrationDocumentNoUpdate()
        {
            // Arrange
            const HttpStatusCode expectedResult = HttpStatusCode.NotFound;
            AppRegistrationModel?model          = null;
            var serviceToTest = new PagesDataLoadService(logger, fakeHttpClient, pagesClientOptions, fakeApiDataService, fakeDataLoadService);

            A.CallTo(() => fakeDataLoadService.GetAppRegistrationByPathAsync(A <string> .Ignored)).Returns(model);

            // Act
            var result = await serviceToTest.RemoveAsync(Guid.NewGuid()).ConfigureAwait(false);

            // Assert
            A.CallTo(() => fakeDataLoadService.GetAppRegistrationByPathAsync(A <string> .Ignored)).MustHaveHappenedOnceExactly();
            A.CallTo(() => fakeDataLoadService.UpdateAppRegistrationAsync(A <AppRegistrationModel> .Ignored)).MustNotHaveHappened();
            A.CallTo(() => fakeApiDataService.GetAsync <PageLocationModel>(A <HttpClient> .Ignored, A <Uri> .Ignored)).MustNotHaveHappened();

            Assert.Equal(expectedResult, result);
        }
Beispiel #12
0
        public void MapRegionModelToAppRegistration(AppRegistrationModel?appRegistrationModel, LegacyRegionModel?legacyRegionModel)
        {
            _ = appRegistrationModel ?? throw new ArgumentNullException(nameof(appRegistrationModel));
            _ = legacyRegionModel ?? throw new ArgumentNullException(nameof(legacyRegionModel));

            if (appRegistrationModel.Regions == null)
            {
                appRegistrationModel.Regions = new List <RegionModel>();
            }

            var model = mapper.Map <RegionModel>(legacyRegionModel);

            var regionToUpdate = appRegistrationModel.Regions.FirstOrDefault(x => x.PageRegion == legacyRegionModel.PageRegion);

            if (regionToUpdate == null)
            {
                appRegistrationModel.Regions.Add(model);
            }
            else
            {
                appRegistrationModel.Regions.Remove(regionToUpdate);
                appRegistrationModel.Regions.Add(model);
            }
        }