@Test public void select_by_query_with_special_characters() { String groupNameWithSpecialCharacters = "group%_%/name"; underTest.insert(dbSession, newGroupDto().setName(groupNameWithSpecialCharacters).setOrganizationUuid("org1")); db.commit(); List<GroupDto> result = underTest.selectByQuery(dbSession, "org1", "roup%_%/nam", 0, 10); int resultCount = underTest.countByQuery(dbSession, "org1", "roup%_%/nam"); assertThat(result).hasSize(1); assertThat(result.get(0).getName()).isEqualTo(groupNameWithSpecialCharacters); assertThat(resultCount).isEqualTo(1); }
@Test public void countByQuery() { OrganizationDto organization1 = db.organizations().insert(organizationDto -> organizationDto.setUuid("org1")); OrganizationDto organization2 = db.organizations().insert(organizationDto -> organizationDto.setUuid("org2")); db.users().insertGroup(organization1, "sonar-users"); db.users().insertGroup(organization1, "SONAR-ADMINS"); db.users().insertGroup(organization1, "customers-group1"); db.users().insertGroup(organization1, "customers-group2"); db.users().insertGroup(organization1, "customers-group3"); // Group on another organization db.users().insertGroup(organization2, "customers-group4"); // Null query assertThat(underTest.countByQuery(dbSession, "org1", null)).isEqualTo(5); // Empty query assertThat(underTest.countByQuery(dbSession, "org1", "")).isEqualTo(5); // Filter on name assertThat(underTest.countByQuery(dbSession, "org1", "sonar")).isEqualTo(2); }
@Override public void handle(Request request, Response response) throws Exception { int page = request.mandatoryParamAsInt(Param.PAGE); int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE); SearchOptions options = new SearchOptions() .setPage(page, pageSize); String query = defaultIfBlank(request.param(Param.TEXT_QUERY), ""); Set<String> fields = neededFields(request); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = groupWsSupport.findOrganizationByKey(dbSession, request.param(PARAM_ORGANIZATION_KEY)); userSession.checkLoggedIn().checkPermission(ADMINISTER, organization); GroupDto defaultGroup = defaultGroupFinder.findDefaultGroup(dbSession, organization.getUuid()); int limit = dbClient.groupDao().countByQuery(dbSession, organization.getUuid(), query); Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(limit); List<GroupDto> groups = dbClient.groupDao().selectByQuery(dbSession, organization.getUuid(), query, options.getOffset(), pageSize); List<Integer> groupIds = groups.stream().map(GroupDto::getId).collect(MoreCollectors.toList(groups.size())); Map<String, Integer> userCountByGroup = dbClient.groupMembershipDao().countUsersByGroups(dbSession, groupIds); writeProtobuf(buildResponse(groups, userCountByGroup, fields, paging, defaultGroup), request, response); } }
@Override public void handle(Request request, Response response) throws Exception { int page = request.mandatoryParamAsInt(Param.PAGE); int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE); SearchOptions options = new SearchOptions() .setPage(page, pageSize); String query = defaultIfBlank(request.param(Param.TEXT_QUERY), ""); Set<String> fields = neededFields(request); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = groupWsSupport.findOrganizationByKey(dbSession, request.param(PARAM_ORGANIZATION_KEY)); userSession.checkLoggedIn().checkPermission(ADMINISTER, organization); GroupDto defaultGroup = defaultGroupFinder.findDefaultGroup(dbSession, organization.getUuid()); int limit = dbClient.groupDao().countByQuery(dbSession, organization.getUuid(), query); Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(limit); List<GroupDto> groups = dbClient.groupDao().selectByQuery(dbSession, organization.getUuid(), query, options.getOffset(), pageSize); List<Integer> groupIds = groups.stream().map(GroupDto::getId).collect(MoreCollectors.toList(groups.size())); Map<String, Integer> userCountByGroup = dbClient.groupMembershipDao().countUsersByGroups(dbSession, groupIds); writeProtobuf(buildResponse(groups, userCountByGroup, fields, paging, defaultGroup), request, response); } }