]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5477 Plug global referential loader on remote WS
authorJulien HENRY <julien.henry@sonarsource.com>
Wed, 23 Jul 2014 13:11:58 +0000 (15:11 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Wed, 23 Jul 2014 13:15:55 +0000 (15:15 +0200)
sonar-batch/src/main/java/org/sonar/batch/bootstrap/GlobalSettings.java
sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java
sonar-batch/src/main/java/org/sonar/batch/mediumtest/BatchMediumTester.java
sonar-batch/src/main/java/org/sonar/batch/referential/DefaultGlobalReferentialsLoader.java
sonar-batch/src/main/java/org/sonar/batch/referential/GlobalReferentialsProvider.java
sonar-batch/src/main/java/org/sonar/batch/settings/DefaultSettingsReferential.java
sonar-batch/src/main/java/org/sonar/batch/settings/SettingsReferential.java
sonar-batch/src/test/java/org/sonar/batch/bootstrap/GlobalSettingsTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/ProjectScanContainerTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/ProjectSettingsTest.java
sonar-batch/src/test/java/org/sonar/batch/settings/DefaultSettingsReferentialTest.java

index baeb2706ad1fca5512cb65f79b44e028be881495..96922f5b93f24cd263ccac25a426306940b25c72 100644 (file)
 package org.sonar.batch.bootstrap;
 
 import org.apache.commons.configuration.Configuration;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.sonar.api.CoreProperties;
 import org.sonar.api.config.PropertyDefinitions;
 import org.sonar.api.config.Settings;
 import org.sonar.api.utils.MessageException;
-import org.sonar.batch.settings.SettingsReferential;
+import org.sonar.batch.protocol.input.GlobalReferentials;
 
 import javax.annotation.Nullable;
 
 public class GlobalSettings extends Settings {
 
-  private static final Logger LOG = LoggerFactory.getLogger(GlobalSettings.class);
-
   private Configuration deprecatedConfiguration;
 
   private final BootstrapProperties bootstrapProps;
-  private final SettingsReferential settingsReferential;
+  private final GlobalReferentials globalReferentials;
   private final AnalysisMode mode;
 
   public GlobalSettings(BootstrapProperties bootstrapProps, PropertyDefinitions propertyDefinitions,
-    SettingsReferential settingsReferential, Configuration deprecatedConfiguration, AnalysisMode mode) {
+    GlobalReferentials globalReferentials, Configuration deprecatedConfiguration, AnalysisMode mode) {
 
     super(propertyDefinitions);
     this.mode = mode;
     getEncryption().setPathToSecretKey(bootstrapProps.property(CoreProperties.ENCRYPTION_SECRET_KEY_PATH));
     this.bootstrapProps = bootstrapProps;
-    this.settingsReferential = settingsReferential;
+    this.globalReferentials = globalReferentials;
     this.deprecatedConfiguration = deprecatedConfiguration;
     init();
   }
 
   private void init() {
-    LOG.info("Load global settings");
-    addProperties(settingsReferential.globalSettings());
+    addProperties(globalReferentials.globalSettings());
     addProperties(bootstrapProps.properties());
   }
 
index d43ef8e0e711ded34dc5f5416efb6eb1ee6c8765..1d0cefb14b43f13289e084da354045b1904baad9 100644 (file)
@@ -92,7 +92,7 @@ public class ServerClient implements BatchComponent {
     }
   }
 
-  private InputSupplier<InputStream> doRequest(String pathStartingWithSlash, @Nullable Integer timeoutMillis) {
+  public InputSupplier<InputStream> doRequest(String pathStartingWithSlash, @Nullable Integer timeoutMillis) {
     Preconditions.checkArgument(pathStartingWithSlash.startsWith("/"), "Path must start with slash /");
     String path = StringEscapeUtils.escapeHtml(pathStartingWithSlash);
 
index cde2d635f2ec95ca1176a2b2661ca294396ef15e..c3aa2646c6ce82846bfbdcd888dff6dea1d30578 100644 (file)
@@ -108,7 +108,7 @@ public class BatchMediumTester {
 
     public BatchMediumTesterBuilder addDefaultQProfile(String language, String name) {
       addQProfile(language, name);
-      settingsReferential.globalSettings().put("sonar.profile." + language, name);
+      globalRefProvider.globalSettings().put("sonar.profile." + language, name);
       return this;
     }
 
@@ -253,6 +253,10 @@ public class BatchMediumTester {
       return ref;
     }
 
+    public Map<String, String> globalSettings() {
+      return ref.globalSettings();
+    }
+
     public FakeGlobalReferentialsLoader add(Metric metric) {
       ref.metrics().add(new org.sonar.batch.protocol.input.Metric(metricId,
         metric.key(),
@@ -292,14 +296,8 @@ public class BatchMediumTester {
 
   private static class FakeSettingsReferential implements SettingsReferential {
 
-    private Map<String, String> globalSettings = new HashMap<String, String>();
     private Map<String, Map<String, String>> projectSettings = new HashMap<String, Map<String, String>>();
 
-    @Override
-    public Map<String, String> globalSettings() {
-      return globalSettings;
-    }
-
     @Override
     public Map<String, String> projectSettings(String projectKey) {
       return projectSettings.containsKey(projectKey) ? projectSettings.get(projectKey) : Collections.<String, String>emptyMap();
index ec7e93382eb16dbe1f27beb6f5415ddd704ad156..239835275c48fdd1390946eddd76b83483b750df 100644 (file)
  */
 package org.sonar.batch.referential;
 
-import org.sonar.api.measures.Metric;
+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 org.sonar.jpa.session.DatabaseSessionFactory;
 
-import java.util.Collection;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 
-/**
- * TODO This is currently implemented by accessing DB but should be replaced by WS call
- */
 public class DefaultGlobalReferentialsLoader implements GlobalReferentialsLoader {
 
-  private static final String ENABLED = "enabled";
+  private static final String BATCH_GLOBAL_URL = "/batch/global";
 
-  private final DatabaseSessionFactory sessionFactory;
+  private final ServerClient serverClient;
 
-  public DefaultGlobalReferentialsLoader(DatabaseSessionFactory sessionFactory) {
-    this.sessionFactory = sessionFactory;
+  public DefaultGlobalReferentialsLoader(ServerClient serverClient) {
+    this.serverClient = serverClient;
   }
 
   @Override
   public GlobalReferentials load() {
-    GlobalReferentials ref = new GlobalReferentials();
-    for (Metric m : sessionFactory.getSession().getResults(Metric.class, ENABLED, true)) {
-      Boolean optimizedBestValue = m.isOptimizedBestValue();
-      Boolean qualitative = m.getQualitative();
-      Boolean userManaged = m.getUserManaged();
-      ref.metrics().add(
-        new org.sonar.batch.protocol.input.Metric(m.getId(), m.getKey(),
-          m.getType().name(),
-          m.getDescription(),
-          m.getDirection(),
-          m.getName(),
-          qualitative != null ? m.getQualitative() : false,
-          userManaged != null ? m.getUserManaged() : false,
-          m.getWorstValue(),
-          m.getBestValue(),
-          optimizedBestValue != null ? optimizedBestValue : false));
+    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 ref;
   }
 
-  private Collection<Metric> doFindAll() {
-    return sessionFactory.getSession().getResults(Metric.class, ENABLED, true);
-  }
 }
index 87da7f92d6591d84fd79a8b51e856827de6ffe37..8348549e20126bf7ce709c71e5e8504341a47568 100644 (file)
 package org.sonar.batch.referential;
 
 import org.picocontainer.injectors.ProviderAdapter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.sonar.api.utils.TimeProfiler;
 import org.sonar.batch.protocol.input.GlobalReferentials;
 
 public class GlobalReferentialsProvider extends ProviderAdapter {
 
+  private static final Logger LOG = LoggerFactory.getLogger(GlobalReferentialsProvider.class);
+
   public GlobalReferentials provide(GlobalReferentialsLoader loader) {
-    return loader.load();
+    TimeProfiler profiler = new TimeProfiler(LOG).start("Load global referentials");
+    try {
+      return loader.load();
+    } finally {
+      profiler.stop();
+    }
   }
 }
index 6241b0804dc8968ac0abf69739d1f3190d43d54a..f9d7d49901850d70270e8cb55689f045849131be 100644 (file)
@@ -46,11 +46,6 @@ public class DefaultSettingsReferential implements SettingsReferential {
     this.analysisMode = analysisMode;
   }
 
-  @Override
-  public Map<String, String> globalSettings() {
-    return downloadSettings(null);
-  }
-
   @Override
   public Map<String, String> projectSettings(String moduleKey) {
     return downloadSettings(moduleKey);
index d4d7367e3ea874bbf9b5959859f95204f4088001..d8e4603826d46123a5d6fda71af0a5d37f81f7d1 100644 (file)
@@ -29,11 +29,6 @@ import java.util.Map;
  */
 public interface SettingsReferential extends BatchComponent {
 
-  /**
-   * Provide global settings
-   */
-  Map<String, String> globalSettings();
-
   /**
    * Provide settings for a given project or sub-project (includes global settings)
    * @param projectKey
index b7b367dea46df83d15d51568d85969c4cb2e4984..34bd571d7ce2a4635f9576c1e3e2770291bd78c5 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.batch.bootstrap;
 
-import com.google.common.collect.ImmutableMap;
 import org.apache.commons.configuration.BaseConfiguration;
 import org.apache.commons.configuration.Configuration;
 import org.junit.Before;
@@ -28,20 +27,19 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.config.PropertyDefinitions;
-import org.sonar.batch.settings.SettingsReferential;
+import org.sonar.batch.protocol.input.GlobalReferentials;
 
 import java.util.Collections;
 
 import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 
 public class GlobalSettingsTest {
 
   @Rule
   public ExpectedException thrown = ExpectedException.none();
 
-  SettingsReferential settingsRef = mock(SettingsReferential.class);
+  GlobalReferentials globalRef;
   ProjectDefinition project = ProjectDefinition.create().setKey("struts");
   Configuration deprecatedConf = new BaseConfiguration();
   BootstrapProperties bootstrapProps;
@@ -50,15 +48,16 @@ public class GlobalSettingsTest {
 
   @Before
   public void prepare() {
+    globalRef = new GlobalReferentials();
     bootstrapProps = new BootstrapProperties(Collections.<String, String>emptyMap());
     mode = mock(AnalysisMode.class);
   }
 
   @Test
   public void should_load_global_settings() {
-    when(settingsRef.globalSettings()).thenReturn(ImmutableMap.of("sonar.cpd.cross", "true"));
+    globalRef.globalSettings().put("sonar.cpd.cross", "true");
 
-    GlobalSettings batchSettings = new GlobalSettings(bootstrapProps, new PropertyDefinitions(), settingsRef, deprecatedConf, mode);
+    GlobalSettings batchSettings = new GlobalSettings(bootstrapProps, new PropertyDefinitions(), globalRef, deprecatedConf, mode);
 
     assertThat(batchSettings.getBoolean("sonar.cpd.cross")).isTrue();
   }
index e3824aa70f11327401243bb03e8d8c3aa1dcc9fc..ce7466ecc232977600a91bffea9c666a89f447a0 100644 (file)
@@ -40,6 +40,7 @@ import org.sonar.batch.bootstrap.BootstrapProperties;
 import org.sonar.batch.bootstrap.ExtensionInstaller;
 import org.sonar.batch.bootstrap.GlobalSettings;
 import org.sonar.batch.profiling.PhasesSumUpTimeProfiler;
+import org.sonar.batch.protocol.input.GlobalReferentials;
 import org.sonar.batch.protocol.input.ProjectReferentials;
 import org.sonar.batch.referential.ProjectReferentialsLoader;
 import org.sonar.batch.scan.maven.MavenPluginExecutor;
@@ -70,10 +71,10 @@ public class ProjectScanContainerTest {
     parentContainer.add(bootstrapProperties);
     parentContainer.add(analysisMode);
     parentContainer.add(new PropertiesConfiguration());
-    SettingsReferential settingsReferential = mock(SettingsReferential.class);
-    settings = new GlobalSettings(bootstrapProperties, new PropertyDefinitions(), settingsReferential, new PropertiesConfiguration(), analysisMode);
+    GlobalReferentials globalRef = new GlobalReferentials();
+    settings = new GlobalSettings(bootstrapProperties, new PropertyDefinitions(), globalRef, new PropertiesConfiguration(), analysisMode);
     parentContainer.add(settings);
-    parentContainer.add(settingsReferential);
+    parentContainer.add(mock(SettingsReferential.class));
     ProjectReferentialsLoader projectReferentialsLoader = new ProjectReferentialsLoader() {
       @Override
       public ProjectReferentials load(ProjectReactor reactor, Settings settings, Languages languages) {
index 2d318ee628e81ec50e5dd4d2c3d1c686350b65af..1c6dd28f3b1f1b1a07d2f16b88ebc9513918fd27 100644 (file)
@@ -34,6 +34,7 @@ import org.sonar.api.utils.MessageException;
 import org.sonar.batch.bootstrap.AnalysisMode;
 import org.sonar.batch.bootstrap.BootstrapProperties;
 import org.sonar.batch.bootstrap.GlobalSettings;
+import org.sonar.batch.protocol.input.GlobalReferentials;
 import org.sonar.batch.settings.SettingsReferential;
 
 import java.util.Collections;
@@ -57,7 +58,7 @@ public class ProjectSettingsTest {
   @Before
   public void prepare() {
     mode = mock(AnalysisMode.class);
-    bootstrapProps = new GlobalSettings(new BootstrapProperties(Collections.<String, String>emptyMap()), new PropertyDefinitions(), settingsRef, deprecatedConf, mode);
+    bootstrapProps = new GlobalSettings(new BootstrapProperties(Collections.<String, String>emptyMap()), new PropertyDefinitions(), new GlobalReferentials(), deprecatedConf, mode);
   }
 
   @Test
index 7fda9daaa6c1035c0a71fd22e3dddddbb5ddac11..8687bf8a0e87f5a725f9401fd445cb295b4ea037 100644 (file)
@@ -36,8 +36,6 @@ public class DefaultSettingsReferentialTest {
   @Rule
   public ExpectedException thrown = ExpectedException.none();
 
-  private static final String JSON_RESPONSE = "[{\"k\":\"sonar.cpd.cross\",\"v\":\"true\"}]";
-
   private static final String REACTOR_JSON_RESPONSE = "[{\"k\":\"sonar.cpd.cross\",\"v\":\"true\"}," +
     "{\"k\":\"sonar.java.coveragePlugin\",\"v\":\"jacoco\"}]";
 
@@ -60,10 +58,4 @@ public class DefaultSettingsReferentialTest {
     assertThat(ref.projectSettings("struts")).hasSize(2).includes(MapAssert.entry("sonar.cpd.cross", "true"));
   }
 
-  @Test
-  public void should_load_global_settings() {
-    when(client.request("/batch_bootstrap/properties?dryRun=false")).thenReturn(JSON_RESPONSE);
-
-    assertThat(ref.globalSettings()).hasSize(1).includes(MapAssert.entry("sonar.cpd.cross", "true"));
-  }
 }