]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5417 Handle permision error for project referential loader
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 26 Aug 2014 12:20:53 +0000 (14:20 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Tue, 26 Aug 2014 12:21:44 +0000 (14:21 +0200)
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/GlobalReferentials.java
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/GlobalReferentialsTest.java
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java
sonar-batch/src/main/java/org/sonar/batch/referential/DefaultGlobalReferentialsLoader.java
sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java
sonar-batch/src/test/java/org/sonar/batch/referential/DefaultProjectReferentialsLoaderTest.java

index 4a15c7042d23560335774ee0d50d21bb174d098a..4fa32d5d68b0aea35994a5f3f1864411d03f0a32 100644 (file)
@@ -21,7 +21,6 @@ package org.sonar.batch.protocol.input;
 
 import com.google.gson.Gson;
 
-import java.io.Reader;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -41,7 +40,7 @@ public class GlobalReferentials {
     return globalSettings;
   }
 
-  public GlobalReferentials addGlobalSetting(String key, String value){
+  public GlobalReferentials addGlobalSetting(String key, String value) {
     globalSettings.put(key, value);
     return this;
   }
@@ -50,7 +49,7 @@ public class GlobalReferentials {
     return metrics;
   }
 
-  public GlobalReferentials addMetric(Metric metric){
+  public GlobalReferentials addMetric(Metric metric) {
     metrics.add(metric);
     return this;
   }
@@ -67,8 +66,8 @@ public class GlobalReferentials {
     return new Gson().toJson(this);
   }
 
-  public static GlobalReferentials fromJson(Reader input) {
-    return new Gson().fromJson(input, GlobalReferentials.class);
+  public static GlobalReferentials fromJson(String json) {
+    return new Gson().fromJson(json, GlobalReferentials.class);
   }
 
 }
index 06109d82b112b2a19d243ef0ae8aa5cfbe860a5e..6a8d5c8691e6c5457bb2c0ec7c1823b00d258381 100644 (file)
@@ -21,7 +21,6 @@ package org.sonar.batch.protocol.input;
 
 import com.google.gson.Gson;
 
-import java.io.Reader;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -83,8 +82,8 @@ public class ProjectReferentials {
     return new Gson().toJson(this);
   }
 
-  public static ProjectReferentials fromJson(Reader input) {
-    return new Gson().fromJson(input, ProjectReferentials.class);
+  public static ProjectReferentials fromJson(String json) {
+    return new Gson().fromJson(json, ProjectReferentials.class);
   }
 
 }
index 6ea2a3bf2187f73c00a89f6f0abb1923d5f7afd0..36c6dc2677f174dec01bd0580f0bb293522c0ed0 100644 (file)
@@ -24,8 +24,6 @@ import org.json.JSONException;
 import org.junit.Test;
 import org.skyscreamer.jsonassert.JSONAssert;
 
-import java.io.StringReader;
-
 import static org.fest.assertions.Assertions.assertThat;
 
 public class GlobalReferentialsTest {
@@ -48,10 +46,10 @@ public class GlobalReferentialsTest {
   @Test
   public void from_json() throws JSONException {
     GlobalReferentials ref = GlobalReferentials
-      .fromJson(new StringReader(
-        "{timestamp:1,"
-          + "metrics:[{id:1,key:ncloc,valueType:DATA,description:Description,direction:-1,name:NCLOC,qualitative:true,userManaged:false,worstValue:2.0,bestValue:1.0,optimizedBestValue:true}],"
-          + "globalSettings:{prop:value}}"));
+      .fromJson(
+      "{timestamp:1,"
+        + "metrics:[{id:1,key:ncloc,valueType:DATA,description:Description,direction:-1,name:NCLOC,qualitative:true,userManaged:false,worstValue:2.0,bestValue:1.0,optimizedBestValue:true}],"
+        + "globalSettings:{prop:value}}");
 
     assertThat(ref.timestamp()).isEqualTo(1);
     Metric metric = ref.metrics().iterator().next();
index 328939ca23d0d08330083d5e6882cd65f3f46671..fbcb0c9f2160e1319c9a69eb615a10867d2a4457 100644 (file)
@@ -24,7 +24,6 @@ import org.json.JSONException;
 import org.junit.Test;
 import org.skyscreamer.jsonassert.JSONAssert;
 
-import java.io.StringReader;
 import java.text.SimpleDateFormat;
 import java.util.HashMap;
 
@@ -53,10 +52,10 @@ public class ProjectReferentialsTest {
 
   @Test
   public void testFromJson() throws JSONException {
-    ProjectReferentials ref = ProjectReferentials.fromJson(new StringReader("{timestamp:1,"
+    ProjectReferentials ref = ProjectReferentials.fromJson("{timestamp:1,"
       + "qprofilesByLanguage:{java:{key:\"squid-java\",name:Java,language:java,rulesUpdatedAt:\"Mar 14, 1984 12:00:00 AM\"}},"
       + "activeRules:[{repositoryKey:repo,ruleKey:rule,severity:MAJOR,internalKey:rule,language:java,params:{}}],"
-      + "settingsByModule:{foo:{prop:value}}}"));
+      + "settingsByModule:{foo:{prop:value}}}");
 
     assertThat(ref.timestamp()).isEqualTo(1);
 
index 1d0cefb14b43f13289e084da354045b1904baad9..d43ef8e0e711ded34dc5f5416efb6eb1ee6c8765 100644 (file)
@@ -92,7 +92,7 @@ public class ServerClient implements BatchComponent {
     }
   }
 
-  public InputSupplier<InputStream> doRequest(String pathStartingWithSlash, @Nullable Integer timeoutMillis) {
+  private InputSupplier<InputStream> doRequest(String pathStartingWithSlash, @Nullable Integer timeoutMillis) {
     Preconditions.checkArgument(pathStartingWithSlash.startsWith("/"), "Path must start with slash /");
     String path = StringEscapeUtils.escapeHtml(pathStartingWithSlash);
 
index 239835275c48fdd1390946eddd76b83483b750df..17fd6821a6c8fec1ba94100c161c2793410b5e3c 100644 (file)
  */
 package org.sonar.batch.referential;
 
-import com.google.common.base.Charsets;
-import com.google.common.io.InputSupplier;
 import org.sonar.batch.bootstrap.ServerClient;
 import org.sonar.batch.protocol.input.GlobalReferentials;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-
 public class DefaultGlobalReferentialsLoader implements GlobalReferentialsLoader {
 
   private static final String BATCH_GLOBAL_URL = "/batch/global";
@@ -40,12 +34,7 @@ public class DefaultGlobalReferentialsLoader implements GlobalReferentialsLoader
 
   @Override
   public GlobalReferentials load() {
-    InputSupplier<InputStream> jsonStream = serverClient.doRequest(BATCH_GLOBAL_URL, null);
-    try {
-      return GlobalReferentials.fromJson(new InputStreamReader(jsonStream.getInput(), Charsets.UTF_8));
-    } catch (IOException e) {
-      throw new IllegalStateException("Unable to load global referentials", e);
-    }
+    return GlobalReferentials.fromJson(serverClient.request(BATCH_GLOBAL_URL));
   }
 
 }
index 4c8f33bdfebef54daf76fc84eae684ab6f1ad164..bcde2f88a05cc928e35a2480969e4d3097709353 100644 (file)
@@ -19,8 +19,6 @@
  */
 package org.sonar.batch.referential;
 
-import com.google.common.base.Charsets;
-import com.google.common.io.InputSupplier;
 import org.sonar.api.batch.bootstrap.ProjectReactor;
 import org.sonar.batch.bootstrap.AnalysisMode;
 import org.sonar.batch.bootstrap.ServerClient;
@@ -28,9 +26,6 @@ import org.sonar.batch.bootstrap.TaskProperties;
 import org.sonar.batch.protocol.input.ProjectReferentials;
 import org.sonar.batch.rule.ModuleQProfiles;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 
@@ -57,11 +52,6 @@ public class DefaultProjectReferentialsLoader implements ProjectReferentialsLoad
       }
     }
     url += "&preview=" + analysisMode.isPreview();
-    InputSupplier<InputStream> jsonStream = serverClient.doRequest(url, null);
-    try {
-      return ProjectReferentials.fromJson(new InputStreamReader(jsonStream.getInput(), Charsets.UTF_8));
-    } catch (IOException e) {
-      throw new IllegalStateException("Unable to load project referentials", e);
-    }
+    return ProjectReferentials.fromJson(serverClient.request(url));
   }
 }
index 2dd3887c6defcd483c09b6da5328daca151a930d..d93f9aecbe171a5efb94632acb17b73b8b5aeefd 100644 (file)
@@ -20,7 +20,6 @@
 package org.sonar.batch.referential;
 
 import com.google.common.collect.Maps;
-import com.google.common.io.InputSupplier;
 import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.batch.bootstrap.ProjectDefinition;
@@ -30,11 +29,6 @@ import org.sonar.batch.bootstrap.ServerClient;
 import org.sonar.batch.bootstrap.TaskProperties;
 import org.sonar.batch.rule.ModuleQProfiles;
 
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-
-import static org.mockito.Matchers.anyInt;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
@@ -53,13 +47,7 @@ public class DefaultProjectReferentialsLoaderTest {
     serverClient = mock(ServerClient.class);
     analysisMode = mock(AnalysisMode.class);
     loader = new DefaultProjectReferentialsLoader(serverClient, analysisMode);
-    when(serverClient.doRequest(anyString(), anyInt())).thenReturn(new InputSupplier<InputStream>() {
-
-      @Override
-      public InputStream getInput() throws IOException {
-        return new ByteArrayInputStream(new byte[0]);
-      }
-    });
+    when(serverClient.request(anyString())).thenReturn("");
     reactor = new ProjectReactor(ProjectDefinition.create().setKey("foo"));
     taskProperties = new TaskProperties(Maps.<String, String>newHashMap(), "");
   }
@@ -68,18 +56,18 @@ public class DefaultProjectReferentialsLoaderTest {
   public void passPreviewParameter() {
     when(analysisMode.isPreview()).thenReturn(false);
     loader.load(reactor, taskProperties);
-    verify(serverClient).doRequest("/batch/project?key=foo&preview=false", null);
+    verify(serverClient).request("/batch/project?key=foo&preview=false");
 
     when(analysisMode.isPreview()).thenReturn(true);
     loader.load(reactor, taskProperties);
-    verify(serverClient).doRequest("/batch/project?key=foo&preview=true", null);
+    verify(serverClient).request("/batch/project?key=foo&preview=true");
   }
 
   @Test
   public void passProfileParameter() {
     taskProperties.properties().put(ModuleQProfiles.SONAR_PROFILE_PROP, "my-profile");
     loader.load(reactor, taskProperties);
-    verify(serverClient).doRequest("/batch/project?key=foo&profile=my-profile&preview=false", null);
+    verify(serverClient).request("/batch/project?key=foo&profile=my-profile&preview=false");
   }
 
 }