Sonar { public Project findProject(String projectKey) throws SonarProjectNotFoundException { Preconditions.checkNotNull(projectKey, "projectKey is mandatory"); try { List<Project> projects = findProjects().getProjects(); for (Project project : projects) { if (project.getKey().equals(projectKey)) { return project; } } throw new SonarProjectNotFoundException("Can't find Sonar project with resourceId: '" + projectKey + "'"); } catch (SonarProjectsNotFoundException e) { throw new SonarProjectNotFoundException("Can't find Sonar project with resourceId: '" + projectKey + "'", e); } } Sonar(String url);  Sonar(String url, String login, String password); Measure findMeasure(String artifactId, String measureKey); Resource findResource(String resourceId); Map<String, SonarQualityMetric> findMetrics(); Projects findProjects(); Project findProject(String projectKey);  }
@Test(expected = SonarProjectNotFoundException.class) public void should_throw_exception_when_project_is_not_found() throws Exception { Projects projects = new Projects(); when(genericSoftwareClient.resource(anyString(), eq(Projects.class), eq(MediaType.APPLICATION_XML_TYPE))) .thenReturn(projects); sonarClient.findProject(""); }
@Test(expected = SonarProjectNotFoundException.class) public void should_throw_exception_when_projects_are_not_found_and_searching_for_a_project() throws Exception { Throwable exception = new ResourceNotFoundException(""); when(genericSoftwareClient.resource(anyString(), eq(Projects.class), eq(MediaType.APPLICATION_XML_TYPE))) .thenThrow(exception); sonarClient.findProject(""); }