From: Duarte Meneses Date: Tue, 29 Sep 2015 07:05:19 +0000 (+0200) Subject: SONAR-6818 Split batch/project WS in several simpler WS X-Git-Tag: 5.2-RC1~160 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f5aa4163eb68aa987923d62bda08a8c49450cb1c;p=sonarqube.git SONAR-6818 Split batch/project WS in several simpler WS --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectAction.java b/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectAction.java index 36d20e4afc0..eaf3de9186b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/batch/ProjectAction.java @@ -20,16 +20,18 @@ 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(); + } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java index 1634de89847..fd0b0e0e4ec 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoader.java @@ -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) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java index 9e041e1e46e..3eace767103 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultQualityProfileLoader.java @@ -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; diff --git a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultServerIssuesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultServerIssuesLoader.java index bf238c03aed..bd8c1361501 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultServerIssuesLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/repository/DefaultServerIssuesLoader.java @@ -38,7 +38,7 @@ public class DefaultServerIssuesLoader implements ServerIssuesLoader { @Override public boolean load(String componentKey, Function consumer) { - WSLoaderResult result = wsLoader.loadStream("/batch/issues?key=" + BatchUtils.encodeForUrl(componentKey)); + WSLoaderResult result = wsLoader.loadStream("/batch/issues.protobuf?key=" + BatchUtils.encodeForUrl(componentKey)); parseIssues(result.get(), consumer); return result.isFromCache(); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java b/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java index 5373c36f945..76fffd15264 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java @@ -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()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultActiveRulesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultActiveRulesLoader.java index 112efd60995..449cba0ccbc 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultActiveRulesLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultActiveRulesLoader.java @@ -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; diff --git a/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultRulesLoader.java b/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultRulesLoader.java index 315106a6179..78ff77fdbf9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultRulesLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/rule/DefaultRulesLoader.java @@ -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; diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/BatchMediumTester.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/BatchMediumTester.java index ef508dcb553..f488d8bfe65 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/BatchMediumTester.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/BatchMediumTester.java @@ -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); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/ChecksMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/ChecksMediumTest.java index 488c56ae7bc..29531f3e537 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/ChecksMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/ChecksMediumTest.java @@ -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); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java index fb6a18d50c4..00a2e569a96 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultProjectRepositoriesLoaderTest.java @@ -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 { diff --git a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultQualityProfileLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultQualityProfileLoaderTest.java index 33b053b74a2..6a52a3f1c50 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultQualityProfileLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultQualityProfileLoaderTest.java @@ -67,7 +67,7 @@ public class DefaultQualityProfileLoaderTest { when(ws.loadStream(anyString())).thenReturn(result); List 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(is, false)); List 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); } diff --git a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultServerIssuesLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultServerIssuesLoaderTest.java index 1801b62e95f..54b1490082b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultServerIssuesLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/repository/DefaultServerIssuesLoaderTest.java @@ -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 result = new ArrayList<>(); loader.load("foo", new Function() { @@ -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(is, true)); + when(wsLoader.loadStream("/batch/issues.protobuf?key=foo")).thenReturn(new WSLoaderResult(is, true)); loader.load("foo", mock(Function.class)); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/ActiveRulesProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/ActiveRulesProviderTest.java index a4f9b56aa3e..76a5cf1b07d 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/ActiveRulesProviderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/ActiveRulesProviderTest.java @@ -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(); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/DefaultActiveRulesLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/DefaultActiveRulesLoaderTest.java index d9cd09d2838..766ad91ab81 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/DefaultActiveRulesLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/DefaultActiveRulesLoaderTest.java @@ -55,7 +55,7 @@ public class DefaultActiveRulesLoaderTest { Collection 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); } diff --git a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java index dfc94827d0b..991fadf8cdc 100644 --- a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java +++ b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java @@ -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 { diff --git a/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java b/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java index 77ee30908e1..5063fbdb22f 100644 --- a/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java +++ b/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java @@ -19,7 +19,11 @@ */ 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"); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java index 2104b3d4e50..d16fb2d3bb9 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRules.java @@ -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 map = activeRulesByRepositoryAndKey.get(ruleKey.repository()); + if(map != null) { + return map.get(ruleKey.rule()); + } + return null; } @Override