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.*;
servicesContainer.addSingleton(QualityGates.class);
servicesContainer.addSingleton(ProjectQgateAssociationDao.class);
servicesContainer.addSingleton(QgateProjectFinder.class);
+ servicesContainer.addSingleton(QgateAppHandler.class);
servicesContainer.addSingleton(QualityGatesWs.class);
// users
--- /dev/null
+/*
+ * 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();
+ }
+}
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
defineConditionActions(controller);
+ controller.newAction("app")
+ .setInternal(true)
+ .setDescription("Get initialization items for the admin UI")
+ .setSince("4.3")
+ .setHandler(appHandler);
+
controller.done();
}
if (defaultQgate != null) {
writer.prop("default", defaultQgate.getId());
}
- writer.prop("edit", qualityGates.currentUserHasWritePermission());
writer.endObject().close();
}
--- /dev/null
+/*
+ * 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());
+ }
+ }
+}
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;
@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");
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");
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");
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");
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");
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");
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");
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");
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");
assertThat(deleteCondition.param("id")).isNotNull();
assertThat(deleteCondition.isInternal()).isFalse();
+ Action appInit = controller.action("app");
+ assertThat(appInit.isInternal()).isTrue();
}
@Test
));
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
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