import org.sonar.api.web.page.Page.Scope;
import org.sonar.api.web.page.PageDefinition;
import org.sonar.core.platform.PluginRepository;
-import org.sonar.core.util.stream.MoreCollectors;
import static com.google.common.base.Preconditions.checkState;
import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static org.sonar.api.web.page.Page.Scope.COMPONENT;
import static org.sonar.api.web.page.Page.Scope.GLOBAL;
+import static org.sonar.api.web.page.Page.Scope.ORGANIZATION;
+import static org.sonar.core.util.stream.MoreCollectors.toList;
@ServerSide
public class PageRepository implements Startable {
.peek(checkWellFormed())
.peek(checkPluginExists())
.sorted(comparing(Page::getKey))
- .collect(MoreCollectors.toList());
+ .collect(toList());
}
@Override
return getPages(GLOBAL, isAdmin, null);
}
+ public List<Page> getOrganizationPages(boolean isAdmin) {
+ return getPages(ORGANIZATION, isAdmin, null);
+ }
+
public List<Page> getComponentPages(boolean isAdmin, String qualifierKey) {
Qualifier qualifier = Qualifier.fromKey(qualifierKey);
return qualifier == null ? emptyList() : getPages(COMPONENT, isAdmin, qualifier);
return getAllPages().stream()
.filter(p -> p.getScope().equals(scope))
.filter(p -> p.isAdmin() == isAdmin)
- .filter(p -> GLOBAL.equals(p.getScope()) || p.getComponentQualifiers().contains(qualifier))
- .collect(MoreCollectors.toList());
+ .filter(p -> !COMPONENT.equals(p.getScope()) || p.getComponentQualifiers().contains(qualifier))
+ .collect(toList());
}
@VisibleForTesting
import static org.mockito.Mockito.when;
import static org.sonar.api.web.page.Page.Scope.COMPONENT;
import static org.sonar.api.web.page.Page.Scope.GLOBAL;
+import static org.sonar.api.web.page.Page.Scope.ORGANIZATION;
public class PageRepositoryTest {
assertThat(result).extracting(Page::getKey).containsExactly("my_plugin/K1", "my_plugin/K2", "my_plugin/K3");
}
+ @Test
+ public void get_organization_pages() {
+ PageDefinition plugin = context -> context
+ .addPage(Page.builder("my_plugin/G1").setName("G1").setScope(GLOBAL).build())
+ .addPage(Page.builder("my_plugin/C1").setName("C1").setScope(COMPONENT).build())
+ .addPage(Page.builder("my_plugin/O1").setName("O1").setScope(ORGANIZATION).build())
+ .addPage(Page.builder("my_plugin/O2").setName("O2").setScope(ORGANIZATION).build())
+ .addPage(Page.builder("my_plugin/O3").setName("O3").setScope(ORGANIZATION).build())
+ .addPage(Page.builder("my_plugin/OA1").setName("OA1").setScope(ORGANIZATION).setAdmin(true).build());
+ underTest = new PageRepository(pluginRepository, new PageDefinition[] {plugin});
+ underTest.start();
+
+ List<Page> result = underTest.getOrganizationPages(false);
+
+ assertThat(result).extracting(Page::getKey).containsExactly("my_plugin/O1", "my_plugin/O2", "my_plugin/O3");
+ }
+
+ @Test
+ public void get_organization_admin_pages() {
+ PageDefinition plugin = context -> context
+ .addPage(Page.builder("my_plugin/O1").setName("O1").setScope(ORGANIZATION).build())
+ .addPage(Page.builder("my_plugin/O2").setName("O2").setScope(ORGANIZATION).setAdmin(true).build());
+ underTest = new PageRepository(pluginRepository, new PageDefinition[] {plugin});
+ underTest.start();
+
+ List<Page> result = underTest.getOrganizationPages(true);
+
+ assertThat(result).extracting(Page::getKey).containsExactly("my_plugin/O2");
+ }
+
@Test
public void fail_if_pages_called_before_server_startup() {
expectedException.expect(NullPointerException.class);
}
public enum Scope {
- GLOBAL, COMPONENT
+ GLOBAL, ORGANIZATION, COMPONENT
}
public enum Qualifier {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Name must be defined and not empty");
}
- if (qualifiers.length > 0 && GLOBAL.equals(scope)) {
+ if (qualifiers.length > 0 && !COMPONENT.equals(scope)) {
throw new IllegalArgumentException(format("The scope must be '%s' when component qualifiers are provided", COMPONENT));
}
if (qualifiers.length == 0 && COMPONENT.equals(scope)) {
* .setScope(Scope.COMPONENT).setQualifiers(Qualifier.PROJECT, Qualifier.MODULE).build())
* // Page on all components (see Qualifier class) supported
* .addPage(Page.builder("my_plugin/component_page").setName("Component Page").setScope(Scope.COMPONENT).build());
+ * // Organization page (when organizations are enabled)
+ * .addPage(Page.builder("my_plugin/org_page").setName("Organization Page").setScope(Scope.ORGANIZATION).build());
* }
* }
* </pre>