@Test public void map_fields() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization, p -> p.setLanguage("java")); underTest.indexOnStartup(emptySet()); assertThatIndexContainsOnly(project); ComponentDoc doc = es.getDocuments(INDEX_TYPE_COMPONENT, ComponentDoc.class).get(0); assertThat(doc.getId()).isEqualTo(project.uuid()); assertThat(doc.getKey()).isEqualTo(project.getDbKey()); assertThat(doc.getProjectUuid()).isEqualTo(project.projectUuid()); assertThat(doc.getName()).isEqualTo(project.name()); assertThat(doc.getLanguage()).isEqualTo(project.language()); assertThat(doc.getOrganization()).isEqualTo(project.getOrganizationUuid()); }
@Test public void setters_and_getters() { ComponentDto componentDto = new ComponentDto() .setId(1L) .setDbKey("org.struts:struts-core:src/org/struts/RequestContext.java") .setName("RequestContext.java") .setLongName("org.struts.RequestContext") .setQualifier("FIL") .setScope("FIL") .setLanguage("java") .setDescription("desc") .setPath("src/org/struts/RequestContext.java") .setCopyComponentUuid("uuid_5") .setRootUuid("uuid_3"); assertThat(componentDto.getId()).isEqualTo(1L); assertThat(componentDto.getDbKey()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java"); assertThat(componentDto.getBranch()).isNull(); assertThat(componentDto.name()).isEqualTo("RequestContext.java"); assertThat(componentDto.longName()).isEqualTo("org.struts.RequestContext"); assertThat(componentDto.qualifier()).isEqualTo("FIL"); assertThat(componentDto.scope()).isEqualTo("FIL"); assertThat(componentDto.path()).isEqualTo("src/org/struts/RequestContext.java"); assertThat(componentDto.language()).isEqualTo("java"); assertThat(componentDto.description()).isEqualTo("desc"); assertThat(componentDto.getRootUuid()).isEqualTo("uuid_3"); assertThat(componentDto.getCopyResourceUuid()).isEqualTo("uuid_5"); assertThat(componentDto.isPrivate()).isFalse(); }
public ComponentDto createForFile(Component file, PathAwareVisitor.Path<ComponentDtoHolder> path) { ComponentDto res = createBase(file); res.setScope(Scopes.FILE); res.setQualifier(getFileQualifier(file)); res.setName(file.getShortName()); res.setLongName(file.getName()); res.setPath(file.getName()); res.setLanguage(file.getFileAttributes().getLanguageKey()); setParentModuleProperties(res, path); return res; }
private ComponentDto createFile(String fileKey, ComponentDto project) { ComponentDto file = ComponentTesting.newFileDto(project, null) .setDbKey(fileKey) .setLanguage(XOO_LANGUAGE); dbClient.componentDao().insert(dbSession, file); dbSession.commit(); return file; }
@Test public void selectByKey() { OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertPrivateProject(organization); ComponentDto directory = db.components().insertComponent(newDirectory(project, "src")); ComponentDto file = db.components().insertComponent(newFileDto(project, directory) .setDbKey("org.struts:struts-core:src/org/struts/RequestContext.java") .setName("RequestContext.java") .setLongName("org.struts.RequestContext") .setLanguage("java") .setPath("src/RequestContext.java")); Optional<ComponentDto> optional = underTest.selectByKey(dbSession, file.getDbKey()); ComponentDto result = optional.get(); assertThat(result.getOrganizationUuid()).isEqualTo(organization.getUuid()); assertThat(result.uuid()).isEqualTo(file.uuid()); assertThat(result.getDbKey()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java"); assertThat(result.path()).isEqualTo("src/RequestContext.java"); assertThat(result.name()).isEqualTo("RequestContext.java"); assertThat(result.longName()).isEqualTo("org.struts.RequestContext"); assertThat(result.qualifier()).isEqualTo("FIL"); assertThat(result.scope()).isEqualTo("FIL"); assertThat(result.language()).isEqualTo("java"); assertThat(result.projectUuid()).isEqualTo(project.uuid()); assertThat(underTest.selectByKey(dbSession, "unknown")).isEmpty(); }
@Test public void search_with_language() { OrganizationDto organizationDto = db.organizations().insert(); insertProjectsAuthorizedForUser( ComponentTesting.newPrivateProjectDto(organizationDto).setDbKey("java-project").setLanguage("java"), ComponentTesting.newPrivateProjectDto(organizationDto).setDbKey("cpp-project").setLanguage("cpp")); SearchWsResponse response = call(new SearchRequest().setOrganization(organizationDto.getKey()).setLanguage("java").setQualifiers(singletonList(PROJECT))); assertThat(response.getComponentsList()).extracting(Component::getKey).containsOnly("java-project"); }
public static ComponentDto newFileDto(ComponentDto module, @Nullable ComponentDto directory, String fileUuid) { String filename = "NAME_" + fileUuid; String path = directory != null ? directory.path() + "/" + filename : module.path() + "/" + filename; return newChildComponent(fileUuid, module, directory == null ? module : directory) .setDbKey(generateKey("FILE_KEY_" + fileUuid, module)) .setName(filename) .setLongName(path) .setScope(Scopes.FILE) .setQualifier(Qualifiers.FILE) .setPath(path) .setCreatedAt(new Date()) .setLanguage("xoo"); }
@Test public void filter_by_language() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto javaFile = db.components().insertComponent(newFileDto(project).setLanguage("java")); ComponentDto jsFile1 = db.components().insertComponent(newFileDto(project).setLanguage("js")); ComponentDto jsFile2 = db.components().insertComponent(newFileDto(project).setLanguage("js")); index(project); SearchIdResult<String> result = underTest.search(ComponentQuery.builder().setLanguage("js").build(), new SearchOptions()); assertThat(result.getIds()).containsExactlyInAnyOrder(jsFile1.uuid(), jsFile2.uuid()); }
private ComponentDto initJsonExampleComponents() throws IOException { OrganizationDto organizationDto = db.organizations().insertForKey("my-org-1"); ComponentDto project = newPrivateProjectDto(organizationDto, "MY_PROJECT_ID") .setDbKey("MY_PROJECT_KEY") .setName("Project Name"); db.components().insertProjectAndSnapshot(project); Date now = new Date(); JsonParser jsonParser = new JsonParser(); JsonElement jsonTree = jsonParser.parse(IOUtils.toString(getClass().getResource("tree-example.json"), UTF_8)); JsonArray components = jsonTree.getAsJsonObject().getAsJsonArray("components"); for (JsonElement componentAsJsonElement : components) { JsonObject componentAsJsonObject = componentAsJsonElement.getAsJsonObject(); String uuid = getJsonField(componentAsJsonObject, "id"); db.components().insertComponent(newChildComponent(uuid, project, project) .setDbKey(getJsonField(componentAsJsonObject, "key")) .setName(getJsonField(componentAsJsonObject, "name")) .setLanguage(getJsonField(componentAsJsonObject, "language")) .setPath(getJsonField(componentAsJsonObject, "path")) .setQualifier(getJsonField(componentAsJsonObject, "qualifier")) .setDescription(getJsonField(componentAsJsonObject, "description")) .setEnabled(true) .setCreatedAt(now)); } db.commit(); return project; }
private ComponentDto persistComponent(ComponentDto componentDto) { ComponentDto existingComponent = existingComponentDtosByUuids.remove(componentDto.uuid()); if (existingComponent == null) { dbClient.componentDao().insert(dbSession, componentDto); return componentDto; } Optional<ComponentUpdateDto> update = compareForUpdate(existingComponent, componentDto); if (update.isPresent()) { ComponentUpdateDto updateDto = update.get(); dbClient.componentDao().update(dbSession, updateDto); // update the fields in memory in order the PathAwareVisitor.Path // to be up-to-date existingComponent.setDbKey(updateDto.getBKey()); existingComponent.setCopyComponentUuid(updateDto.getBCopyComponentUuid()); existingComponent.setDescription(updateDto.getBDescription()); existingComponent.setEnabled(updateDto.isBEnabled()); existingComponent.setUuidPath(updateDto.getBUuidPath()); existingComponent.setLanguage(updateDto.getBLanguage()); existingComponent.setLongName(updateDto.getBLongName()); existingComponent.setModuleUuid(updateDto.getBModuleUuid()); existingComponent.setModuleUuidPath(updateDto.getBModuleUuidPath()); existingComponent.setName(updateDto.getBName()); existingComponent.setPath(updateDto.getBPath()); // We don't have a b_scope. The applyBChangesForRootComponentUuid query is using a case ... when to infer scope from the qualifier existingComponent.setScope(componentDto.scope()); existingComponent.setQualifier(updateDto.getBQualifier()); } return existingComponent; }
public static ComponentDto newProjectCopy(String uuid, ComponentDto project, ComponentDto view) { checkNotNull(project.getId(), "The project need to be persisted before creating this technical project."); return newChildComponent(uuid, view, view) .setDbKey(view.getDbKey() + project.getDbKey()) .setName(project.name()) .setLongName(project.longName()) .setCopyComponentUuid(project.uuid()) .setScope(Scopes.FILE) .setQualifier(Qualifiers.PROJECT) .setPath(null) .setLanguage(null); }
public static ComponentDto newModuleDto(String uuid, ComponentDto parentModuleOrProject) { return newChildComponent(uuid, parentModuleOrProject, parentModuleOrProject) .setModuleUuidPath(parentModuleOrProject.moduleUuidPath() + uuid + UUID_PATH_SEPARATOR) .setDbKey(generateKey("MODULE_KEY_" + uuid, parentModuleOrProject)) .setName("NAME_" + uuid) .setLongName("LONG_NAME_" + uuid) .setPath("module") .setScope(Scopes.PROJECT) .setQualifier(Qualifiers.MODULE) .setLanguage(null); }
@Test public void search_by_rule_key() { RuleDto rule = newRule(); OrganizationDto organization = db.organizations().insert(o -> o.setKey("my-org-1")); ComponentDto project = db.components().insertComponent(ComponentTesting.newPublicProjectDto(organization, "PROJECT_ID").setDbKey("PROJECT_KEY").setLanguage("java")); ComponentDto file = db.components().insertComponent(newFileDto(project, null, "FILE_ID").setDbKey("FILE_KEY").setLanguage("java")); IssueDto issue = IssueTesting.newIssue(rule.getDefinition(), project, file); dbClient.issueDao().insert(session, issue); session.commit(); indexIssues(); userSession.logIn("john") .addProjectPermission(ISSUE_ADMIN, project); // granted by Anyone indexPermissions(); TestResponse execute = ws.newRequest() .setParam(PARAM_RULES, rule.getKey().toString()) .setParam("additionalFields", "_all") .execute(); execute.assertJson(this.getClass(), "result_for_rule_search.json"); }
public static ComponentDto newProjectBranch(ComponentDto project, BranchDto branchDto) { checkArgument(project.qualifier().equals(Qualifiers.PROJECT) || project.qualifier().equals(Qualifiers.APP)); checkArgument(project.getMainBranchProjectUuid() == null); String branchName = branchDto.getKey(); String branchSeparator = branchDto.getBranchType() == PULL_REQUEST ? PULL_REQUEST_SEPARATOR : BRANCH_KEY_SEPARATOR; String uuid = branchDto.getUuid(); return new ComponentDto() .setUuid(uuid) .setOrganizationUuid(project.getOrganizationUuid()) .setUuidPath(UUID_PATH_OF_ROOT) .setProjectUuid(uuid) .setModuleUuidPath(UUID_PATH_SEPARATOR + uuid + UUID_PATH_SEPARATOR) .setRootUuid(uuid) // name of the branch is not mandatory on the main branch .setDbKey(branchName != null ? project.getDbKey() + branchSeparator + branchName : project.getKey()) .setMainBranchProjectUuid(project.uuid()) .setName(project.name()) .setLongName(project.longName()) .setDescription(project.description()) .setScope(project.scope()) .setQualifier(project.qualifier()) .setPath(null) .setLanguage(null) .setEnabled(true) .setPrivate(project.isPrivate()); } }
private static ComponentDto newProjectDto(String organizationUuid, String uuid, boolean isPrivate) { return new ComponentDto() .setOrganizationUuid(organizationUuid) .setUuid(uuid) .setUuidPath(UUID_PATH_OF_ROOT) .setProjectUuid(uuid) .setModuleUuidPath(UUID_PATH_SEPARATOR + uuid + UUID_PATH_SEPARATOR) .setRootUuid(uuid) .setDbKey("KEY_" + uuid) .setName("NAME_" + uuid) .setLongName("LONG_NAME_" + uuid) .setDescription("DESCRIPTION_" + uuid) .setScope(Scopes.PROJECT) .setQualifier(Qualifiers.PROJECT) .setPath(null) .setLanguage(null) .setEnabled(true) .setPrivate(isPrivate); }
@Test public void test_json_example() { OrganizationDto organizationDto = db.organizations().insertForKey("my-org-1"); db.components().insertComponent(newView(organizationDto)); ComponentDto project = newPrivateProjectDto(organizationDto, "project-uuid").setName("Project Name").setDbKey("project-key"); ComponentDto module = newModuleDto("module-uuid", project).setName("Module Name").setDbKey("module-key"); ComponentDto directory = newDirectory(module, "path/to/directoy").setUuid("directory-uuid").setDbKey("directory-key").setName("Directory Name"); db.components().insertComponents(project, module, directory, newFileDto(module, directory, "file-uuid").setDbKey("file-key").setLanguage("java").setName("File Name")); setBrowsePermissionOnUserAndIndex(project); String response = ws.newRequest() .setMediaType(MediaTypes.JSON) .setParam(PARAM_ORGANIZATION, organizationDto.getKey()) .setParam(PARAM_QUALIFIERS, Joiner.on(",").join(PROJECT, DIRECTORY, FILE)) .execute().getInput(); assertJson(response).isSimilarTo(ws.getDef().responseExampleAsString()); }
private void insertJsonExampleComponentsAndSnapshots() { OrganizationDto organizationDto = db.organizations().insertForKey("my-org-1"); ComponentDto project = db.components().insertComponent(newPrivateProjectDto(organizationDto, "AVIF98jgA3Ax6PH2efOW") .setDbKey("com.sonarsource:java-markdown") .setName("Java Markdown") .setDescription("Java Markdown Project") .setQualifier(Qualifiers.PROJECT) .setTagsString("language, plugin")); db.components().insertSnapshot(project, snapshot -> snapshot .setVersion("1.1") .setCreatedAt(parseDateTime("2017-03-01T11:39:03+0100").getTime()) .setPeriodDate(parseDateTime("2017-01-01T11:39:03+0100").getTime())); ComponentDto directory = newDirectory(project, "AVIF-FfgA3Ax6PH2efPF", "src/main/java/com/sonarsource/markdown/impl") .setDbKey("com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl") .setName("src/main/java/com/sonarsource/markdown/impl") .setQualifier(Qualifiers.DIRECTORY); db.components().insertComponent(directory); db.components().insertComponent( newFileDto(directory, directory, "AVIF-FffA3Ax6PH2efPD") .setDbKey("com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl/Rule.java") .setName("Rule.java") .setPath("src/main/java/com/sonarsource/markdown/impl/Rule.java") .setLanguage("java") .setQualifier(Qualifiers.FILE)); } }
@Test public void load_additional_fields_with_issue_admin_permission() { UserDto simon = db.users().insertUser(u -> u.setLogin("simon").setName("Simon").setEmail("simon@email.com")); UserDto fabrice = db.users().insertUser(u -> u.setLogin("fabrice").setName("Fabrice").setEmail("fabrice@email.com")); OrganizationDto organization = db.organizations().insert(o -> o.setKey("my-org-1")); ComponentDto project = db.components().insertComponent(ComponentTesting.newPublicProjectDto(organization, "PROJECT_ID").setDbKey("PROJECT_KEY").setLanguage("java")); grantPermissionToAnyone(project, ISSUE_ADMIN); indexPermissions(); ComponentDto file = db.components().insertComponent(newFileDto(project, null, "FILE_ID").setDbKey("FILE_KEY").setLanguage("js")); IssueDto issue = newDto(newRule(), file, project) .setKee("82fd47d4-b650-4037-80bc-7b112bd4eac2") .setAuthorLogin(fabrice.getLogin()) .setAssigneeUuid(simon.getUuid()); dbClient.issueDao().insert(session, issue); session.commit(); indexIssues(); userSession.logIn("john") .addProjectPermission(ISSUE_ADMIN, project); // granted by Anyone ws.newRequest() .setParam("additionalFields", "_all").execute() .assertJson(this.getClass(), "load_additional_fields_with_issue_admin_permission.json"); }
.setDbKey("com.sonarsource:java-markdown:src/main/java/com/sonarsource/markdown/impl/ElementImpl.java") .setName("ElementImpl.java") .setLanguage("java") .setQualifier(FILE) .setPath("src/main/java/com/sonarsource/markdown/impl/ElementImpl.java")); .setDbKey("com.sonarsource:java-markdown:src/test/java/com/sonarsource/markdown/impl/ElementImplTest.java") .setName("ElementImplTest.java") .setLanguage("java") .setQualifier(UNIT_TEST_FILE) .setPath("src/test/java/com/sonarsource/markdown/impl/ElementImplTest.java"));
.setDbKey("MY_PROJECT:ElementImpl.java") .setName("ElementImpl.java") .setLanguage("java") .setPath("src/main/java/com/sonarsource/markdown/impl/ElementImpl.java"));