public async void GetTweetsAsync_WhenOverLimit_CallsNextGetTweetsAsync()
        {
            // Arrange.
            var startDateTime = DateTimeOffset.UtcNow;
            var endDateTime   = DateTimeOffset.UtcNow;

            endDateTime = endDateTime.AddDays(1);
            var expectedTweets = new List <Tweet>();
            var mockedTweets   = new List <List <Tweet> >();

            mockedTweets.Add(getTweets("first", 100));
            mockedTweets.Add(getTweets("second", 50));
            expectedTweets.AddRange(mockedTweets[0]);
            expectedTweets.AddRange(mockedTweets[1]);

            // Mock the response from http client.
            var responseMessage1     = getHttpResponseMessage(HttpStatusCode.OK, mockedTweets[0]);
            var responseMessage2     = getHttpResponseMessage(HttpStatusCode.OK, mockedTweets[1]);
            var mockedMessageHandler = getMockedMessageHandler(responseMessage1, responseMessage2);

            tweetServiceClient = new TweetServices(new HttpClient(mockedMessageHandler.Object), new MockedLogger());


            // Act.
            IList <Tweet> actualTweets = await tweetServiceClient.GetTweetsAsync(startDateTime, endDateTime);

            // Assert.
            AssertTweets(expectedTweets, actualTweets);
        }
        public async void GetTweetsAsync_WhenErrorResponse_ThrowsException()
        {
            // Arrange.
            var startDateTime = DateTimeOffset.UtcNow;
            var endDateTime   = DateTimeOffset.UtcNow;

            endDateTime = endDateTime.AddDays(1);
            var expectedTweets = new List <Tweet>();
            var mockedTweets   = getTweets("first", 1);

            expectedTweets.AddRange(mockedTweets);

            // Mock the response from http client.
            var responseMessage      = getHttpResponseMessage(HttpStatusCode.NotFound, mockedTweets);
            var mockedMessageHandler = getMockedMessageHandler(responseMessage);

            tweetServiceClient = new TweetServices(new HttpClient(mockedMessageHandler.Object), new MockedLogger());

            // Act.
            // Assert.
            await Assert.ThrowsAnyAsync <Exception>(() =>
            {
                return(tweetServiceClient.GetTweetsAsync(startDateTime, endDateTime));
            });
        }
        public async void GetTweetsAsync_UsingSameTime_ReturnsCorrectTweets()
        {
            // Arrange.
            var startDateTime = new DateTimeOffset(2016, 1, 1, 0, 0, 0, new TimeSpan());
            var endDateTime   = new DateTimeOffset(2016, 1, 1, 0, 0, 0, new TimeSpan());

            var expectedTweetCount = 0;

            tweetServiceClient = new TweetServices(new HttpClient(), new TweetMocked.MockedLogger());


            // Act.
            IList <Tweet> actualTweets = await tweetServiceClient.GetTweetsAsync(startDateTime, endDateTime);

            // Assert.
            Assert.NotNull(actualTweets);
            Assert.Equal(expectedTweetCount, actualTweets.Count);
            AssertUniqueTweets(actualTweets);
        }
        private static void PrintTweets(Logger logger, int option)
        {
            Console.WriteLine("Getting tweet identifier from 2016 & 2017 UTC.");

            var startDateTime = new DateTimeOffset(2016, 1, 1, 0, 0, 0, new TimeSpan());
            var endDateTime   = new DateTimeOffset(2018, 1, 1, 0, 0, 0, new TimeSpan());

            endDateTime.AddMilliseconds(-1);

            var           tweetServiceClient = new TweetServices(new HttpClient(), logger);
            IList <Tweet> tweets             = tweetServiceClient.GetTweetsAsync(startDateTime, endDateTime).Result;

            Console.WriteLine(string.Format("Found {0} tweets", tweets.Count));

            StreamWriter writetext = null;

            if (option > 1)
            {
                writetext           = new StreamWriter("tweets.txt");
                writetext.AutoFlush = true;
                Console.SetOut(writetext);
            }
            foreach (var tweet in tweets)
            {
                Console.WriteLine(tweet);
                Console.WriteLine();
            }

            if (writetext != null)
            {
                writetext.Close();
            }

            if (option == 3)
            {
                using (writetext = new StreamWriter("logs.txt"))
                {
                    writetext.WriteLine(logger.getAllLoggedMessage());
                }
            }
        }
        public async void GetTweetsAsync_WhenOverLimitWithSameTimeStamp_ContainsNoDuplicateTweets()
        {
            // Arrange.
            var startDateTime = DateTimeOffset.UtcNow;
            var endDateTime   = DateTimeOffset.UtcNow;

            endDateTime = endDateTime.AddDays(1);
            var expectedTweets = new List <Tweet>();
            var mockedTweets   = new List <List <Tweet> >();

            mockedTweets.Add(getTweets("first", 100));
            mockedTweets.Add(getTweets("second", 50));
            expectedTweets.AddRange(mockedTweets[0]);

            // Make the first tweet of second api equal to previous call.
            mockedTweets[1][0] = new Tweet()
            {
                id    = mockedTweets[0][99].id,
                stamp = mockedTweets[0][99].stamp,
                text  = mockedTweets[0][99].text,
            };

            // Skip the first one.
            expectedTweets.AddRange(mockedTweets[1].GetRange(1, 49));


            // Mock the response from http client.
            var responseMessage1     = getHttpResponseMessage(HttpStatusCode.OK, mockedTweets[0]);
            var responseMessage2     = getHttpResponseMessage(HttpStatusCode.OK, mockedTweets[1]);
            var mockedMessageHandler = getMockedMessageHandler(responseMessage1, responseMessage2);

            tweetServiceClient = new TweetServices(new HttpClient(mockedMessageHandler.Object), new MockedLogger());


            // Act.
            IList <Tweet> actualTweets = await tweetServiceClient.GetTweetsAsync(startDateTime, endDateTime);

            // Assert.
            AssertTweets(expectedTweets, actualTweets);
        }
        public async void GetTweetsAsync_WhenEmptyTweets_ReturnsEmptyTweets()
        {
            // Arrange.
            var startDateTime = DateTimeOffset.UtcNow;
            var endDateTime   = DateTimeOffset.UtcNow;

            endDateTime = endDateTime.AddDays(1);
            var expectedTweets = new List <Tweet>();
            var mockedTweets   = getTweets("first", 0);

            expectedTweets.AddRange(mockedTweets);

            // Mock the response from http client.
            var responseMessage      = getHttpResponseMessage(HttpStatusCode.OK, mockedTweets);
            var mockedMessageHandler = getMockedMessageHandler(responseMessage);

            tweetServiceClient = new TweetServices(new HttpClient(mockedMessageHandler.Object), new MockedLogger());

            // Act.
            IList <Tweet> actualTweets = await tweetServiceClient.GetTweetsAsync(startDateTime, endDateTime);

            // Assert.
            AssertTweets(expectedTweets, actualTweets);
        }