aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2017-09-05 14:51:31 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2017-09-06 09:43:42 +0200
commit8451b5ffbc0a75bc88520698f3713030f9981bdb (patch)
tree9af8bcdbae7a9e02de2fd99c565c54998c55bec2 /tests/src
parentd06fdc6045943e9833c1dddbb05a3071ef7bae79 (diff)
downloadsonarqube-8451b5ffbc0a75bc88520698f3713030f9981bdb.tar.gz
sonarqube-8451b5ffbc0a75bc88520698f3713030f9981bdb.zip
Add QGateTester to clean quality gates between each IT
Diffstat (limited to 'tests/src')
-rw-r--r--tests/src/test/java/org/sonarqube/tests/QGateTester.java108
-rw-r--r--tests/src/test/java/org/sonarqube/tests/Session.java2
-rw-r--r--tests/src/test/java/org/sonarqube/tests/Tester.java11
3 files changed, 121 insertions, 0 deletions
diff --git a/tests/src/test/java/org/sonarqube/tests/QGateTester.java b/tests/src/test/java/org/sonarqube/tests/QGateTester.java
new file mode 100644
index 00000000000..8bf99d34828
--- /dev/null
+++ b/tests/src/test/java/org/sonarqube/tests/QGateTester.java
@@ -0,0 +1,108 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 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 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.
+ *
+ * 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 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.sonarqube.tests;
+
+import com.google.gson.Gson;
+import com.google.gson.annotations.SerializedName;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.sonarqube.ws.WsProjects.CreateWsResponse.Project;
+import org.sonarqube.ws.WsQualityGates.CreateWsResponse;
+import org.sonarqube.ws.client.GetRequest;
+import org.sonarqube.ws.client.PostRequest;
+import org.sonarqube.ws.client.qualitygate.QualityGatesService;
+import org.sonarqube.ws.client.qualitygate.SelectWsRequest;
+
+public class QGateTester {
+ private static final AtomicInteger ID_GENERATOR = new AtomicInteger();
+
+ private final Session session;
+
+ QGateTester(Session session) {
+ this.session = session;
+ }
+
+ public QualityGatesService service() {
+ return session.wsClient().qualityGates();
+ }
+
+ void deleteAll() {
+ String json = session.wsClient().wsConnector().call(new GetRequest("api/qualitygates/list")).failIfNotSuccessful().content();
+ ListResponse response = ListResponse.parse(json);
+ response.getQualityGates().stream()
+ .filter(qualityGate -> !qualityGate.getId().equals(response.getDefault()))
+ .forEach(qualityGate -> session.wsClient().wsConnector().call(new PostRequest("api/qualitygates/destroy").setParam("id", qualityGate.getId())).failIfNotSuccessful());
+ }
+
+ public CreateWsResponse generate() {
+ int id = ID_GENERATOR.getAndIncrement();
+ return session.wsClient().qualityGates().create("QualityGate" + id);
+ }
+
+ public void associateProject(CreateWsResponse qualityGate, Project project){
+ service().associateProject(new SelectWsRequest().setGateId(qualityGate.getId()).setProjectKey(project.getKey()));
+ }
+
+ public static class ListResponse {
+
+ @SerializedName("default")
+ private final String defaultQGate;
+ @SerializedName("qualitygates")
+ private final List<QGate> qualityGates = new ArrayList<>();
+
+ public ListResponse(String defaultQGate) {
+ this.defaultQGate = defaultQGate;
+ }
+
+ public List<QGate> getQualityGates() {
+ return qualityGates;
+ }
+
+ public String getDefault() {
+ return defaultQGate;
+ }
+
+ public static ListResponse parse(String json) {
+ Gson gson = new Gson();
+ return gson.fromJson(json, ListResponse.class);
+ }
+
+ public static class QGate {
+ @SerializedName("id")
+ private final String id;
+ @SerializedName("name")
+ private final String name;
+
+ public QGate(String id, String name) {
+ this.id = id;
+ this.name = name;
+ }
+
+ public String getId() {
+ return id;
+ }
+
+ public String getName() {
+ return name;
+ }
+ }
+ }
+}
diff --git a/tests/src/test/java/org/sonarqube/tests/Session.java b/tests/src/test/java/org/sonarqube/tests/Session.java
index ce225da4129..23b0b652d4e 100644
--- a/tests/src/test/java/org/sonarqube/tests/Session.java
+++ b/tests/src/test/java/org/sonarqube/tests/Session.java
@@ -37,4 +37,6 @@ public interface Session {
SettingTester settings();
+ QGateTester qGates();
+
}
diff --git a/tests/src/test/java/org/sonarqube/tests/Tester.java b/tests/src/test/java/org/sonarqube/tests/Tester.java
index 8041625cd45..e0d5d4468d1 100644
--- a/tests/src/test/java/org/sonarqube/tests/Tester.java
+++ b/tests/src/test/java/org/sonarqube/tests/Tester.java
@@ -98,6 +98,7 @@ public class Tester extends ExternalResource implements Session {
users().deleteAll();
projects().deleteAll();
settings().deleteAll();
+ qGates().deleteAll();
}
public Session asAnonymous() {
@@ -185,6 +186,11 @@ public class Tester extends ExternalResource implements Session {
return rootSession.settings();
}
+ @Override
+ public QGateTester qGates() {
+ return rootSession.qGates();
+ }
+
private static class SessionImpl implements Session {
private final WsClient client;
@@ -226,5 +232,10 @@ public class Tester extends ExternalResource implements Session {
public SettingTester settings() {
return new SettingTester(this);
}
+
+ @Override
+ public QGateTester qGates() {
+ return new QGateTester(this);
+ }
}
}