import com.google.common.collect.Lists;
import com.google.common.collect.Table;
import com.google.common.collect.TreeBasedTable;
-import java.util.Collection;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentQuery;
import org.sonar.db.permission.CountByProjectAndPermissionDto;
import org.sonarqube.ws.client.permission.SearchProjectPermissionsWsRequest;
-import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static org.sonar.api.utils.Paging.forPageIndex;
import static org.sonar.server.component.ResourceTypeFunctions.RESOURCE_TYPE_TO_QUALIFIER;
public class SearchProjectPermissionsDataLoader {
private final DbClient dbClient;
private final PermissionDependenciesFinder finder;
- private final Collection<String> rootQualifiers;
+ private final String[] rootQualifiers;
public SearchProjectPermissionsDataLoader(DbClient dbClient, PermissionDependenciesFinder finder, ResourceTypes resourceTypes) {
this.dbClient = dbClient;
this.finder = finder;
- this.rootQualifiers = Collections2.transform(resourceTypes.getRoots(), RESOURCE_TYPE_TO_QUALIFIER);
+ this.rootQualifiers = Collections2.transform(resourceTypes.getRoots(), RESOURCE_TYPE_TO_QUALIFIER).toArray(new String[resourceTypes.getRoots().size()]);
}
SearchProjectPermissionsData load(SearchProjectPermissionsWsRequest request) {
DbSession dbSession = dbClient.openSession(false);
try {
SearchProjectPermissionsData.Builder data = newBuilder();
- int countRootComponents = countRootComponents(dbSession, qualifiers(request.getQualifier()), request);
+ int countRootComponents = countRootComponents(dbSession, request);
List<ComponentDto> rootComponents = searchRootComponents(dbSession, request, paging(request, countRootComponents));
List<Long> rootComponentIds = Lists.transform(rootComponents, ComponentToIdFunction.INSTANCE);
.andTotal(total);
}
- private int countRootComponents(DbSession dbSession, Collection<String> qualifiers, SearchProjectPermissionsWsRequest request) {
- return dbClient.componentDao().countRootComponents(dbSession, qualifiers, request.getQuery());
+ private int countRootComponents(DbSession dbSession, SearchProjectPermissionsWsRequest request) {
+ return dbClient.componentDao().countByQuery(dbSession, toDbQuery(request));
}
private List<ComponentDto> searchRootComponents(DbSession dbSession, SearchProjectPermissionsWsRequest request, Paging paging) {
- String query = request.getQuery();
Optional<WsProjectRef> project = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey());
if (project.isPresent()) {
return singletonList(finder.getRootComponentOrModule(dbSession, project.get()));
}
- return dbClient.componentDao().selectComponents(dbSession, qualifiers(request.getQualifier()), paging.offset(), paging.pageSize(), query);
+ return dbClient.componentDao().selectByQuery(dbSession, toDbQuery(request), paging.offset(), paging.pageSize());
}
- private Collection<String> qualifiers(@Nullable String requestQualifier) {
+ private ComponentQuery toDbQuery(SearchProjectPermissionsWsRequest wsRequest) {
+ return ComponentQuery.builder()
+ .setQualifiers(qualifiers(wsRequest.getQualifier()))
+ .setNameOrKeyQuery(wsRequest.getQuery())
+ .build();
+ }
+
+ private String[] qualifiers(@Nullable String requestQualifier) {
return requestQualifier == null
? rootQualifiers
- : singleton(requestQualifier);
+ : new String[] {requestQualifier};
}
private Table<Long, String, Integer> userCountByRootComponentIdAndPermission(DbSession dbSession, List<Long> rootComponentIds) {
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.component.ComponentDbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ResourceTypesRule;
import org.sonar.db.user.GroupDto;
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
+ ComponentDbTester componentDb = new ComponentDbTester(db);
WsActionTester ws;
I18nRule i18n = new I18nRule();
@Test
public void search_by_query_on_name() {
- insertComponent(newProjectDto().setName("project-name"));
- insertComponent(newProjectDto().setName("another-name"));
- commit();
+ componentDb.insertProjectAndSnapshot(newProjectDto().setName("project-name"));
+ componentDb.insertProjectAndSnapshot(newProjectDto().setName("another-name"));
+ componentDb.indexProjects();
String result = ws.newRequest()
.setParam(TEXT_QUERY, "project")
}
@Test
- public void search_by_query_on_key() {
- insertComponent(newProjectDto().setKey("project-key"));
- insertComponent(newProjectDto().setKey("another-key"));
- commit();
+ public void search_by_query_on_key_must_match_exactly() {
+ componentDb.insertProjectAndSnapshot(newProjectDto().setKey("project-key"));
+ componentDb.insertProjectAndSnapshot(newProjectDto().setKey("another-key"));
+ componentDb.indexProjects();
String result = ws.newRequest()
- .setParam(TEXT_QUERY, "project")
+ .setParam(TEXT_QUERY, "project-key")
.execute().getInput();
assertThat(result).contains("project-key")
@Test
public void handle_more_than_1000_projects() {
for (int i = 1; i <= 1001; i++) {
- insertComponent(newProjectDto("project-uuid-" + i));
+ componentDb.insertProjectAndSnapshot(newProjectDto("project-uuid-" + i));
}
- commit();
+ componentDb.indexProjects();
String result = ws.newRequest()
.setParam(TEXT_QUERY, "project")
return mapper(session).selectProjects();
}
- /**
- * Does not return component copies
- */
- public List<ComponentDto> selectComponents(DbSession session, Collection<String> qualifiers, int offset, int limit, @Nullable String query) {
- Map<String, Object> parameters = newHashMapWithExpectedSize(3);
- addProjectQualifier(parameters);
- addPartialQueryParameterIfNotNull(parameters, query);
- addQualifiers(parameters, qualifiers);
-
- return mapper(session).selectComponents(parameters, new RowBounds(offset, limit));
- }
-
- public int countRootComponents(DbSession session, Collection<String> qualifiers, @Nullable String query) {
- Map<String, Object> parameters = newHashMapWithExpectedSize(2);
- addPartialQueryParameterIfNotNull(parameters, query);
- addQualifiers(parameters, qualifiers);
-
- return mapper(session).countRootComponents(parameters);
- }
-
public List<ComponentDto> selectProvisionedProjects(DbSession session, int offset, int limit, @Nullable String query) {
Map<String, Object> parameters = newHashMapWithExpectedSize(2);
addProjectQualifier(parameters);
parameters.put("qualifier", Qualifiers.PROJECT);
}
- private static void addQualifiers(Map<String, Object> parameters, Collection<String> qualifiers) {
- parameters.put("qualifiers", qualifiers);
- }
-
public void insert(DbSession session, ComponentDto item) {
mapper(session).insert(item);
}
assertThat(underTest.selectByKey(dbSession, "PROJECT_2")).isPresent();
}
- @Test
- public void select_components_with_paging_query_and_qualifiers() {
- underTest.insert(dbSession, newProjectDto().setName("aaaa-name"));
- underTest.insert(dbSession, newView());
- underTest.insert(dbSession, newDeveloper("project-name"));
- for (int i = 9; i >= 1; i--) {
- underTest.insert(dbSession, newProjectDto().setName("project-" + i));
- }
-
- List<ComponentDto> result = underTest.selectComponents(dbSession, singleton(Qualifiers.PROJECT), 1, 3, "project");
-
- assertThat(result).hasSize(3);
- assertThat(result).extracting("name").containsExactly("project-2", "project-3", "project-4");
- }
-
@Test
public void select_by_query_with_paging_query_and_qualifiers() {
componentDb.insertProjectAndSnapshot(newProjectDto().setName("aaaa-name"));