@Test public void scrollLineHashes_handles_scrolling_more_than_1000_files() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = new Random().nextBoolean() ? dbTester.components().insertPrivateProject(organization) : dbTester.components().insertPublicProject(organization); List<ComponentDto> files = IntStream.range(0, 1001 + new Random().nextInt(5)) .mapToObj(i -> { ComponentDto file = dbTester.components().insertComponent(newFileDto(project)); dbTester.fileSources().insertFileSource(file); return file; }) .collect(Collectors.toList()); LineHashesWithKeyDtoHandler handler = new LineHashesWithKeyDtoHandler(); underTest.scrollLineHashes(dbSession, files.stream().map(ComponentDto::uuid).collect(Collectors.toSet()), handler); assertThat(handler.dtos).hasSize(files.size()); files.forEach(t -> assertThat(handler.getByUuid(t.uuid())).isPresent()); }
@Test public void scrollAllFilesForFileMove_scrolls_large_number_of_files_and_uts() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = random.nextBoolean() ? db.components().insertPrivateProject(organization) : db.components().insertPublicProject(organization); List<ComponentAndSource> files = IntStream.range(0, 300 + random.nextInt(500)) .mapToObj(i -> { String qualifier = random.nextBoolean() ? FILE : UNIT_TEST_FILE; ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project).setDbKey("f_" + i).setQualifier(qualifier)); FileSourceDto fileSource = db.fileSources().insertFileSource(file); return new ComponentAndSource(file, fileSource); }) .collect(Collectors.toList()); RecordingResultHandler resultHandler = new RecordingResultHandler(); underTest.scrollAllFilesForFileMove(dbSession, project.uuid(), resultHandler); assertThat(resultHandler.dtos).hasSize(files.size()); files.forEach(f -> verifyFileMoveRowDto(resultHandler, f)); }
@Test public void scrollLineHashes_does_not_scroll_hashes_of_component_without_path() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = new Random().nextBoolean() ? dbTester.components().insertPrivateProject(organization) : dbTester.components().insertPublicProject(organization); ComponentDto file1 = dbTester.components().insertComponent(newFileDto(project)); FileSourceDto fileSource1 = dbTester.fileSources().insertFileSource(file1); ComponentDto file2 = dbTester.components().insertComponent(newFileDto(project).setPath(null)); FileSourceDto fileSource2 = dbTester.fileSources().insertFileSource(file2); LineHashesWithKeyDtoHandler handler = scrollLineHashes(file2.uuid(), file1.uuid()); assertThat(handler.dtos).hasSize(1); verifyLinesHashes(handler, file1, fileSource1); }
private ComponentAndSource insertFileAndSource(ComponentDto parent, String qualifier) { ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(parent).setQualifier(qualifier)); FileSourceDto fileSource = db.fileSources().insertFileSource(file); return new ComponentAndSource(file, fileSource); }
@Test public void should_cache_line_hash_version_from_db() { ComponentDto project = db.components().insertPublicProject(); ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project)); db.fileSources().insertFileSource(file, dto -> dto.setLineHashesVersion(LineHashVersion.WITH_SIGNIFICANT_CODE.getDbValue())); Component component = ReportComponent.builder(Component.Type.FILE, 1).setKey("key").setUuid(file.uuid()).build(); assertThat(underTest.hasLineHashesWithSignificantCode(component)).isTrue(); assertThat(db.countRowsOfTable("file_sources")).isOne(); db.executeUpdateSql("delete from file_sources"); db.commit(); assertThat(db.countRowsOfTable("file_sources")).isZero(); // still true because it uses cache assertThat(underTest.hasLineHashesWithSignificantCode(component)).isTrue(); } }
@Test public void scrollLineHashes_scrolls_hashes_of_specific_keys() { OrganizationDto organization = dbTester.organizations().insert(); ComponentDto project = new Random().nextBoolean() ? dbTester.components().insertPrivateProject(organization) : dbTester.components().insertPublicProject(organization); ComponentDto file1 = dbTester.components().insertComponent(newFileDto(project)); FileSourceDto fileSource1 = dbTester.fileSources().insertFileSource(file1); ComponentDto file2 = dbTester.components().insertComponent(newFileDto(project)); FileSourceDto fileSource2 = dbTester.fileSources().insertFileSource(file2); ComponentDto file3 = dbTester.components().insertComponent(newFileDto(project)); FileSourceDto fileSource3 = dbTester.fileSources().insertFileSource(file3); LineHashesWithKeyDtoHandler handler = scrollLineHashes(file1.uuid()); assertThat(handler.dtos).hasSize(1); verifyLinesHashes(handler, file1, fileSource1); handler = scrollLineHashes(file2.uuid()); assertThat(handler.dtos).hasSize(1); verifyLinesHashes(handler, file2, fileSource2); handler = scrollLineHashes(file2.uuid(), file1.uuid(), file3.uuid()); assertThat(handler.dtos).hasSize(3); verifyLinesHashes(handler, file1, fileSource1); verifyLinesHashes(handler, file2, fileSource2); verifyLinesHashes(handler, file3, fileSource3); }
@Test public void show_hashes() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organizationDto); ComponentDto file = db.components().insertComponent(newFileDto(project)); FileSourceDto fileSource = db.fileSources().insertFileSource(file, f -> f.setLineHashes(singletonList("ABC"))); loginAsProjectViewer(project); TestRequest request = tester.newRequest().setParam("key", file.getDbKey()); assertThat(request.execute().getInput()).isEqualTo("ABC"); }
@Test public void scrollAllFilesForFileMove_ignores_non_file_and_non_ut_component_with_source() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = random.nextBoolean() ? db.components().insertPrivateProject(organization) : db.components().insertPublicProject(organization); db.fileSources().insertFileSource(project); ComponentDto module = db.components().insertComponent(ComponentTesting.newModuleDto(project)); db.fileSources().insertFileSource(module); ComponentDto dir = db.components().insertComponent(ComponentTesting.newDirectory(module, "foo")); db.fileSources().insertFileSource(dir); ComponentAndSource file = insertFileAndSource(module, FILE); ComponentAndSource ut = insertFileAndSource(dir, UNIT_TEST_FILE); ComponentDto portfolio = random.nextBoolean() ? db.components().insertPublicPortfolio(organization) : db.components().insertPrivatePortfolio(organization); db.fileSources().insertFileSource(portfolio); ComponentDto subView = db.components().insertSubView(portfolio); db.fileSources().insertFileSource(subView); ComponentDto application = random.nextBoolean() ? db.components().insertPrivateApplication(organization) : db.components().insertPublicApplication(organization); db.fileSources().insertFileSource(application); RecordingResultHandler resultHandler = new RecordingResultHandler(); underTest.scrollAllFilesForFileMove(dbSession, project.uuid(), resultHandler); underTest.scrollAllFilesForFileMove(dbSession, portfolio.uuid(), resultHandler); underTest.scrollAllFilesForFileMove(dbSession, application.uuid(), resultHandler); assertThat(resultHandler.dtos).hasSize(2); verifyFileMoveRowDto(resultHandler, file); verifyFileMoveRowDto(resultHandler, ut); }
@Test public void hasLineHashWithSignificantCode_should_return_true() { ComponentDto project = db.components().insertPublicProject(); ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project)); db.fileSources().insertFileSource(file, dto -> dto.setLineHashesVersion(LineHashVersion.WITH_SIGNIFICANT_CODE.getDbValue())); Component component = ReportComponent.builder(Component.Type.FILE, 1).setKey("key").setUuid(file.uuid()).build(); assertThat(underTest.hasLineHashesWithSignificantCode(component)).isTrue(); }
@Test public void scrollAllFilesForFileMove_ignores_files_with_null_path() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = random.nextBoolean() ? db.components().insertPrivateProject(organization) : db.components().insertPublicProject(organization); ComponentAndSource file = insertFileAndSource(project, FILE); ComponentAndSource ut = insertFileAndSource(project, UNIT_TEST_FILE); ComponentDto fileNoPath = db.components().insertComponent(ComponentTesting.newFileDto(project).setPath(null).setQualifier(FILE)); db.fileSources().insertFileSource(fileNoPath); ComponentDto utNoPath = db.components().insertComponent(ComponentTesting.newFileDto(project).setPath(null).setQualifier(UNIT_TEST_FILE)); db.fileSources().insertFileSource(utNoPath); RecordingResultHandler resultHandler = new RecordingResultHandler(); underTest.scrollAllFilesForFileMove(dbSession, project.uuid(), resultHandler); assertThat(resultHandler.dtos).hasSize(2); verifyFileMoveRowDto(resultHandler, file); verifyFileMoveRowDto(resultHandler, ut); }
@Test public void select_enabled_module_files_tree_from_project() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto module = db.components().insertComponent(newModuleDto(project)); ComponentDto fileDirectlyOnModule = db.components().insertComponent(newFileDto(module)); FileSourceDto fileSourceDirectlyOnModule = db.fileSources().insertFileSource(fileDirectlyOnModule); ComponentDto subModule = db.components().insertComponent(newModuleDto(module)); ComponentDto directory = db.components().insertComponent(newDirectory(subModule, "src")); ComponentDto file = db.components().insertComponent(newFileDto(subModule, directory)); FileSourceDto fileSource = db.fileSources().insertFileSource(file); // From root project assertThat(underTest.selectEnabledFilesFromProject(dbSession, project.uuid())) .extracting(FilePathWithHashDto::getUuid, FilePathWithHashDto::getModuleUuid, FilePathWithHashDto::getSrcHash, FilePathWithHashDto::getPath, FilePathWithHashDto::getRevision) .containsExactlyInAnyOrder( tuple(fileDirectlyOnModule.uuid(), module.uuid(), fileSourceDirectlyOnModule.getSrcHash(), fileDirectlyOnModule.path(), fileSourceDirectlyOnModule.getRevision()), tuple(file.uuid(), subModule.uuid(), fileSource.getSrcHash(), file.path(), fileSource.getRevision())); // From module assertThat(underTest.selectEnabledFilesFromProject(dbSession, module.uuid())).isEmpty(); // From sub module assertThat(underTest.selectEnabledFilesFromProject(dbSession, subModule.uuid())).isEmpty(); // From directory assertThat(underTest.selectEnabledFilesFromProject(dbSession, directory.uuid())).isEmpty(); assertThat(underTest.selectEnabledFilesFromProject(dbSession, "unknown")).isEmpty(); }
@Test public void fail_when_using_branch_db_key() throws Exception { ComponentDto project = db.components().insertMainBranch(); userSession.addProjectPermission(UserRole.CODEVIEWER, project); ComponentDto branch = db.components().insertProjectBranch(project); ComponentDto file = db.components().insertComponent(newFileDto(branch)); db.fileSources().insertFileSource(file); expectedException.expect(NotFoundException.class); expectedException.expectMessage(format("Component key '%s' not found", file.getDbKey())); ws.newRequest() .setParam("key", file.getDbKey()) .execute(); }
assertThat(db.countSql("select count(*) from issues where resolution = 'REMOVED'")).isEqualTo(0); db.fileSources().insertFileSource(srcFile); FileSourceDto nonSelectedFileSource = db.fileSources().insertFileSource(nonSelectedFile); assertThat(db.countRowsOfTable("file_sources")).isEqualTo(2);
@Test public void fail_on_unknown_branch() { ComponentDto project = db.components().insertMainBranch(); userSession.addProjectPermission(UserRole.CODEVIEWER, project); ComponentDto branch = db.components().insertProjectBranch(project); ComponentDto file = db.components().insertComponent(newFileDto(branch)); db.fileSources().insertFileSource(file); expectedException.expect(NotFoundException.class); expectedException.expectMessage(format("Component '%s' on branch 'unknown' not found", file.getKey())); ws.newRequest() .setParam("key", file.getKey()) .setParam("branch", "unknown") .execute(); }
@Test public void raw_from_file() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.CODEVIEWER, project); ComponentDto file = db.components().insertComponent(newFileDto(project)); db.fileSources().insertFileSource(file, s -> s.setSourceData( Data.newBuilder() .addLines(Line.newBuilder().setLine(1).setSource("public class HelloWorld {").build()) .addLines(Line.newBuilder().setLine(2).setSource("}").build()) .build())); String result = ws.newRequest() .setParam("key", file.getKey()) .execute().getInput(); assertThat(result).isEqualTo("public class HelloWorld {\n}\n"); }
@Test public void fail_on_missing_permission() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organizationDto); ComponentDto file = db.components().insertComponent(newFileDto(project)); FileSourceDto fileSource = db.fileSources().insertFileSource(file); userSessionRule.logIn(db.users().insertUser()); expectedException.expect(ForbiddenException.class); tester.newRequest().setParam("key", file.getKey()).execute(); }
@Test public void raw_from_branch_file() { ComponentDto project = db.components().insertMainBranch(); userSession.addProjectPermission(UserRole.CODEVIEWER, project); ComponentDto branch = db.components().insertProjectBranch(project); ComponentDto file = db.components().insertComponent(newFileDto(branch)); db.fileSources().insertFileSource(file, s -> s.setSourceData( Data.newBuilder() .addLines(Line.newBuilder().setLine(1).setSource("public class HelloWorld {").build()) .addLines(Line.newBuilder().setLine(2).setSource("}").build()) .build())); String result = ws.newRequest() .setParam("key", file.getKey()) .setParam("branch", file.getBranch()) .execute().getInput(); assertThat(result).isEqualTo("public class HelloWorld {\n}\n"); }
ComponentDto module = db.components().insertComponent(newModuleDto(project)); ComponentDto fileDirectlyOnModule = db.components().insertComponent(newFileDto(module)); FileSourceDto fileSourceDirectlyOnModule = db.fileSources().insertFileSource(fileDirectlyOnModule); ComponentDto subModule = db.components().insertComponent(newModuleDto(module)); ComponentDto directory = db.components().insertComponent(newDirectory(subModule, "src")); ComponentDto file = db.components().insertComponent(newFileDto(subModule, directory)); FileSourceDto fileSource = db.fileSources().insertFileSource(file);