]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8761 add organizationsEnabled to response of api/navigation/global
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Thu, 9 Feb 2017 12:37:20 +0000 (13:37 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 10 Feb 2017 22:07:21 +0000 (23:07 +0100)
server/sonar-server/src/main/java/org/sonar/server/ui/ws/GlobalAction.java
server/sonar-server/src/main/resources/org/sonar/server/ui/ws/global-example.json
server/sonar-server/src/test/java/org/sonar/server/ui/ws/GlobalActionTest.java
server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalActionTest/organization_support.json [new file with mode: 0644]

index 6f0a7c39aaaef54f47894ba8cd148372227b8ec0..1e2a84d57c068f729c11db100b29604099602b20 100644 (file)
@@ -29,8 +29,10 @@ import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService.NewController;
 import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.db.Database;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
 import org.sonar.db.dialect.H2;
+import org.sonar.server.organization.OrganizationFlags;
 import org.sonar.server.ui.PageRepository;
 import org.sonar.server.ui.VersionFormatter;
 
@@ -57,14 +59,17 @@ public class GlobalAction implements NavigationWsAction {
   private final Settings settings;
   private final ResourceTypes resourceTypes;
   private final Server server;
-  private final Database database;
+  private final DbClient dbClient;
+  private final OrganizationFlags organizationFlags;
 
-  public GlobalAction(PageRepository pageRepository, Settings settings, ResourceTypes resourceTypes, Server server, Database database) {
+  public GlobalAction(PageRepository pageRepository, Settings settings, ResourceTypes resourceTypes, Server server,
+    DbClient dbClient, OrganizationFlags organizationFlags) {
     this.pageRepository = pageRepository;
     this.settings = settings;
     this.resourceTypes = resourceTypes;
     this.server = server;
-    this.database = database;
+    this.dbClient = dbClient;
+    this.organizationFlags = organizationFlags;
   }
 
   @Override
@@ -86,6 +91,7 @@ public class GlobalAction implements NavigationWsAction {
     writeQualifiers(json);
     writeVersion(json);
     writeDatabaseProduction(json);
+    writeOrganizationSupport(json);
     json.endObject().close();
   }
 
@@ -127,6 +133,12 @@ public class GlobalAction implements NavigationWsAction {
   }
 
   private void writeDatabaseProduction(JsonWriter json) {
-    json.prop("productionDatabase", !database.getDialect().getId().equals(H2.ID));
+    json.prop("productionDatabase", !dbClient.getDatabase().getDialect().getId().equals(H2.ID));
+  }
+
+  private void writeOrganizationSupport(JsonWriter json) {
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      json.prop("organizationsEnabled", organizationFlags.isEnabled(dbSession));
+    }
   }
 }
index bef24054066ca437a83af6ef6d92aed2db2489d9..d339df3d19f193a0970384b79be95067bdf04d7c 100644 (file)
@@ -24,5 +24,6 @@
     "POL"
   ],
   "version": "6.2",
-  "productionDatabase": true
+  "productionDatabase": true,
+  "organizationsEnabled": false
 }
index c0958b6f515317c46146d546e9136221d58dd57f..179ff4b1ce19234c359a50441ee6b36cc90cccb6 100644 (file)
@@ -30,15 +30,17 @@ import org.sonar.api.resources.ResourceTypes;
 import org.sonar.api.web.page.Page;
 import org.sonar.api.web.page.PageDefinition;
 import org.sonar.core.platform.PluginRepository;
-import org.sonar.db.Database;
+import org.sonar.db.DbClient;
 import org.sonar.db.dialect.H2;
 import org.sonar.db.dialect.MySql;
+import org.sonar.server.organization.TestOrganizationFlags;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ui.PageRepository;
 import org.sonar.server.ws.WsActionTester;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 import static org.sonar.test.JsonAssert.assertJson;
@@ -51,9 +53,10 @@ public class GlobalActionTest {
   private Settings settings = new MapSettings();
 
   private Server server = mock(Server.class);
-  private Database database = mock(Database.class);
+  private DbClient dbClient = mock(DbClient.class, RETURNS_DEEP_STUBS);
 
   private WsActionTester ws;
+  private TestOrganizationFlags organizationFlags = TestOrganizationFlags.standalone();
 
   @Test
   public void empty_call() throws Exception {
@@ -143,11 +146,19 @@ public class GlobalActionTest {
   @Test
   public void return_if_production_database_or_not() throws Exception {
     init();
-    when(database.getDialect()).thenReturn(new MySql());
+    when(dbClient.getDatabase().getDialect()).thenReturn(new MySql());
 
     executeAndVerify("production_database.json");
   }
 
+  @Test
+  public void organization_support_is_enabled() throws Exception {
+    init();
+    organizationFlags.setEnabled(true);
+
+    executeAndVerify("organization_support.json");
+  }
+
   @Test
   public void test_example_response() throws Exception {
     init(createPages(), new ResourceTypeTree[] {
@@ -169,7 +180,7 @@ public class GlobalActionTest {
     settings.setProperty("sonar.updatecenter.activate", false);
     settings.setProperty("sonar.technicalDebt.ratingGrid", "0.05,0.1,0.2,0.5");
     when(server.getVersion()).thenReturn("6.2");
-    when(database.getDialect()).thenReturn(new MySql());
+    when(dbClient.getDatabase().getDialect()).thenReturn(new MySql());
 
     String result = call();
     assertJson(ws.getDef().responseExampleAsString()).isSimilarTo(result);
@@ -180,7 +191,7 @@ public class GlobalActionTest {
   }
 
   private void init(org.sonar.api.web.page.Page[] pages, ResourceTypeTree[] resourceTypeTrees) {
-    when(database.getDialect()).thenReturn(new H2());
+    when(dbClient.getDatabase().getDialect()).thenReturn(new H2());
     when(server.getVersion()).thenReturn("6.42");
     PluginRepository pluginRepository = mock(PluginRepository.class);
     when(pluginRepository.hasPlugin(anyString())).thenReturn(true);
@@ -190,7 +201,7 @@ public class GlobalActionTest {
       }
     }});
     pageRepository.start();
-    ws = new WsActionTester(new GlobalAction(pageRepository, settings, new ResourceTypes(resourceTypeTrees), server, database));
+    ws = new WsActionTester(new GlobalAction(pageRepository, settings, new ResourceTypes(resourceTypeTrees), server, dbClient, organizationFlags));
   }
 
   private void executeAndVerify(String json) {
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalActionTest/organization_support.json b/server/sonar-server/src/test/resources/org/sonar/server/ui/ws/GlobalActionTest/organization_support.json
new file mode 100644 (file)
index 0000000..58c7c33
--- /dev/null
@@ -0,0 +1,3 @@
+{
+  "organizationsEnabled": true
+}