@Test public void build_query() { ComponentQuery underTest = ComponentQuery.builder() .setNameOrKeyQuery("key") .setAnyBranchAnalyzedBefore(100L) .setAnyBranchAnalyzedAfter(200L) .setCreatedAfter(new Date(300L)) .setQualifiers(PROJECT) .build(); assertThat(underTest.getNameOrKeyQuery()).isEqualTo("key"); assertThat(underTest.getQualifiers()).containsOnly(PROJECT); assertThat(underTest.getAnyBranchAnalyzedBefore()).isEqualTo(100L); assertThat(underTest.getAnyBranchAnalyzedAfter()).isEqualTo(200L); assertThat(underTest.getCreatedAfter().getTime()).isEqualTo(300L); assertThat(underTest.isOnProvisionedOnly()).isFalse(); assertThat(underTest.isPartialMatchOnKey()).isFalse(); assertThat(underTest.hasEmptySetOfComponents()).isFalse(); }
@Test public void selectByQuery_on_empty_list_of_component_id() { db.components().insertPrivateProject(); ComponentQuery dbQuery = ComponentQuery.builder().setQualifiers(PROJECT).setComponentIds(emptySet()).build(); List<ComponentDto> result = underTest.selectByQuery(dbSession, dbQuery, 0, 10); int count = underTest.countByQuery(dbSession, dbQuery); assertThat(result).isEmpty(); assertThat(count).isEqualTo(0); }
static ComponentQuery buildDbQuery(SearchRequest request) { List<String> qualifiers = request.getQualifiers(); ComponentQuery.Builder query = ComponentQuery.builder() .setQualifiers(qualifiers.toArray(new String[qualifiers.size()])); ofNullable(request.getQuery()).ifPresent(q -> { query.setNameOrKeyQuery(q); query.setPartialMatchOnKey(true); }); ofNullable(request.getVisibility()).ifPresent(v -> query.setPrivate(Visibility.isPrivate(v))); ofNullable(request.getAnalyzedBefore()).ifPresent(d -> query.setAnalyzedBefore(parseDateOrDateTime(d).getTime())); query.setOnProvisionedOnly(request.isOnProvisionedOnly()); ofNullable(request.getProjects()).ifPresent(keys -> query.setComponentKeys(new HashSet<>(keys))); ofNullable(request.getProjectIds()).ifPresent(uuids -> query.setComponentUuids(new HashSet<>(uuids))); return query.build(); }
static ComponentQuery buildDbQuery(SearchRequest request) { List<String> qualifiers = request.getQualifiers(); ComponentQuery.Builder query = ComponentQuery.builder() .setQualifiers(qualifiers.toArray(new String[qualifiers.size()])); setNullable(request.getQuery(), q -> { query.setNameOrKeyQuery(q); query.setPartialMatchOnKey(true); return query; }); setNullable(request.getVisibility(), v -> query.setPrivate(Visibility.isPrivate(v))); setNullable(request.getAnalyzedBefore(), d -> query.setAnalyzedBefore(parseDateOrDateTime(d).getTime())); setNullable(request.isOnProvisionedOnly(), query::setOnProvisionedOnly); setNullable(request.getProjects(), keys -> query.setComponentKeys(new HashSet<>(keys))); setNullable(request.getProjectIds(), uuids -> query.setComponentUuids(new HashSet<>(uuids))); return query.build(); }
public static Builder builder() { return new Builder(); }
private static ComponentQuery buildDbQuery(BulkApplyTemplateRequest request) { Collection<String> qualifiers = request.getQualifiers(); ComponentQuery.Builder query = ComponentQuery.builder() .setQualifiers(qualifiers.toArray(new String[qualifiers.size()])); setNullable(request.getQuery(), q -> { query.setNameOrKeyQuery(q); query.setPartialMatchOnKey(true); return query; }); setNullable(request.getVisibility(), v -> query.setPrivate(Visibility.isPrivate(v))); setNullable(request.getAnalyzedBefore(), d -> query.setAnalyzedBefore(parseDateOrDateTime(d).getTime())); setNullable(request.isOnProvisionedOnly(), query::setOnProvisionedOnly); setNullable(request.getProjects(), keys -> query.setComponentKeys(new HashSet<>(keys))); return query.build(); }
private List<ComponentDto> loadComponents(DbSession dbSession, String componentQuery) { ComponentQuery componentDtoQuery = ComponentQuery.builder() .setNameOrKeyQuery(componentQuery) .setQualifiers(POSSIBLE_QUALIFIERS) .build(); return dbClient.componentDao().selectByQuery(dbSession, componentDtoQuery, 0, CeTaskQuery.MAX_COMPONENT_UUIDS); }
private ComponentQuery toDbQuery(SearchProjectPermissionsRequest wsRequest) { return ComponentQuery.builder() .setQualifiers(qualifiers(wsRequest.getQualifier())) .setNameOrKeyQuery(wsRequest.getQuery()) .build(); }
@VisibleForTesting ProjectsResult searchProjects(DbSession dbSession, SearchMyProjectsRequest request) { int userId = requireNonNull(userSession.getUserId(), "Current user must be authenticated"); List<Long> componentIds = dbClient.roleDao().selectComponentIdsByPermissionAndUserId(dbSession, UserRole.ADMIN, userId); ComponentQuery dbQuery = ComponentQuery.builder() .setQualifiers(Qualifiers.PROJECT) .setComponentIds(ImmutableSet.copyOf(componentIds.subList(0, Math.min(componentIds.size(), DatabaseUtils.PARTITION_SIZE_FOR_ORACLE)))) .build(); return new ProjectsResult( dbClient.componentDao().selectByQuery(dbSession, dbQuery, offset(request.getPage(), request.getPageSize()), request.getPageSize()), dbClient.componentDao().countByQuery(dbSession, dbQuery)); }
private static ComponentQuery buildDbQuery(@Nullable String nameOrKeyQuery) { ComponentQuery.Builder dbQuery = ComponentQuery.builder() .setQualifiers(Qualifiers.PROJECT) .setOnProvisionedOnly(true); setNullable(nameOrKeyQuery, q -> { dbQuery.setPartialMatchOnKey(true); dbQuery.setNameOrKeyQuery(q); return dbQuery; }); return dbQuery.build(); }
Supplier<ComponentQuery.Builder> query = () -> ComponentQuery.builder().setQualifiers(PROJECT).setOnProvisionedOnly(true); assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().build(), 0, 10)) .extracting(ComponentDto::uuid) .containsOnly(provisionedProject.uuid()); assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().build(), 2, 10)).isEmpty(); assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setQualifiers("XXX").build(), 0, 10)).isEmpty(); assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(PROJECT, "XXX").build(), 0, 10)) .extracting(ComponentDto::uuid) .containsOnly(provisionedProject.uuid()); assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(PROJECT, Qualifiers.VIEW).build(), 0, 10)) .extracting(ComponentDto::uuid) .containsOnly(provisionedProject.uuid(), provisionedPortfolio.uuid()); assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setNameOrKeyQuery(provisionedProject.getDbKey()).build(), 0, 10)) .extracting(ComponentDto::uuid) .containsExactly(provisionedProject.uuid()); assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setNameOrKeyQuery("pROvisiONed.proJEcT").setPartialMatchOnKey(true).build(), 0, 10)) .extracting(ComponentDto::uuid) .containsExactly(provisionedProject.uuid()); assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setNameOrKeyQuery("missing").setPartialMatchOnKey(true).build(), 0, 10)).isEmpty(); assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setNameOrKeyQuery("to be escaped '\"\\%").setPartialMatchOnKey(true).build(), 0, 10)) .isEmpty(); assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().setNameOrKeyQuery("ned proj").setPartialMatchOnKey(true).build(), 0, 10)) .extracting(ComponentDto::uuid) .containsExactly(provisionedProject.uuid());
@Override public void handle(Request request, Response response) throws Exception { String organizationKey = request.mandatoryParam(PARAM_ORGANIZATION); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = checkFoundWithOptional( dbClient.organizationDao().selectByKey(dbSession, organizationKey), "No organization with key '%s'", organizationKey); if (organization.getSubscription() == PAID) { // If the organization is PAID without any public project then // the organization is only visible to members ComponentQuery query = ComponentQuery.builder() .setQualifiers(Qualifiers.PROJECT) .setPrivate(false) .build(); if (dbClient.componentDao().countByQuery(dbSession, organization.getUuid(), query) == 0) { userSession.checkMembership(organization); } } boolean newProjectPrivate = dbClient.organizationDao().getNewProjectPrivate(dbSession, organization); JsonWriter json = response.newJsonWriter(); json.beginObject(); writeOrganization(json, organization, dbClient.organizationAlmBindingDao().selectByOrganization(dbSession, organization), newProjectPrivate); json.endObject() .close(); } }
@Test public void selectByQuery_onProvisionedOnly_filters_projects_with_analysis_on_branch() { Supplier<ComponentQuery.Builder> query = () -> ComponentQuery.builder() .setQualifiers(PROJECT) .setOnProvisionedOnly(true); // the project does not have any analysis OrganizationDto organization = db.organizations().insert(); ComponentDto project = db.components().insertMainBranch(organization); assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().build(), 0, 10)) .extracting(ComponentDto::uuid) .containsOnly(project.uuid()); // the project does not have analysis of main branch but only // analysis of non-main branches ComponentDto branchWithoutAnalysis = db.components().insertProjectBranch(project); ComponentDto branchWithAnalysis = db.components().insertProjectBranch(project); db.components().insertSnapshot(branchWithAnalysis); assertThat(underTest.selectByQuery(dbSession, organization.getUuid(), query.get().build(), 0, 10)) .isEmpty(); }
@Test public void selectByQuery_with_paging_query_and_qualifiers() { OrganizationDto organizationDto = db.organizations().insert(); db.components().insertProjectAndSnapshot(newPrivateProjectDto(organizationDto).setName("aaaa-name")); db.components().insertProjectAndSnapshot(newView(organizationDto)); for (int i = 9; i >= 1; i--) { db.components().insertProjectAndSnapshot(newPrivateProjectDto(organizationDto).setName("project-" + i)); } ComponentQuery query = ComponentQuery.builder().setNameOrKeyQuery("oJect").setQualifiers(PROJECT).build(); List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 1, 3); int count = underTest.countByQuery(dbSession, query); assertThat(result).hasSize(3); assertThat(count).isEqualTo(9); assertThat(result).extracting(ComponentDto::name).containsExactly("project-2", "project-3", "project-4"); assertThat(result).extracting(ComponentDto::getOrganizationUuid).containsOnly(organizationDto.getUuid()); }
public static Builder builder() { return new Builder(); }
@Test public void selectByQuery_on_component_keys() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto sonarqube = db.components().insertComponent(newPrivateProjectDto(organizationDto)); ComponentDto jdk8 = db.components().insertComponent(newPrivateProjectDto(organizationDto)); ComponentDto cLang = db.components().insertComponent(newPrivateProjectDto(organizationDto)); ComponentQuery query = ComponentQuery.builder().setQualifiers(PROJECT) .setComponentKeys(newHashSet(sonarqube.getDbKey(), jdk8.getDbKey())).build(); List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10); assertThat(result).hasSize(2).extracting(ComponentDto::getDbKey) .containsExactlyInAnyOrder(sonarqube.getDbKey(), jdk8.getDbKey()) .doesNotContain(cLang.getDbKey()); }
@Test public void selectByQuery_on_component_ids() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto sonarqube = db.components().insertComponent(newPrivateProjectDto(organizationDto)); ComponentDto jdk8 = db.components().insertComponent(newPrivateProjectDto(organizationDto)); ComponentDto cLang = db.components().insertComponent(newPrivateProjectDto(organizationDto)); ComponentQuery query = ComponentQuery.builder().setQualifiers(PROJECT) .setComponentIds(newHashSet(sonarqube.getId(), jdk8.getId())).build(); List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10); assertThat(result).hasSize(2).extracting(ComponentDto::getId) .containsOnlyOnce(sonarqube.getId(), jdk8.getId()) .doesNotContain(cLang.getId()); }
private static ComponentQuery buildDbQuery(BulkApplyTemplateRequest request) { Collection<String> qualifiers = request.getQualifiers(); ComponentQuery.Builder query = ComponentQuery.builder() .setQualifiers(qualifiers.toArray(new String[qualifiers.size()])); ofNullable(request.getQuery()).ifPresent(q -> { query.setNameOrKeyQuery(q); query.setPartialMatchOnKey(true); }); ofNullable(request.getVisibility()).ifPresent(v -> query.setPrivate(Visibility.isPrivate(v))); ofNullable(request.getAnalyzedBefore()).ifPresent(d -> query.setAnalyzedBefore(parseDateOrDateTime(d).getTime())); query.setOnProvisionedOnly(request.isOnProvisionedOnly()); ofNullable(request.getProjects()).ifPresent(keys -> query.setComponentKeys(new HashSet<>(keys))); return query.build(); }
@Test public void fail_if_no_qualifier_provided() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("At least one qualifier must be provided"); ComponentQuery.builder().build(); }
@Test public void selectByQuery_on_component_uuids() { OrganizationDto organizationDto = db.organizations().insert(); ComponentDto sonarqube = db.components().insertComponent(newPrivateProjectDto(organizationDto)); ComponentDto jdk8 = db.components().insertComponent(newPrivateProjectDto(organizationDto)); ComponentDto cLang = db.components().insertComponent(newPrivateProjectDto(organizationDto)); ComponentQuery query = ComponentQuery.builder().setQualifiers(PROJECT) .setComponentUuids(newHashSet(sonarqube.uuid(), jdk8.uuid())).build(); List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10); assertThat(result).hasSize(2).extracting(ComponentDto::uuid) .containsOnlyOnce(sonarqube.uuid(), jdk8.uuid()) .doesNotContain(cLang.uuid()); }