From 6c8d497329ec034ad3a6f71cf216261cd26498e6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Wed, 24 Aug 2016 12:22:44 +0200 Subject: [PATCH] fix coverage of CeHttpServer --- .../java/org/sonar/ce/httpd/CeHttpServer.java | 9 ++-- .../org/sonar/ce/httpd/CeHttpServerTest.java | 44 +++++++++++++++---- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/httpd/CeHttpServer.java b/server/sonar-ce/src/main/java/org/sonar/ce/httpd/CeHttpServer.java index 7927ac8add5..78ad1afe0dd 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/httpd/CeHttpServer.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/httpd/CeHttpServer.java @@ -38,6 +38,7 @@ import static com.google.common.base.Preconditions.checkState; import static fi.iki.elonen.NanoHTTPD.Response.Status.INTERNAL_ERROR; import static fi.iki.elonen.NanoHTTPD.Response.Status.NOT_FOUND; import static java.lang.Integer.parseInt; +import static java.lang.String.format; import static java.util.Objects.requireNonNull; import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_INDEX; import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH; @@ -96,9 +97,7 @@ public class CeHttpServer { return "http://" + nanoHttpd.getHostname() + ":" + nanoHttpd.getListeningPort(); } - private class CeNanoHttpd extends NanoHTTPD { - private final NanoHTTPD.Response response404 = newFixedLengthResponse(NOT_FOUND, MIME_PLAINTEXT, "Error 404, not found."); - + private static class CeNanoHttpd extends NanoHTTPD { private final ActionRegistryImpl actionRegistry; CeNanoHttpd(String hostname, int port, ActionRegistryImpl actionRegistry) { @@ -110,10 +109,10 @@ public class CeHttpServer { public Response serve(IHTTPSession session) { return actionRegistry.getAction(session) .map(action -> serveFromAction(session, action)) - .orElse(response404); + .orElseGet(() -> newFixedLengthResponse(NOT_FOUND, MIME_PLAINTEXT, format("Error 404, '%s' not found.", session.getUri()))); } - private Response serveFromAction(IHTTPSession session, HttpAction action) { + private static Response serveFromAction(IHTTPSession session, HttpAction action) { try { return action.serve(session); } catch (Exception e) { diff --git a/server/sonar-ce/src/test/java/org/sonar/ce/httpd/CeHttpServerTest.java b/server/sonar-ce/src/test/java/org/sonar/ce/httpd/CeHttpServerTest.java index fc327b5d2a4..50c7aa1dfe6 100644 --- a/server/sonar-ce/src/test/java/org/sonar/ce/httpd/CeHttpServerTest.java +++ b/server/sonar-ce/src/test/java/org/sonar/ce/httpd/CeHttpServerTest.java @@ -23,6 +23,7 @@ import fi.iki.elonen.NanoHTTPD; import java.io.File; import java.io.IOException; import java.net.ConnectException; +import java.util.Arrays; import java.util.Properties; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -36,7 +37,6 @@ import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonar.process.DefaultProcessCommands; -import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.process.ProcessEntryPoint.PROPERTY_PROCESS_INDEX; import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH; @@ -48,6 +48,7 @@ public class CeHttpServerTest { @Rule public ExpectedException expectedException = ExpectedException.none(); + private static final RuntimeException FAILING_ACTION = new IllegalStateException("Simulating the action failed"); private CeHttpServer underTest; private File sharedDir; @@ -57,7 +58,7 @@ public class CeHttpServerTest { Properties properties = new Properties(); properties.setProperty(PROPERTY_PROCESS_INDEX, "1"); properties.setProperty(PROPERTY_SHARED_PATH, sharedDir.getAbsolutePath()); - underTest = new CeHttpServer(properties, singletonList(new PomPomAction())); + underTest = new CeHttpServer(properties, Arrays.asList(new PomPomAction(), new FailingAction())); underTest.start(); } @@ -74,8 +75,12 @@ public class CeHttpServerTest { } @Test - public void unkownUrl_return_a_404() throws IOException { - assertThat(call(underTest.getUrl() + "/dfdsfdsfsdsd").code()).isEqualTo(404); + public void return_http_response_with_code_404_and_exception_message_as_body_when_url_has_no_matching_action() throws IOException { + String action = "/dfdsfdsfsdsd"; + Response response = call(underTest.getUrl() + action); + + assertThat(response.code()).isEqualTo(404); + assertThat(response.body().string()).isEqualTo("Error 404, '" + action + "' not found."); } @Test @@ -102,11 +107,6 @@ public class CeHttpServerTest { assertIsPomPomResponse(response); } - private void assertIsPomPomResponse(Response response) throws IOException { - assertThat(response.code()).isEqualTo(200); - assertThat(IOUtils.toString(response.body().byteStream())).isEqualTo("ok"); - } - @Test public void stop_stops_http_server() throws Exception { underTest.stop(); @@ -114,6 +114,19 @@ public class CeHttpServerTest { call(underTest.getUrl()); } + @Test + public void return_http_response_with_code_500_and_exception_message_as_body_when_action_throws_exception() throws IOException { + Response response = call(underTest.getUrl() + "/failing"); + + assertThat(response.code()).isEqualTo(500); + assertThat(response.body().string()).isEqualTo(FAILING_ACTION.getMessage()); + } + + private void assertIsPomPomResponse(Response response) throws IOException { + assertThat(response.code()).isEqualTo(200); + assertThat(IOUtils.toString(response.body().byteStream())).isEqualTo("ok"); + } + private static Response call(String url) throws IOException { Request request = new Request.Builder().get().url(url).build(); return new OkHttpClient().newCall(request).execute(); @@ -130,4 +143,17 @@ public class CeHttpServerTest { return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_PLAINTEXT, "ok"); } } + + private static class FailingAction implements HttpAction { + + @Override + public void register(ActionRegistry registry) { + registry.register("failing", this); + } + + @Override + public NanoHTTPD.Response serve(NanoHTTPD.IHTTPSession session) { + throw FAILING_ACTION; + } + } } -- 2.39.5