From babaeb8987ce4aeee25d0c3588c5cfc68ddb4f72 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 7 Jul 2011 15:06:02 +0200 Subject: SONAR-2581 Support server setup in Java Web Service client --- .../org/sonar/wsclient/services/ServerSetup.java | 50 ++++++++++++++++++++++ .../sonar/wsclient/services/ServerSetupQuery.java | 37 ++++++++++++++++ .../unmarshallers/ServerSetupUnmarshaller.java | 43 +++++++++++++++++++ .../wsclient/unmarshallers/Unmarshallers.java | 1 + .../wsclient/services/ServerSetupQueryTest.java | 34 +++++++++++++++ .../unmarshallers/ServerSetupUnmarshallerTest.java | 48 +++++++++++++++++++++ .../src/test/resources/server_setup/ko.json | 4 ++ .../src/test/resources/server_setup/ok.json | 3 ++ 8 files changed, 220 insertions(+) create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/services/ServerSetup.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/services/ServerSetupQuery.java create mode 100644 sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ServerSetupUnmarshaller.java create mode 100644 sonar-ws-client/src/test/java/org/sonar/wsclient/services/ServerSetupQueryTest.java create mode 100644 sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ServerSetupUnmarshallerTest.java create mode 100644 sonar-ws-client/src/test/resources/server_setup/ko.json create mode 100644 sonar-ws-client/src/test/resources/server_setup/ok.json (limited to 'sonar-ws-client') diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ServerSetup.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ServerSetup.java new file mode 100644 index 00000000000..bbab4bf4c44 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ServerSetup.java @@ -0,0 +1,50 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.wsclient.services; + +/** + * @since 2.9 + */ +public class ServerSetup extends Model { + private String status; + private String message; + + public String getStatus() { + return status; + } + + public ServerSetup setStatus(String status) { + this.status = status; + return this; + } + + public String getMessage() { + return message; + } + + public ServerSetup setMessage(String message) { + this.message = message; + return this; + } + + public boolean isSuccessful() { + return "ok".equals(status); + } +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ServerSetupQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ServerSetupQuery.java new file mode 100644 index 00000000000..83c969d7576 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ServerSetupQuery.java @@ -0,0 +1,37 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.wsclient.services; + +/** + * @since 2.9 + */ +public class ServerSetupQuery extends CreateQuery { + public static final String BASE_URL = "/api/server/setup"; + + @Override + public String getUrl() { + return BASE_URL; + } + + @Override + public Class getModelClass() { + return ServerSetup.class; + } +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ServerSetupUnmarshaller.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ServerSetupUnmarshaller.java new file mode 100644 index 00000000000..d2896d579b5 --- /dev/null +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/ServerSetupUnmarshaller.java @@ -0,0 +1,43 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.wsclient.unmarshallers; + +import org.sonar.wsclient.services.ServerSetup; +import org.sonar.wsclient.services.WSUtils; + +import java.util.List; + +/** + * @since 2.9 + */ +public class ServerSetupUnmarshaller implements Unmarshaller { + public ServerSetup toModel(String json) { + WSUtils utils = WSUtils.getINSTANCE(); + Object map = utils.parse(json); + ServerSetup server = new ServerSetup() + .setStatus(utils.getString(map, "status")) + .setMessage(utils.getString(map, "msg")); + return server; + } + + public List toModels(String json) { + throw new UnsupportedOperationException(); + } +} diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java index 578491d2c13..71be7418dcf 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/unmarshallers/Unmarshallers.java @@ -39,6 +39,7 @@ public final class Unmarshallers { unmarshallers.put(Source.class, new SourceUnmarshaller()); unmarshallers.put(Violation.class, new ViolationUnmarshaller()); unmarshallers.put(Server.class, new ServerUnmarshaller()); + unmarshallers.put(ServerSetup.class, new ServerSetupUnmarshaller()); unmarshallers.put(DependencyTree.class, new DependencyTreeUnmarshaller()); unmarshallers.put(Event.class, new EventUnmarshaller()); unmarshallers.put(Favourite.class, new FavouriteUnmarshaller()); diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ServerSetupQueryTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ServerSetupQueryTest.java new file mode 100644 index 00000000000..bd405916329 --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ServerSetupQueryTest.java @@ -0,0 +1,34 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.wsclient.services; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; + +public class ServerSetupQueryTest extends QueryTestCase { + @Test + public void index() { + ServerSetupQuery query = new ServerSetupQuery(); + assertThat(query.getUrl(), is("/api/server/setup")); + assertThat(query.getModelClass().getName(), is(ServerSetup.class.getName())); + } +} diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ServerSetupUnmarshallerTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ServerSetupUnmarshallerTest.java new file mode 100644 index 00000000000..60df75b7bdd --- /dev/null +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/unmarshallers/ServerSetupUnmarshallerTest.java @@ -0,0 +1,48 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2011 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.wsclient.unmarshallers; + +import org.junit.Test; +import org.sonar.wsclient.services.Server; +import org.sonar.wsclient.services.ServerSetup; + +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +public class ServerSetupUnmarshallerTest extends UnmarshallerTestCase { + + @Test + public void testSuccessfulSetup() { + ServerSetup setup = new ServerSetupUnmarshaller().toModel(loadFile("/server_setup/ok.json")); + assertThat(setup.getStatus(), is("ok")); + assertThat(setup.getMessage(), nullValue()); + assertThat(setup.isSuccessful(), is(true)); + } + + @Test + public void testFailedSetup() { + ServerSetup setup = new ServerSetupUnmarshaller().toModel(loadFile("/server_setup/ko.json")); + assertThat(setup.getStatus(), is("ko")); + assertThat(setup.getMessage(), is("error")); + assertThat(setup.isSuccessful(), is(false)); + } + +} diff --git a/sonar-ws-client/src/test/resources/server_setup/ko.json b/sonar-ws-client/src/test/resources/server_setup/ko.json new file mode 100644 index 00000000000..3cbef3d1a36 --- /dev/null +++ b/sonar-ws-client/src/test/resources/server_setup/ko.json @@ -0,0 +1,4 @@ +{ + "status" : "ko", + "msg": "error" +} \ No newline at end of file diff --git a/sonar-ws-client/src/test/resources/server_setup/ok.json b/sonar-ws-client/src/test/resources/server_setup/ok.json new file mode 100644 index 00000000000..6518b73dc3b --- /dev/null +++ b/sonar-ws-client/src/test/resources/server_setup/ok.json @@ -0,0 +1,3 @@ +{ + "status" : "ok" +} \ No newline at end of file -- cgit v1.2.3