import com.google.gson.Gson;
-import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
return globalSettings;
}
- public GlobalReferentials addGlobalSetting(String key, String value){
+ public GlobalReferentials addGlobalSetting(String key, String value) {
globalSettings.put(key, value);
return this;
}
return metrics;
}
- public GlobalReferentials addMetric(Metric metric){
+ public GlobalReferentials addMetric(Metric metric) {
metrics.add(metric);
return this;
}
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);
}
}
import com.google.gson.Gson;
-import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
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);
}
}
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
-import java.io.StringReader;
-
import static org.fest.assertions.Assertions.assertThat;
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();
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;
-import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.HashMap;
@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);
}
}
- 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);
*/
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";
@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));
}
}
*/
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;
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;
}
}
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));
}
}
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;
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;
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(), "");
}
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");
}
}