import com.google.common.collect.Lists;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.ibatis.session.RowBounds;
import org.sonar.db.DbSession;
import org.sonar.db.RowNotFoundException;
+import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Maps.newHashMapWithExpectedSize;
import static org.sonar.db.DatabaseUtils.executeLargeInputs;
return mapper(dbSession).selectByProjectUuid(projectUuid);
}
+ /**
+ * Retrieve enabled components keys with given qualifiers
+ *
+ * Used by Views plugin
+ */
+ public Set<ComponentDto> selectComponentsByQualifiers(DbSession dbSession, Set<String> qualifiers) {
+ checkArgument(!qualifiers.isEmpty(), "Qualifiers cannot be empty");
+ return new HashSet<>(mapper(dbSession).selectComponentsByQualifiers(qualifiers));
+ }
+
private static void addPartialQueryParameterIfNotNull(Map<String, Object> parameters, @Nullable String keyOrNameFilter) {
if (keyOrNameFilter != null) {
parameters.put("query", "%" + keyOrNameFilter.toUpperCase() + "%");
import org.sonar.test.DbTests;
import static com.google.common.collect.Lists.newArrayList;
+import static com.google.common.collect.Sets.newHashSet;
import static java.util.Collections.singleton;
+import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat;
import static org.sonar.db.component.ComponentTesting.newDeveloper;
public void get_by_keys() {
db.prepareDbUnit(getClass(), "shared.xml");
- List<ComponentDto> results = underTest.selectByKeys(dbSession, Collections.singletonList("org.struts:struts-core:src/org/struts/RequestContext.java"));
+ List<ComponentDto> results = underTest.selectByKeys(dbSession, singletonList("org.struts:struts-core:src/org/struts/RequestContext.java"));
assertThat(results).hasSize(1);
ComponentDto result = results.get(0);
assertThat(result.language()).isEqualTo("java");
assertThat(result.parentProjectId()).isEqualTo(2);
- assertThat(underTest.selectByKeys(dbSession, Collections.singletonList("unknown"))).isEmpty();
+ assertThat(underTest.selectByKeys(dbSession, singletonList("unknown"))).isEmpty();
}
@Test
assertThat(underTest.existsById(111L, dbSession)).isFalse();
}
+ @Test
+ public void select_component_keys_by_qualifiers() {
+ db.prepareDbUnit(getClass(), "shared.xml");
+
+ assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("TRK"))).extracting("kee").containsOnly("org.struts:struts", "org.disabled.project");
+ assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("BRC"))).extracting("kee").containsOnly("org.struts:struts-core");
+ assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("DIR"))).extracting("kee").containsOnly("org.struts:struts-core:src/org/struts");
+ assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("FIL"))).extracting("kee").containsOnly("org.struts:struts-core:src/org/struts/RequestContext.java");
+ assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("unknown"))).isEmpty();
+ }
+
+ @Test
+ public void fail_with_IAE_select_component_keys_by_qualifiers_on_empty_qualifier() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Qualifiers cannot be empty");
+
+ db.prepareDbUnit(getClass(), "shared.xml");
+ underTest.selectComponentsByQualifiers(dbSession, Collections.<String>emptySet());
+ }
+
@Test
public void find_sub_projects_by_component_keys() {
db.prepareDbUnit(getClass(), "multi-modules.xml");