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; }
@Test public void json_example() throws IOException { ComponentDto project = initJsonExampleComponents(); logInWithBrowsePermission(project); String response = ws.newRequest() .setParam(PARAM_COMPONENT_ID, project.uuid()) .execute().getInput(); JsonAssert.assertJson(response) .withStrictArrayOrder() .isSimilarTo(getClass().getResource("tree-example.json")); }
private static ComponentDto newFileDto(ComponentDto moduleOrProject, int i) { return newFileDto(moduleOrProject, null, i); }
@Test public void return_leaves() { ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"); db.components().insertProjectAndSnapshot(project); ComponentDto module = newModuleDto("module-uuid-1", project); db.components().insertComponent(module); db.components().insertComponent(newFileDto(project, 1)); db.components().insertComponent(newFileDto(module, 2)); ComponentDto directory = newDirectory(project, "directory-path-1"); db.components().insertComponent(directory); db.components().insertComponent(newFileDto(module, directory, 3)); db.commit(); logInWithBrowsePermission(project); TreeWsResponse response = ws.newRequest() .setParam(PARAM_STRATEGY, "leaves") .setParam(PARAM_COMPONENT_ID, "project-uuid") .setParam(PARAM_QUALIFIERS, FILE).executeProtobuf(TreeWsResponse.class); assertThat(response.getComponentsCount()).isEqualTo(3); assertThat(response.getPaging().getTotal()).isEqualTo(3); assertThat(response.getComponentsList()).extracting("id").containsExactly("file-uuid-1", "file-uuid-2", "file-uuid-3"); }
@Test public void response_is_empty_on_provisioned_projects() { ComponentDto project = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid")); logInWithBrowsePermission(project); TreeWsResponse response = ws.newRequest() .setParam(PARAM_COMPONENT_ID, "project-uuid").executeProtobuf(TreeWsResponse.class); assertThat(response.getBaseComponent().getId()).isEqualTo("project-uuid"); assertThat(response.getComponentsList()).isEmpty(); assertThat(response.getPaging().getTotal()).isEqualTo(0); assertThat(response.getPaging().getPageSize()).isEqualTo(100); assertThat(response.getPaging().getPageIndex()).isEqualTo(1); }
@Test public void filter_descendants_by_qualifier() { ComponentDto project = newPrivateProjectDto(db.organizations().insert(), "project-uuid"); db.components().insertProjectAndSnapshot(project); db.components().insertComponent(newFileDto(project, 1)); db.components().insertComponent(newFileDto(project, 2)); db.components().insertComponent(newModuleDto("module-uuid-1", project)); db.commit(); logInWithBrowsePermission(project); TreeWsResponse response = ws.newRequest() .setParam(PARAM_STRATEGY, "all") .setParam(PARAM_QUALIFIERS, FILE) .setParam(PARAM_COMPONENT_ID, "project-uuid").executeProtobuf(TreeWsResponse.class); assertThat(response.getComponentsList()).extracting("id").containsExactly("file-uuid-1", "file-uuid-2"); }
@Test public void fail_when_base_component_is_removed() { ComponentDto project = db.components().insertComponent(newPrivateProjectDto(db.getDefaultOrganization())); db.components().insertComponent(ComponentTesting.newFileDto(project).setDbKey("file-key").setEnabled(false)); logInWithBrowsePermission(project); expectedException.expect(NotFoundException.class); expectedException.expectMessage("Component key 'file-key' not found"); ws.newRequest() .setParam(PARAM_COMPONENT, "file-key") .execute(); }
@Test public void return_descendants() { ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid"); SnapshotDto projectSnapshot = db.components().insertProjectAndSnapshot(project); ComponentDto module = newModuleDto("module-uuid-1", project); db.components().insertComponent(module); db.components().insertComponent(newFileDto(project, 10)); for (int i = 2; i <= 9; i++) { db.components().insertComponent(newFileDto(module, i)); } ComponentDto directory = newDirectory(module, "directory-path-1"); db.components().insertComponent(directory); db.components().insertComponent(newFileDto(module, directory, 1)); db.commit(); logInWithBrowsePermission(project); TreeWsResponse response = ws.newRequest() .setParam(PARAM_STRATEGY, "all") .setParam(PARAM_COMPONENT_ID, "module-uuid-1") .setParam(Param.PAGE, "2") .setParam(Param.PAGE_SIZE, "3") .setParam(Param.TEXT_QUERY, "file-name") .setParam(Param.ASCENDING, "true") .setParam(Param.SORT, "path").executeProtobuf(TreeWsResponse.class); assertThat(response.getComponentsCount()).isEqualTo(3); assertThat(response.getPaging().getTotal()).isEqualTo(9); assertThat(response.getComponentsList()).extracting("id").containsExactly("file-uuid-4", "file-uuid-5", "file-uuid-6"); }
@Test public void project_branch_reference_from_application_branch() { ComponentDto application = db.components().insertMainBranch(c -> c.setQualifier(APP).setDbKey("app-key")); ComponentDto applicationBranch = db.components().insertProjectBranch(application, a -> a.setKey("app-branch")); ComponentDto project = db.components().insertPrivateProject(p -> p.setDbKey("project-key")); ComponentDto projectBranch = db.components().insertProjectBranch(project, b -> b.setKey("project-branch")); ComponentDto techProjectBranch = db.components().insertComponent(newProjectCopy(projectBranch, applicationBranch) .setDbKey(applicationBranch.getKey() + applicationBranch.getBranch() + projectBranch.getDbKey())); logInWithBrowsePermission(application); TreeWsResponse result = ws.newRequest() .setParam(MeasuresWsParameters.PARAM_COMPONENT, applicationBranch.getKey()) .setParam(MeasuresWsParameters.PARAM_BRANCH, applicationBranch.getBranch()) .executeProtobuf(TreeWsResponse.class); assertThat(result.getBaseComponent()) .extracting(Component::getKey, Component::getBranch) .containsExactlyInAnyOrder(applicationBranch.getKey(), applicationBranch.getBranch()); assertThat(result.getComponentsList()) .extracting(Component::getKey, Component::getBranch, Component::getRefId, Component::getRefKey) .containsExactlyInAnyOrder(tuple(techProjectBranch.getKey(), projectBranch.getBranch(), projectBranch.uuid(), project.getKey())); }
@Test public void return_children() { ComponentDto project = newPrivateProjectDto(db.organizations().insert(), "project-uuid"); db.components().insertProjectAndSnapshot(project); ComponentDto module = newModuleDto("module-uuid-1", project); db.components().insertComponent(module); db.components().insertComponent(newFileDto(project, 1)); for (int i = 2; i <= 9; i++) { db.components().insertComponent(newFileDto(module, i)); } ComponentDto directory = newDirectory(module, "directory-path-1"); db.components().insertComponent(directory); db.components().insertComponent(newFileDto(module, directory, 10)); db.commit(); logInWithBrowsePermission(project); TreeWsResponse response = ws.newRequest() .setParam(PARAM_STRATEGY, "children") .setParam(PARAM_COMPONENT_ID, "module-uuid-1") .setParam(Param.PAGE, "2") .setParam(Param.PAGE_SIZE, "3") .setParam(Param.TEXT_QUERY, "file-name") .setParam(Param.ASCENDING, "false") .setParam(Param.SORT, "name").executeProtobuf(TreeWsResponse.class); assertThat(response.getComponentsCount()).isEqualTo(3); assertThat(response.getPaging().getTotal()).isEqualTo(8); assertThat(response.getComponentsList()).extracting("id").containsExactly("file-uuid-6", "file-uuid-5", "file-uuid-4"); }
@Test public void sort_descendants_by_qualifier() { ComponentDto project = newPrivateProjectDto(db.organizations().insert(), "project-uuid"); db.components().insertProjectAndSnapshot(project); db.components().insertComponent(newFileDto(project, 1)); db.components().insertComponent(newFileDto(project, 2)); ComponentDto module = newModuleDto("module-uuid-1", project); db.components().insertComponent(module); db.components().insertComponent(newDirectory(project, "path/directory/", "directory-uuid-1")); db.commit(); logInWithBrowsePermission(project); TreeWsResponse response = ws.newRequest() .setParam(PARAM_STRATEGY, "all") .setParam(Param.SORT, "qualifier, name") .setParam(PARAM_COMPONENT_ID, "project-uuid").executeProtobuf(TreeWsResponse.class); assertThat(response.getComponentsList()).extracting("id").containsExactly("module-uuid-1", "path/directory/", "file-uuid-1", "file-uuid-2"); }