aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2017-01-09 22:18:14 +0100
committerJulien HENRY <henryju@yahoo.fr>2017-01-12 11:26:48 +0100
commit1a063b96deafadd4b8b81f84f81d1ee5dfb289fe (patch)
treede173b016fffbf85115f142516ce7dd2471c66f1 /sonar-scanner-engine/src/test
parented5f35755cde0043524d7dc72689dffbd0bb7c1d (diff)
downloadsonarqube-1a063b96deafadd4b8b81f84f81d1ee5dfb289fe.tar.gz
sonarqube-1a063b96deafadd4b8b81f84f81d1ee5dfb289fe.zip
SONAR-8583 Use api/metrics/search to load metrics
Diffstat (limited to 'sonar-scanner-engine/src/test')
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java39
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisContextReportPublisherTest.java13
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultMetricsRepositoryLoaderTest.java (renamed from sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultGlobalRepositoriesLoaderTest.java)31
-rw-r--r--sonar-scanner-engine/src/test/resources/org/sonar/scanner/repository/DefaultMetricsRepositoryLoaderTest/page1.json27
-rw-r--r--sonar-scanner-engine/src/test/resources/org/sonar/scanner/repository/DefaultMetricsRepositoryLoaderTest/page2.json17
5 files changed, 80 insertions, 47 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java
index e6b475f7515..83bbeff529f 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java
@@ -54,11 +54,11 @@ import org.sonar.batch.bootstrapper.IssueListener;
import org.sonar.batch.bootstrapper.LogOutput;
import org.sonar.scanner.bootstrap.GlobalMode;
import org.sonar.scanner.issue.tracking.ServerLineHashesLoader;
-import org.sonar.scanner.protocol.input.GlobalRepositories;
import org.sonar.scanner.protocol.input.ScannerInput.ServerIssue;
import org.sonar.scanner.report.ReportPublisher;
import org.sonar.scanner.repository.FileData;
-import org.sonar.scanner.repository.GlobalRepositoriesLoader;
+import org.sonar.scanner.repository.MetricsRepository;
+import org.sonar.scanner.repository.MetricsRepositoryLoader;
import org.sonar.scanner.repository.ProjectRepositories;
import org.sonar.scanner.repository.ProjectRepositoriesLoader;
import org.sonar.scanner.repository.QualityProfileLoader;
@@ -116,7 +116,7 @@ public class ScannerMediumTester {
}
public static class BatchMediumTesterBuilder {
- private final FakeGlobalRepositoriesLoader globalRefProvider = new FakeGlobalRepositoriesLoader();
+ private final FakeMetricsRepositoryLoader globalRefProvider = new FakeMetricsRepositoryLoader();
private final FakeProjectRepositoriesLoader projectRefProvider = new FakeProjectRepositoriesLoader();
private final FakePluginInstaller pluginInstaller = new FakePluginInstaller();
private final FakeServerIssuesLoader serverIssues = new FakeServerIssuesLoader();
@@ -201,7 +201,6 @@ public class ScannerMediumTester {
public BatchMediumTesterBuilder addDefaultQProfile(String language, String name) {
addQProfile(language, name);
- globalRefProvider.globalSettings().put("sonar.profile." + language, name);
return this;
}
@@ -380,34 +379,20 @@ public class ScannerMediumTester {
}
}
- private static class FakeGlobalRepositoriesLoader implements GlobalRepositoriesLoader {
+ private static class FakeMetricsRepositoryLoader implements MetricsRepositoryLoader {
private int metricId = 1;
- private GlobalRepositories ref = new GlobalRepositories();
+ private List<Metric> metrics = new ArrayList<>();
@Override
- public GlobalRepositories load() {
- return ref;
- }
-
- public Map<String, String> globalSettings() {
- return ref.globalSettings();
- }
-
- public FakeGlobalRepositoriesLoader add(Metric<?> metric) {
- Boolean optimizedBestValue = metric.isOptimizedBestValue();
- ref.metrics().add(new org.sonar.scanner.protocol.input.Metric(metricId,
- metric.key(),
- metric.getType().name(),
- metric.getDescription(),
- metric.getDirection(),
- metric.getName(),
- metric.getQualitative(),
- metric.getUserManaged(),
- metric.getWorstValue(),
- metric.getBestValue(),
- optimizedBestValue != null ? optimizedBestValue : false));
+ public MetricsRepository load() {
+ return new MetricsRepository(metrics);
+ }
+
+ public FakeMetricsRepositoryLoader add(Metric<?> metric) {
+ metric.setId(metricId++);
+ metrics.add(metric);
metricId++;
return this;
}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisContextReportPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisContextReportPublisherTest.java
index 48515e786b1..a31638c2d1b 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisContextReportPublisherTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisContextReportPublisherTest.java
@@ -35,10 +35,9 @@ import org.sonar.api.utils.System2;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.core.platform.PluginInfo;
+import org.sonar.scanner.bootstrap.GlobalSettings;
import org.sonar.scanner.bootstrap.ScannerPluginRepository;
-import org.sonar.scanner.protocol.input.GlobalRepositories;
import org.sonar.scanner.protocol.output.ScannerReportWriter;
-import org.sonar.scanner.report.AnalysisContextReportPublisher;
import org.sonar.scanner.repository.ProjectRepositories;
import org.sonar.updatecenter.common.Version;
@@ -65,7 +64,7 @@ public class AnalysisContextReportPublisherTest {
private AnalysisMode analysisMode = mock(AnalysisMode.class);
private System2 system2;
private ProjectRepositories projectRepos;
- private GlobalRepositories globalRepositories;
+ private GlobalSettings globalSettings;
@Before
public void prepare() throws Exception {
@@ -73,8 +72,8 @@ public class AnalysisContextReportPublisherTest {
system2 = mock(System2.class);
when(system2.properties()).thenReturn(new Properties());
projectRepos = mock(ProjectRepositories.class);
- globalRepositories = mock(GlobalRepositories.class);
- publisher = new AnalysisContextReportPublisher(analysisMode, pluginRepo, system2, projectRepos, globalRepositories);
+ globalSettings = mock(GlobalSettings.class);
+ publisher = new AnalysisContextReportPublisher(analysisMode, pluginRepo, system2, projectRepos, globalSettings);
}
@Test
@@ -105,7 +104,7 @@ public class AnalysisContextReportPublisherTest {
public void dumpServerSideGlobalProps() throws Exception {
logTester.setLevel(LoggerLevel.DEBUG);
ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder());
- when(globalRepositories.globalSettings()).thenReturn(ImmutableMap.of(COM_FOO, "bar", SONAR_SKIP, "true"));
+ when(globalSettings.getServerSideSettings()).thenReturn(ImmutableMap.of(COM_FOO, "bar", SONAR_SKIP, "true"));
publisher.init(writer);
@@ -206,7 +205,7 @@ public class AnalysisContextReportPublisherTest {
@Test
public void shouldNotDumpSensitiveGlobalProperties() throws Exception {
ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder());
- when(globalRepositories.globalSettings()).thenReturn(ImmutableMap.of("sonar.login", "my_token", "sonar.password", "azerty", "sonar.cpp.license.secured", "AZERTY"));
+ when(globalSettings.getServerSideSettings()).thenReturn(ImmutableMap.of("sonar.login", "my_token", "sonar.password", "azerty", "sonar.cpp.license.secured", "AZERTY"));
publisher.init(writer);
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultGlobalRepositoriesLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultMetricsRepositoryLoaderTest.java
index 3dc7cf3f337..897f412d599 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultGlobalRepositoriesLoaderTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultMetricsRepositoryLoaderTest.java
@@ -22,58 +22,63 @@ package org.sonar.scanner.repository;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
+import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.scanner.WsTestUtil;
import org.sonar.scanner.bootstrap.ScannerWsClient;
-import org.sonar.scanner.protocol.input.GlobalRepositories;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
-public class DefaultGlobalRepositoriesLoaderTest {
- private static final String BATCH_GLOBAL_URL = "/batch/global";
+public class DefaultMetricsRepositoryLoaderTest {
+ private static final String WS_URL = "/api/metrics/search?f=name,description,direction,qualitative,custom&ps=500&p=";
private ScannerWsClient wsClient;
- private DefaultGlobalRepositoriesLoader globalRepositoryLoader;
+ private DefaultMetricsRepositoryLoader metricsRepositoryLoader;
@Rule
public ExpectedException exception = ExpectedException.none();
@Before
- public void setUp() {
+ public void setUp() throws IOException {
wsClient = mock(ScannerWsClient.class);
- WsTestUtil.mockReader(wsClient, BATCH_GLOBAL_URL, new StringReader(new GlobalRepositories().toJson()));
- globalRepositoryLoader = new DefaultGlobalRepositoriesLoader(wsClient);
+ WsTestUtil.mockReader(wsClient, WS_URL + "1", new StringReader(IOUtils.toString(this.getClass().getResourceAsStream("DefaultMetricsRepositoryLoaderTest/page1.json"))));
+ WsTestUtil.mockReader(wsClient, WS_URL + "2", new StringReader(IOUtils.toString(this.getClass().getResourceAsStream("DefaultMetricsRepositoryLoaderTest/page2.json"))));
+ metricsRepositoryLoader = new DefaultMetricsRepositoryLoader(wsClient);
}
@Test
public void test() {
- globalRepositoryLoader.load();
- WsTestUtil.verifyCall(wsClient, BATCH_GLOBAL_URL);
+ MetricsRepository metricsRepository = metricsRepositoryLoader.load();
+ assertThat(metricsRepository.metrics()).hasSize(3);
+ WsTestUtil.verifyCall(wsClient, WS_URL + "1");
+ WsTestUtil.verifyCall(wsClient, WS_URL + "2");
verifyNoMoreInteractions(wsClient);
}
@Test
public void testIOError() throws IOException {
Reader reader = mock(Reader.class);
- when(reader.read(any(char[].class))).thenThrow(new IOException());
+ when(reader.read(any(char[].class), anyInt(), anyInt())).thenThrow(new IOException());
WsTestUtil.mockReader(wsClient, reader);
exception.expect(IllegalStateException.class);
- globalRepositoryLoader.load();
+ metricsRepositoryLoader.load();
}
@Test
public void testCloseError() throws IOException {
Reader reader = mock(Reader.class);
- when(reader.read(any(char[].class))).thenReturn(-1);
+ when(reader.read(any(char[].class), anyInt(), anyInt())).thenReturn(-1);
doThrow(new IOException()).when(reader).close();
WsTestUtil.mockReader(wsClient, reader);
exception.expect(IllegalStateException.class);
- globalRepositoryLoader.load();
+ metricsRepositoryLoader.load();
}
}
diff --git a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/repository/DefaultMetricsRepositoryLoaderTest/page1.json b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/repository/DefaultMetricsRepositoryLoaderTest/page1.json
new file mode 100644
index 00000000000..76abd728d85
--- /dev/null
+++ b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/repository/DefaultMetricsRepositoryLoaderTest/page1.json
@@ -0,0 +1,27 @@
+{
+ "metrics": [
+ {
+ "id": "10",
+ "key": "accessors",
+ "type": "INT",
+ "name": "Accessors",
+ "description": "Accessors",
+ "direction": -1,
+ "qualitative": false,
+ "custom": false
+ },
+ {
+ "id": "227",
+ "key": "new_technical_debt",
+ "type": "WORK_DUR",
+ "name": "Added Technical Debt",
+ "description": "Added technical debt",
+ "direction": -1,
+ "qualitative": true,
+ "custom": false
+ }
+ ],
+ "total": 3,
+ "p": 1,
+ "ps": 2
+}
diff --git a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/repository/DefaultMetricsRepositoryLoaderTest/page2.json b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/repository/DefaultMetricsRepositoryLoaderTest/page2.json
new file mode 100644
index 00000000000..470f680fe75
--- /dev/null
+++ b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/repository/DefaultMetricsRepositoryLoaderTest/page2.json
@@ -0,0 +1,17 @@
+{
+ "metrics": [
+ {
+ "id": "282",
+ "key": "afferent_coupling_average",
+ "type": "FLOAT",
+ "name": "Afferent Coupling (Average)",
+ "description": "Shows the Afferent Coupling for a package. This is a count of the number of other packages that depend on the classes within this package. It is an indicator of the package's responsibility.",
+ "direction": -1,
+ "qualitative": true,
+ "custom": false
+ }
+ ],
+ "total": 3,
+ "p": 2,
+ "ps": 2
+}