]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5113 Add WS for quality gate JS application initialization (periods, permission...
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 5 Mar 2014 15:38:05 +0000 (16:38 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 5 Mar 2014 15:38:12 +0000 (16:38 +0100)
sonar-server/src/main/java/org/sonar/server/platform/Platform.java
sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QgateAppHandler.java [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java
sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QgateAppHandlerTest.java [new file with mode: 0644]
sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QualityGatesWsTest.java

index 1fb708fb73097d594562201c29000267d2d8680c..619cc7b662511386de76493f62aa115e5eb3f84e 100644 (file)
@@ -102,6 +102,7 @@ import org.sonar.server.permission.PermissionFinder;
 import org.sonar.server.plugins.*;
 import org.sonar.server.qualitygate.QgateProjectFinder;
 import org.sonar.server.qualitygate.QualityGates;
+import org.sonar.server.qualitygate.ws.QgateAppHandler;
 import org.sonar.server.qualitygate.ws.QualityGatesWs;
 import org.sonar.server.qualityprofile.*;
 import org.sonar.server.rule.*;
@@ -311,6 +312,7 @@ public final class Platform {
     servicesContainer.addSingleton(QualityGates.class);
     servicesContainer.addSingleton(ProjectQgateAssociationDao.class);
     servicesContainer.addSingleton(QgateProjectFinder.class);
+    servicesContainer.addSingleton(QgateAppHandler.class);
     servicesContainer.addSingleton(QualityGatesWs.class);
 
     // users
diff --git a/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QgateAppHandler.java b/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QgateAppHandler.java
new file mode 100644 (file)
index 0000000..8e74a11
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.qualitygate.ws;
+
+import org.sonar.api.i18n.I18n;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.RequestHandler;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.core.timemachine.Periods;
+import org.sonar.server.qualitygate.QualityGates;
+
+import java.util.Locale;
+
+public class QgateAppHandler implements RequestHandler {
+
+  private static final String[] MESSAGE_KEYS = {
+      "add_verb",
+      "alerts.error_tooltip",
+      "alerts.notes.error",
+      "alerts.notes.ok",
+      "alerts.notes.warn",
+      "alerts.operator.!=",
+      "alerts.operator.<",
+      "alerts.operator.=",
+      "alerts.operator.>",
+      "alerts.select_metric",
+      "alerts.warn_tooltip",
+      "are_you_sure",
+      "cancel",
+      "copy",
+      "create",
+      "default",
+      "delete",
+      "more",
+      "name",
+      "quality_gates.add",
+      "quality_gates.add_condition",
+      "quality_gates.copy",
+      "quality_gates.health_icons",
+      "quality_gates.introduction",
+      "quality_gates.no_conditions",
+      "quality_gates.noQualityGates",
+      "quality_gates.page",
+      "quality_gates.projects",
+      "quality_gates.projects.all",
+      "quality_gates.projects.deselect_hint",
+      "quality_gates.projects.noResults",
+      "quality_gates.projects.select_hint",
+      "quality_gates.projects.with",
+      "quality_gates.projects.without",
+      "quality_gates.projects_for_default",
+      "quality_gates.rename",
+      "rename",
+      "save",
+      "set_as_default",
+      "unset_as_default",
+      "update_verb",
+      "value",
+      "work_duration.x_days",
+      "work_duration.x_hours",
+      "work_duration.x_minutes",
+  };
+
+  private final QualityGates qualityGates;
+
+  private final Periods periods;
+
+  private final I18n i18n;
+
+  public QgateAppHandler(QualityGates qualityGates, Periods periods, I18n i18n) {
+    this.qualityGates = qualityGates;
+    this.periods = periods;
+    this.i18n = i18n;
+  }
+
+  @Override
+  public void handle(Request request, Response response) {
+    JsonWriter writer = response.newJsonWriter().beginObject();
+    addPermissions(writer);
+    addPeriods(writer);
+    addMessages(writer);
+    writer.endObject().close();
+  }
+
+  private void addPermissions(JsonWriter writer) {
+    writer.prop("edit", qualityGates.currentUserHasWritePermission());
+  }
+
+  private void addPeriods(JsonWriter writer) {
+    writer.name("periods").beginArray();
+    for (int i=0; i < 3; i ++) {
+      writer.beginObject().prop("key", i + 1).prop("text", periods.label(i + 1)).endObject();
+    }
+    writer.endArray();
+  }
+
+  private void addMessages(JsonWriter writer) {
+    writer.name("messages").beginObject();
+    for (String message: MESSAGE_KEYS) {
+      writer.prop(message, i18n.message(Locale.getDefault(), message, message));
+    }
+    writer.endObject();
+  }
+}
index 126f33f2405e3cf0bcdab0685be693abc7c06177..74ea268c1d7f91e65251e1ba01543230dff3706f 100644 (file)
@@ -56,9 +56,12 @@ public class QualityGatesWs implements WebService {
 
   private final QgateProjectFinder projectFinder;
 
-  public QualityGatesWs(QualityGates qualityGates, QgateProjectFinder projectFinder) {
+  private final QgateAppHandler appHandler;
+
+  public QualityGatesWs(QualityGates qualityGates, QgateProjectFinder projectFinder, QgateAppHandler appHandler) {
     this.qualityGates = qualityGates;
     this.projectFinder = projectFinder;
+    this.appHandler = appHandler;
   }
 
   @Override
@@ -71,6 +74,12 @@ public class QualityGatesWs implements WebService {
 
     defineConditionActions(controller);
 
+    controller.newAction("app")
+      .setInternal(true)
+      .setDescription("Get initialization items for the admin UI")
+      .setSince("4.3")
+      .setHandler(appHandler);
+
     controller.done();
   }
 
@@ -372,7 +381,6 @@ public class QualityGatesWs implements WebService {
     if (defaultQgate != null) {
       writer.prop("default", defaultQgate.getId());
     }
-    writer.prop("edit", qualityGates.currentUserHasWritePermission());
     writer.endObject().close();
   }
 
diff --git a/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QgateAppHandlerTest.java b/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/QgateAppHandlerTest.java
new file mode 100644 (file)
index 0000000..5b916d2
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.qualitygate.ws;
+
+import org.json.simple.JSONValue;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.mockito.stubbing.Answer;
+import org.sonar.api.i18n.I18n;
+import org.sonar.api.server.ws.WsTester;
+import org.sonar.core.timemachine.Periods;
+import org.sonar.server.qualitygate.QualityGates;
+
+import java.util.*;
+import java.util.Map.Entry;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doAnswer;
+
+@RunWith(MockitoJUnitRunner.class)
+public class QgateAppHandlerTest {
+
+  @Mock
+  private QualityGates qGates;
+
+  @Mock
+  private Periods periods;
+
+  @Mock
+  private I18n i18n;
+
+  WsTester tester;
+
+  @Before
+  public void setUp() {
+    tester = new WsTester(new QualityGatesWs(qGates, null, new QgateAppHandler(qGates, periods, i18n)));
+  }
+
+  @Test
+  @SuppressWarnings({"unchecked", "rawtypes"})
+  public void should_initialize_app() throws Exception {
+    doAnswer(new Answer<String>() {
+      @Override
+      public String answer(InvocationOnMock invocation) throws Throwable {
+        return (String) invocation.getArguments()[1];
+      }
+    }).when(i18n).message(any(Locale.class), any(String.class), any(String.class));
+    String json = tester.newRequest("app").execute().outputAsString();
+    Map responseJson = (Map) JSONValue.parse(json);
+    assertThat((Boolean) responseJson.get("edit")).isFalse();
+    Collection<Map> periods = (Collection<Map>) responseJson.get("periods");
+    assertThat(periods).hasSize(3);
+    Map messages = (Map) responseJson.get("messages");
+    assertThat(messages).isNotNull().isNotEmpty().hasSize(45);
+    for (Entry message: (Set<Entry>) messages.entrySet()) {
+      assertThat(message.getKey()).isEqualTo(message.getValue());
+    }
+  }
+}
index 065b408430be05e8889e76bdca46cd91d6c7322c..e426d2b95cfef8a252226be9d880d08258d30377 100644 (file)
@@ -28,7 +28,8 @@ import org.mockito.Mock;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.sonar.api.measures.Metric;
 import org.sonar.api.measures.Metric.ValueType;
-import org.sonar.api.server.ws.WebService;
+import org.sonar.api.server.ws.WebService.Action;
+import org.sonar.api.server.ws.WebService.Controller;
 import org.sonar.api.server.ws.WsTester;
 import org.sonar.core.qualitygate.db.ProjectQgateAssociation;
 import org.sonar.core.qualitygate.db.ProjectQgateAssociationQuery;
@@ -57,29 +58,32 @@ public class QualityGatesWsTest {
   @Mock
   private QgateProjectFinder projectFinder;
 
+  @Mock
+  private QgateAppHandler appHandler;
+
   WsTester tester;
 
   @Before
   public void setUp() {
-    tester = new WsTester(new QualityGatesWs(qGates, projectFinder));
+    tester = new WsTester(new QualityGatesWs(qGates, projectFinder, appHandler));
   }
 
   @Test
   public void define_ws() throws Exception {
-    WebService.Controller controller = tester.controller("api/qualitygates");
+    Controller controller = tester.controller("api/qualitygates");
     assertThat(controller).isNotNull();
     assertThat(controller.path()).isEqualTo("api/qualitygates");
     assertThat(controller.description()).isNotEmpty();
-    assertThat(controller.actions()).hasSize(15);
+    assertThat(controller.actions()).hasSize(16);
 
-    WebService.Action list = controller.action("list");
+    Action list = controller.action("list");
     assertThat(list).isNotNull();
     assertThat(list.handler()).isNotNull();
     assertThat(list.since()).isEqualTo("4.3");
     assertThat(list.isPost()).isFalse();
     assertThat(list.isInternal()).isFalse();
 
-    WebService.Action show = controller.action("show");
+    Action show = controller.action("show");
     assertThat(show).isNotNull();
     assertThat(show.handler()).isNotNull();
     assertThat(show.since()).isEqualTo("4.3");
@@ -87,7 +91,7 @@ public class QualityGatesWsTest {
     assertThat(show.param("id")).isNotNull();
     assertThat(show.isInternal()).isFalse();
 
-    WebService.Action create = controller.action("create");
+    Action create = controller.action("create");
     assertThat(create).isNotNull();
     assertThat(create.handler()).isNotNull();
     assertThat(create.since()).isEqualTo("4.3");
@@ -95,7 +99,7 @@ public class QualityGatesWsTest {
     assertThat(create.param("name")).isNotNull();
     assertThat(create.isInternal()).isFalse();
 
-    WebService.Action copy = controller.action("copy");
+    Action copy = controller.action("copy");
     assertThat(copy).isNotNull();
     assertThat(copy.handler()).isNotNull();
     assertThat(copy.since()).isEqualTo("4.3");
@@ -104,7 +108,7 @@ public class QualityGatesWsTest {
     assertThat(copy.param("name")).isNotNull();
     assertThat(copy.isInternal()).isFalse();
 
-    WebService.Action destroy = controller.action("destroy");
+    Action destroy = controller.action("destroy");
     assertThat(destroy).isNotNull();
     assertThat(destroy.handler()).isNotNull();
     assertThat(destroy.since()).isEqualTo("4.3");
@@ -112,7 +116,7 @@ public class QualityGatesWsTest {
     assertThat(destroy.param("id")).isNotNull();
     assertThat(destroy.isInternal()).isFalse();
 
-    WebService.Action rename = controller.action("rename");
+    Action rename = controller.action("rename");
     assertThat(rename).isNotNull();
     assertThat(rename.handler()).isNotNull();
     assertThat(rename.since()).isEqualTo("4.3");
@@ -121,7 +125,7 @@ public class QualityGatesWsTest {
     assertThat(rename.param("name")).isNotNull();
     assertThat(rename.isInternal()).isFalse();
 
-    WebService.Action setDefault = controller.action("set_as_default");
+    Action setDefault = controller.action("set_as_default");
     assertThat(setDefault).isNotNull();
     assertThat(setDefault.handler()).isNotNull();
     assertThat(setDefault.since()).isEqualTo("4.3");
@@ -129,14 +133,14 @@ public class QualityGatesWsTest {
     assertThat(setDefault.param("id")).isNotNull();
     assertThat(setDefault.isInternal()).isFalse();
 
-    WebService.Action unsetDefault = controller.action("unset_default");
+    Action unsetDefault = controller.action("unset_default");
     assertThat(unsetDefault).isNotNull();
     assertThat(unsetDefault.handler()).isNotNull();
     assertThat(unsetDefault.since()).isEqualTo("4.3");
     assertThat(unsetDefault.isPost()).isTrue();
     assertThat(unsetDefault.isInternal()).isFalse();
 
-    WebService.Action createCondition = controller.action("create_condition");
+    Action createCondition = controller.action("create_condition");
     assertThat(createCondition).isNotNull();
     assertThat(createCondition.handler()).isNotNull();
     assertThat(createCondition.since()).isEqualTo("4.3");
@@ -149,7 +153,7 @@ public class QualityGatesWsTest {
     assertThat(createCondition.param("period")).isNotNull();
     assertThat(createCondition.isInternal()).isFalse();
 
-    WebService.Action updateCondition = controller.action("update_condition");
+    Action updateCondition = controller.action("update_condition");
     assertThat(updateCondition).isNotNull();
     assertThat(updateCondition.handler()).isNotNull();
     assertThat(updateCondition.since()).isEqualTo("4.3");
@@ -162,7 +166,7 @@ public class QualityGatesWsTest {
     assertThat(updateCondition.param("period")).isNotNull();
     assertThat(updateCondition.isInternal()).isFalse();
 
-    WebService.Action deleteCondition = controller.action("delete_condition");
+    Action deleteCondition = controller.action("delete_condition");
     assertThat(deleteCondition).isNotNull();
     assertThat(deleteCondition.handler()).isNotNull();
     assertThat(deleteCondition.since()).isEqualTo("4.3");
@@ -170,6 +174,8 @@ public class QualityGatesWsTest {
     assertThat(deleteCondition.param("id")).isNotNull();
     assertThat(deleteCondition.isInternal()).isFalse();
 
+    Action appInit = controller.action("app");
+    assertThat(appInit.isInternal()).isTrue();
   }
 
   @Test
@@ -250,7 +256,7 @@ public class QualityGatesWsTest {
     ));
     when(qGates.currentUserHasWritePermission()).thenReturn(false);
     tester.newRequest("list").execute().assertJson(
-        "{'qualitygates':[{'id':42,'name':'Golden'},{'id':43,'name':'Star'},{'id':666,'name':'Ninth'}],'edit':false}");
+        "{'qualitygates':[{'id':42,'name':'Golden'},{'id':43,'name':'Star'},{'id':666,'name':'Ninth'}]}");
   }
 
   @Test
@@ -261,10 +267,9 @@ public class QualityGatesWsTest {
       new QualityGateDto().setId(43L).setName("Star"),
       new QualityGateDto().setId(666L).setName("Ninth")
     ));
-    when(qGates.currentUserHasWritePermission()).thenReturn(true);
     when(qGates.getDefault()).thenReturn(defaultQgate);
     tester.newRequest("list").execute().assertJson(
-        "{'qualitygates':[{'id':42,'name':'Golden'},{'id':43,'name':'Star'},{'id':666,'name':'Ninth'}],'default':42,'edit':true}");
+        "{'qualitygates':[{'id':42,'name':'Golden'},{'id':43,'name':'Star'},{'id':666,'name':'Ninth'}],'default':42}");
   }
 
   @Test