]> source.dussan.org Git - sonarqube.git/commitdiff
fix coverage of CeHttpServer
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 24 Aug 2016 10:22:44 +0000 (12:22 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 25 Aug 2016 13:05:41 +0000 (15:05 +0200)
server/sonar-ce/src/main/java/org/sonar/ce/httpd/CeHttpServer.java
server/sonar-ce/src/test/java/org/sonar/ce/httpd/CeHttpServerTest.java

index 7927ac8add5613e58d8334dd2fedb66618c8d7b1..78ad1afe0dd6343a359054631238d562f514943b 100644 (file)
@@ -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) {
index fc327b5d2a43b2f8cc2bad80e49ed678d824113e..50c7aa1dfe662ee20176aead2cc31e7b9af4709f 100644 (file)
@@ -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;
+    }
+  }
 }