From 5ac06f760d8d80d5073074e7a11e780d9663f47b Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 24 Feb 2015 11:18:39 +0100 Subject: [PATCH] Add missing test --- .../platform/ws/SystemInfoWsAction.java | 4 -- .../platform/ws/SystemInfoWsActionTest.java | 51 ++++++++++--------- .../java/org/sonar/server/ws/WsTester.java | 4 ++ 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/SystemInfoWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/SystemInfoWsAction.java index 2bf05694553..136340c696c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/ws/SystemInfoWsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ws/SystemInfoWsAction.java @@ -67,8 +67,4 @@ public class SystemInfoWsAction implements SystemWsAction { } json.endObject(); } - - Monitor[] getMonitors() { - return monitors; - } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemInfoWsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemInfoWsActionTest.java index f42f3dc09c8..eeaeaa8dc38 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemInfoWsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/platform/ws/SystemInfoWsActionTest.java @@ -20,38 +20,30 @@ package org.sonar.server.platform.ws; -import org.apache.commons.lang.StringUtils; -import org.assertj.core.api.Condition; -import org.junit.ClassRule; import org.junit.Test; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; +import org.sonar.api.server.ws.internal.SimpleGetRequest; import org.sonar.core.permission.GlobalPermissions; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.platform.monitoring.Monitor; -import org.sonar.server.tester.ServerTester; import org.sonar.server.user.MockUserSession; +import org.sonar.server.ws.WsTester; + +import java.util.LinkedHashMap; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class SystemInfoWsActionTest { - @ClassRule - public static ServerTester serverTester = new ServerTester(); - - private SystemInfoWsAction sut; - - @Test - public void all_monitors_have_a_name() throws Exception { - sut = serverTester.get(SystemInfoWsAction.class); - - assertThat(sut.getMonitors()).areNot(withEmptyName()); - } + Monitor monitor1 = mock(Monitor.class); + Monitor monitor2 = mock(Monitor.class); + SystemInfoWsAction sut = new SystemInfoWsAction(monitor1, monitor2); @Test(expected = ForbiddenException.class) public void should_fail_when_does_not_have_admin_right() throws Exception { - sut = serverTester.get(SystemInfoWsAction.class); MockUserSession.set() .setLogin("login") .setName("name") @@ -60,12 +52,25 @@ public class SystemInfoWsActionTest { sut.handle(mock(Request.class), mock(Response.class)); } - private Condition withEmptyName() { - return new Condition() { - @Override - public boolean matches(Monitor m) { - return StringUtils.isEmpty(m.name()); - } - }; + @Test + public void write_json() throws Exception { + MockUserSession.set() + .setLogin("login") + .setName("name") + .setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); + + LinkedHashMap attributes1 = new LinkedHashMap<>(); + attributes1.put("foo", "bar"); + LinkedHashMap attributes2 = new LinkedHashMap<>(); + attributes2.put("one", 1); + attributes2.put("two", 2); + when(monitor1.name()).thenReturn("Monitor One"); + when(monitor1.attributes()).thenReturn(attributes1); + when(monitor2.name()).thenReturn("Monitor Two"); + when(monitor2.attributes()).thenReturn(attributes2); + + WsTester.TestResponse response = new WsTester.TestResponse(); + sut.handle(new SimpleGetRequest(), response); + assertThat(response.outputAsString()).isEqualTo("{\"Monitor One\":{\"foo\":\"bar\"},\"Monitor Two\":{\"one\":1,\"two\":2}}"); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/ws/WsTester.java b/server/sonar-server/src/test/java/org/sonar/server/ws/WsTester.java index ac0347c9efd..8325cf15c7a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ws/WsTester.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ws/WsTester.java @@ -159,6 +159,10 @@ public class WsTester { IOUtils.closeQuietly(output); return this; } + + public String outputAsString() { + return new String(output.toByteArray(), Charsets.UTF_8); + } } public static class Result { -- 2.39.5