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) {
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') }
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
* @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;
return id;
}
- public Server setVersion(String version) {
- this.version = version;
+ public Server setVersion(String s) {
+ this.version = s;
return this;
}
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;
+ }
+
}
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) {
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());
+ }
}