SonarConnection implements MetricCapability, TestCapability { @Override public TestResult analyzeIntegrationTests(SoftwareProjectId projectId) { checkConnected(); checkSoftwareProjectId(projectId); TestResult integrationTestResult = new TestResult(); try { String artifactId = projectId.getProjectId(); if (Strings.isNullOrEmpty(artifactId)) { if (LOG.isDebugEnabled()) { LOG.debug("can't analyze project " + projectId + " without artifactId. Is it a maven project ?"); } } else { Measure itCoverageMeasure = sonarClient.findMeasure(artifactId, "it_coverage"); if (itCoverageMeasure != null) { Double itCoverage = itCoverageMeasure.getValue(); integrationTestResult.setCoverage(itCoverage); } } } catch (SonarMeasureNotFoundException e) { if (LOG.isDebugEnabled()) { LOG.debug("Integration tests informations are not available for project " + projectId + ", cause " + e.getMessage()); } } return integrationTestResult; } SonarConnection(); @Override void connect(String url, String login, String password); @Override Map<String, List<QualityMetric>> getMetricsByCategory(); @Override Map<SoftwareProjectId, String> listSoftwareProjectIds(); @Override void close(); @Override boolean equals(Object o); @Override int hashCode(); @Override String getDescription(SoftwareProjectId softwareProjectId); @Override SoftwareProjectId identify(ProjectKey projectKey); @Override TestResult analyzeUnitTests(SoftwareProjectId projectId); @Override TestResult analyzeIntegrationTests(SoftwareProjectId projectId); @Override QualityResult analyzeQuality(SoftwareProjectId projectId, String... metrics); @Override String getMavenId(SoftwareProjectId softwareProjectId); @Override String getName(SoftwareProjectId softwareProjectId); @Override boolean isClosed(); @Override boolean isProjectDisabled(SoftwareProjectId softwareProjectId); BuildTime getBuildTime(SoftwareProjectId softwareProjectId, Integer buildId); @Deprecated // NOT USED List<Integer> getBuildIds(SoftwareProjectId softwareProjectId); @Deprecated // NOT USED BuildState getBuildState(SoftwareProjectId softwareProjectId, Integer buildId); @Deprecated // NOT USED Date getEstimatedFinishTime(SoftwareProjectId softwareProjectId, Integer buildId); @Deprecated // NOT USED boolean isBuilding(SoftwareProjectId softwareProjectId, Integer buildId); @Deprecated // NOT USED int getLastBuildId(SoftwareProjectId softwareProjectId); @Deprecated // NOT USED List<Commiter> getBuildCommiters(SoftwareProjectId softwareProjectId, Integer buildId);  }
@Test public void should_build_valid_integration_test_result() throws SonarMeasureNotFoundException { Measure measure = new Measure(); measure.setValue(8D); when(sonarClient.findMeasure("artifactId", "it_coverage")).thenReturn(measure); SoftwareProjectId projectId = new SoftwareProjectId("artifactId"); TestResult integrationTestResult = sonar.analyzeIntegrationTests(projectId); assertEquals(8, integrationTestResult.getCoverage(), 0); }
@Test public void should_not_fail_if_quality_measures_are_not_found() throws SonarMeasureNotFoundException { SoftwareProjectId projectId = new SoftwareProjectId("artifactId"); when(sonarClient.findMeasure(anyString(), anyString())).thenThrow( new SonarMeasureNotFoundException("not found")); sonar.analyzeIntegrationTests(projectId); }
@Test public void should_not_fail_if_project_id_has_no_artifact_id() { SoftwareProjectId projectId = new SoftwareProjectId(""); sonar.analyzeIntegrationTests(projectId); }