]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6818 Split batch/project WS in several simpler WS
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Tue, 29 Sep 2015 07:05:19 +0000 (09:05 +0200)
committerDuarte Meneses <duarte.meneses@sonarsource.com>
Wed, 30 Sep 2015 14:28:08 +0000 (16:28 +0200)
17 files changed:
server/sonar-server/src/main/java/org/sonar/server/batch/ProjectAction.java
sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java
sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java
sonar-batch/src/main/java/org/sonar/batch/repository/DefaultServerIssuesLoader.java
sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java
sonar-batch/src/main/java/org/sonar/batch/rule/DefaultActiveRulesLoader.java
sonar-batch/src/main/java/org/sonar/batch/rule/DefaultRulesLoader.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/BatchMediumTester.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/ChecksMediumTest.java
sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java
sonar-batch/src/test/java/org/sonar/batch/repository/DefaultQualityProfileLoaderTest.java
sonar-batch/src/test/java/org/sonar/batch/repository/DefaultServerIssuesLoaderTest.java
sonar-batch/src/test/java/org/sonar/batch/rule/ActiveRulesProviderTest.java
sonar-batch/src/test/java/org/sonar/batch/rule/DefaultActiveRulesLoaderTest.java
sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java
sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java

index 36d20e4afc0337dcb8a689021cca772ca3afbadd..eaf3de9186bdbf607893cd2d67e20d1c3293a322 100644 (file)
 
 package org.sonar.server.batch;
 
+import org.sonarqube.ws.WsBatch.WsProjectResponse.FileData.Builder;
+
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
+
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.batch.protocol.input.FileData;
 import org.sonar.batch.protocol.input.ProjectRepositories;
 import org.sonarqube.ws.WsBatch.WsProjectResponse;
-
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
 
 public class ProjectAction implements BatchWsAction {
@@ -147,10 +149,14 @@ public class ProjectAction implements BatchWsAction {
   }
 
   private static WsProjectResponse.FileData toFileDataResponse(FileData fileData) {
-    return WsProjectResponse.FileData.newBuilder()
-      .setHash(fileData.hash())
-      .setRevision(fileData.revision())
-      .build();
-  }
+    Builder fileDataBuilder = WsProjectResponse.FileData.newBuilder();
+    if (fileData.hash() != null) {
+      fileDataBuilder.setHash(fileData.hash());
+    }
+    if (fileData.revision() != null) {
+      fileDataBuilder.setRevision(fileData.revision());
+    }
 
+    return fileDataBuilder.build();
+  }
 }
index 1634de89847c79ffc70c3bb9407cbaa5125f58ac..fd0b0e0e4ece9d7edb9dc0f803c73c0c71a02ec9 100644 (file)
@@ -39,7 +39,7 @@ import org.sonarqube.ws.WsBatch.WsProjectResponse.Settings;
 
 public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoader {
   private static final Logger LOG = LoggerFactory.getLogger(DefaultProjectRepositoriesLoader.class);
-  private static final String BATCH_PROJECT_URL = "/batch/project";
+  private static final String BATCH_PROJECT_URL = "/batch/project.protobuf";
   private final WSLoader loader;
 
   public DefaultProjectRepositoriesLoader(WSLoader loader) {
index 9e041e1e46e5ff9830ff1507022f86083fc973f7..3eace7671034736e1526c5dd002b3e4608c1b349 100644 (file)
@@ -36,7 +36,7 @@ import java.util.Collection;
 import java.util.List;
 
 public class DefaultQualityProfileLoader implements QualityProfileLoader {
-  private static final String WS_URL = "/qualityprofiles/search";
+  private static final String WS_URL = "/api/qualityprofiles/search.protobuf";
 
   private WSLoader wsLoader;
 
index bf238c03aedafdebb566c226f9c7a48111df0fd0..bd8c1361501924e58a6b21995fe315092a303e8c 100644 (file)
@@ -38,7 +38,7 @@ public class DefaultServerIssuesLoader implements ServerIssuesLoader {
 
   @Override
   public boolean load(String componentKey, Function<ServerIssue, Void> consumer) {
-    WSLoaderResult<InputStream> result = wsLoader.loadStream("/batch/issues?key=" + BatchUtils.encodeForUrl(componentKey));
+    WSLoaderResult<InputStream> result = wsLoader.loadStream("/batch/issues.protobuf?key=" + BatchUtils.encodeForUrl(componentKey));
     parseIssues(result.get(), consumer);
     return result.isFromCache();
   }
index 5373c36f94594d46d6f7fbbeadb1bc62956fd4f2..76fffd15264a70cc5b7492aa6c4396f10fb08e81 100644 (file)
@@ -83,7 +83,7 @@ public class ActiveRulesProvider extends ProviderAdapter {
     ActiveRulesBuilder builder = new ActiveRulesBuilder();
 
     for (Rule activeRule : loadedRules) {
-      NewActiveRule newActiveRule = builder.create(RuleKey.of(activeRule.getRepo(), activeRule.getKey()));
+      NewActiveRule newActiveRule = builder.create(RuleKey.parse(activeRule.getKey()));
       newActiveRule.setName(activeRule.getName());
       newActiveRule.setSeverity(activeRule.getSeverity());
       newActiveRule.setLanguage(activeRule.getLang());
index 112efd609958c6338b77ea20da8faaabf8583050..449cba0ccbc1ca18220bf1dd1e7cce3e30bb9abc 100644 (file)
@@ -35,7 +35,7 @@ import java.io.InputStream;
 import java.util.List;
 
 public class DefaultActiveRulesLoader implements ActiveRulesLoader {
-  private static final String RULES_SEARCH_URL = "/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey";
+  private static final String RULES_SEARCH_URL = "/api/rules/search.protobuf?f=repo,name,severity,lang,internalKey,templateKey,params&activation=true";
 
   private final WSLoader wsLoader;
 
index 315106a6179dae1c9c2b99767233bf0b3855e3ac..78ff77fdbf98a4f9091401f7885b4f3ca3d847f7 100644 (file)
@@ -35,7 +35,7 @@ import java.io.InputStream;
 import java.util.List;
 
 public class DefaultRulesLoader implements RulesLoader {
-  private static final String RULES_SEARCH_URL = "/api/rules/list";
+  private static final String RULES_SEARCH_URL = "/api/rules/list.protobuf";
 
   private final WSLoader wsLoader;
 
index ef508dcb553de0a0fac4e1972d8a814b6fca7300..f488d8bfe6541e15d1e9702d93a6810e142c2f9a 100644 (file)
@@ -33,7 +33,6 @@ import org.apache.commons.lang.mutable.MutableBoolean;
 
 import javax.annotation.Nullable;
 
-import org.sonar.batch.cache.ProjectCacheStatus;
 import org.sonarqube.ws.Rules.ListResponse.Rule;
 import org.sonar.batch.bootstrapper.IssueListener;
 import org.sonar.api.server.rule.RulesDefinition.Repository;
@@ -228,7 +227,10 @@ public class BatchMediumTester {
 
       org.sonarqube.ws.Rules.Rule.Builder builder = org.sonarqube.ws.Rules.Rule.newBuilder();
       builder.setRepo(repositoryKey);
-      builder.setKey(ruleKey);
+      if (internalKey != null) {
+        builder.setInternalKey(internalKey);
+      }
+      builder.setKey(repositoryKey + ":" + ruleKey);
       builder.setName(name);
 
       if (templateRuleKey != null) {
@@ -237,9 +239,6 @@ public class BatchMediumTester {
       if (languag != null) {
         builder.setLang(languag);
       }
-      if (internalKey != null) {
-        builder.setInternalKey(internalKey);
-      }
       if (severity != null) {
         builder.setSeverity(severity);
       }
index 488c56ae7bca74a641b480bada80b2527b5159c3..29531f3e537df0c4eccc5edf0319f784ce28bb1b 100644 (file)
@@ -113,7 +113,7 @@ public class ChecksMediumTest {
     @Nullable String internalKey, @Nullable String languag, String paramKey, String paramValue) {
     org.sonarqube.ws.Rules.Rule.Builder builder = org.sonarqube.ws.Rules.Rule.newBuilder();
     builder.setRepo(repositoryKey);
-    builder.setKey(ruleKey);
+    builder.setKey(repositoryKey + ":" + ruleKey);
     if (templateRuleKey != null) {
       builder.setTemplateKey(templateRuleKey);
     }
index fb6a18d50c4bf3b9e27ff45945afccee93a54b08..00a2e569a9623c764c6f3b0c88583bb8e7c27e03 100644 (file)
@@ -64,10 +64,10 @@ public class DefaultProjectRepositoriesLoaderTest {
   @Test
   public void passIssuesModeParameter() {
     loader.load(PROJECT_KEY, false, null);
-    verify(wsLoader).loadStream("/batch/project?key=foo%3F");
+    verify(wsLoader).loadStream("/batch/project.protobuf?key=foo%3F");
 
     loader.load(PROJECT_KEY, true, null);
-    verify(wsLoader).loadStream("/batch/project?key=foo%3F&issues=true");
+    verify(wsLoader).loadStream("/batch/project.protobuf?key=foo%3F&issues=true");
   }
 
   @Test
@@ -80,7 +80,7 @@ public class DefaultProjectRepositoriesLoaderTest {
   @Test
   public void passAndEncodeProjectKeyParameter() {
     loader.load(PROJECT_KEY, false, null);
-    verify(wsLoader).loadStream("/batch/project?key=foo%3F");
+    verify(wsLoader).loadStream("/batch/project.protobuf?key=foo%3F");
   }
 
   private InputStream mockData() throws IOException {
index 33b053b74a244745dd44b43e1b6d8fc83cbbc2d1..6a52a3f1c50159b209a970f7ff7d672fd363d244 100644 (file)
@@ -67,7 +67,7 @@ public class DefaultQualityProfileLoaderTest {
     when(ws.loadStream(anyString())).thenReturn(result);
 
     List<QualityProfile> loaded = qpLoader.load("foo#2", "my-profile#2", null);
-    verify(ws).loadStream("/qualityprofiles/search?projectKey=foo%232&profileName=my-profile%232");
+    verify(ws).loadStream("/api/qualityprofiles/search.protobuf?projectKey=foo%232&profileName=my-profile%232");
     verifyNoMoreInteractions(ws);
     assertThat(loaded).hasSize(1);
   }
@@ -90,7 +90,7 @@ public class DefaultQualityProfileLoaderTest {
     when(ws.loadStream(anyString())).thenReturn(new WSLoaderResult<InputStream>(is, false));
 
     List<QualityProfile> loaded = qpLoader.loadDefault(null);
-    verify(ws).loadStream("/qualityprofiles/search?defaults=true");
+    verify(ws).loadStream("/api/qualityprofiles/search.protobuf?defaults=true");
     verifyNoMoreInteractions(ws);
     assertThat(loaded).hasSize(1);
   }
index 1801b62e95f728077b7d3d86d837ea30f1b11606..54b1490082b14be10f619c462f0e1a581c878ef7 100644 (file)
@@ -58,7 +58,7 @@ public class DefaultServerIssuesLoaderTest {
       .writeDelimitedTo(bos);
 
     InputStream is = new ByteArrayInputStream(bos.toByteArray());
-    when(wsLoader.loadStream("/batch/issues?key=foo")).thenReturn(new WSLoaderResult<>(is, true));
+    when(wsLoader.loadStream("/batch/issues.protobuf?key=foo")).thenReturn(new WSLoaderResult<>(is, true));
 
     final List<ServerIssue> result = new ArrayList<>();
     loader.load("foo", new Function<BatchInput.ServerIssue, Void>() {
@@ -77,7 +77,7 @@ public class DefaultServerIssuesLoaderTest {
   public void testError() throws IOException {
     InputStream is = mock(InputStream.class);
     when(is.read()).thenThrow(IOException.class);
-    when(wsLoader.loadStream("/batch/issues?key=foo")).thenReturn(new WSLoaderResult<InputStream>(is, true));
+    when(wsLoader.loadStream("/batch/issues.protobuf?key=foo")).thenReturn(new WSLoaderResult<InputStream>(is, true));
     loader.load("foo", mock(Function.class));
   }
 }
index a4f9b56aa3e268696610d0d6314621cdda3a3cdf..76a5cf1b07d19054e5c775a40b48feb4632f79c9 100644 (file)
@@ -89,6 +89,6 @@ public class ActiveRulesProviderTest {
   }
 
   private static Rule mockRule(String name) {
-    return Rule.newBuilder().setName(name).setRepo(name).setKey(name).build();
+    return Rule.newBuilder().setName(name).setRepo(name).setKey(name + ":" + name).build();
   }
 }
index d9cd09d28387a7e6db19ecf0b4873f6d81c7d42b..766ad91ab81934a0b7dbc41e4f500ed686f6c566 100644 (file)
@@ -55,7 +55,7 @@ public class DefaultActiveRulesLoaderTest {
     Collection<Rule> activeRules = loader.load("java-sonar-way-26368", null);
     assertThat(activeRules).hasSize(100);
 
-    verify(ws).loadStream("/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey&qprofile=java-sonar-way-26368");
+    verify(ws).loadStream("/api/rules/search.protobuf?f=repo,name,severity,lang,internalKey,templateKey,params&activation=true&qprofile=java-sonar-way-26368");
     verifyNoMoreInteractions(ws);
 
   }
index dfc94827d0ba51e48098495cd6f360df84b3342e..991fadf8cdc7ec093fe0554fd066553147f7f122 100644 (file)
@@ -90,6 +90,9 @@ public class PersistentCache {
     try {
       lock();
       Path path = getCacheCopy(key);
+      if (path == null) {
+        return null;
+      }
       return new DeleteFileOnCloseInputStream(new FileInputStream(path.toFile()), path);
 
     } finally {
index 77ee30908e105e035fef324f388df350860302b3..5063fbdb22f3357a576d572365fcb75fc80285b8 100644 (file)
  */
 package org.sonar.home.cache;
 
+import org.apache.commons.io.IOUtils;
+
 import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -71,6 +75,15 @@ public class PersistentCacheTest {
     assertThat(new String(Files.readAllBytes(lockFile), StandardCharsets.UTF_8)).isEqualTo("test");
   }
 
+  @Test
+  public void testStream() throws IOException {
+    cache.put("id", "test".getBytes());
+    InputStream stream = cache.getStream("id");
+    assertThat(IOUtils.toString(stream)).isEqualTo("test");
+
+    assertThat(cache.getStream("non existing")).isNull();
+  }
+
   @Test
   public void testClear() throws Exception {
     Path lockFile = cache.getDirectory().resolve("lock");
index 2104b3d4e50139ddab2e97116e4bb623639e080f..d16fb2d3bb977251ce39d310c45d0da904d2d84b 100644 (file)
@@ -65,7 +65,11 @@ public class DefaultActiveRules implements ActiveRules {
 
   @Override
   public ActiveRule find(RuleKey ruleKey) {
-    return activeRulesByRepositoryAndKey.containsKey(ruleKey.repository()) ? activeRulesByRepositoryAndKey.get(ruleKey.repository()).get(ruleKey.rule()) : null;
+    Map<String, ActiveRule> map = activeRulesByRepositoryAndKey.get(ruleKey.repository());
+    if(map != null) {
+      return map.get(ruleKey.rule());
+    }
+    return null;
   }
 
   @Override