public async void ReturnProjectStatusGivenValidArgs()
        {
            // arrange
            const string projectName = "asdf";
            const string expected    = "{'name' : 'Test McGee'}";

            JenkinsSettings settings = new JenkinsSettings();

            settings.HostURL = "http://example.org";

            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When($"{settings.HostURL}/job/{projectName}/api/json?depth=1")
            .Respond("application/json", expected);

            HttpClient httpClient = new HttpClient(mockHttp);

            JenkinsApiClient jenkinsApiClient = new JenkinsApiClient(settings, httpClient);

            // act
            var jsonObject = await jenkinsApiClient.GetProjectStatus(projectName, 1);

            // assert
            var actual = JsonConvert.SerializeObject(jsonObject);

            Assert.Equal(FormatJson(expected), FormatJson(actual));
        }
        public async void ReturnAssetGivenValidArgs()
        {
            // arrange
            const string projectName = "asdf";
            const string buildId     = "qwer";
            const string asset       = "zxcv";
            const string expected    = "hjkl";

            JenkinsSettings settings = new JenkinsSettings();

            settings.HostURL = "http://example.org";

            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When($"{settings.HostURL}/job/{projectName}/{buildId}/{asset}")
            .Respond("application/octet-stream", expected);

            HttpClient httpClient = new HttpClient(mockHttp);

            JenkinsApiClient jenkinsApiClient = new JenkinsApiClient(settings, httpClient);

            // act
            var assetContent = await jenkinsApiClient.GetAsset(projectName, buildId, asset);

            // assert
            var actual = System.Text.Encoding.UTF8.GetString(assetContent);

            Assert.Equal(expected, actual);
        }
 public JenkinsRCovStatsController(
     ILogger <JenkinsRCovStatsController> logger,
     JenkinsApiClient apiClient)
 {
     this.logger    = logger;
     this.apiClient = apiClient;
 }
 public JenkinsJUnitStatsController(
     IOptions <CachingSettings> settings,
     ILogger <JenkinsJUnitStatsController> logger,
     JenkinsApiClient apiClient,
     HttpClient httpClient)
 {
     this.settings   = settings.Value;
     this.logger     = logger;
     this.apiClient  = apiClient;
     this.httpClient = httpClient;
 }
Example #5
0
        public async void ReturnJUnitStatsBadgeWithoutCachingGivenValidArgs(string caption, int failCount, int skipCount, int totalCount, string expectedColor)
        {
            // arrange

            // setup mock HTTP client
            const string projectName = "asdf";
            const string buildId     = "qwer";

            string expectedJson = "{\"actions\":[{\"causes\":[{\"shortDescription\":\"Started by user ACME Worker\",\"userId\":\"acme\",\"userName\":\"ACME Worker\"}]},{},{\"buildsByBranchName\":{\"refs/remotes/origin/develop\":{\"buildNumber\":75,\"buildResult\":null,\"marked\":{\"SHA1\":\"90e22e2f39a8ffff3488c5faaa2f1e2e801cd000\",\"branch\":[{\"SHA1\":\"90e22e2f39a8ffff3488c5faaa2f1e2e801cd000\",\"name\":\"refs/remotes/origin/develop\"}]},\"revision\":{\"SHA1\":\"90e22e2f39a8ffff3488c5faaa2f1e2e801cd000\",\"branch\":[{\"SHA1\":\"90e22e2f39a8ffff3488c5faaa2f1e2e801cd000\",\"name\":\"refs/remotes/origin/develop\"}]}}},\"lastBuiltRevision\":{\"SHA1\":\"90e22e2f39a8ffff3488c5faaa2f1e2e801cd000\",\"branch\":[{\"SHA1\":\"90e22e2f39a8ffff3488c5faaa2f1e2e801cd000\",\"name\":\"refs/remotes/origin/develop\"}]},\"remoteUrls\":[\"[email protected]:acme/myproject.git\"],\"scmName\":\"\"},{},{},{\"failCount\":" + failCount.ToString() + ",\"skipCount\":" + skipCount.ToString() + ",\"totalCount\":" + totalCount.ToString() + ",\"urlName\":\"testReport\"},{},{},{}],\"artifacts\":[],\"building\":false,\"builtOn\":\"my_project_slave\",\"changeSet\":{\"items\":[],\"kind\":\"git\"},\"culprits\":[],\"description\":null,\"displayName\":\"#75\",\"duration\":675832,\"estimatedDuration\":666143,\"executor\":null,\"fullDisplayName\":\"My Project #75\",\"id\":\"75\",\"keepLog\":false,\"number\":75,\"queueId\":438162,\"result\":\"SUCCESS\",\"timestamp\":1467236801123,\"url\":\"http://ci.example.org/job/My%20Project/75/\"}";

            var settings = new JenkinsSettings()
            {
                HostURL = "http://example.org"
            };

            // setup mock HTTP handler for the API client
            var mockApiHttp = new MockHttpMessageHandler();

            mockApiHttp.When($"{settings.HostURL}/job/{projectName}/{buildId}/api/json?depth=0")
            .Respond("application/json", expectedJson);
            var apiHttpClient = new HttpClient(mockApiHttp);

            // setup mock HTTP handler for the shields.io HTTP client
            const string expectedSvg    = "<svg/>";
            var          mockShieldHttp = new MockHttpMessageHandler();
            var          realTotal      = totalCount - skipCount;
            var          badgeName      = $"{caption}-{realTotal - failCount}%2F{realTotal}-{expectedColor}.svg";

            mockShieldHttp.When($"https://img.shields.io/badge/{badgeName}")
            .Respond("image/svg+xml", expectedSvg);
            var shieldHttpClient = new HttpClient(mockShieldHttp);

            // setup the SUT
            var apiClient       = new JenkinsApiClient(settings, apiHttpClient);
            var logger          = new LoggerFactory().CreateLogger <JenkinsJUnitStatsController>();
            var cachingSettings = CreateCachingSettings(false);

            var controller = new JenkinsJUnitStatsController(cachingSettings, logger, apiClient, shieldHttpClient);

            // make sure the controller has a non-null Request property (used for logging)
            SetupControllerContext(controller);

            // act
            var response = await controller.Get(projectName, buildId, caption);

            // assert
            Assert.IsType <FileContentResult>(response);
            var content = response as FileContentResult;
            var actual  = System.Text.Encoding.UTF8.GetString(content.FileContents);

            Assert.Equal(expectedSvg, actual);
        }
Example #6
0
        public async void ReturnJaCoCoStatsBadgeGivenValidArgs(string percentageValue, string expectedBadgeText)
        {
            // arrange

            // setup mock HTTP client
            const string projectName = "asdf";
            const string buildId     = "qwer";

            string expected = "{\"actions\":[{\"causes\":[{\"shortDescription\":\"Started by user ACME Worker\",\"userId\":\"acme\",\"userName\":\"ACME Worker\"}]},{},{\"buildsByBranchName\":{\"refs/remotes/origin/develop\":{\"buildNumber\":75,\"buildResult\":null,\"marked\":{\"SHA1\":\"aaa22e2f39a8bbb83488c5f74eee1e2e801cffff\",\"branch\":[{\"SHA1\":\"aaa22e2f39a8bbb83488c5f74eee1e2e801cffff\",\"name\":\"refs/remotes/origin/develop\"}]},\"revision\":{\"SHA1\":\"aaa22e2f39a8bbb83488c5f74eee1e2e801cffff\",\"branch\":[{\"SHA1\":\"aaa22e2f39a8bbb83488c5f74eee1e2e801cffff\",\"name\":\"refs/remotes/origin/develop\"}]}}},\"lastBuiltRevision\":{\"SHA1\":\"aaa22e2f39a8bbb83488c5f74eee1e2e801cffff\",\"branch\":[{\"SHA1\":\"aaa22e2f39a8bbb83488c5f74eee1e2e801cffff\",\"name\":\"refs/remotes/origin/develop\"}]},\"remoteUrls\":[\"[email protected]:acme/myproject.git\"],\"scmName\":\"\"},{\"tags\":[]},{},{\"failCount\":0,\"skipCount\":5,\"totalCount\":292,\"urlName\":\"testReport\"},{\"branchCoverage\":{\"covered\":3109,\"missed\":72598,\"percentage\":4,\"percentageFloat\":4.1066213,\"total\":75707},\"classCoverage\":{\"covered\":393,\"missed\":896,\"percentage\":30,\"percentageFloat\":30.48875,\"total\":1289},\"complexityScore\":{\"covered\":2447,\"missed\":46340,\"percentage\":5,\"percentageFloat\":5.015681,\"total\":48787},\"instructionCoverage\":{\"covered\":83384,\"missed\":705189,\"percentage\":11,\"percentageFloat\":10.574037,\"total\":788573},\"lineCoverage\":{\"covered\":14409,\"missed\":137728,\"percentage\":9,\"percentageFloat\":9.471068,\"total\":152137},\"methodCoverage\":{\"covered\":1763,\"missed\":8913,\"percentage\":" + percentageValue + ",\"percentageFloat\":16.513676,\"total\":10676},\"previousResult\":{}},{},{}],\"artifacts\":[],\"building\":false,\"builtOn\":\"forge_6_slave\",\"changeSet\":{\"items\":[],\"kind\":\"git\"},\"culprits\":[],\"description\":null,\"displayName\":\"#75\",\"duration\":675832,\"estimatedDuration\":666143,\"executor\":null,\"fullDisplayName\":\"My Project #75\",\"id\":\"75\",\"keepLog\":false,\"number\":75,\"queueId\":438162,\"result\":\"SUCCESS\",\"timestamp\":1467236801123,\"url\":\"http://ci.dev.admin.net/job/My%20Project/75/\"}";

            var settings = new JenkinsSettings()
            {
                HostURL = "http://example.org"
            };

            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When($"{settings.HostURL}/job/{projectName}/{buildId}/api/json?depth=1")
            .Respond("application/json", expected);

            var httpClient = new HttpClient(mockHttp);

            // setup the SUT
            var apiClient  = new JenkinsApiClient(settings, httpClient);
            var logger     = new LoggerFactory().CreateLogger <JenkinsJaCoCoStatsController>();
            var controller = new JenkinsJaCoCoStatsController(logger, apiClient);

            // make sure the controller has a non-null Request property (used for logging)
            SetupControllerContext(controller);

            // set working directory so the test finds assets
            Directory.SetCurrentDirectory("../netcoreapp2.0/");

            // act
            var response = await controller.Get(projectName, buildId);

            // assert
            Assert.IsType <FileStreamResult>(response);
            var content = response as FileStreamResult;
            var actual  = new StreamReader(content.FileStream, Encoding.UTF8).ReadToEnd();

            Assert.Matches("<text ?.+>" + expectedBadgeText + "<\\/text>", actual);
        }
Example #7
0
        public async void ReturnBuildStatusBadgeGivenValidArgs(string successStatus, string expectedBadgeText)
        {
            // arrange

            // setup mock HTTP client
            const string projectName = "asdf";
            const string buildId     = "qwer";

            string expected = "{\"actions\":[{\"causes\":[{\"shortDescription\":\"Started by user ACME Worker\",\"userId\":\"acme\",\"userName\":\"ACME Worker\"}]},{},{\"buildsByBranchName\":{\"refs/remotes/origin/develop\":{\"buildNumber\":75,\"buildResult\":null,\"marked\":{\"SHA1\":\"90e22e2f39a8ffff3488c5faaa2f1e2e801cd000\",\"branch\":[{\"SHA1\":\"90e22e2f39a8ffff3488c5faaa2f1e2e801cd000\",\"name\":\"refs/remotes/origin/develop\"}]},\"revision\":{\"SHA1\":\"90e22e2f39a8ffff3488c5faaa2f1e2e801cd000\",\"branch\":[{\"SHA1\":\"90e22e2f39a8ffff3488c5faaa2f1e2e801cd000\",\"name\":\"refs/remotes/origin/develop\"}]}}},\"lastBuiltRevision\":{\"SHA1\":\"90e22e2f39a8ffff3488c5faaa2f1e2e801cd000\",\"branch\":[{\"SHA1\":\"90e22e2f39a8ffff3488c5faaa2f1e2e801cd000\",\"name\":\"refs/remotes/origin/develop\"}]},\"remoteUrls\":[\"[email protected]:acme/myproject.git\"],\"scmName\":\"\"},{},{},{\"failCount\":0,\"skipCount\":5,\"totalCount\":292,\"urlName\":\"testReport\"},{},{},{}],\"artifacts\":[],\"building\":false,\"builtOn\":\"my_project_slave\",\"changeSet\":{\"items\":[],\"kind\":\"git\"},\"culprits\":[],\"description\":null,\"displayName\":\"#75\",\"duration\":675832,\"estimatedDuration\":666143,\"executor\":null,\"fullDisplayName\":\"My Project #75\",\"id\":\"75\",\"keepLog\":false,\"number\":75,\"queueId\":438162,\"result\":\"" + successStatus + "\",\"timestamp\":1467236801123,\"url\":\"http://ci.example.org/job/My%20Project/75/\"}";

            var settings = new JenkinsSettings()
            {
                HostURL = "http://example.org"
            };

            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When($"{settings.HostURL}/job/{projectName}/{buildId}/api/json?depth=0")
            .Respond("application/json", expected);

            var httpClient = new HttpClient(mockHttp);

            // setup the SUT
            var apiClient  = new JenkinsApiClient(settings, httpClient);
            var logger     = new LoggerFactory().CreateLogger <JenkinsBuildStatusController>();
            var controller = new JenkinsBuildStatusController(logger, apiClient);

            // make sure the controller has a non-null Request property (used for logging)
            SetupControllerContext(controller);

            // set working directory so the test finds assets
            Directory.SetCurrentDirectory("../netcoreapp2.0/");

            // act
            var response = await controller.Get(projectName, buildId);

            // assert
            Assert.IsType <FileStreamResult>(response);
            var content = response as FileStreamResult;
            var actual  = new StreamReader(content.FileStream, Encoding.UTF8).ReadToEnd();

            Assert.Matches("<text ?.+>" + expectedBadgeText + "<\\/text>", actual);
        }
        public async void ReturnRCovStatsAmongMultipleHealthReports(string percentageValue, string expectedBadgeText)
        {
            // arrange

            // setup mock HTTP client
            const string projectName = "asdf";
            string       expected    = "{\"actions\":[{},{},{}],\"description\":\"\",\"displayName\":\"My Project\",\"displayNameOrNull\":null,\"name\":\"My Project\",\"url\":\"http://ci.example.org/job/My%20Project/\",\"buildable\":true,\"builds\":[{\"number\":124,\"url\":\"http://ci.example.org/job/My%20Project/124/\"},{\"number\":123,\"url\":\"http://ci.example.org/job/My%20Project/123/\"},{\"number\":122,\"url\":\"http://ci.example.org/job/My%20Project/122/\"},{\"number\":121,\"url\":\"http://ci.example.org/job/My%20Project/121/\"},{\"number\":120,\"url\":\"http://ci.example.org/job/My%20Project/120/\"},{\"number\":119,\"url\":\"http://ci.example.org/job/My%20Project/119/\"},{\"number\":118,\"url\":\"http://ci.example.org/job/My%20Project/118/\"},{\"number\":117,\"url\":\"http://ci.example.org/job/My%20Project/117/\"},{\"number\":116,\"url\":\"http://ci.example.org/job/My%20Project/116/\"},{\"number\":115,\"url\":\"http://ci.example.org/job/My%20Project/115/\"}],\"color\":\"blue\",\"firstBuild\":{\"number\":115,\"url\":\"http://ci.example.org/job/My%20Project/115/\"},\"healthReport\":[{\"description\":\"Rcov coverage: Code coverage 92.66%(" + percentageValue + ")\",\"iconClassName\":\"icon-health-80plus\",\"iconUrl\":\"health-80plus.png\",\"score\":100},{\"description\":\"Build stability: No recent builds failed.\",\"iconClassName\":\"icon-health-80plus\",\"iconUrl\":\"health-80plus.png\",\"score\":100}],\"inQueue\":false,\"keepDependencies\":false,\"lastBuild\":{\"number\":124,\"url\":\"http://ci.example.org/job/My%20Project/124/\"},\"lastCompletedBuild\":{\"number\":124,\"url\":\"http://ci.example.org/job/My%20Project/124/\"},\"lastFailedBuild\":{\"number\":119,\"url\":\"http://ci.example.org/job/My%20Project/119/\"},\"lastStableBuild\":{\"number\":124,\"url\":\"http://ci.example.org/job/My%20Project/124/\"},\"lastSuccessfulBuild\":{\"number\":124,\"url\":\"http://ci.example.org/job/My%20Project/124/\"},\"lastUnstableBuild\":null,\"lastUnsuccessfulBuild\":{\"number\":119,\"url\":\"http://ci.example.org/job/My%20Project/119/\"},\"nextBuildNumber\":125,\"property\":[{},{\"stageName\":\"CI\",\"taskName\":\"Rspec Testing\"},{\"customMessage\":\"\",\"includeCustomMessage\":false,\"includeTestSummary\":false,\"notifyAborted\":false,\"notifyBackToNormal\":false,\"notifyFailure\":false,\"notifyNotBuilt\":false,\"notifyRepeatedFailure\":false,\"notifySuccess\":false,\"notifyUnstable\":false,\"room\":\"\",\"showCommitList\":false,\"startNotification\":false,\"teamDomain\":\"\",\"token\":\"\"}],\"queueItem\":null,\"concurrentBuild\":false,\"downstreamProjects\":[{\"name\":\"My Project - Static Analysis\",\"url\":\"http://ci.example.org/job/My%20Project%20-%20Static%20Analysis/\",\"color\":\"blue\"}],\"scm\":{},\"upstreamProjects\":[]}";

            var settings = new JenkinsSettings()
            {
                HostURL = "http://example.org"
            };

            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When($"{settings.HostURL}/job/{projectName}/api/json?depth=0")
            .Respond("application/json", expected);

            var httpClient = new HttpClient(mockHttp);

            // setup the SUT
            var apiClient  = new JenkinsApiClient(settings, httpClient);
            var logger     = new LoggerFactory().CreateLogger <JenkinsRCovStatsController>();
            var controller = new JenkinsRCovStatsController(logger, apiClient);

            // make sure the controller has a non-null Request property (used for logging)
            SetupControllerContext(controller);

            // set working directory so the test finds assets
            Directory.SetCurrentDirectory("../netcoreapp2.0/");

            // act
            var actual = await controller.Get(projectName);

            // assert
            Assert.IsType <FileStreamResult>(actual);
            var result  = actual as FileStreamResult;
            var content = new StreamReader(result.FileStream, Encoding.UTF8).ReadToEnd();

            Assert.DoesNotMatch("<text ?.+>error<\\/text>", content);
            Assert.Matches("<text ?.+>" + expectedBadgeText + "<\\/text>", content);
        }
Example #9
0
        public async void ReturnAssetGivenValidArgs()
        {
            // arrange

            // setup mock HTTP client
            const string projectName = "asdf";
            const string buildId     = "qwer";
            const string asset       = "hjkl.png";

            const string expected = "uiop";

            var settings = new JenkinsSettings()
            {
                HostURL = "http://example.org"
            };

            var mockHttp = new MockHttpMessageHandler();

            mockHttp.When($"{settings.HostURL}/job/{projectName}/{buildId}/{asset}")
            .Respond("image/png", expected);

            var httpClient = new HttpClient(mockHttp);

            // setup the SUT
            var apiClient  = new JenkinsApiClient(settings, httpClient);
            var logger     = new LoggerFactory().CreateLogger <JenkinsAssetsController>();
            var controller = new JenkinsAssetsController(logger, apiClient);

            // make sure the controller has a non-null Request property (used for logging)
            SetupControllerContext(controller);

            // act
            var response = await controller.Get(projectName, buildId, asset);

            // assert
            Assert.IsType <FileContentResult>(response);
            var content = response as FileContentResult;
            var actual  = System.Text.Encoding.UTF8.GetString(content.FileContents);

            Assert.Equal(expected, actual);
        }