private Optional<ComponentDto> loadBaseComponent(String rawComponentKey) { ComponentDto baseComponent = baseModulesByKey.get(rawComponentKey); if (baseComponent == null) { // Load component from key to be able to detect issue (try to analyze a module, etc.) return componentDao.selectByKey(session, rawComponentKey); } return Optional.of(baseComponent); } }
@CheckForNull private ComponentDto loadComponent(DbSession session, String componentKey, @Nullable String branch, @Nullable String pullRequest) { if (branch != null) { return componentDao.selectByKeyAndBranch(session, componentKey, branch).orElse(null); } else if (pullRequest != null) { return componentDao.selectByKeyAndPullRequest(session, componentKey, pullRequest).orElse(null); } else { return componentDao.selectByKey(session, componentKey).orElse(null); } }
public ComponentDto selectOrFailByKey(DbSession session, String key) { Optional<ComponentDto> component = selectByKey(session, key); if (!component.isPresent()) { throw new RowNotFoundException(String.format("Component key '%s' not found", key)); } return component.get(); }
private ComponentDto toProject(String projectKey) { try (DbSession dbSession = dbClient.openSession(false)) { Optional<ComponentDto> opt = dbClient.componentDao().selectByKey(dbSession, projectKey); checkState(opt.isPresent(), "Project with key '%s' can't be found", projectKey); return opt.get(); } }
private Optional<ComponentDto> getProjectByKeyOrId(DbSession dbSession, String component) { try { long componentId = Long.parseLong(component); return ofNullable(dbClient.componentDao().selectById(dbSession, componentId).orElse(null)); } catch (NumberFormatException e) { return ofNullable(dbClient.componentDao().selectByKey(dbSession, component).orElse(null)); } }
private Optional<ComponentDto> loadComponent(DbSession dbSession, String component) { try { long componentId = Long.parseLong(component); return Optional.ofNullable(dbClient.componentDao().selectById(dbSession, componentId).orElse(null)); } catch (NumberFormatException e) { return Optional.ofNullable(dbClient.componentDao().selectByKey(dbSession, component).orElse(null)); } }
private ComponentDto getByKey(DbSession dbSession, String key, String label) { return checkComponent(dbClient.componentDao().selectByKey(dbSession, key), "%s key '%s' not found", label, key); }
private void assertComponentKeyUpdated(String oldKey, String newKey) { assertThat(dbClient.componentDao().selectByKey(dbSession, oldKey)).isEmpty(); assertThat(dbClient.componentDao().selectByKey(dbSession, newKey)).isPresent(); }
private void assertExistButDisabled(String... keys) { for (String key : keys) { ComponentDto dto = dbClient.componentDao().selectByKey(db.getSession(), key).get(); assertThat(dto.isEnabled()).isFalse(); } }
private void assertComponentKeyUpdated(String oldKey, String newKey) { assertThat(dbClient.componentDao().selectByKey(dbSession, oldKey)).isEmpty(); assertThat(dbClient.componentDao().selectByKey(dbSession, newKey)).isPresent(); }
private void assertComponentKeyHasBeenUpdated(String oldKey, String newKey) { assertThat(dbClient.componentDao().selectByKey(dbSession, oldKey)).isEmpty(); assertThat(dbClient.componentDao().selectByKey(dbSession, newKey)).isPresent(); }
private ComponentDto getComponentFromDb(String componentKey) { return dbClient.componentDao().selectByKey(dbTester.getSession(), componentKey).get(); }
@Test(expected = ForbiddenException.class) public void require_code_viewer() throws Exception { String fileKey = "src/Foo.java"; when(componentDao.selectByKey(session, fileKey)).thenReturn(Optional.of(file)); tester.newGetRequest("api/sources", "show").setParam("key", fileKey).execute(); } }
@Test public void show_source() throws Exception { String fileKey = "src/Foo.java"; userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project); when(componentDao.selectByKey(session, fileKey)).thenReturn(Optional.of(file)); when(sourceService.getLinesAsHtml(eq(session), eq(file.uuid()), anyInt(), anyInt())).thenReturn(Optional.of(newArrayList( "/*", " * Header", " */", "", "public class <span class=\"sym-31 sym\">HelloWorld</span> {", "}"))); WsTester.TestRequest request = tester.newGetRequest("api/sources", "show").setParam("key", fileKey); request.execute().assertJson(getClass(), "show_source.json"); }
private Optional<ComponentDto> selectByKey(String key) { return db.getDbClient().componentDao().selectByKey(db.getSession(), key); } }
@Test public void show_source_with_from_and_to_params() throws Exception { String fileKey = "src/Foo.java"; userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project); when(componentDao.selectByKey(session, fileKey)).thenReturn(Optional.of(file)); when(sourceService.getLinesAsHtml(session, file.uuid(), 3, 5)).thenReturn(Optional.of(newArrayList( " */", "", "public class <span class=\"sym-31 sym\">HelloWorld</span> {"))); WsTester.TestRequest request = tester .newGetRequest("api/sources", "show") .setParam("key", fileKey) .setParam("from", "3") .setParam("to", "5"); request.execute().assertJson(getClass(), "show_source_with_params_from_and_to.json"); }
@Test public void delete() { ComponentDto project1 = db.components().insertPrivateProject(db.getDefaultOrganization(), (t) -> t.setDbKey("PROJECT_1")); db.components().insertPrivateProject(db.getDefaultOrganization(), (t) -> t.setDbKey("PROJECT_2")); underTest.delete(dbSession, project1.getId()); dbSession.commit(); assertThat(underTest.selectByKey(dbSession, "PROJECT_1")).isEmpty(); assertThat(underTest.selectByKey(dbSession, "PROJECT_2")).isPresent(); }
@Test public void persist_file_directly_attached_on_root_directory() { ComponentDto projectDto = prepareProject(); treeRootHolder.setRoot( asTreeRoot(projectDto) .addChildren( builder(FILE, 2).setUuid("DEFG").setKey(projectDto.getDbKey() + ":pom.xml") .setName("pom.xml") .build()) .build()); underTest.execute(new TestComputationStepContext()); assertThat(dbClient.componentDao().selectByKey(db.getSession(), projectDto.getDbKey() + ":/")).isNotPresent(); ComponentDto file = dbClient.componentDao().selectByKey(db.getSession(), projectDto.getDbKey() + ":pom.xml").get(); assertThat(file.name()).isEqualTo("pom.xml"); assertThat(file.path()).isEqualTo("pom.xml"); }
@Test public void create_project() { userSession.addPermission(PROVISION_PROJECTS, db.getDefaultOrganization()); CreateWsResponse response = call(CreateRequest.builder() .setKey(DEFAULT_PROJECT_KEY) .setName(DEFAULT_PROJECT_NAME) .build()); assertThat(response.getProject()) .extracting(Project::getKey, Project::getName, Project::getQualifier, Project::getVisibility) .containsOnly(DEFAULT_PROJECT_KEY, DEFAULT_PROJECT_NAME, "TRK", "public"); assertThat(db.getDbClient().componentDao().selectByKey(db.getSession(), DEFAULT_PROJECT_KEY).get()) .extracting(ComponentDto::getDbKey, ComponentDto::name, ComponentDto::qualifier, ComponentDto::scope, ComponentDto::isPrivate, ComponentDto::getMainBranchProjectUuid) .containsOnly(DEFAULT_PROJECT_KEY, DEFAULT_PROJECT_NAME, "TRK", "PRJ", false, null); }
@Test public void update_module_key() { ComponentDto project = insertSampleRootProject(); ComponentDto module = ComponentTesting.newModuleDto(project).setDbKey("sample:root:module"); db.components().insertComponent(module); ComponentDto file = ComponentTesting.newFileDto(module, null).setDbKey("sample:root:module:src/File.xoo"); db.components().insertComponent(file); logInAsProjectAdministrator(project); underTest.updateKey(dbSession, module, "sample:root2:module"); dbSession.commit(); assertThat(dbClient.componentDao().selectByKey(dbSession, project.getDbKey())).isPresent(); assertComponentKeyHasBeenUpdated(module.getDbKey(), "sample:root2:module"); assertComponentKeyHasBeenUpdated(file.getDbKey(), "sample:root2:module:src/File.xoo"); // do not index the module but the project assertThat(projectIndexers.hasBeenCalled(project.uuid(), ProjectIndexer.Cause.PROJECT_KEY_UPDATE)).isTrue(); }