@Override public ProjectRepositories load(String projectKey, boolean isIssuesMode, @Nullable String branchBase) { return new SingleProjectRepository(fileDataMap, lastAnalysisDate); }
@CheckForNull public FileData fileData(String moduleKeyWithBranch, String path) { SingleProjectRepository repository = repositoriesPerModule.get(moduleKeyWithBranch); return repository == null ? null : repository.fileData(path); }
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())); } }
@CheckForNull public FileData fileData(String moduleKeyWithBranch, DefaultInputFile inputFile) { if (this instanceof SingleProjectRepository) { return ((SingleProjectRepository) this).fileData(inputFile.getProjectRelativePath()); } else { return ((MultiModuleProjectRepository) this).fileData(moduleKeyWithBranch, inputFile.getModuleRelativePath()); } }
@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 test_file_data_when_file_does_not_exist() { FileData fileData = repository.fileData("/Def.java"); assertThat(fileData).isNull(); } }
@Before public void setUp() { MockitoAnnotations.initMocks(this); Map<String, FileData> fileMap = Maps.newHashMap(); project = new SingleProjectRepository(fileMap, new Date()); provider = new ProjectRepositoriesProvider(); when(props.getKeyWithBranch()).thenReturn("key"); }
@Test public void test_file_data_when_file_exists() { FileData fileData = repository.fileData("/Abc.java"); assertNotNull(fileData); assertThat(fileData.hash()).isEqualTo("123"); assertThat(fileData.revision()).isEqualTo("456"); }
@Test public void detect_status_without_metadata() { DefaultInputFile mockedFile = mock(DefaultInputFile.class); when(mockedFile.getProjectRelativePath()).thenReturn("module/src/Foo.java"); when(mockedFile.path()).thenReturn(Paths.get("module", "src", "Foo.java")); SingleProjectRepository ref = new SingleProjectRepository(createFileDataPerPathMap(), null); ScmChangedFiles changedFiles = new ScmChangedFiles(Collections.singletonList(Paths.get("module", "src", "Foo.java"))); StatusDetection statusDetection = new StatusDetection(ref, changedFiles); assertThat(statusDetection.getStatusWithoutMetadata("foo", mockedFile)).isEqualTo(InputFile.Status.ADDED); verify(mockedFile).path(); verify(mockedFile).getProjectRelativePath(); verifyNoMoreInteractions(mockedFile); }
@CheckForNull public FileData fileData(String moduleKeyWithBranch, String path) { SingleProjectRepository repository = repositoriesPerModule.get(moduleKeyWithBranch); return repository == null ? null : repository.fileData(path); }
@Test public void detect_status_branches_confirm() { SingleProjectRepository ref = new SingleProjectRepository(createFileDataPerPathMap(), null); ScmChangedFiles changedFiles = new ScmChangedFiles(Collections.singletonList(Paths.get("module", "src", "Foo.java"))); StatusDetection statusDetection = new StatusDetection(ref, changedFiles); assertThat(statusDetection.status("foo", createFile("src/Foo.java"), "XXXXX")).isEqualTo(InputFile.Status.CHANGED); }
@CheckForNull public FileData fileData(String moduleKeyWithBranch, DefaultInputFile inputFile) { if (this instanceof SingleProjectRepository) { return ((SingleProjectRepository) this).fileData(inputFile.getProjectRelativePath()); } else { return ((MultiModuleProjectRepository) this).fileData(moduleKeyWithBranch, inputFile.getModuleRelativePath()); } }
@Before public void setUp() { Date lastAnalysisDate = new Date(); repository = new SingleProjectRepository(Maps.newHashMap("/Abc.java", new FileData("123", "456")), lastAnalysisDate); }
@Test public void detect_status_branches_exclude() { SingleProjectRepository ref = new SingleProjectRepository(createFileDataPerPathMap(), null); ScmChangedFiles changedFiles = new ScmChangedFiles(Collections.emptyList()); StatusDetection statusDetection = new StatusDetection(ref, changedFiles); // normally changed assertThat(statusDetection.status("foo", createFile("src/Foo.java"), "XXXXX")).isEqualTo(InputFile.Status.SAME); // normally added assertThat(statusDetection.status("foo", createFile("src/Other.java"), "QWERT")).isEqualTo(InputFile.Status.SAME); }
@Before public void setUp() { Date lastAnalysisDate = new Date(); SingleProjectRepository repository1 = new SingleProjectRepository(Maps.newHashMap("/Abc.java", new FileData("123", "456")), lastAnalysisDate); SingleProjectRepository repository2 = new SingleProjectRepository(Maps.newHashMap("/Def.java", new FileData("567", "321")), lastAnalysisDate); Map<String, SingleProjectRepository> moduleRepositories = Maps.newHashMap("module1", repository1); moduleRepositories.put("module2", repository2); repository = new MultiModuleProjectRepository(moduleRepositories, lastAnalysisDate); }
@Test public void detect_status() { SingleProjectRepository ref = new SingleProjectRepository(createFileDataPerPathMap(), null); ScmChangedFiles changedFiles = new ScmChangedFiles(null); StatusDetection statusDetection = new StatusDetection(ref, changedFiles); assertThat(statusDetection.status("foo", createFile("src/Foo.java"), "ABCDE")).isEqualTo(InputFile.Status.SAME); assertThat(statusDetection.status("foo", createFile("src/Foo.java"), "XXXXX")).isEqualTo(InputFile.Status.CHANGED); assertThat(statusDetection.status("foo", createFile("src/Other.java"), "QWERT")).isEqualTo(InputFile.Status.ADDED); }
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(); } }