]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9054 PageDefinition has ORGANIZATION scope
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 11 Apr 2017 07:00:21 +0000 (09:00 +0200)
committerGrégoire Aubert <gregaubert@users.noreply.github.com>
Wed, 12 Apr 2017 14:36:29 +0000 (16:36 +0200)
server/sonar-server/src/main/java/org/sonar/server/ui/PageRepository.java
server/sonar-server/src/test/java/org/sonar/server/ui/PageRepositoryTest.java
sonar-plugin-api/src/main/java/org/sonar/api/web/page/Page.java
sonar-plugin-api/src/main/java/org/sonar/api/web/page/PageDefinition.java

index 516849eb8aa6e931ee6180aedc87054bf6cd3290..1adf918d83a85de046f28b50512958d2f0173b9f 100644 (file)
@@ -34,7 +34,6 @@ import org.sonar.api.web.page.Page.Qualifier;
 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;
@@ -42,6 +41,8 @@ import static java.util.Comparator.comparing;
 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 {
@@ -78,7 +79,7 @@ public class PageRepository implements Startable {
       .peek(checkWellFormed())
       .peek(checkPluginExists())
       .sorted(comparing(Page::getKey))
-      .collect(MoreCollectors.toList());
+      .collect(toList());
   }
 
   @Override
@@ -90,6 +91,10 @@ public class PageRepository implements Startable {
     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);
@@ -99,8 +104,8 @@ public class PageRepository implements Startable {
     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
index 1060bb1d0ea79ae47f416a39c7f52324dd6c2429..cde31b07f52f603585834871543a026233fb77d6 100644 (file)
@@ -38,6 +38,7 @@ import static org.mockito.Mockito.mock;
 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 {
 
@@ -113,6 +114,36 @@ 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);
index b14bbe2812ca509f2e942bba39b34ab69124935f..5e6391c2405191c8c89018144eba8ba5cecbf276 100644 (file)
@@ -75,7 +75,7 @@ public final class Page {
   }
 
   public enum Scope {
-    GLOBAL, COMPONENT
+    GLOBAL, ORGANIZATION, COMPONENT
   }
 
   public enum Qualifier {
@@ -158,7 +158,7 @@ public final class Page {
       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)) {
index c607c9939af0c9df79080dd0091e69414735e32e..37f4f4d9f3be94efc6ea0f1e7111b3b8cd0300e8 100644 (file)
@@ -46,6 +46,8 @@ import org.sonar.api.server.ServerSide;
  *        .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>