From: Julien HENRY Date: Tue, 26 Aug 2014 12:20:53 +0000 (+0200) Subject: SONAR-5417 Handle permision error for project referential loader X-Git-Tag: 4.5-RC1~87 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5bba55cd38c983e09767246e777d8d317942ab5d;p=sonarqube.git SONAR-5417 Handle permision error for project referential loader --- diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/GlobalReferentials.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/GlobalReferentials.java index 4a15c7042d2..4fa32d5d68b 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/GlobalReferentials.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/GlobalReferentials.java @@ -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); } } diff --git a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java index 06109d82b11..6a8d5c8691e 100644 --- a/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java +++ b/sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ProjectReferentials.java @@ -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); } } diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/GlobalReferentialsTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/GlobalReferentialsTest.java index 6ea2a3bf218..36c6dc2677f 100644 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/GlobalReferentialsTest.java +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/GlobalReferentialsTest.java @@ -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(); diff --git a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java index 328939ca23d..fbcb0c9f216 100644 --- a/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java +++ b/sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java @@ -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); diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java index 1d0cefb14b4..d43ef8e0e71 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java @@ -92,7 +92,7 @@ public class ServerClient implements BatchComponent { } } - public InputSupplier doRequest(String pathStartingWithSlash, @Nullable Integer timeoutMillis) { + private InputSupplier doRequest(String pathStartingWithSlash, @Nullable Integer timeoutMillis) { Preconditions.checkArgument(pathStartingWithSlash.startsWith("/"), "Path must start with slash /"); String path = StringEscapeUtils.escapeHtml(pathStartingWithSlash); diff --git a/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultGlobalReferentialsLoader.java b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultGlobalReferentialsLoader.java index 239835275c4..17fd6821a6c 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultGlobalReferentialsLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultGlobalReferentialsLoader.java @@ -19,15 +19,9 @@ */ 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 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)); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java index 4c8f33bdfeb..bcde2f88a05 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java @@ -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 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)); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/referential/DefaultProjectReferentialsLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/referential/DefaultProjectReferentialsLoaderTest.java index 2dd3887c6de..d93f9aecbe1 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/referential/DefaultProjectReferentialsLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/referential/DefaultProjectReferentialsLoaderTest.java @@ -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() { - - @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.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"); } }