]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8438 New web service api/system/ping
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 5 Feb 2017 16:52:55 +0000 (17:52 +0100)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 6 Feb 2017 12:35:57 +0000 (13:35 +0100)
it/it-tests/src/test/java/it/Category4Suite.java
it/it-tests/src/test/java/it/serverSystem/PingTest.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/main/java/org/sonar/server/platform/ws/PingAction.java [new file with mode: 0644]
server/sonar-server/src/main/resources/org/sonar/server/platform/ws/ping-example.txt [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/platform/ws/PingActionTest.java [new file with mode: 0644]

index db87ef57d3cf327f68f357154cc95b14a6f3857c..897cf0b023effba5ae1bfb736a050ee173e3a893 100644 (file)
@@ -35,6 +35,7 @@ import it.projectSearch.SearchProjectsTest;
 import it.qualityProfile.QualityProfilesPageTest;
 import it.serverSystem.HttpHeadersTest;
 import it.serverSystem.LogsTest;
+import it.serverSystem.PingTest;
 import it.serverSystem.ServerSystemTest;
 import it.ui.SourceViewerTest;
 import it.ui.UiTest;
@@ -58,6 +59,7 @@ import static util.ItUtils.xooPlugin;
 @Suite.SuiteClasses({
   // server system
   ServerSystemTest.class,
+  PingTest.class,
   // user
   MyAccountPageTest.class,
   FavoritesWsTest.class,
diff --git a/it/it-tests/src/test/java/it/serverSystem/PingTest.java b/it/it-tests/src/test/java/it/serverSystem/PingTest.java
new file mode 100644 (file)
index 0000000..41cf78b
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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 it.serverSystem;
+
+import com.sonar.orchestrator.Orchestrator;
+import it.Category4Suite;
+import okhttp3.Response;
+import org.junit.ClassRule;
+import org.junit.Test;
+import util.ItUtils;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class PingTest {
+
+  @ClassRule
+  public static final Orchestrator orchestrator = Category4Suite.ORCHESTRATOR;
+
+  @Test
+  public void ping_answers_pong() throws Exception {
+    Response response = ItUtils.call(orchestrator.getServer().getUrl() + "/api/system/ping");
+
+    assertThat(response.body().string()).isEqualTo("pong");
+    assertThat(response.header("Content-Type")).isEqualTo("text/plain");
+  }
+}
index be66249b2ec34484b5b1c7ce854e6a73fb7145a4..e21f40ff16179575de6553d51533815cd1f41df0 100644 (file)
@@ -110,6 +110,7 @@ import org.sonar.server.platform.ws.InfoAction;
 import org.sonar.server.platform.ws.L10nWs;
 import org.sonar.server.platform.ws.LogsAction;
 import org.sonar.server.platform.ws.MigrateDbAction;
+import org.sonar.server.platform.ws.PingAction;
 import org.sonar.server.platform.ws.RestartAction;
 import org.sonar.server.platform.ws.ServerWs;
 import org.sonar.server.platform.ws.StatusAction;
@@ -459,6 +460,7 @@ public class PlatformLevel4 extends PlatformLevel {
       ServerLogging.class,
       RestartAction.class,
       InfoAction.class,
+      PingAction.class,
       UpgradesAction.class,
       StatusAction.class,
       SystemWs.class,
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/PingAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/PingAction.java
new file mode 100644 (file)
index 0000000..496b210
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.platform.ws;
+
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+
+import static org.apache.commons.io.IOUtils.write;
+
+public class PingAction implements SystemWsAction {
+  @Override
+  public void define(WebService.NewController controller) {
+    controller.createAction("ping")
+      .setDescription("Answers \"pong\" as plain-text")
+      .setSince("6.3")
+      .setResponseExample(getClass().getResource("ping-example.txt"))
+      .setHandler(this);
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    response.stream().setMediaType("text/plain");
+    write("pong", response.stream().output());
+  }
+}
diff --git a/server/sonar-server/src/main/resources/org/sonar/server/platform/ws/ping-example.txt b/server/sonar-server/src/main/resources/org/sonar/server/platform/ws/ping-example.txt
new file mode 100644 (file)
index 0000000..8e55469
--- /dev/null
@@ -0,0 +1 @@
+pong
diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/PingActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/PingActionTest.java
new file mode 100644 (file)
index 0000000..4da578d
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.platform.ws;
+
+import org.junit.Test;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.server.ws.TestResponse;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class PingActionTest {
+
+  private PingAction underTest = new PingAction();
+  private WsActionTester tester = new WsActionTester(underTest);
+
+  @Test
+  public void test_definition() {
+    WebService.Action action = tester.getDef();
+
+    assertThat(action.key()).isEqualTo("ping");
+    assertThat(action.isPost()).isFalse();
+    assertThat(action.isInternal()).isFalse();
+    assertThat(action.params()).isEmpty();
+  }
+
+  @Test
+  public void returns_pong_as_plain_text() {
+    TestResponse response = tester.newRequest().execute();
+
+    assertThat(response.getMediaType()).isEqualTo("text/plain");
+    assertThat(response.getInput()).isEqualTo("pong");
+    assertThat(response.getInput()).isEqualTo(tester.getDef().responseExampleAsString());
+  }
+}