示例#1
0
        public async Task Given_CollectionPage_It_Stops_Iterating_Across_Pages()
        {
            // Create the 17 events to initiallize the original collection page.
            List <Event> testEvents      = new List <Event>();
            int          inputEventCount = 17;

            for (int i = 0; i < inputEventCount; i++)
            {
                testEvents.Add(new Event()
                {
                    Subject = $"Subject{i.ToString()}"
                });
            }

            // Create the 5 events to initialize the next collection page.
            UserEventsCollectionPage nextPage = new UserEventsCollectionPage();
            int nextPageEventCount            = 5;

            for (int i = 0; i < nextPageEventCount; i++)
            {
                nextPage.Add(new Event()
                {
                    Subject = $"Subject for next page events: {i.ToString()}"
                });
            }

            // Create the CancellationTokenSource to test the cancellation of paging in the delegate.
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
            var pagingToken = cancellationTokenSource.Token;

            // Create the delegate to process each entity returned in the pages. The delegate will cancel
            // paging when the target subject is encountered.
            Func <Event, bool> processEachEvent = (e) =>
            {
                bool shouldContinue = true;

                if (e.Subject.Contains("Subject3"))
                {
                    cancellationTokenSource.Cancel();
                }

                if (e.Subject.Contains("Subject for next page events"))
                {
                    Assert.True(false, "Unexpectedly paged the next page of results.");
                }

                return(shouldContinue);
            };

            Mocks.MockUserEventsCollectionRequest mockUserEventsCollectionRequest = new Mocks.MockUserEventsCollectionRequest(nextPage);
            var mockUserEventsCollectionPage = new Mocks.MockUserEventsCollectionPage(testEvents, mockUserEventsCollectionRequest) as IUserEventsCollectionPage;

            eventPageIterator = PageIterator <Event> .CreatePageIterator(graphClient, mockUserEventsCollectionPage, processEachEvent);

            await eventPageIterator.IterateAsync(pagingToken);

            Assert.True(cancellationTokenSource.IsCancellationRequested, "The delegate page iterator did not cancel requests to fetch more pages.");
        }
示例#2
0
        public async Task Given_CollectionPage_It_Iterates_Across_Pages()
        {
            // Create the 17 events to initiallize the original collection page.
            List <Event> originalCollectionPageEvents = new List <Event>();
            int          inputEventCount = 17;

            for (int i = 0; i < inputEventCount; i++)
            {
                originalCollectionPageEvents.Add(new Event()
                {
                    Subject = $"Subject{i.ToString()}"
                });
            }

            // Create the 5 events to initialize the next collection page.
            UserEventsCollectionPage nextPage = new UserEventsCollectionPage();
            int nextPageEventCount            = 5;

            for (int i = 0; i < nextPageEventCount; i++)
            {
                nextPage.Add(new Event()
                {
                    Subject = $"Subject for next page events: {i.ToString()}"
                });
            }
            nextPage.AdditionalData = new Dictionary <string, object>();

            bool reachedNextPage = false;

            // Create the delegate to process each entity returned in the pages. The delegate will
            // signal that we reached an event in the next page.
            Func <Event, bool> processEachEvent = (e) =>
            {
                if (e.Subject.Contains("Subject for next page events"))
                {
                    reachedNextPage = true;
                    return(false);
                }

                return(true);
            };

            Mocks.MockUserEventsCollectionRequest mockUserEventsCollectionRequest = new Mocks.MockUserEventsCollectionRequest(nextPage);
            var mockUserEventsCollectionPage = new Mocks.MockUserEventsCollectionPage(originalCollectionPageEvents, mockUserEventsCollectionRequest, "nextLink") as IUserEventsCollectionPage;

            eventPageIterator = PageIterator <Event> .CreatePageIterator(graphClient, mockUserEventsCollectionPage, processEachEvent);

            await eventPageIterator.IterateAsync();

            Assert.True(reachedNextPage, "The delegate page iterator did not reach the next page.");
            Assert.Equal(PagingState.Paused, eventPageIterator.State);
        }
示例#3
0
        public void Given_PageIterator_It_Has_PagingState_NotStarted()
        {
            // Arrange
            List <Event>             originalCollectionPageEvents = new List <Event>();
            UserEventsCollectionPage nextPage = new UserEventsCollectionPage();

            Mocks.MockUserEventsCollectionRequest mockUserEventsCollectionRequest = new Mocks.MockUserEventsCollectionRequest(nextPage);
            var mockUserEventsCollectionPage = new Mocks.MockUserEventsCollectionPage(originalCollectionPageEvents, mockUserEventsCollectionRequest, "nextlink") as IUserEventsCollectionPage;

            // Act
            eventPageIterator = PageIterator <Event> .CreatePageIterator(graphClient, mockUserEventsCollectionPage, (e) => { return(true); });

            // Assert
            Assert.Equal <PagingState>(PagingState.NotStarted, eventPageIterator.State);
        }
示例#4
0
        public async Task Given_RequestConfigurator_It_Is_Invoked()
        {
            // Create the 17 events to initiallize the original collection page.
            List <Event> originalCollectionPageEvents = new List <Event>();
            int          inputEventCount = 17;

            for (int i = 0; i < inputEventCount; i++)
            {
                originalCollectionPageEvents.Add(new Event()
                {
                    Subject = $"Subject{i.ToString()}"
                });
            }

            // Create the 5 events to initialize the next collection page.
            UserEventsCollectionPage nextPage = new UserEventsCollectionPage();
            int nextPageEventCount            = 5;

            for (int i = 0; i < nextPageEventCount; i++)
            {
                nextPage.Add(new Event()
                {
                    Subject = $"Subject for next page events: {i.ToString()}"
                });
            }
            nextPage.AdditionalData = new Dictionary <string, object>();

            // Create the delegate to process each entity returned in the pages.
            Func <Event, bool> processEachEvent = (e) => { return(true); };

            // Create the delegate to configure the next page request. The delegate will signal that it was invoked.
            bool requestConfiguratorInvoked = false;

            Func <IBaseRequest, IBaseRequest> requestConfigurator = (request) =>
            {
                requestConfiguratorInvoked = true;
                return(request);
            };

            Mocks.MockUserEventsCollectionRequest mockUserEventsCollectionRequest = new Mocks.MockUserEventsCollectionRequest(nextPage);
            var mockUserEventsCollectionPage = new Mocks.MockUserEventsCollectionPage(originalCollectionPageEvents, mockUserEventsCollectionRequest, "nextLink") as IUserEventsCollectionPage;

            eventPageIterator = PageIterator <Event> .CreatePageIterator(graphClient, mockUserEventsCollectionPage, processEachEvent, requestConfigurator);

            await eventPageIterator.IterateAsync();

            Assert.True(requestConfiguratorInvoked, "The delegate request configurator not invoked.");
        }
示例#5
0
        public async Task Given_CollectionPage_It_Detects_Nextlink_Loop()
        {
            // Create the 17 events to initiallize the original collection page.
            List <Event> originalCollectionPageEvents = new List <Event>();
            int          inputEventCount = 5;

            for (int i = 0; i < inputEventCount; i++)
            {
                originalCollectionPageEvents.Add(new Event()
                {
                    Subject = $"Subject{i.ToString()}"
                });
            }

            // Create the 5 events to initialize the next collection page.
            UserEventsCollectionPage nextPage = new UserEventsCollectionPage();
            int nextPageEventCount            = 5;

            for (int i = 0; i < nextPageEventCount; i++)
            {
                nextPage.Add(new Event()
                {
                    Subject = $"Subject for next page events: {i.ToString()}"
                });
            }

            // This will be the same nextLink value as the one set in MockUserEventsCollectionPage cstor.
            nextPage.AdditionalData = new Dictionary <string, object>()
            {
                { Constants.OdataInstanceAnnotations.NextLink, "testNextlink" }
            };

            // Create the delegate to process each entity returned in the pages. The delegate will
            // signal that we reached an event in the next page.
            Func <Event, bool> processEachEvent = (e) =>
            {
                return(true);
            };

            Mocks.MockUserEventsCollectionRequest mockUserEventsCollectionRequest = new Mocks.MockUserEventsCollectionRequest(nextPage);
            var mockUserEventsCollectionPage = new Mocks.MockUserEventsCollectionPage(originalCollectionPageEvents, mockUserEventsCollectionRequest, "nextLink") as IUserEventsCollectionPage;

            eventPageIterator = PageIterator <Event> .CreatePageIterator(graphClient, mockUserEventsCollectionPage, processEachEvent);

            await Assert.ThrowsAsync <ServiceException>(async() => await eventPageIterator.IterateAsync());
        }
示例#6
0
        public async Task Given_CollectionPage_It_Handles_Empty_NextPage()
        {
            try
            {
                // Create the 17 events to initiallize the original collection page.
                List <Event> originalCollectionPageEvents = new List <Event>();
                int          inputEventCount = 17;
                for (int i = 0; i < inputEventCount; i++)
                {
                    originalCollectionPageEvents.Add(new Event()
                    {
                        Subject = $"Subject{i.ToString()}"
                    });
                }

                // Create empty next collection page.
                UserEventsCollectionPage nextPage = new UserEventsCollectionPage();

                // Create the delegate to process each entity returned in the pages.
                Func <Event, bool> processEachEvent = (e) =>
                {
                    return(true);
                };

                Mocks.MockUserEventsCollectionRequest mockUserEventsCollectionRequest = new Mocks.MockUserEventsCollectionRequest(nextPage);
                var mockUserEventsCollectionPage = new Mocks.MockUserEventsCollectionPage(originalCollectionPageEvents, mockUserEventsCollectionRequest) as IUserEventsCollectionPage;

                eventPageIterator = PageIterator <Event> .CreatePageIterator(graphClient, mockUserEventsCollectionPage, processEachEvent);

                await eventPageIterator.IterateAsync();
            }
            catch (Exception)
            {
                Assert.True(false, "Unexpected exception occurred when next page contains no elements.");
            }
        }