public ProjectRepositories addFileDataToModule(String moduleKey, @Nullable String path, FileData fileData) { if (path == null || (fileData.hash() == null && fileData.revision() == null)) { return this; } SingleProjectRepository repository = repositoryPerModule.computeIfAbsent(moduleKey, k -> new SingleProjectRepository()); repository.addFileData(path, fileData); return this; }
@Test public void add_file_data() { FileData fileData = new FileData("123", "456"); repository.addFileData("/Abc.java", fileData); assertThat(repository.fileData()).hasSize(1); assertThat(repository.fileData()).contains(Maps.immutableEntry("/Abc.java", fileData)); assertThat(repository.fileDataByPath("/Abc.java")).isEqualTo(fileData); }
@Test public void add_file_data_doesnt_add_the_file_without_path() { FileData fileData = new FileData("123", "456"); repository.addFileData(null, fileData); assertThat(repository.fileData()).hasSize(0); }
@Test public void return_file_data_from_single_project() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organizationDto); userSession.logIn().addProjectPermission(SCAN_EXECUTION, project); ComponentDto file = db.components().insertComponent(newFileDto(project)); dbClient.fileSourceDao().insert(dbSession, newFileSourceDto(file).setSrcHash("123456")); db.commit(); ProjectRepositories ref = underTest.load(ProjectDataQuery.create().setProjectKey(project.getKey())); assertTrue(ref instanceof SingleProjectRepository); SingleProjectRepository singleProjectRepository = ((SingleProjectRepository) ref); assertThat(singleProjectRepository.fileData()).hasSize(1); FileData fileData = singleProjectRepository.fileDataByPath(file.path()); assertThat(fileData).isNotNull(); assertThat(fileData.hash()).isEqualTo("123456"); }
@Before public void setUp() { repository = new SingleProjectRepository(); }
private static void addFileData(SingleProjectRepository data, List<FilePathWithHashDto> files) { for (FilePathWithHashDto file : files) { FileData fileData = new FileData(file.getSrcHash(), file.getRevision()); data.addFileData(file.getPath(), fileData); } }
@CheckForNull public FileData fileData(String moduleKeyWithBranch, @Nullable String path) { Optional<SingleProjectRepository> moduleRepository = Optional.ofNullable(repositoryPerModule.get(moduleKeyWithBranch)); return moduleRepository .map(singleProjectRepository -> singleProjectRepository.fileDataByPath(path)) .orElse(null); } }
private static Map<String, WsProjectResponse.FileData> buildFileDataByPath(SingleProjectRepository data) { return data.fileData().entrySet() .stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> toFileDataResponse(e.getValue()))); }
@Test public void add_file_data_doesnt_add_the_file_without_revision_and_hash() { FileData fileData = new FileData(null, null); repository.addFileData("/Abc.java", fileData); assertThat(repository.fileData()).hasSize(0); } }
data = repository; } else { SingleProjectRepository repository = new SingleProjectRepository(); addFileData(repository, files); data = repository;
@CheckForNull public FileData fileData(String moduleKeyWithBranch, @Nullable String path) { Optional<SingleProjectRepository> moduleRepository = Optional.ofNullable(repositoryPerModule.get(moduleKeyWithBranch)); return moduleRepository .map(singleProjectRepository -> singleProjectRepository.fileDataByPath(path)) .orElse(null); } }
private static Map<String, WsProjectResponse.FileDataByPath> buildFileDataByModuleAndPath(MultiModuleProjectRepository data) { return data.repositoriesByModule().entrySet() .stream() .map(entry -> Maps.immutableEntry(entry.getKey(), buildFileDataByPath(entry.getValue().fileData()))) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); }
@Test public void use_new_file_structure_for_projects_without_submodules() { String projectKey = "org.codehaus.sonar:sonar"; ProjectRepositories projectRepositories = new SingleProjectRepository() .addFileData("src/main/java/SomeClass.java", new FileData("789456", "123456789")); when(projectDataLoader.load(any(ProjectDataQuery.class))).thenReturn(projectRepositories); WsProjectResponse wsProjectResponse = ws.newRequest() .setParam("key", projectKey) .setParam("profile", "Default") .executeProtobuf(WsProjectResponse.class); assertThat(wsProjectResponse.getFileDataByModuleAndPathMap()).isEmpty(); assertThat(wsProjectResponse.getFileDataByPathCount()).isEqualTo(1); assertThat(wsProjectResponse.getFileDataByPathMap().get("src/main/java/SomeClass.java")).isNotNull(); }
public ProjectRepositories addFileDataToModule(String moduleKey, @Nullable String path, FileData fileData) { if (path == null || (fileData.hash() == null && fileData.revision() == null)) { return this; } SingleProjectRepository repository = repositoryPerModule.computeIfAbsent(moduleKey, k -> new SingleProjectRepository()); repository.addFileData(path, fileData); return this; }