aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/test/java/org
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-03-06 15:36:41 +0100
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-03-07 08:42:46 +0100
commit06a0ff9b16fc25d6475df3502c53289360db5103 (patch)
tree3bec15fbdc24af078e3426242dd4f04be134fdc3 /sonar-batch/src/test/java/org
parent3d47e325554ea8a0ed347c98c9c1b7bb9b4efff6 (diff)
downloadsonarqube-06a0ff9b16fc25d6475df3502c53289360db5103.tar.gz
sonarqube-06a0ff9b16fc25d6475df3502c53289360db5103.zip
SONAR-5094 Load quality gate definitions and start generating the Quality Gate Status measure
Diffstat (limited to 'sonar-batch/src/test/java/org')
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java10
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateProviderTest.java113
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateVerifierTest.java100
3 files changed, 223 insertions, 0 deletions
diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java
index 98889eb865f..f9f31bcb287 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/ServerClientTest.java
@@ -32,6 +32,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.batch.bootstrapper.EnvironmentInformation;
+import org.sonar.wsclient.SonarClient;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@@ -136,6 +137,15 @@ public class ServerClientTest {
newServerClient().request("/foo");
}
+ @Test
+ public void should_give_access_to_ws_client() throws Exception {
+ server = new MockHttpServer();
+ server.start();
+
+ SonarClient wsClient = newServerClient().wsClient();
+ assertThat(wsClient).isNotNull();
+ }
+
private ServerClient newServerClient() {
when(settings.property(eq("sonar.host.url"), anyString())).thenReturn("http://localhost:" + server.getPort());
return new ServerClient(settings, new EnvironmentInformation("Junit", "4"));
diff --git a/sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateProviderTest.java
new file mode 100644
index 00000000000..746c47f3a58
--- /dev/null
+++ b/sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateProviderTest.java
@@ -0,0 +1,113 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube 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 GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.batch.qualitygate;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.slf4j.Logger;
+import org.sonar.api.config.Settings;
+import org.sonar.api.utils.MessageException;
+import org.sonar.batch.bootstrap.ServerClient;
+import org.sonar.wsclient.SonarClient;
+import org.sonar.wsclient.base.HttpException;
+import org.sonar.wsclient.qualitygate.QualityGateClient;
+import org.sonar.wsclient.qualitygate.QualityGateDetails;
+
+import java.net.HttpURLConnection;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class QualityGateProviderTest {
+
+ @Mock
+ private Settings settings;
+
+ @Mock
+ private ServerClient client;
+
+ @Mock
+ private QualityGateClient qualityGateClient;
+
+ @Mock
+ private Logger logger;
+
+ @Before
+ public void initMocks() {
+ SonarClient wsClient = mock(SonarClient.class);
+ when(client.wsClient()).thenReturn(wsClient);
+ when(wsClient.qualityGateClient()).thenReturn(qualityGateClient);
+ }
+
+ @Test
+ public void should_load_empty_quality_gate_from_default_settings() {
+ assertThat(new QualityGateProvider().provide(settings, client).conditions()).isEmpty();
+ assertThat(new QualityGateProvider().init(settings, client, logger).isEnabled()).isFalse();
+ verify(logger).info("No quality gate is configured.");
+ }
+
+ @Test
+ public void should_load_quality_gate_using_name() {
+ String qGateName = "Sonar way";
+ when(settings.getString("sonar.qualitygate")).thenReturn(qGateName);
+ QualityGateDetails qGate = mock(QualityGateDetails.class);
+ when(qualityGateClient.show(qGateName)).thenReturn(qGate);
+ when(qGate.name()).thenReturn(qGateName);
+ QualityGate actualGate = new QualityGateProvider().init(settings, client, logger);
+ assertThat(actualGate.name()).isEqualTo(qGateName);
+ assertThat(actualGate.isEnabled()).isTrue();
+ verify(logger).info("Loaded quality gate '{}'", qGateName);
+ }
+
+ @Test
+ public void should_load_quality_gate_using_id() {
+ long qGateId = 12345L;
+ String qGateName = "Sonar way";
+ when(settings.getString("sonar.qualitygate")).thenReturn(Long.toString(qGateId));
+ QualityGateDetails qGate = mock(QualityGateDetails.class);
+ when(qualityGateClient.show(qGateId)).thenReturn(qGate);
+ when(qGate.name()).thenReturn(qGateName);
+ assertThat(new QualityGateProvider().init(settings, client, logger).name()).isEqualTo(qGateName);
+ verify(logger).info("Loaded quality gate '{}'", qGateName);
+ }
+
+ @Test(expected = MessageException.class)
+ public void should_stop_analysis_if_gate_not_found() {
+ String qGateName = "Sonar way";
+ when(settings.getString("sonar.qualitygate")).thenReturn(qGateName);
+ when(qualityGateClient.show(qGateName)).thenThrow(new HttpException("http://server/api/qualitygates/show?name=Sonar%20way", HttpURLConnection.HTTP_NOT_FOUND));
+ new QualityGateProvider().provide(settings, client);
+ }
+
+ @Test(expected = HttpException.class)
+ public void should_stop_analysis_if_server_error() {
+ String qGateName = "Sonar way";
+ when(settings.getString("sonar.qualitygate")).thenReturn(qGateName);
+ when(qualityGateClient.show(qGateName)).thenThrow(new HttpException("http://server/api/qualitygates/show?name=Sonar%20way", HttpURLConnection.HTTP_NOT_ACCEPTABLE));
+ new QualityGateProvider().provide(settings, client);
+ }
+
+}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateVerifierTest.java b/sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateVerifierTest.java
new file mode 100644
index 00000000000..4a56c238971
--- /dev/null
+++ b/sonar-batch/src/test/java/org/sonar/batch/qualitygate/QualityGateVerifierTest.java
@@ -0,0 +1,100 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube 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 GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.batch.qualitygate;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.batch.DecoratorBarriers;
+import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.i18n.I18n;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Measure;
+import org.sonar.api.resources.Project;
+import org.sonar.api.resources.Resource;
+import org.sonar.core.timemachine.Periods;
+
+import java.util.Locale;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class QualityGateVerifierTest {
+
+ QualityGateVerifier verifier;
+ DecoratorContext context;
+ QualityGate qualityGate;
+
+ Measure measureClasses;
+ Measure measureCoverage;
+ Measure measureComplexity;
+ Resource project;
+ Snapshot snapshot;
+ Periods periods;
+ I18n i18n;
+
+ @Before
+ public void before() {
+ context = mock(DecoratorContext.class);
+ periods = mock(Periods.class);
+ i18n = mock(I18n.class);
+ when(i18n.message(any(Locale.class), eq("variation"), eq("variation"))).thenReturn("variation");
+
+ measureClasses = new Measure(CoreMetrics.CLASSES, 20d);
+ measureCoverage = new Measure(CoreMetrics.COVERAGE, 35d);
+ measureComplexity = new Measure(CoreMetrics.COMPLEXITY, 50d);
+
+ when(context.getMeasure(CoreMetrics.CLASSES)).thenReturn(measureClasses);
+ when(context.getMeasure(CoreMetrics.COVERAGE)).thenReturn(measureCoverage);
+ when(context.getMeasure(CoreMetrics.COMPLEXITY)).thenReturn(measureComplexity);
+
+ snapshot = mock(Snapshot.class);
+ qualityGate = mock(QualityGate.class);
+ when(qualityGate.isEnabled()).thenReturn(true);
+ verifier = new QualityGateVerifier(qualityGate);
+ project = new Project("foo");
+ }
+
+ @Test
+ public void should_be_executed_if_quality_gate_is_enabled() throws Exception {
+ assertThat(verifier.shouldExecuteOnProject((Project) project)).isTrue();
+ when(qualityGate.isEnabled()).thenReturn(false);
+ assertThat(verifier.shouldExecuteOnProject((Project) project)).isFalse();
+ }
+
+ @Test
+ public void test_toString() {
+ assertThat(verifier.toString()).isEqualTo("QualityGateVerifier");
+ }
+
+ @Test
+ public void generates_quality_gates_status() {
+ assertThat(verifier.generatesQualityGateStatus()).isEqualTo(CoreMetrics.QUALITY_GATE_STATUS);
+ }
+
+ @Test
+ public void depends_on_variations() {
+ assertThat(verifier.dependsOnVariations()).isEqualTo(DecoratorBarriers.END_OF_TIME_MACHINE);
+ }
+
+}