Search in sources :

Example 16 with ServerApi

use of org.sonarsource.sonarlint.core.serverapi.ServerApi in project sonarlint-core by SonarSource.

the class LocalStorageSynchronizerTests method should_synchronize_a_project_with_a_single_active_rule.

@Test
void should_synchronize_a_project_with_a_single_active_rule(@TempDir Path tmpDir) {
    mockServer.addStringResponse("/api/system/status", "{\"id\": \"1\", \"status\": \"UP\", \"version\": \"1\"}");
    mockServer.addProtobufResponse("/api/settings/values.protobuf?component=projectKey", Settings.ValuesWsResponse.newBuilder().addSettings(Settings.Setting.newBuilder().setKey("settingKey").setValue("settingValue").build()).build());
    mockServer.addProtobufResponse("/api/qualityprofiles/search.protobuf?project=projectKey", Qualityprofiles.SearchWsResponse.newBuilder().addProfiles(Qualityprofiles.SearchWsResponse.QualityProfile.newBuilder().setKey("qpKey").setIsDefault(true).setName("qpName").setLanguage("js").setLanguageName("Javascript").setActiveRuleCount(1).setRulesUpdatedAt("2020-10-27T23:08:58+0000").setUserUpdatedAt("2020-10-27T23:08:58+0000").build()).build());
    mockServer.addProtobufResponse("/api/rules/search.protobuf?qprofile=qpKey&activation=true&f=templateKey,actives&types=CODE_SMELL,BUG,VULNERABILITY&ps=500&p=1", Rules.SearchResponse.newBuilder().addRules(Rules.Rule.newBuilder().setKey("ruleKey").setTemplateKey("templateKey").build()).setActives(Rules.Actives.newBuilder().putActives("ruleKey", Rules.ActiveList.newBuilder().addActiveList(Rules.Active.newBuilder().addParams(Rules.Active.Param.newBuilder().setKey("paramKey").setValue("paramValue").build()).setSeverity("MAJOR").build()).build()).build()).build());
    mockServer.addProtobufResponse("/api/project_branches/list.protobuf?project=projectKey", ProjectBranches.ListWsResponse.newBuilder().addBranches(ProjectBranches.Branch.newBuilder().setName("master").setIsMain(true).setType(BranchType.BRANCH)).addBranches(ProjectBranches.Branch.newBuilder().setName("feature/foo").setIsMain(false).setType(BranchType.BRANCH)).build());
    var synchronizer = new LocalStorageSynchronizer(Set.of(Language.JS), emptySet(), new PluginsStorage(tmpDir), new ProjectStorage(tmpDir));
    synchronizer.synchronize(new ServerApi(mockServer.serverApiHelper()), Set.of("projectKey"), progressMonitor);
    var analyzerConfigFile = tmpDir.resolve("70726f6a6563744b6579/analyzer_config.pb");
    assertThat(analyzerConfigFile).exists();
    var analyzerConfiguration = ProtobufUtil.readFile(analyzerConfigFile, Sonarlint.AnalyzerConfiguration.parser());
    assertThat(analyzerConfiguration.getSettingsMap()).containsEntry("settingKey", "settingValue");
    var ruleSetsByLanguageKeyMap = analyzerConfiguration.getRuleSetsByLanguageKeyMap();
    assertThat(ruleSetsByLanguageKeyMap).containsKey("js");
    var ruleSet = ruleSetsByLanguageKeyMap.get("js");
    assertThat(ruleSet.getRulesCount()).isEqualTo(1);
    var activeRule = ruleSet.getRulesList().get(0);
    assertThat(activeRule.getRuleKey()).isEqualTo("ruleKey");
    assertThat(activeRule.getSeverity()).isEqualTo("MAJOR");
    assertThat(activeRule.getTemplateKey()).isEqualTo("templateKey");
    assertThat(activeRule.getParamsMap()).containsEntry("paramKey", "paramValue");
    var projectBranchesFile = tmpDir.resolve("70726f6a6563744b6579/project_branches.pb");
    assertThat(projectBranchesFile).exists();
    var projectBranches = ProtobufUtil.readFile(projectBranchesFile, Sonarlint.ProjectBranches.parser());
    assertThat(projectBranches.getBranchNameList()).containsExactlyInAnyOrder("master", "feature/foo");
    assertThat(projectBranches.getMainBranchName()).isEqualTo("master");
}
Also used : ServerApi(org.sonarsource.sonarlint.core.serverapi.ServerApi) Test(org.junit.jupiter.api.Test)

Example 17 with ServerApi

use of org.sonarsource.sonarlint.core.serverapi.ServerApi in project sonarlint-core by SonarSource.

the class LocalStorageSynchronizerTests method should_not_synchronize_up_to_date_ruleset.

@Test
void should_not_synchronize_up_to_date_ruleset(@TempDir Path tmpDir) {
    var storageFile = tmpDir.resolve("70726f6a6563744b6579/analyzer_config.pb");
    FileUtils.mkdirs(storageFile.getParent());
    ProtobufUtil.writeToFile(Sonarlint.AnalyzerConfiguration.newBuilder().putAllRuleSetsByLanguageKey(Map.of("js", Sonarlint.RuleSet.newBuilder().setLastModified("2020-10-27T23:08:58+0000").build())).build(), storageFile);
    mockServer.addStringResponse("/api/system/status", "{\"id\": \"1\", \"status\": \"UP\", \"version\": \"1\"}");
    mockServer.addProtobufResponse("/api/settings/values.protobuf?component=projectKey", Settings.ValuesWsResponse.newBuilder().addSettings(Settings.Setting.newBuilder().setKey("settingKey").setValue("settingValue").build()).build());
    mockServer.addProtobufResponse("/api/qualityprofiles/search.protobuf?project=projectKey", Qualityprofiles.SearchWsResponse.newBuilder().addProfiles(Qualityprofiles.SearchWsResponse.QualityProfile.newBuilder().setKey("qpKey").setIsDefault(true).setName("qpName").setLanguage("js").setLanguageName("Javascript").setActiveRuleCount(1).setRulesUpdatedAt("2020-10-27T23:08:58+0000").setUserUpdatedAt("2020-10-27T23:08:58+0000").build()).build());
    var synchronizer = new LocalStorageSynchronizer(Set.of(Language.JS), emptySet(), new PluginsStorage(tmpDir), new ProjectStorage(tmpDir));
    synchronizer.synchronize(new ServerApi(mockServer.serverApiHelper()), Set.of("projectKey"), progressMonitor);
    assertThat(storageFile).exists();
    var analyzerConfiguration = ProtobufUtil.readFile(storageFile, Sonarlint.AnalyzerConfiguration.parser());
    assertThat(analyzerConfiguration.getSettingsMap()).containsEntry("settingKey", "settingValue");
}
Also used : ServerApi(org.sonarsource.sonarlint.core.serverapi.ServerApi) Test(org.junit.jupiter.api.Test)

Example 18 with ServerApi

use of org.sonarsource.sonarlint.core.serverapi.ServerApi in project sonarlint-core by SonarSource.

the class PluginsSynchronizerTests method should_synchronize_plugins.

@Test
void should_synchronize_plugins(@TempDir Path tmp) throws Exception {
    var dest = tmp.resolve("destDir");
    Files.createDirectory(dest);
    mockServer.addStringResponse("/api/plugins/installed", "{\"plugins\": [" + "{\"key\": \"java\", \"hash\": \"de5308f43260d357acc97712ce4c5475\", \"filename\": \"sonar-java-plugin-5.13.1.18282.jar\", \"sonarLintSupported\": true}," + "{\"key\": \"javascript\", \"hash\": \"79dba9cab72d8d31767f47c03d169598\", \"filename\": \"sonar-javascript-plugin-5.2.1.7778.jar\", \"sonarLintSupported\": true}" + "]}");
    mockServer.addStringResponse("/api/plugins/download?plugin=java", "content-java");
    mockServer.addStringResponse("/api/plugins/download?plugin=javascript", "content-js");
    underTest = new PluginsSynchronizer(Set.of(Language.JAVA, Language.JS), new PluginsStorage(dest), emptySet());
    var anyPluginUpdated = underTest.synchronize(new ServerApi(mockServer.serverApiHelper()), new ProgressMonitor(null));
    var references = ProtobufUtil.readFile(dest.resolve("plugin_references.pb"), PluginReferences.parser());
    assertThat(references.getPluginsByKeyMap().values()).extracting("key", "hash", "filename").containsOnly(tuple("java", "de5308f43260d357acc97712ce4c5475", "sonar-java-plugin-5.13.1.18282.jar"), tuple("javascript", "79dba9cab72d8d31767f47c03d169598", "sonar-javascript-plugin-5.2.1.7778.jar"));
    assertThat(dest.resolve("sonar-java-plugin-5.13.1.18282.jar")).hasContent("content-java");
    assertThat(dest.resolve("sonar-javascript-plugin-5.2.1.7778.jar")).hasContent("content-js");
    assertThat(anyPluginUpdated).isTrue();
}
Also used : ProgressMonitor(org.sonarsource.sonarlint.core.commons.progress.ProgressMonitor) ServerApi(org.sonarsource.sonarlint.core.serverapi.ServerApi) Test(org.junit.jupiter.api.Test)

Example 19 with ServerApi

use of org.sonarsource.sonarlint.core.serverapi.ServerApi in project sonarlint-core by SonarSource.

the class PluginsSynchronizerTests method should_not_synchronize_the_old_typescript_plugin_if_language_not_enabled.

@Test
void should_not_synchronize_the_old_typescript_plugin_if_language_not_enabled(@TempDir Path tmp) throws Exception {
    var dest = tmp.resolve("destDir");
    Files.createDirectory(dest);
    mockServer.addStringResponse("/api/plugins/installed", "{\"plugins\": [" + "{\"key\": \"typescript\", \"hash\": \"de5308f43260d357acc97712ce4c5475\", \"filename\": \"sonar-typescript-plugin-1.9.0.3766.jar\", \"sonarLintSupported\": true}" + "]}");
    underTest = new PluginsSynchronizer(Set.of(Language.JAVA), new PluginsStorage(dest), emptySet());
    var anyPluginUpdated = underTest.synchronize(new ServerApi(mockServer.serverApiHelper()), new ProgressMonitor(null));
    assertThat(dest.resolve("plugin_references.pb")).doesNotExist();
    assertThat(dest.resolve("sonar-typescript-plugin-1.9.0.3766.jar")).doesNotExist();
    assertThat(anyPluginUpdated).isFalse();
}
Also used : ProgressMonitor(org.sonarsource.sonarlint.core.commons.progress.ProgressMonitor) ServerApi(org.sonarsource.sonarlint.core.serverapi.ServerApi) Test(org.junit.jupiter.api.Test)

Example 20 with ServerApi

use of org.sonarsource.sonarlint.core.serverapi.ServerApi in project sonarlint-core by SonarSource.

the class PluginsSynchronizerTests method should_synchronize_the_old_typescript_plugin_if_language_enabled.

@Test
void should_synchronize_the_old_typescript_plugin_if_language_enabled(@TempDir Path tmp) throws Exception {
    var dest = tmp.resolve("destDir");
    Files.createDirectory(dest);
    mockServer.addStringResponse("/api/plugins/installed", "{\"plugins\": [" + "{\"key\": \"typescript\", \"hash\": \"de5308f43260d357acc97712ce4c5475\", \"filename\": \"sonar-typescript-plugin-1.9.0.3766.jar\", \"sonarLintSupported\": true}" + "]}");
    mockServer.addStringResponse("/api/plugins/download?plugin=typescript", "content-ts");
    underTest = new PluginsSynchronizer(Set.of(Language.TS), new PluginsStorage(dest), emptySet());
    var anyPluginUpdated = underTest.synchronize(new ServerApi(mockServer.serverApiHelper()), new ProgressMonitor(null));
    var references = ProtobufUtil.readFile(dest.resolve("plugin_references.pb"), PluginReferences.parser());
    assertThat(references.getPluginsByKeyMap().values()).extracting("key", "hash", "filename").containsOnly(tuple("typescript", "de5308f43260d357acc97712ce4c5475", "sonar-typescript-plugin-1.9.0.3766.jar"));
    assertThat(dest.resolve("sonar-typescript-plugin-1.9.0.3766.jar")).hasContent("content-ts");
    assertThat(anyPluginUpdated).isTrue();
}
Also used : ProgressMonitor(org.sonarsource.sonarlint.core.commons.progress.ProgressMonitor) ServerApi(org.sonarsource.sonarlint.core.serverapi.ServerApi) Test(org.junit.jupiter.api.Test)

Aggregations

ServerApi (org.sonarsource.sonarlint.core.serverapi.ServerApi)23 Test (org.junit.jupiter.api.Test)13 ProgressMonitor (org.sonarsource.sonarlint.core.commons.progress.ProgressMonitor)13 Test (org.junit.Test)5 HashMap (java.util.HashMap)3 ServerApiHelper (org.sonarsource.sonarlint.core.serverapi.ServerApiHelper)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Optional (java.util.Optional)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Set (java.util.Set)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1