コード例 #1
0
        public async Task PagesDataLoadServiceRemoveAsyncRegistrationDocumentUpdated()
        {
            // Arrange
            const HttpStatusCode expectedResult = HttpStatusCode.OK;
            var testGuid = Guid.NewGuid();
            AppRegistrationModel model = new AppRegistrationModel {
                Path = "/pages", PageLocations = new Dictionary <Guid, PageLocationModel> {
                    { testGuid, new PageLocationModel {
                          Locations = new List <string> {
                              "http://somewhere.com/a-place-a-page"
                          }
                      } }
                }
            };
            var serviceToTest = new PagesDataLoadService(logger, fakeHttpClient, pagesClientOptions, fakeApiDataService, fakeDataLoadService);

            A.CallTo(() => fakeDataLoadService.GetAppRegistrationByPathAsync(A <string> .Ignored)).Returns(model);
            A.CallTo(() => fakeApiDataService.GetAsync <PageLocationModel>(A <HttpClient> .Ignored, A <Uri> .Ignored)).Returns(PageLocationModels.FirstOrDefault().Value);

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

            // Assert
            A.CallTo(() => fakeDataLoadService.GetAppRegistrationByPathAsync(A <string> .Ignored)).MustHaveHappenedOnceExactly();
            A.CallTo(() => fakeDataLoadService.UpdateAppRegistrationAsync(A <AppRegistrationModel> .Ignored)).MustHaveHappenedOnceExactly();

            Assert.Equal(expectedResult, result);
        }
        public async Task RefreshHashcodesAsyncReturnsSuccessfulZeroCount()
        {
            // arrange
            var appRegistrationModel = new AppRegistrationModel
            {
                Path            = "a-path",
                CssScriptNames  = new Dictionary <string, string?> {
                },
                JavaScriptNames = new Dictionary <string, string?> {
                },
            };
            int expectedCount = appRegistrationModel.CssScriptNames.Count + appRegistrationModel.JavaScriptNames.Count;

            A.CallTo(() => fakeDocumentService.UpsertAsync(A <AppRegistrationModel> .Ignored)).Returns(HttpStatusCode.OK);

            var service = BuildServiceToTest();

            // act
            var result = await service.RefreshHashcodesAsync(appRegistrationModel, DefaultCdnLocation).ConfigureAwait(false);

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

            Assert.Equal(expectedCount, result);
        }
        public async Task RefreshHashcodesAsyncReturnsSuccessfulCount()
        {
            // arrange
            var appRegistrationModel = new AppRegistrationModel
            {
                Path           = "a-path",
                CssScriptNames = new Dictionary <string, string?>
                {
                    { "/a-file-location.css", null },
                    { "/another-file-location.css", "a-hash-code" },
                    { "http://somewhere.com//a-file-location.css", "a-hash-code" },
                },
                JavaScriptNames = new Dictionary <string, string?>
                {
                    { "/a-file-location.js", null },
                    { "/another-file-location.js", "a-hash-code" },
                    { "http://somewhere.com//a-file-location.js", "a-hash-code" },
                },
            };
            int expectedCount = appRegistrationModel.CssScriptNames.Count + appRegistrationModel.JavaScriptNames.Count;

            var service = BuildServiceToTest();

            // act
            var result = await service.RefreshHashcodesAsync(appRegistrationModel, DefaultCdnLocation).ConfigureAwait(false);

            // assert
            Assert.Equal(expectedCount, result);
        }
        public async Task PagesDataLoadServiceCreateOrUpdateAsyncRegistrationDocumentlocationsUpdated()
        {
            // Arrange
            var testGuid = Guid.NewGuid();
            AppRegistrationModel model = new AppRegistrationModel {
                Path = "/pages", PageLocations = new Dictionary <Guid, PageLocationModel> {
                    { testGuid, new PageLocationModel {
                          Locations = new List <string> {
                              "http://somewhere.com/a-place-a-page"
                          }
                      } }
                }
            };
            int expectedPageLocationCount = model.PageLocations.Count;
            var serviceToTest             = new PagesDataLoadService(logger, fakeHttpClient, pagesClientOptions, fakeApiDataService, fakeLegacyDataLoadService);

            A.CallTo(() => fakeLegacyDataLoadService.GetAppRegistrationByPathAsync(A <string> .Ignored)).Returns(model);
            A.CallTo(() => fakeApiDataService.GetAsync <PageLocationModel>(A <HttpClient> .Ignored, A <Uri> .Ignored)).Returns(PageLocationModels.FirstOrDefault().Value);

            // Act
            await serviceToTest.CreateOrUpdateAsync(testGuid).ConfigureAwait(false);

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

            Assert.NotNull(model.PageLocations);
            Assert.Equal(expectedPageLocationCount, model.PageLocations !.Count);
        }
        public async Task UpdateAppRegistrationAsync(AppRegistrationModel appRegistrationModel)
        {
            _ = appRegistrationModel ?? throw new ArgumentNullException(nameof(appRegistrationModel));

            logger.LogInformation($"Upserting App Registration: {JsonConvert.SerializeObject(appRegistrationModel)}");

            if (modelValidationService.ValidateModel(appRegistrationModel))
            {
                var upsertResult = await documentService.UpsertAsync(appRegistrationModel).ConfigureAwait(false);

                if (upsertResult == HttpStatusCode.OK || upsertResult == HttpStatusCode.Created)
                {
                    logger.LogInformation($"Upserted app registration: {appRegistrationModel.Path}: Status code: {upsertResult}");
                }
                else
                {
                    logger.LogError($"Failed to upsert app registration: {appRegistrationModel.Path}: Status code: {upsertResult}");
                }
            }
        }
        public async Task RefreshHashcodesAsyncReturnsExceptionWhenNoCdnLocation()
        {
            // arrange
            var appRegistrationModel = new AppRegistrationModel
            {
                Path            = "a-path",
                JavaScriptNames = new Dictionary <string, string?> {
                },
            };

            var service = BuildServiceToTest();

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

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

            Assert.Equal("Value cannot be null. (Parameter 'cdnLocation')", exceptionResult.Message);
        }
        public async Task UpdateHashCodesAsyncReturnsIsSuccessfulWhenNoUpdates()
        {
            // arrange
            var appRegistrationModel = new AppRegistrationModel
            {
                Path            = "a-path",
                JavaScriptNames = new Dictionary <string, string?> {
                },
            };

            A.CallTo(() => fakeDocumentService.UpsertAsync(A <AppRegistrationModel> .Ignored)).Returns(HttpStatusCode.OK);

            var service = BuildServiceToTest();

            // act
            await service.UpdateHashCodesAsync(appRegistrationModel, DefaultCdnLocation).ConfigureAwait(false);

            // assert
            A.CallTo(() => fakeDocumentService.UpsertAsync(A <AppRegistrationModel> .Ignored)).MustNotHaveHappened();
        }
        public async Task RefreshHashcodesAsyncReturnsSuccessfulZeroCountWhenNoScripts()
        {
            // arrange
            const int expectedCount        = 0;
            var       appRegistrationModel = new AppRegistrationModel
            {
                Path = "a-path",
            };

            A.CallTo(() => fakeDocumentService.UpsertAsync(A <AppRegistrationModel> .Ignored)).Returns(HttpStatusCode.OK);

            var service = BuildServiceToTest();

            // act
            var result = await service.RefreshHashcodesAsync(appRegistrationModel, DefaultCdnLocation).ConfigureAwait(false);

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

            Assert.Equal(expectedCount, result);
        }
コード例 #9
0
        public async Task PagesDataLoadServiceLoadAsyncRegistrationDocumentUpdated()
        {
            // Arrange
            AppRegistrationModel model = new AppRegistrationModel {
                Path = "/pages"
            };
            var serviceToTest = new PagesDataLoadService(logger, fakeHttpClient, pagesClientOptions, fakeApiDataService, fakeDataLoadService);

            A.CallTo(() => fakeDataLoadService.GetAppRegistrationByPathAsync(A <string> .Ignored)).Returns(model);
            A.CallTo(() => fakeApiDataService.GetAsync <Dictionary <Guid, PageLocationModel> >(A <HttpClient> .Ignored, A <Uri> .Ignored)).Returns(PageLocationModels);

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

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

            Assert.NotNull(model.PageLocations);
            Assert.Equal(PageLocationModels.Count, model.PageLocations !.Count);
        }
コード例 #10
0
        public async Task PagesDataLoadServiceCreateOrUpdateAsyncNullPagesNoRegistrationDocumentUpdated()
        {
            // Arrange
            AppRegistrationModel model = new AppRegistrationModel {
                Path = "/pages"
            };
            var serviceToTest = new PagesDataLoadService(logger, fakeHttpClient, pagesClientOptions, fakeApiDataService, fakeDataLoadService);

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

            PageLocationModel?pageModel = null;

            A.CallTo(() => fakeApiDataService.GetAsync <PageLocationModel>(A <HttpClient> .Ignored, A <Uri> .Ignored)).Returns(pageModel);

            // Act
            var result = await serviceToTest.CreateOrUpdateAsync(Guid.Parse("66088614-5c55-4698-8382-42b47ec0be10")).ConfigureAwait(false);

            // Assert
            Assert.Equal(HttpStatusCode.NotFound, result);
            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)).MustHaveHappenedOnceExactly();
        }
コード例 #11
0
        public async Task PagesDataLoadServiceCreateOrUpdateAsyncRegistrationDocumentLocationsReplaced()
        {
            // Arrange
            var testGuid = Guid.NewGuid();
            AppRegistrationModel model = new AppRegistrationModel {
                Path = "/pages", PageLocations = null,
            };
            var serviceToTest = new PagesDataLoadService(logger, fakeHttpClient, pagesClientOptions, fakeApiDataService, fakeDataLoadService);

            A.CallTo(() => fakeDataLoadService.GetAppRegistrationByPathAsync(A <string> .Ignored)).Returns(model);
            A.CallTo(() => fakeApiDataService.GetAsync <PageLocationModel>(A <HttpClient> .Ignored, A <Uri> .Ignored)).Returns(PageLocationModels.FirstOrDefault().Value);

            // Act
            await serviceToTest.CreateOrUpdateAsync(testGuid).ConfigureAwait(false);

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

            Assert.NotNull(model.PageLocations);
            Assert.Single(model.PageLocations !);
        }
コード例 #12
0
        public async Task WhenInvokedReturnsPaths()
        {
            var appRegistrationModel1 = new AppRegistrationModel()
            {
                Path = "path1", IsOnline = true, OfflineHtml = "OfflineHtml1", TopNavigationText = "Offline Html1"
            };
            var appRegistrationModel2 = new AppRegistrationModel()
            {
                Path = "path2", IsOnline = true, OfflineHtml = "OfflineHtml2", TopNavigationText = "Offline Html2"
            };
            var appRegistrationModels = new List <AppRegistrationModel>()
            {
                appRegistrationModel1, appRegistrationModel2
            };

            appRegistryDataService.Setup(x => x.GetAppRegistrationModels()).ReturnsAsync(appRegistrationModels);

            var result = await viewComponent.InvokeAsync();

            var viewComponentModel = result.ViewDataModelAs <ListPathsViewModel>();

            Assert.Equal(appRegistrationModels.Count, viewComponentModel.AppRegistrationModels.Count());
        }
        public ApplicationServiceTests()
        {
            appRegistryDataService = A.Fake <IAppRegistryDataService>();
            mapper           = new ApplicationToPageModelMapper(appRegistryDataService);
            contentRetriever = A.Fake <IContentRetriever>();
            contentProcessor = A.Fake <IContentProcessorService>();

            markupMessages = new MarkupMessages
            {
                AppOfflineHtml    = "<h3>App offline</h3>",
                RegionOfflineHtml = new Dictionary <PageRegion, string>
                {
                    {
                        PageRegion.Head, "<h3>Head Region is offline</h3>"
                    },
                    {
                        PageRegion.Breadcrumb, "<h3>Breadcrumb Region is offline</h3>"
                    },
                    {
                        PageRegion.BodyTop, "<h3>BodyTop Region is offline</h3>"
                    },
                    {
                        PageRegion.Body, "<h3>Body Region is offline</h3>"
                    },
                    {
                        PageRegion.SidebarRight, "<h3>SidebarRight Region is offline</h3>"
                    },
                    {
                        PageRegion.SidebarLeft, "<h3>SidebarLeft Region is offline</h3>"
                    },
                    {
                        PageRegion.BodyFooter, "<h3>BodyFooter Region is offline</h3>"
                    },
                    {
                        PageRegion.HeroBanner, "<h3>HeroBanner Region is offline</h3>"
                    },
                },
            };

            var headRegionEndPoint   = $"{RequestBaseUrl}/headRegionEndpoint";
            var bodyRegionEndPoint   = $"{RequestBaseUrl}/bodyRegionEndpoint";
            var footerRegionEndPoint = $"{RequestBaseUrl}/footerRegionEndpoint";

            defaultHeadRegion = new RegionModel {
                PageRegion = PageRegion.Head, RegionEndpoint = headRegionEndPoint, IsHealthy = true, OfflineHtml = OfflineHtml
            };
            defaultBodyRegion = new RegionModel {
                PageRegion = PageRegion.Body, RegionEndpoint = bodyRegionEndPoint, IsHealthy = true, OfflineHtml = OfflineHtml
            };
            defaultBodyFooterRegion = new RegionModel {
                PageRegion = PageRegion.BodyFooter, RegionEndpoint = footerRegionEndPoint, IsHealthy = true, OfflineHtml = OfflineHtml
            };
            defaultRegions = new List <RegionModel>
            {
                defaultHeadRegion,
                defaultBodyRegion,
                defaultBodyFooterRegion,
            };
            defaultAppRegistrationModel = new AppRegistrationModel {
                Path = ChildAppPath, TopNavigationOrder = 1, IsOnline = true, Regions = defaultRegions
            };
            pagesAppRegistrationModel = new AppRegistrationModel {
                Path = AppRegistryPathNameForPagesApp, TopNavigationOrder = 1, IsOnline = true, Regions = defaultRegions
            };

            defaultPageViewModel = new PageViewModel
            {
                PageRegionContentModels = new List <PageRegionContentModel>
                {
                    new PageRegionContentModel
                    {
                        PageRegionType = PageRegion.Body,
                    },
                },
            };

            defaultApplicationModel = new ApplicationModel {
                AppRegistrationModel = defaultAppRegistrationModel, Article = "index"
            };
            pagesApplicationModel = new ApplicationModel {
                AppRegistrationModel = pagesAppRegistrationModel
            };
            offlineApplicationModel = new ApplicationModel {
                AppRegistrationModel = new AppRegistrationModel {
                    IsOnline = false, OfflineHtml = OfflineHtml
                }
            };
            offlineApplicationModelWithoutMarkup = new ApplicationModel {
                AppRegistrationModel = new AppRegistrationModel {
                    IsOnline = false, OfflineHtml = null
                }
            };

            A.CallTo(() => appRegistryDataService.GetAppRegistrationModel($"{ChildAppPath}/{ChildAppData}")).Returns(nullAppRegistrationModel);
            A.CallTo(() => appRegistryDataService.GetAppRegistrationModel(ChildAppPath)).Returns(defaultAppRegistrationModel);
            A.CallTo(() => appRegistryDataService.GetAppRegistrationModel(AppRegistryPathNameForPagesApp)).Returns(pagesAppRegistrationModel);
            A.CallTo(() => contentRetriever.GetContent($"{defaultHeadRegion.RegionEndpoint}/index", defaultApplicationModel.AppRegistrationModel.Path, defaultHeadRegion, A <bool> .Ignored, RequestBaseUrl)).Returns(HeadRegionContent);
            A.CallTo(() => contentRetriever.GetContent($"{defaultBodyRegion.RegionEndpoint}/index", defaultApplicationModel.AppRegistrationModel.Path, defaultBodyRegion, A <bool> .Ignored, RequestBaseUrl)).Returns(BodyRegionContent);
            A.CallTo(() => contentRetriever.GetContent($"{defaultBodyFooterRegion.RegionEndpoint}", defaultApplicationModel.AppRegistrationModel.Path, defaultBodyFooterRegion, A <bool> .Ignored, RequestBaseUrl)).Returns(BodyFooterRegionContent);
            A.CallTo(() => contentRetriever.GetContent($"{defaultBodyFooterRegion.RegionEndpoint}/index", defaultApplicationModel.AppRegistrationModel.Path, defaultBodyFooterRegion, A <bool> .Ignored, RequestBaseUrl)).Returns(BodyFooterRegionContent);

            A.CallTo(() => contentProcessor.Process(HeadRegionContent, A <string> .Ignored, A <string> .Ignored)).Returns(HeadRegionContent);
            A.CallTo(() => contentProcessor.Process(BodyRegionContent, A <string> .Ignored, A <string> .Ignored)).Returns(BodyRegionContent);
            A.CallTo(() => contentProcessor.Process(BodyFooterRegionContent, A <string> .Ignored, A <string> .Ignored)).Returns(BodyFooterRegionContent);

            defaultFormPostParams = new List <KeyValuePair <string, string> > {
                new KeyValuePair <string, string>("formParam1", "testvalue")
            };

            taskHelper = A.Fake <ITaskHelper>();
            A.CallTo(() => taskHelper.TaskCompletedSuccessfully(A <Task> .Ignored)).Returns(true);

            applicationService = new ApplicationService(appRegistryDataService, contentRetriever, contentProcessor, taskHelper, markupMessages)
            {
                RequestBaseUrl = RequestBaseUrl
            };
        }