Exemplo n.º 1
0
        private static async Task <AdvertisementSummaryPageResource> GetAllAdvertisementsNextPageExampleAsync(AdvertisementSummaryPageResource summaryPage)
        {
            try
            {
                await TransientErrorRetryPolicy.ExecuteAsync(async() => summaryPage = await summaryPage.NextPageAsync());

                Console.WriteLine($"Next page of advertisements:{JsonConvert.SerializeObject(summaryPage, Formatting.Indented)}");
            }
            catch (RequestException ex)
            {
                LogException(ex);
            }

            return(summaryPage);
        }
        public async Task GetAllAdvertisementsNoNextPage()
        {
            AdvertisementSummaryPageResource pageResource = new AdvertisementSummaryPageResource
            {
                Links = new Links(this.Fixture.AdPostingApiServiceBaseUri)
                {
                    { "self", new Link {
                          Href = "/advertisement"
                      } }
                }
            };

            var actualException = await Assert.ThrowsAsync <NotSupportedException>(async() => await pageResource.NextPageAsync());

            var expectedException = new NotSupportedException("There are no more results");

            actualException.ShouldBeEquivalentToException(expectedException);
        }
        public async Task GetAllAdvertisementsByAdvertiserNextPage()
        {
            const string advertiserId     = "456";
            const string advertisementId1 = "f7302df2-704b-407c-a42a-62ff822b5461";
            const string beforeJobId      = "5";
            const string queryString      = "advertiserId=" + advertiserId + "&beforeId=" + beforeJobId;
            const string selfLink         = "/advertisement?" + queryString;
            OAuth2Token  oAuth2Token      = new OAuth2TokenBuilder().Build();

            this.Fixture.AdPostingApiService
            .Given("A page size of 3 with more than 1 page of data")
            .UponReceiving("a GET advertisements request for the second page of advertisements belonging to the advertiser")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Get,
                Path    = "/advertisement",
                Query   = queryString,
                Headers = new Dictionary <string, string>
                {
                    { "Authorization", "Bearer " + oAuth2Token.AccessToken },
                    { "Accept", $"{ResponseContentTypes.AdvertisementListVersion1}, {ResponseContentTypes.AdvertisementErrorVersion1}" },
                    { "User-Agent", AdPostingApiFixture.UserAgentHeaderValue }
                }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, string>
                {
                    { "Content-Type", ResponseContentTypes.AdvertisementListVersion1 },
                    { "X-Request-Id", RequestId }
                },
                Body = new
                {
                    _embedded = new
                    {
                        advertisements = new[]
                        {
                            new AdvertisementSummaryResponseContentBuilder()
                            .WithId(advertisementId1)
                            .WithAdvertiserId(advertiserId)
                            .WithJobTitle("Exciting Developer role in a great CBD location. Great $$")
                            .WithJobReference("JOB1111")
                            .WithResponseLink("self", this.GenerateSelfLink(advertisementId1))
                            .WithResponseLink("view", this.GenerateViewLink(advertisementId1))
                            .Build()
                        }
                    },
                    _links = new
                    {
                        self = new { href = selfLink }
                    }
                }
            });

            AdvertisementSummaryPageResource pageResource = new AdvertisementSummaryPageResource
            {
                Links = new Links(this.Fixture.AdPostingApiServiceBaseUri)
                {
                    { "self", new Link {
                          Href = "/advertisement"
                      } },
                    { "next", new Link {
                          Href = $"/advertisement?advertiserId={advertiserId}&beforeId={beforeJobId}"
                      } }
                }
            };

            var oAuthClient = Mock.Of <IOAuth2TokenClient>(c => c.GetOAuth2TokenAsync() == Task.FromResult(oAuth2Token));
            AdvertisementSummaryPageResource nextPageResource;

            using (var client = new Hal.Client(new HttpClient(new AdPostingApiMessageHandler(new OAuthMessageHandler(oAuthClient)))))
            {
                pageResource.Initialise(client);

                nextPageResource = await pageResource.NextPageAsync();
            }

            AdvertisementSummaryPageResource expectedNextPageResource = new AdvertisementSummaryPageResource
            {
                AdvertisementSummaries = new List <AdvertisementSummaryResource>
                {
                    new AdvertisementSummaryResource
                    {
                        Id           = new Guid(advertisementId1),
                        AdvertiserId = advertiserId,
                        JobReference = "JOB1111",
                        JobTitle     = "Exciting Developer role in a great CBD location. Great $$",
                        Links        = new Links(this.Fixture.AdPostingApiServiceBaseUri)
                        {
                            { "self", new Link {
                                  Href = $"/advertisement/{advertisementId1}"
                              } },
                            { "view", new Link {
                                  Href = $"/advertisement/{advertisementId1}/view"
                              } }
                        }
                    }
                },
                Links = new Links(this.Fixture.AdPostingApiServiceBaseUri)
                {
                    { "self", new Link {
                          Href = selfLink
                      } }
                },
                RequestId = RequestId
            };

            nextPageResource.ShouldBeEquivalentTo(expectedNextPageResource);
        }
Exemplo n.º 4
0
        public async Task GetAllAdvertisementsNextPage()
        {
            const string advertisementId1 = "fa6939b5-c91f-4f6a-9600-1ea74963fbb2";
            const string advertisementId2 = "f7302df2-704b-407c-a42a-62ff822b5461";
            const string advertisementId3 = "3b138935-f65b-4ec7-91d8-fc250757b53d";
            const string beforeJobId      = "6";
            OAuth2Token  oAuth2Token      = new OAuth2TokenBuilder().Build();

            this.Fixture.MockProviderService
            .Given("A page size of 3 with more than 1 page of data")
            .UponReceiving("a GET advertisements request for the last page of data")
            .With(new ProviderServiceRequest
            {
                Method  = HttpVerb.Get,
                Path    = "/advertisement",
                Query   = "beforeId=" + beforeJobId,
                Headers = new Dictionary <string, object>
                {
                    { "Authorization", "Bearer " + oAuth2Token.AccessToken },
                    { "Accept", $"{ResponseContentTypes.AdvertisementListVersion1}, {ResponseContentTypes.AdvertisementErrorVersion1}" },
                    { "User-Agent", AdPostingApiFixture.UserAgentHeaderValue }
                }
            })
            .WillRespondWith(new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, object>
                {
                    { "Content-Type", ResponseContentTypes.AdvertisementListVersion1 },
                    { "X-Request-Id", RequestId }
                },
                Body = new
                {
                    _embedded = new
                    {
                        advertisements = new[]
                        {
                            new AdvertisementSummaryResponseContentBuilder()
                            .WithId(advertisementId3)
                            .WithAdvertiserId("456")
                            .WithJobTitle("Exciting tester role in a great CBD location. Great $$")
                            .WithJobReference("JOB2222")
                            .WithResponseLink("self", this.GenerateSelfLink(advertisementId3))
                            .WithResponseLink("view", this.GenerateViewLink(advertisementId3))
                            .Build(),
                            new AdvertisementSummaryResponseContentBuilder()
                            .WithId(advertisementId2)
                            .WithAdvertiserId("456")
                            .WithJobTitle("Exciting Developer role in a great CBD location. Great $$")
                            .WithJobReference("JOB1111")
                            .WithResponseLink("self", this.GenerateSelfLink(advertisementId2))
                            .WithResponseLink("view", this.GenerateViewLink(advertisementId2))
                            .Build(),
                            new AdvertisementSummaryResponseContentBuilder()
                            .WithId(advertisementId1)
                            .WithAdvertiserId("123")
                            .WithJobTitle("Exciting Developer role in a great CBD location. Great $$")
                            .WithJobReference("JOB1234")
                            .WithResponseLink("self", this.GenerateSelfLink(advertisementId1))
                            .WithResponseLink("view", this.GenerateViewLink(advertisementId1))
                            .Build()
                        }
                    },
                    _links = new
                    {
                        self = new { href = $"/advertisement?beforeId={beforeJobId}" }
                    }
                }
            });

            AdvertisementSummaryPageResource pageResource = new AdvertisementSummaryPageResource
            {
                Links = new Links(this.Fixture.AdPostingApiServiceBaseUri)
                {
                    { "self", new Link {
                          Href = "/advertisement"
                      } },
                    { "next", new Link {
                          Href = $"/advertisement?beforeId={beforeJobId}"
                      } }
                }
            };

            var oAuthClient = Mock.Of <IOAuth2TokenClient>(c => c.GetOAuth2TokenAsync() == Task.FromResult(oAuth2Token));
            AdvertisementSummaryPageResource nextPageResource;

            HttpMessageHandler pipeline = new HttpClientHandler()
                                          .DecorateWith(new OAuthMessageHandler(oAuthClient))
                                          .DecorateWith(new AdPostingApiMessageHandler());

            using (var client = new Hal.Client(new HttpClient(pipeline)))
            {
                pageResource.Initialise(client);

                nextPageResource = await pageResource.NextPageAsync();
            }

            AdvertisementSummaryPageResource expectedNextPageResource = new AdvertisementSummaryPageResource
            {
                AdvertisementSummaries = new List <AdvertisementSummaryResource>
                {
                    new AdvertisementSummaryResource
                    {
                        Id           = new Guid(advertisementId3),
                        AdvertiserId = "456",
                        JobReference = "JOB2222",
                        JobTitle     = "Exciting tester role in a great CBD location. Great $$",
                        Links        = new Links(this.Fixture.AdPostingApiServiceBaseUri)
                        {
                            { "self", new Link {
                                  Href = $"/advertisement/{advertisementId3}"
                              } },
                            { "view", new Link {
                                  Href = $"/advertisement/{advertisementId3}/view"
                              } }
                        }
                    },
                    new AdvertisementSummaryResource
                    {
                        Id           = new Guid(advertisementId2),
                        AdvertiserId = "456",
                        JobReference = "JOB1111",
                        JobTitle     = "Exciting Developer role in a great CBD location. Great $$",
                        Links        = new Links(this.Fixture.AdPostingApiServiceBaseUri)
                        {
                            { "self", new Link {
                                  Href = $"/advertisement/{advertisementId2}"
                              } },
                            { "view", new Link {
                                  Href = $"/advertisement/{advertisementId2}/view"
                              } }
                        }
                    },
                    new AdvertisementSummaryResource
                    {
                        Id           = new Guid(advertisementId1),
                        AdvertiserId = "123",
                        JobReference = "JOB1234",
                        JobTitle     = "Exciting Developer role in a great CBD location. Great $$",
                        Links        = new Links(this.Fixture.AdPostingApiServiceBaseUri)
                        {
                            { "self", new Link {
                                  Href = $"/advertisement/{advertisementId1}"
                              } },
                            { "view", new Link {
                                  Href = $"/advertisement/{advertisementId1}/view"
                              } }
                        }
                    }
                },
                Links = new Links(this.Fixture.AdPostingApiServiceBaseUri)
                {
                    { "self", new Link {
                          Href = $"/advertisement?beforeId={beforeJobId}"
                      } }
                },
                RequestId = RequestId
            };

            nextPageResource.ShouldBeEquivalentTo(expectedNextPageResource);
        }