]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1911 New web service to get server status
authorsimonbrandhof <simon.brandhof@gmail.com>
Tue, 2 Nov 2010 18:15:15 +0000 (18:15 +0000)
committersimonbrandhof <simon.brandhof@gmail.com>
Tue, 2 Nov 2010 18:15:15 +0000 (18:15 +0000)
sonar-gwt-api/src/main/java/org/sonar/wsclient/gwt/unmarshallers/ServerUnmarshaller.java
sonar-server/src/main/webapp/WEB-INF/app/controllers/api/server_controller.rb
sonar-ws-client/src/main/java/org/sonar/wsclient/services/Server.java
sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ServerUnmarshaller.java
sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ServerUnmarshallerTest.java

index 87646fb030516e0cdf21f689e93d18abc2f90420..ad87a8133e68380f7a3fc68b2727366c501531f8 100644 (file)
@@ -31,9 +31,15 @@ public class ServerUnmarshaller implements Unmarshaller<Server> {
 
   public Server toModel(JavaScriptObject json) {
     JSONObject map = new JSONObject(json);
-    return new Server()
+    Server server = new Server()
         .setId(JsonUtils.getString(map, "id"))
         .setVersion(JsonUtils.getString(map, "version"));
+    server.setStatusMessage(JsonUtils.getString(map, "status_msg"));
+    String status = JsonUtils.getString(map, "status");
+    if (status != null) {
+      server.setStatus(Server.Status.valueOf(status));
+    }
+    return server;
   }
 
   public List<Server> toModels(JavaScriptObject json) {
index 65a4dbe41fb30e1665d7042973481d1978d05d2e..f50ec79f4fa3def5b076427e0ec798d981d375d0 100644 (file)
@@ -32,6 +32,7 @@ class Api::ServerController < Api::ApiController
 
   def index
     hash={:id => Java::OrgSonarServerPlatform::Platform.getServer().getId(), :version => Java::OrgSonarServerPlatform::Platform.getServer().getVersion()}
+    complete_with_status(hash)
     respond_to do |format| 
       format.json{ render :json => jsonp(hash) }
       format.xml { render :xml => hash.to_xml(:skip_types => true, :root => 'server') }
@@ -67,4 +68,15 @@ class Api::ServerController < Api::ApiController
     end
     hash
   end
+
+  def complete_with_status(hash)
+    if DatabaseVersion.uptodate?
+      hash[:status]='UP'
+    elsif ActiveRecord::Base.connected?
+      hash[:status]='SETUP'
+    else
+      hash[:status]='DOWN'
+      hash[:status_msg]='Not connected to database'
+    end
+  end
 end
index 4919fbb123d6ec428d450bb76a6c324a82d59a99..7967a348ce54bfaf4b5f9673e00509634e1c1760 100644 (file)
@@ -23,8 +23,15 @@ package org.sonar.wsclient.services;
  * @author Evgeny Mandrikov
  */
 public class Server extends Model {
+
+  public static enum Status {
+    SETUP, UP, DOWN;
+  }
+
   private String id;
   private String version;
+  private Status status;
+  private String statusMessage;
 
   public String getVersion() {
     return version;
@@ -34,8 +41,8 @@ public class Server extends Model {
     return id;
   }
 
-  public Server setVersion(String version) {
-    this.version = version;
+  public Server setVersion(String s) {
+    this.version = s;
     return this;
   }
 
@@ -43,4 +50,23 @@ public class Server extends Model {
     this.id = id;
     return this;
   }
+
+  public Status getStatus() {
+    return status;
+  }
+
+  public String getStatusMessage() {
+    return statusMessage;
+  }
+
+  public Server setStatus(Status status) {
+    this.status = status;
+    return this;
+  }
+
+  public Server setStatusMessage(String statusMessage) {
+    this.statusMessage = statusMessage;
+    return this;
+  }
+
 }
index d980876e3f8b9cc7de293e0b162c9fe7392efdf9..a17a36189d4751de3ad74bac81adc45533c82324 100644 (file)
@@ -31,9 +31,15 @@ import java.util.List;
 public class ServerUnmarshaller implements Unmarshaller<Server> {
   public Server toModel(String json) {
     JSONObject map = (JSONObject) JSONValue.parse(json);
-    return new Server()
+    Server server = new Server()
         .setId(JsonUtils.getString(map, "id"))
-        .setVersion(JsonUtils.getString(map, "version"));
+        .setVersion(JsonUtils.getString(map, "version"))
+        .setStatusMessage(JsonUtils.getString(map, "status_msg"));
+    String status = JsonUtils.getString(map, "status");
+    if (status != null) {
+      server.setStatus(Server.Status.valueOf(status));
+    }
+    return server;
   }
 
   public List<Server> toModels(String json) {
index 4db8b113aa93087139d278c9346d073efa7f1c5d..830996828f45cb3781294be38bd239b21825d7c0 100644 (file)
@@ -24,16 +24,27 @@ import org.sonar.wsclient.services.Server;
 
 import java.io.IOException;
 
+import static org.hamcrest.CoreMatchers.nullValue;
 import static org.hamcrest.core.Is.is;
 import static org.junit.Assert.assertThat;
 
 public class ServerUnmarshallerTest {
 
   @Test
-  public void toModel() throws IOException {
-    Server server = new ServerUnmarshaller().toModel("{\"id\":\"123456789\", \"version\":\"2.0-SNAPSHOT\"}");
+  public void testToModel() throws IOException {
+    Server server = new ServerUnmarshaller().toModel("{\"id\":\"123456789\", \"version\":\"2.0-SNAPSHOT\", \"status\":\"UP\", \"status_msg\":\"everything is under control\"}");
     assertThat(server.getId(), is("123456789"));
     assertThat(server.getVersion(), is("2.0-SNAPSHOT"));
+    assertThat(server.getStatus(), is(Server.Status.UP));
+    assertThat(server.getStatusMessage(), is("everything is under control"));
   }
 
+  @Test
+  public void shouldNotFailIfStatusIsMissing() throws IOException {
+    Server server = new ServerUnmarshaller().toModel("{\"id\":\"123456789\", \"version\":\"2.0-SNAPSHOT\"}");
+    assertThat(server.getId(), is("123456789"));
+    assertThat(server.getVersion(), is("2.0-SNAPSHOT"));
+    assertThat(server.getStatus(), nullValue());
+    assertThat(server.getStatusMessage(), nullValue());
+  }
 }