This class should be dropped as no plugins provide new types anymore.
That was used only by the developer cockpit plugin, which is end
of life.
public class ResourceTypesRule extends ResourceTypes {
private Set<ResourceType> allResourceTypes = emptySet();
private Set<ResourceType> rootResourceTypes = emptySet();
- private List<String> childrenQualifiers = emptyList();
private List<String> leavesQualifiers = emptyList();
- @Override
- public Collection<ResourceType> getAll() {
- return allResourceTypes;
+ public ResourceTypesRule() {
+ super(new ResourceTypeTree[0]);
}
+
@Override
- public Collection<ResourceType> getAllOrdered() {
+ public Collection<ResourceType> getAll() {
return allResourceTypes;
}
return this;
}
- public ResourceTypesRule setChildrenQualifiers(String... qualifiers) {
- childrenQualifiers = ImmutableList.copyOf(qualifiers);
- return this;
- }
-
public ResourceTypesRule setAllQualifiers(String... qualifiers) {
Set<ResourceType> resourceTypes = new HashSet<>();
for (String qualifier : qualifiers) {
.findAny().orElse(null);
}
- @Override
- public Collection<ResourceType> getAllWithPropertyKey(String propertyKey) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Collection<ResourceType> getAllWithPropertyValue(String propertyKey, String propertyValue) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Collection<ResourceType> getAllWithPropertyValue(String propertyKey, boolean propertyValue) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public List<String> getChildrenQualifiers(String qualifier) {
- return this.childrenQualifiers;
- }
-
- @Override
- public List<ResourceType> getChildren(String qualifier) {
- throw new UnsupportedOperationException();
- }
-
@Override
public List<String> getLeavesQualifiers(String qualifier) {
return this.leavesQualifiers;
}
-
- @Override
- public ResourceTypeTree getTree(String qualifier) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public ResourceType getRoot(String qualifier) {
- throw new UnsupportedOperationException();
- }
}
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
-import static org.sonar.api.resources.Qualifiers.DIRECTORY;
import static org.sonar.api.resources.Qualifiers.FILE;
-import static org.sonar.api.resources.Qualifiers.MODULE;
import static org.sonar.api.resources.Qualifiers.PROJECT;
import static org.sonar.api.resources.Qualifiers.UNIT_TEST_FILE;
import static org.sonar.db.component.ComponentTesting.newChildComponent;
private ResourceTypesRule resourceTypes = new ResourceTypesRule()
.setRootQualifiers(PROJECT)
- .setChildrenQualifiers(MODULE, FILE, DIRECTORY)
.setLeavesQualifiers(FILE, UNIT_TEST_FILE);
private ComponentDbTester componentDb = new ComponentDbTester(db);
private DbClient dbClient = db.getDbClient();
private WsActionTester ws = new WsActionTester(new TreeAction(dbClient, new ComponentFinder(dbClient, resourceTypes), resourceTypes, userSession, Mockito.mock(I18n.class)));
@Test
- public void verify_definition() throws Exception {
+ public void verify_definition() {
WebService.Action action = ws.getDef();
assertThat(action.since()).isEqualTo("5.4");
}
@Test
- public void return_children() throws IOException {
+ public void return_children() {
ComponentDto project = newPrivateProjectDto(db.organizations().insert(), "project-uuid");
componentDb.insertProjectAndSnapshot(project);
ComponentDto module = newModuleDto("module-uuid-1", project);
}
@Test
- public void return_descendants() throws IOException {
+ public void return_descendants() {
ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid");
SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(project);
ComponentDto module = newModuleDto("module-uuid-1", project);
}
@Test
- public void filter_descendants_by_qualifier() throws IOException {
+ public void filter_descendants_by_qualifier() {
ComponentDto project = newPrivateProjectDto(db.organizations().insert(), "project-uuid");
componentDb.insertProjectAndSnapshot(project);
componentDb.insertComponent(newFileDto(project, 1));
}
@Test
- public void return_leaves() throws IOException {
+ public void return_leaves() {
ComponentDto project = newPrivateProjectDto(db.getDefaultOrganization(), "project-uuid");
componentDb.insertProjectAndSnapshot(project);
ComponentDto module = newModuleDto("module-uuid-1", project);
}
@Test
- public void sort_descendants_by_qualifier() throws IOException {
+ public void sort_descendants_by_qualifier() {
ComponentDto project = newPrivateProjectDto(db.organizations().insert(), "project-uuid");
componentDb.insertProjectAndSnapshot(project);
componentDb.insertComponent(newFileDto(project, 1));
import static org.sonar.api.measures.Metric.ValueType.RATING;
import static org.sonar.api.resources.Qualifiers.DIRECTORY;
import static org.sonar.api.resources.Qualifiers.FILE;
-import static org.sonar.api.resources.Qualifiers.MODULE;
import static org.sonar.api.resources.Qualifiers.PROJECT;
import static org.sonar.api.resources.Qualifiers.UNIT_TEST_FILE;
import static org.sonar.api.server.ws.WebService.Param.SORT;
import static org.sonar.db.component.ComponentTesting.newFileDto;
import static org.sonar.db.component.ComponentTesting.newProjectCopy;
import static org.sonar.db.component.SnapshotTesting.newAnalysis;
-import static org.sonar.server.measure.ws.ComponentTreeAction.LEAVES_STRATEGY;
-import static org.sonar.server.measure.ws.ComponentTreeAction.METRIC_PERIOD_SORT;
-import static org.sonar.server.measure.ws.ComponentTreeAction.METRIC_SORT;
-import static org.sonar.server.measure.ws.ComponentTreeAction.NAME_SORT;
-import static org.sonar.server.measure.ws.ComponentTreeAction.WITH_MEASURES_ONLY_METRIC_SORT_FILTER;
-import static org.sonar.test.JsonAssert.assertJson;
import static org.sonar.server.component.ws.MeasuresWsParameters.ADDITIONAL_PERIODS;
import static org.sonar.server.component.ws.MeasuresWsParameters.DEPRECATED_PARAM_BASE_COMPONENT_ID;
import static org.sonar.server.component.ws.MeasuresWsParameters.DEPRECATED_PARAM_BASE_COMPONENT_KEY;
import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_METRIC_SORT_FILTER;
import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_QUALIFIERS;
import static org.sonar.server.component.ws.MeasuresWsParameters.PARAM_STRATEGY;
+import static org.sonar.server.measure.ws.ComponentTreeAction.LEAVES_STRATEGY;
+import static org.sonar.server.measure.ws.ComponentTreeAction.METRIC_PERIOD_SORT;
+import static org.sonar.server.measure.ws.ComponentTreeAction.METRIC_SORT;
+import static org.sonar.server.measure.ws.ComponentTreeAction.NAME_SORT;
+import static org.sonar.server.measure.ws.ComponentTreeAction.WITH_MEASURES_ONLY_METRIC_SORT_FILTER;
+import static org.sonar.test.JsonAssert.assertJson;
public class ComponentTreeActionTest {
@Rule
private I18nRule i18n = new I18nRule();
private ResourceTypesRule resourceTypes = new ResourceTypesRule()
.setRootQualifiers(PROJECT)
- .setChildrenQualifiers(MODULE, FILE, DIRECTORY)
.setLeavesQualifiers(FILE, UNIT_TEST_FILE);
private ComponentDbTester componentDb = new ComponentDbTester(db);
private DbClient dbClient = db.getDbClient();
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
-import java.util.Objects;
-import java.util.Set;
-import java.util.function.Predicate;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.server.ServerSide;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;
-import static java.util.Collections.unmodifiableSet;
import static java.util.Objects.requireNonNull;
/**
@ComputeEngineSide
public class ResourceTypes {
- public static final Predicate<ResourceType> AVAILABLE_FOR_FILTERS = input -> input != null && input.getBooleanProperty("supportsMeasureFilters");
-
private final Map<String, ResourceTypeTree> treeByQualifier;
private final Map<String, ResourceType> typeByQualifier;
- private final Collection<ResourceType> orderedTypes;
private final Collection<ResourceType> rootTypes;
- public ResourceTypes() {
- this(new ResourceTypeTree[0]);
- }
-
public ResourceTypes(ResourceTypeTree[] trees) {
requireNonNull(trees);
treeByQualifier = unmodifiableMap(new LinkedHashMap<>(treeMap));
typeByQualifier = unmodifiableMap(new LinkedHashMap<>(typeMap));
rootTypes = unmodifiableList(new ArrayList<>(rootsSet));
- orderedTypes = unmodifiableSet(orderedTypes(typeMap));
- }
-
- private static Set<ResourceType> orderedTypes(Map<String, ResourceType> typeByQualifier) {
- Map<String, ResourceType> mutableTypesByQualifier = new LinkedHashMap<>(typeByQualifier);
- ResourceType view = mutableTypesByQualifier.remove(Qualifiers.VIEW);
- ResourceType subView = mutableTypesByQualifier.remove(Qualifiers.SUBVIEW);
- ResourceType application = mutableTypesByQualifier.remove(Qualifiers.APP);
-
- return Stream.concat(Stream.of(view, subView, application), mutableTypesByQualifier.values().stream())
- .filter(Objects::nonNull)
- .collect(Collectors.toCollection(LinkedHashSet::new));
}
public ResourceType get(String qualifier) {
return typeByQualifier.values();
}
- public Collection<ResourceType> getAllOrdered() {
- return orderedTypes;
- }
-
public Collection<ResourceType> getRoots() {
return rootTypes;
}
- public Collection<ResourceType> getAll(Predicate<ResourceType> predicate) {
- return typeByQualifier.values().stream()
- .filter(predicate)
- .collect(Collectors.toList());
- }
-
- public Collection<ResourceType> getAllWithPropertyKey(String propertyKey) {
- return typeByQualifier.values()
- .stream()
- .filter(Objects::nonNull)
- .filter(input -> input.hasProperty(propertyKey))
- .collect(Collectors.toList());
- }
-
- public Collection<ResourceType> getAllWithPropertyValue(String propertyKey, String propertyValue) {
- return typeByQualifier.values()
- .stream()
- .filter(Objects::nonNull)
- .filter(input -> Objects.equals(propertyValue, input.getStringProperty(propertyKey)))
- .collect(Collectors.toList());
- }
-
- public Collection<ResourceType> getAllWithPropertyValue(String propertyKey, boolean propertyValue) {
- return typeByQualifier.values()
- .stream()
- .filter(Objects::nonNull)
- .filter(input -> input.getBooleanProperty(propertyKey) == propertyValue)
- .collect(Collectors.toList());
- }
-
- public List<String> getChildrenQualifiers(String qualifier) {
- ResourceTypeTree tree = getTree(qualifier);
- if (tree != null) {
- return tree.getChildren(qualifier);
- }
- return Collections.emptyList();
- }
-
- public List<ResourceType> getChildren(String qualifier) {
- return getChildrenQualifiers(qualifier)
- .stream()
- .map(typeByQualifier::get)
- .collect(Collectors.toList());
- }
-
public List<String> getLeavesQualifiers(String qualifier) {
ResourceTypeTree tree = getTree(qualifier);
if (tree != null) {
return Collections.emptyList();
}
- public ResourceTypeTree getTree(String qualifier) {
+ private ResourceTypeTree getTree(String qualifier) {
return treeByQualifier.get(qualifier);
}
-
- public ResourceType getRoot(String qualifier) {
- return getTree(qualifier).getRootType();
- }
-
}
assertThat(qualifiers(types.getAll())).containsExactly(Qualifiers.PROJECT, Qualifiers.DIRECTORY, Qualifiers.FILE, Qualifiers.VIEW, Qualifiers.SUBVIEW, Qualifiers.APP);
}
- @Test
- public void get_all_ordered() {
- assertThat(qualifiers(types.getAllOrdered())).containsExactly(Qualifiers.VIEW, Qualifiers.SUBVIEW, Qualifiers.APP, Qualifiers.PROJECT, Qualifiers.DIRECTORY, Qualifiers.FILE);
- }
-
@Test
public void get_roots() {
assertThat(qualifiers(types.getRoots())).containsOnly(Qualifiers.PROJECT, Qualifiers.VIEW, Qualifiers.APP);
}
- @Test
- public void get_all_predicate() {
- Collection<ResourceType> forFilters = types.getAll(ResourceTypes.AVAILABLE_FOR_FILTERS);
- assertThat(qualifiers(forFilters)).containsOnly(Qualifiers.PROJECT, Qualifiers.VIEW, Qualifiers.APP).doesNotHaveDuplicates();
- }
-
- @Test
- public void get_all_with_property_key() {
- assertThat(qualifiers(types.getAllWithPropertyKey("supportsMeasureFilters"))).containsOnly(Qualifiers.APP, Qualifiers.VIEW, Qualifiers.PROJECT);
- }
-
- @Test
- public void get_all_with_property_value() {
- assertThat(qualifiers(types.getAllWithPropertyValue("supportsMeasureFilters", "true"))).containsOnly(Qualifiers.APP, Qualifiers.VIEW, Qualifiers.PROJECT);
- assertThat(qualifiers(types.getAllWithPropertyValue("supportsMeasureFilters", true))).containsOnly(Qualifiers.APP, Qualifiers.VIEW, Qualifiers.PROJECT);
- assertThat(qualifiers(types.getAllWithPropertyValue("supportsMeasureFilters", false))).containsOnly(Qualifiers.SUBVIEW, Qualifiers.DIRECTORY, Qualifiers.FILE);
- }
-
- @Test
- public void get_children_qualifiers() {
- assertThat(types.getChildrenQualifiers(Qualifiers.PROJECT)).containsExactly(Qualifiers.DIRECTORY);
- assertThat(types.getChildrenQualifiers(Qualifiers.SUBVIEW)).containsExactly(Qualifiers.PROJECT);
- assertThat(types.getChildrenQualifiers("xxx")).isEmpty();
- assertThat(types.getChildrenQualifiers(Qualifiers.FILE)).isEmpty();
- }
-
- @Test
- public void get_children() {
- assertThat(qualifiers(types.getChildren(Qualifiers.PROJECT))).contains(Qualifiers.DIRECTORY);
- assertThat(qualifiers(types.getChildren(Qualifiers.SUBVIEW))).contains(Qualifiers.PROJECT);
- }
-
@Test
public void get_leaves_qualifiers() {
assertThat(types.getLeavesQualifiers(Qualifiers.PROJECT)).containsExactly(Qualifiers.FILE);
assertThat(types.getLeavesQualifiers("xxx")).isEmpty();
}
- @Test
- public void get_tree() {
- assertThat(qualifiers(types.getTree(Qualifiers.VIEW).getTypes())).containsOnly(Qualifiers.VIEW, Qualifiers.SUBVIEW).doesNotHaveDuplicates();
- assertThat(qualifiers(types.getTree(Qualifiers.APP).getTypes())).containsOnly(Qualifiers.APP).doesNotHaveDuplicates();
- assertThat(types.getTree("xxx")).isNull();
- }
-
- @Test
- public void get_root() {
- assertThat(types.getRoot(Qualifiers.FILE).getQualifier()).isEqualTo(Qualifiers.PROJECT);
- }
-
@Test(expected = IllegalStateException.class)
public void fail_on_duplicated_qualifier() {
ResourceTypeTree tree1 = ResourceTypeTree.builder()