private static ProjectRepositories processStream(InputStream is) throws IOException { WsProjectResponse response = WsProjectResponse.parseFrom(is); if (response.getFileDataByModuleAndPathCount() == 0) { return new SingleProjectRepository(constructFileDataMap(response.getFileDataByPathMap()), new Date(response.getLastAnalysisDate())); } else { final Map<String, SingleProjectRepository> repositoriesPerModule = new HashMap<>(); response.getFileDataByModuleAndPathMap().keySet().forEach(moduleKey -> { WsProjectResponse.FileDataByPath filePaths = response.getFileDataByModuleAndPathMap().get(moduleKey); repositoriesPerModule.put(moduleKey, new SingleProjectRepository( constructFileDataMap(filePaths.getFileDataByPathMap()), new Date(response.getLastAnalysisDate()))); }); return new MultiModuleProjectRepository(repositoriesPerModule, new Date(response.getLastAnalysisDate())); } }
@Override public ProjectRepositories load(String projectKey, boolean issuesMode, @Nullable String branchBase) { GetRequest request = new GetRequest(getUrl(projectKey, issuesMode, branchBase)); try (WsResponse response = wsClient.call(request)) { try (InputStream is = response.contentStream()) { return processStream(is); } catch (IOException e) { throw new IllegalStateException("Couldn't load project repository for " + projectKey, e); } } catch (RuntimeException e) { if (shouldThrow(e)) { throw e; } LOG.debug("Project repository not available - continuing without it"); return new SingleProjectRepository(); } }
@Test public void deserializeResponse() throws IOException { loader.load(PROJECT_KEY, false, null); }
@Before public void prepare() throws IOException { wsClient = mock(ScannerWsClient.class); InputStream is = mockData(); WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is); loader = new DefaultProjectRepositoriesLoader(wsClient); }
@Override public ProjectRepositories load(String projectKey, boolean issuesMode, @Nullable String branchBase) { GetRequest request = new GetRequest(getUrl(projectKey, issuesMode, branchBase)); try (WsResponse response = wsClient.call(request)) { try (InputStream is = response.contentStream()) { return processStream(is); } catch (IOException e) { throw new IllegalStateException("Couldn't load project repository for " + projectKey, e); } } catch (RuntimeException e) { if (shouldThrow(e)) { throw e; } LOG.debug("Project repository not available - continuing without it"); return new SingleProjectRepository(); } }
@Test public void parsingError() throws IOException { InputStream is = mock(InputStream.class); when(is.read()).thenThrow(IOException.class); WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=foo%3F", is); loader.load(PROJECT_KEY, false, null); }
private static ProjectRepositories processStream(InputStream is) throws IOException { WsProjectResponse response = WsProjectResponse.parseFrom(is); if (response.getFileDataByModuleAndPathCount() == 0) { return new SingleProjectRepository(constructFileDataMap(response.getFileDataByPathMap()), new Date(response.getLastAnalysisDate())); } else { final Map<String, SingleProjectRepository> repositoriesPerModule = new HashMap<>(); response.getFileDataByModuleAndPathMap().keySet().forEach(moduleKey -> { WsProjectResponse.FileDataByPath filePaths = response.getFileDataByModuleAndPathMap().get(moduleKey); repositoriesPerModule.put(moduleKey, new SingleProjectRepository( constructFileDataMap(filePaths.getFileDataByPathMap()), new Date(response.getLastAnalysisDate()))); }); return new MultiModuleProjectRepository(repositoriesPerModule, new Date(response.getLastAnalysisDate())); } }
@Test public void continueOnError() { when(wsClient.call(any(WsRequest.class))).thenThrow(IllegalStateException.class); ProjectRepositories proj = loader.load(PROJECT_KEY, false, null); assertThat(proj.exists()).isEqualTo(false); }
@Test public void passAndEncodeProjectKeyParameter() { loader.load(PROJECT_KEY, false, null); WsTestUtil.verifyCall(wsClient, "/batch/project.protobuf?key=foo%3F"); }
@Test public void readRealResponse() throws IOException { InputStream is = getTestResource("project.protobuf"); WsTestUtil.mockStream(wsClient, "/batch/project.protobuf?key=org.sonarsource.github%3Asonar-github-plugin&issues_mode=true", is); DefaultInputFile file = mock(DefaultInputFile.class); when(file.getModuleRelativePath()).thenReturn("src/test/java/org/sonar/plugins/github/PullRequestIssuePostJobTest.java"); ProjectRepositories proj = loader.load("org.sonarsource.github:sonar-github-plugin", true, null); FileData fd = proj.fileData("org.sonarsource.github:sonar-github-plugin", file); assertThat(fd.revision()).isEqualTo("27bf2c54633d05c5df402bbe09471fe43bd9e2e5"); assertThat(fd.hash()).isEqualTo("edb6b3b9ab92d8dc53ba90ab86cd422e"); }
@Test(expected = IllegalStateException.class) public void failFastHttpError() { HttpException http = new HttpException("url", 403, null); IllegalStateException e = new IllegalStateException("http error", http); WsTestUtil.mockException(wsClient, e); loader.load(PROJECT_KEY, false, null); }
@Test public void failFastHttpErrorMessageException() { thrown.expect(MessageException.class); thrown.expectMessage("http error"); HttpException http = new HttpException("uri", 403, null); MessageException e = MessageException.of("http error", http); WsTestUtil.mockException(wsClient, e); loader.load(PROJECT_KEY, false, null); }