Beispiel #1
0
        public async Task VerifyNoChangeIfEmptyYouTubePropertySpecified()
        {
            // Arrange
            string originalContent   = "#Hello\n* world\n* 1234";
            string youtube           = "";
            string expectedLogOutput = "No YouTube ID provided, not embedding a YouTube Video.";

            mockHttpMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(() =>
            {
                return(new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.OK
                });
            });

            var client = new HttpClient(mockHttpMessageHandler.Object);

            mockFactory.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client);

            // Act
            DevToService devtoService       = new DevToService(mockFactory.Object, mockLogger.Object);
            string       contentWithYouTube = await devtoService.AppendYouTubeInformation(originalContent, youtube);

            Func <object, Type, bool> state = (v, t) => v.ToString().CompareTo(expectedLogOutput) == 0;

            mockLogger.Verify(l => l.Log(
                                  LogLevel.Information,
                                  It.IsAny <EventId>(),
                                  It.Is <It.IsAnyType>((v, t) => state(v, t)),
                                  It.IsAny <Exception>(),
                                  (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Exactly(1));
        }
Beispiel #2
0
        public async Task VerifyTweetContentConvertedSuccessfully()
        {
            // Arrange
            string originalContent = "#Hello\n* world\n* 1234\n{{< tweet 1395779887412170752 >}}";
            string expectedContent = "#Hello\n* world\n* 1234\n{% twitter 1395779887412170752 %}";

            // Act
            DevToService mediumService = new DevToService(mockFactory.Object, mockLogger.Object);
            string       actualContent = await mediumService.ReplaceEmbeddedTweets(originalContent);

            // Assert
            Assert.Equal(expectedContent, actualContent);
        }
Beispiel #3
0
        public async Task VerifyYouTubeLiquidTagAddedAtEndOfBody()
        {
            // Arrange
            string originalContent   = "#Hello\n* world\n* 1234";
            string youtube           = "abc123456";
            string expectedLogOutput = $"Youtube ID {youtube} added";

            mockHttpMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(() =>
            {
                return(new HttpResponseMessage()
                {
                    StatusCode = HttpStatusCode.OK
                });
            });

            var client = new HttpClient(mockHttpMessageHandler.Object);

            mockFactory.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client);

            // Act
            DevToService devtoService       = new DevToService(mockFactory.Object, mockLogger.Object);
            string       contentWithYouTube = await devtoService.AppendYouTubeInformation(originalContent, youtube);

            // Assert
            Assert.Contains($"{{% youtube {youtube} %}}", contentWithYouTube);


            Func <object, Type, bool> state = (v, t) => v.ToString().CompareTo(expectedLogOutput) == 0;

            mockLogger.Verify(l => l.Log(
                                  LogLevel.Information,
                                  It.IsAny <EventId>(),
                                  It.Is <It.IsAnyType>((v, t) => state(v, t)),
                                  It.IsAny <Exception>(),
                                  (Func <It.IsAnyType, Exception, string>)It.IsAny <object>()), Times.Exactly(1));
        }
Beispiel #4
0
        private async Task Save()
        {
            await GithubService.GetCommits();

            await TwitterService.GetTwitterFav();

            await GithubService.GetGitHubStars();

            await GithubService.GetGitHubRepo();

            await TwitterService.GetTwitterFollowers();

            await TwitterService.GetTwitterFollowing();

            await TwitterService.GetNumberOfTweets();

            await GithubService.GetGitHubFollowers();

            await GithubService.GetGitHubFollowing();

            await DevToService.GetDevTo();

            var r   = new Random();
            var rnd = r.Next(2);

            if (rnd == 1)
            {
                await PowerService.GetElec();
            }
            else
            {
                await PowerService.GetGas();
            }

            UriHelper.NavigateTo("/metrics", true);
        }
Beispiel #5
0
        public async Task AssertHttpCircuitBreakerWorksCorrectly()
        {
            // Arrange - Setup the handler for the mocked HTTP call
            _isCircuitBroken   = false;
            _isCircuitRestored = false;
            _isRetryCalled     = false;
            _retryCount        = 0;

            mockHttpMessageHandler.Protected()
            .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>())
            .ReturnsAsync(() =>
            {
                if (_retryCount > 5)
                {
                    return(new HttpResponseMessage()
                    {
                        StatusCode = HttpStatusCode.OK
                    });
                }
                else
                {
                    return(new HttpResponseMessage()
                    {
                        StatusCode = HttpStatusCode.TooManyRequests
                    });
                }
            });


            var client = new HttpClient(mockHttpMessageHandler.Object);

            mockFactory.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client);

            // Arrange - Setup the Service/Poco details
            DevToService devtoService = new DevToService(mockFactory.Object, mockLogger.Object);
            DevToPoco    devtoPoco    = new DevToPoco()
            {
                article = new Article()
                {
                    body_markdown = "#Test My Test",
                    description   = "This is a description",
                    canonical_url = "https://www.cloudwithchris.com",
                    published     = false,
                    series        = "Cloud Drops",
                    tags          = new List <string>()
                    {
                        "DevOps", "GitHub"
                    },
                    title = "Descriptive Title"
                }
            };
            Mock <CancellationTokenSource> mockCancellationTokenSource = new Mock <CancellationTokenSource>();

            // Act
            try
            {
                await GetRetryPolicyAsync().WrapAsync(GetCircuitBreakerPolicyAsync()).ExecuteAsync(async() =>
                {
                    return(await devtoService.CreatePostAsync(devtoPoco, "integrationToken", mockCancellationTokenSource.Object));
                });
            }
            catch (BrokenCircuitException ex)
            {
                Console.WriteLine(ex.Message);
            }

            // Assert
            Assert.True(_isRetryCalled);
            Assert.True(_isCircuitBroken);
            Assert.True(_isCircuitRestored);
        }