-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProfileRegistrarTest.java
More file actions
131 lines (110 loc) · 4.99 KB
/
ProfileRegistrarTest.java
File metadata and controls
131 lines (110 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* SonarSource Ruby
* Copyright (C) 2018-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonarsource.slang;
import com.sonar.orchestrator.container.Edition;
import com.sonar.orchestrator.junit4.OrchestratorRule;
import com.sonar.orchestrator.junit4.OrchestratorRuleBuilder;
import com.sonar.orchestrator.locator.FileLocation;
import com.sonar.orchestrator.locator.Location;
import com.sonar.orchestrator.locator.MavenLocation;
import java.io.File;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.junit.ClassRule;
import org.junit.Test;
import org.sonarqube.ws.client.HttpConnector;
import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.WsClientFactories;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration test that verifies RubyProfileRegistrar functionality by using a separate
* Orchestrator instance with both the Ruby plugin and a test plugin that registers a custom rule.
*/
public class ProfileRegistrarTest {
@ClassRule
public static final OrchestratorRule ORCHESTRATOR;
static {
OrchestratorRuleBuilder orchestratorBuilder = OrchestratorRule.builderEnv();
addRubyPlugin(orchestratorBuilder);
addTestPlugin(orchestratorBuilder);
ORCHESTRATOR = orchestratorBuilder
.useDefaultAdminCredentialsForBuilds(true)
.setEdition(Edition.ENTERPRISE_LW)
.activateLicense()
.setSonarVersion(System.getProperty("sonar.runtimeVersion", "LATEST_RELEASE"))
.build();
}
private static void addRubyPlugin(OrchestratorRuleBuilder builder) {
String slangVersion = System.getProperty("slangVersion");
Location pluginLocation;
String plugin = "sonar-ruby-plugin";
if (StringUtils.isEmpty(slangVersion)) {
// use the plugin that was built on local machine
pluginLocation = FileLocation.byWildcardMavenFilename(
new File("../../" + plugin + "/build/libs"),
plugin + "-*-all.jar");
} else {
// QA environment downloads the plugin built by the CI job
pluginLocation = MavenLocation.of("org.sonarsource.slang", plugin, slangVersion);
}
builder.addPlugin(pluginLocation);
}
private static void addTestPlugin(OrchestratorRuleBuilder builder) {
// Always use the test plugin that was built on local machine
String testPlugin = "test-plugin";
Location testPluginLocation = FileLocation.byWildcardMavenFilename(
new File("../test-plugin/build/libs"),
testPlugin + "-*.jar");
builder.addPlugin(testPluginLocation);
}
private static WsClient newWsClient() {
return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
.url(ORCHESTRATOR.getServer().getUrl())
.build());
}
@Test
public void testPluginRegistersRuleInDefaultRubyProfile() {
var wsClient = newWsClient();
// First, query the quality profiles to find the profile key for Ruby's "Sonar way" profile
var profileSearchRequest = new org.sonarqube.ws.client.qualityprofiles.SearchRequest()
.setLanguage("ruby");
var profileResponse = wsClient.qualityprofiles().search(profileSearchRequest);
var rubyProfile = profileResponse.getProfilesList().stream()
.filter(profile -> "Sonar way".equals(profile.getName()) && profile.getIsBuiltIn())
.findFirst()
.orElseThrow(() -> new AssertionError("Built-in 'Sonar way' profile not found for Ruby language"));
var profileKey = rubyProfile.getKey();
// Query the active rules in the built-in "Sonar way" profile for Ruby language using the profile key
var rulesSearchRequest = new org.sonarqube.ws.client.rules.SearchRequest()
.setLanguages(List.of("ruby"))
.setQprofile(profileKey)
.setActivation("true");
var rulesResponse = wsClient.rules().search(rulesSearchRequest);
// Verify that the profile contains the TEST001 rule from ruby-test repository
// This rule is registered by the TestProfileRegistrar in the test-plugin
var testRules = rulesResponse.getRulesList().stream()
.filter(rule -> "ruby-test:TEST001".equals(rule.getKey()))
.toList();
assertThat(testRules)
.as("Rule ruby-test:TEST001 should be registered in the default Ruby profile by TestProfileRegistrar")
.hasSize(1);
var testRule = testRules.get(0);
assertThat(testRule.getKey()).isEqualTo("ruby-test:TEST001");
assertThat(testRule.getRepo()).isEqualTo("ruby-test");
}
}