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

index c663ee68f85533d23a9c6cee1914683644d4e24f..fc2ddb60b43f8b02dbf62acab12536e61aeedc85 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.ce.http;
 
 import java.io.File;
+import java.io.IOException;
 import java.net.URI;
 import java.util.Optional;
 import okhttp3.OkHttpClient;
@@ -27,11 +28,10 @@ import okhttp3.RequestBody;
 import org.apache.commons.io.IOUtils;
 import org.sonar.api.config.Settings;
 import org.sonar.api.utils.log.LoggerLevel;
-import org.sonar.api.utils.log.Loggers;
 import org.sonar.process.DefaultProcessCommands;
 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
-import org.sonar.server.platform.ws.ChangeLogLevelAction;
 
+import static java.util.Objects.requireNonNull;
 import static org.sonar.process.ProcessEntryPoint.PROPERTY_SHARED_PATH;
 import static org.sonar.process.ProcessId.COMPUTE_ENGINE;
 
@@ -79,6 +79,7 @@ public class CeHttpClient {
   }
 
   public void changeLogLevel(LoggerLevel level) {
+    requireNonNull(level, "level can't be null");
     call(new ChangeLogLevelActionClient(level));
   }
 
@@ -107,10 +108,12 @@ public class CeHttpClient {
         .build();
       okhttp3.Response response = new OkHttpClient().newCall(request).execute();
       if (response.code() != 200) {
-        Loggers.get(ChangeLogLevelAction.class).error(
-          "Failed to change log level in Compute Engine. Code was '{}' and response was '{}'",
-          response.code(),
-          response.body().string());
+        throw new IOException(
+          String.format(
+            "Failed to change log level in Compute Engine. Code was '%s' and response was '%s' for url '%s'",
+            response.code(),
+            response.body().string(),
+            url));
       }
       return null;
     }
@@ -123,7 +126,7 @@ public class CeHttpClient {
       }
       return actionClient.getDefault();
     } catch (Exception e) {
-      throw new IllegalStateException("Can not get system info of process " + COMPUTE_ENGINE, e);
+      throw new IllegalStateException("Failed to call HTTP server of process " + COMPUTE_ENGINE, e);
     }
   }
 
index 8a2759128ceea45474b8b8389278289a3c6fbfec..64cdd6c5f5b202525030bd9d41f303e76664064c 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.ce.http;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.Optional;
 import okhttp3.mockwebserver.MockResponse;
 import okhttp3.mockwebserver.MockWebServer;
@@ -30,6 +31,7 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.rules.TemporaryFolder;
 import org.sonar.api.config.Settings;
+import org.sonar.api.utils.log.LoggerLevel;
 import org.sonar.process.DefaultProcessCommands;
 import org.sonar.process.ProcessEntryPoint;
 import org.sonar.process.ProcessId;
@@ -37,6 +39,7 @@ import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
 
 import static java.lang.String.format;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.test.ExceptionCauseMatcher.hasType;
 
 public class CeHttpClientTest {
   @Rule
@@ -71,10 +74,7 @@ public class CeHttpClientTest {
     server.enqueue(new MockResponse().setBody(response));
 
     // initialize registration of process
-    try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, ProcessId.COMPUTE_ENGINE.getIpcIndex())) {
-      processCommands.setUp();
-      processCommands.setHttpUrl(format("http://%s:%d", server.getHostName(), server.getPort()));
-    }
+    setUpWithHttpUrl(ProcessId.COMPUTE_ENGINE);
 
     Optional<ProtobufSystemInfo.SystemInfo> info = underTest.retrieveSystemInfo();
     assertThat(info.get().getSectionsCount()).isEqualTo(0);
@@ -85,13 +85,58 @@ public class CeHttpClientTest {
     server.enqueue(new MockResponse().setResponseCode(500));
 
     // initialize registration of process
-    try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, ProcessId.COMPUTE_ENGINE.getIpcIndex())) {
-      processCommands.setUp();
-      processCommands.setHttpUrl(format("http://%s:%d", server.getHostName(), server.getPort()));
-    }
+    setUpWithHttpUrl(ProcessId.COMPUTE_ENGINE);
 
     expectedException.expect(IllegalStateException.class);
-    expectedException.expectMessage("Can not get system info of process " + ProcessId.COMPUTE_ENGINE);
+    expectedException.expectMessage("Failed to call HTTP server of process " + ProcessId.COMPUTE_ENGINE);
+    expectedException.expectCause(hasType(IOException.class)
+      .andMessage(format("Server returned HTTP response code: 500 for URL: http://%s:%d/systemInfo", server.getHostName(), server.getPort())));
     underTest.retrieveSystemInfo();
   }
+
+  @Test
+  public void changeLogLevel_throws_NPE_if_level_argument_is_null() {
+    expectedException.expect(NullPointerException.class);
+    expectedException.expectMessage("level can't be null");
+
+    underTest.changeLogLevel(null);
+  }
+
+  @Test
+  public void changeLogLevel_throws_ISE_if_http_error() {
+    String message = "blah";
+    server.enqueue(new MockResponse().setResponseCode(500).setBody(message));
+
+    // initialize registration of process
+    setUpWithHttpUrl(ProcessId.COMPUTE_ENGINE);
+
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Failed to call HTTP server of process " + ProcessId.COMPUTE_ENGINE);
+    expectedException.expectCause(hasType(IOException.class)
+      .andMessage(format("Failed to change log level in Compute Engine. Code was '500' and response was 'blah' for url " +
+        "'http://%s:%s/changeLogLevel'", server.getHostName(), server.getPort())));
+
+    underTest.changeLogLevel(LoggerLevel.DEBUG);
+  }
+
+  @Test
+  public void changeLogLevel_does_not_fail_when_http_code_is_200() {
+    server.enqueue(new MockResponse().setResponseCode(200));
+
+    setUpWithHttpUrl(ProcessId.COMPUTE_ENGINE);
+
+    underTest.changeLogLevel(LoggerLevel.TRACE);
+  }
+
+  @Test
+  public void changelogLevel_does_not_fail_if_process_is_down() {
+    underTest.changeLogLevel(LoggerLevel.INFO);
+  }
+
+  private void setUpWithHttpUrl(ProcessId processId) {
+    try (DefaultProcessCommands processCommands = DefaultProcessCommands.secondary(ipcSharedDir, processId.getIpcIndex())) {
+      processCommands.setUp();
+      processCommands.setHttpUrl(format("http://%s:%d", server.getHostName(), server.getPort()));
+    }
+  }
 }