private int countRootComponents(DbSession dbSession, SearchProjectPermissionsRequest request) { return dbClient.componentDao().countByQuery(dbSession, toDbQuery(request)); }
@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)); }
@Test public void countByQuery_with_organization_filters_on_specified_organization() { OrganizationDto organization1 = db.organizations().insert(); OrganizationDto organization2 = db.organizations().insert(); ComponentDto project1 = db.components().insertPrivateProject(organization1); ComponentDto project2 = db.components().insertPrivateProject(organization2); assertThat(underTest.countByQuery(dbSession, ALL_PROJECTS_COMPONENT_QUERY)) .isEqualTo(2); assertThat(underTest.countByQuery(dbSession, organization1.getUuid(), ALL_PROJECTS_COMPONENT_QUERY)) .isEqualTo(1); assertThat(underTest.countByQuery(dbSession, organization2.getUuid(), ALL_PROJECTS_COMPONENT_QUERY)) .isEqualTo(1); assertThat(underTest.countByQuery(dbSession, "non existent organization uuid", ALL_PROJECTS_COMPONENT_QUERY)) .isEqualTo(0); }
@Test public void countByQuery_with_organization_throws_NPE_of_organizationUuid_is_null() { expectedException.expect(NullPointerException.class); expectedException.expectMessage("organizationUuid can't be null"); underTest.countByQuery(dbSession, null, ALL_PROJECTS_COMPONENT_QUERY); }
@Test public void selectByQuery_on_empty_list_of_component_key() { db.components().insertPrivateProject(); ComponentQuery dbQuery = ComponentQuery.builder().setQualifiers(PROJECT).setComponentKeys(emptySet()).build(); List<ComponentDto> result = underTest.selectByQuery(dbSession, dbQuery, 0, 10); int count = underTest.countByQuery(dbSession, dbQuery); assertThat(result).isEmpty(); assertThat(count).isEqualTo(0); }
@Test public void countByQuery_should_not_include_branches() { ComponentDto main = db.components().insertMainBranch(); ComponentDto branch = db.components().insertProjectBranch(main); assertThat(underTest.countByQuery(dbSession, ALL_PROJECTS_COMPONENT_QUERY)).isEqualTo(1); }
@Test public void selectByQuery_on_empty_list_of_component_uuids() { db.components().insertPrivateProject(); ComponentQuery dbQuery = ComponentQuery.builder().setQualifiers(PROJECT).setComponentUuids(emptySet()).build(); List<ComponentDto> result = underTest.selectByQuery(dbSession, dbQuery, 0, 10); int count = underTest.countByQuery(dbSession, dbQuery); assertThat(result).isEmpty(); assertThat(count).isEqualTo(0); }
private void assertThatCountByQueryThrowsIAE(ComponentQuery.Builder query, String expectedMessage) { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage(expectedMessage); underTest.countByQuery(dbSession, query.build()); }
private Paging buildPaging(DbSession dbSession, SearchRequest request, OrganizationDto organization, ComponentQuery query) { int total = dbClient.componentDao().countByQuery(dbSession, organization.getUuid(), query); return Paging.forPageIndex(request.getPage()) .withPageSize(request.getPageSize()) .andTotal(total); }
@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); }
@Test public void count_provisioned() { OrganizationDto organization = db.organizations().insert(); db.components().insertPrivateProject(organization); db.components().insertProjectAndSnapshot(newPrivateProjectDto(organization)); db.components().insertProjectAndSnapshot(ComponentTesting.newView(organization)); Supplier<ComponentQuery.Builder> query = () -> ComponentQuery.builder().setOnProvisionedOnly(true); assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(PROJECT).build())).isEqualTo(1); assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(Qualifiers.VIEW).build())).isEqualTo(0); assertThat(underTest.countByQuery(dbSession, organization.getUuid(), query.get().setQualifiers(PROJECT, Qualifiers.VIEW).build())).isEqualTo(1); }
@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()); }
@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(); } }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); int page = request.mandatoryParamAsInt(Param.PAGE); int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE); Set<String> desiredFields = desiredFields(request); String query = request.param(Param.TEXT_QUERY); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = support.getOrganization(dbSession, request.param(PARAM_ORGANIZATION)); userSession.checkPermission(PROVISION_PROJECTS, organization); ComponentQuery dbQuery = buildDbQuery(query); List<ComponentDto> projects = dbClient.componentDao().selectByQuery(dbSession, organization.getUuid(), dbQuery, offset(page, pageSize), pageSize); int nbOfProjects = dbClient.componentDao().countByQuery(dbSession, organization.getUuid(), dbQuery); ProvisionedWsResponse result = ProvisionedWsResponse.newBuilder() .addAllProjects(writeProjects(projects, desiredFields)) .setPaging(Paging.newBuilder() .setTotal(nbOfProjects) .setPageIndex(page) .setPageSize(pageSize)) .build(); writeProtobuf(result, request, response); } }
private int countRootComponents(DbSession dbSession, SearchProjectPermissionsRequest request) { return dbClient.componentDao().countByQuery(dbSession, toDbQuery(request)); }
@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 Paging buildPaging(DbSession dbSession, SearchRequest request, OrganizationDto organization, ComponentQuery query) { int total = dbClient.componentDao().countByQuery(dbSession, organization.getUuid(), query); return Paging.forPageIndex(request.getPage()) .withPageSize(request.getPageSize()) .andTotal(total); }
@Override public void handle(Request request, Response response) throws Exception { userSession.checkLoggedIn(); int page = request.mandatoryParamAsInt(Param.PAGE); int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE); Set<String> desiredFields = desiredFields(request); String query = request.param(Param.TEXT_QUERY); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = support.getOrganization(dbSession, request.param(PARAM_ORGANIZATION)); userSession.checkPermission(PROVISION_PROJECTS, organization); ComponentQuery dbQuery = buildDbQuery(query); List<ComponentDto> projects = dbClient.componentDao().selectByQuery(dbSession, organization.getUuid(), dbQuery, offset(page, pageSize), pageSize); int nbOfProjects = dbClient.componentDao().countByQuery(dbSession, organization.getUuid(), dbQuery); ProvisionedWsResponse result = ProvisionedWsResponse.newBuilder() .addAllProjects(writeProjects(projects, desiredFields)) .setPaging(Paging.newBuilder() .setTotal(nbOfProjects) .setPageIndex(page) .setPageSize(pageSize)) .build(); writeProtobuf(result, request, response); } }