]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10134 Returning protobuf in copy action response
authorGuillaume Jambet <guillaume.jambet@sonarsource.com>
Tue, 5 Dec 2017 14:28:00 +0000 (15:28 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 14 Dec 2017 16:03:35 +0000 (17:03 +0100)
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/CopyAction.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWs.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/CopyActionTest.java

index 2d2ecee9df91750b4a84278d0efc05c2d75d77c6..d91e28783f4c5e61a3e296d02a9f897567ad8f97 100644 (file)
@@ -26,15 +26,16 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.qualitygate.QualityGateDto;
 import org.sonar.server.organization.DefaultOrganizationProvider;
+import org.sonar.server.qualitygate.QualityGateFinder;
 import org.sonar.server.qualitygate.QualityGateUpdater;
 import org.sonar.server.user.UserSession;
 
 import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES;
 import static org.sonar.server.qualitygate.ws.QualityGatesWs.parseId;
-import static org.sonar.server.qualitygate.ws.QualityGatesWs.writeQualityGate;
 import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID;
 import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME;
-import static org.sonar.server.ws.WsUtils.checkFound;
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
+import static org.sonarqube.ws.Qualitygates.QualityGate.newBuilder;
 
 public class CopyAction implements QualityGatesWsAction {
 
@@ -42,12 +43,15 @@ public class CopyAction implements QualityGatesWsAction {
   private final UserSession userSession;
   private final DefaultOrganizationProvider organizationProvider;
   private final QualityGateUpdater qualityGateUpdater;
+  private final QualityGateFinder qualityGateFinder;
 
-  public CopyAction(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider organizationProvider, QualityGateUpdater qualityGateUpdater) {
+  public CopyAction(DbClient dbClient, UserSession userSession, DefaultOrganizationProvider organizationProvider,
+    QualityGateUpdater qualityGateUpdater, QualityGateFinder qualityGateFinder) {
     this.dbClient = dbClient;
     this.userSession = userSession;
     this.organizationProvider = organizationProvider;
     this.qualityGateUpdater = qualityGateUpdater;
+    this.qualityGateFinder = qualityGateFinder;
   }
 
   @Override
@@ -77,15 +81,21 @@ public class CopyAction implements QualityGatesWsAction {
 
     userSession.checkPermission(ADMINISTER_QUALITY_GATES, organizationProvider.get().getUuid());
 
-    QualityGateDto result;
+    QualityGateDto qualityGateDto = doCopy(id, destinationName);
+
+    writeProtobuf(newBuilder()
+      .setId(qualityGateDto.getId())
+      .setName(qualityGateDto.getName())
+      .build(), request, response);
+  }
+
+  private QualityGateDto doCopy(Long id, String destinationName) {
     try (DbSession dbSession = dbClient.openSession(false)) {
-      QualityGateDto qualityGateDto = dbClient.qualityGateDao().selectById(dbSession, id);
-      checkFound(qualityGateDto, "No quality gate has been found for id %s", (long) id);
-      result = qualityGateUpdater.copy(dbSession, qualityGateDto, destinationName);
+      QualityGateDto qualityGateDto = qualityGateFinder.getById(dbSession, id);
+      QualityGateDto result = qualityGateUpdater.copy(dbSession, qualityGateDto, destinationName);
       dbSession.commit();
+      return result;
     }
-
-    writeQualityGate(result, response.newJsonWriter()).close();
   }
 
 }
index dbdd165d5c8b0f941f86b889d66ff83d50478526..c3eb5c640636b3c9e99ef98a48d19dacdfc91002 100644 (file)
@@ -22,17 +22,13 @@ package org.sonar.server.qualitygate.ws;
 import org.sonar.api.server.ws.Change;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.WebService;
-import org.sonar.api.utils.text.JsonWriter;
 import org.sonar.db.qualitygate.QualityGateConditionDto;
-import org.sonar.db.qualitygate.QualityGateDto;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.ws.RemovedWebServiceHandler;
 
 import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.CONTROLLER_QUALITY_GATES;
 import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ERROR;
-import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_ID;
 import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_METRIC;
-import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME;
 import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_OPERATOR;
 import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_PERIOD;
 import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_WARNING;
@@ -112,11 +108,4 @@ public class QualityGatesWs implements WebService {
     }
   }
 
-  static JsonWriter writeQualityGate(QualityGateDto qualityGate, JsonWriter writer) {
-    return writer.beginObject()
-      .prop(PARAM_ID, qualityGate.getId())
-      .prop(PARAM_NAME, qualityGate.getName())
-      .endObject();
-  }
-
 }
index 8c6ecdfb068b5f36c7ab8cf081e8e4bdeebe3464..1a8d8c1158c15c5f8f490233549b33553dadd2da 100644 (file)
@@ -35,9 +35,11 @@ import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.organization.TestDefaultOrganizationProvider;
+import org.sonar.server.qualitygate.QualityGateFinder;
 import org.sonar.server.qualitygate.QualityGateUpdater;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsActionTester;
+import org.sonarqube.ws.Qualitygates.QualityGate;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.AssertionsForClassTypes.tuple;
@@ -61,8 +63,9 @@ public class CopyActionTest {
   private DbSession dbSession = db.getSession();
   private TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
   private QualityGateUpdater qualityGateUpdater = new QualityGateUpdater(dbClient, UuidFactoryFast.getInstance());
+  private QualityGateFinder qualityGateFinder = new QualityGateFinder(dbClient);
 
-  private CopyAction underTest = new CopyAction(dbClient, userSession, defaultOrganizationProvider, qualityGateUpdater);
+  private CopyAction underTest = new CopyAction(dbClient, userSession, defaultOrganizationProvider, qualityGateUpdater, qualityGateFinder);
   private WsActionTester ws = new WsActionTester(underTest);
 
   @Test
@@ -130,6 +133,20 @@ public class CopyActionTest {
     assertThat(actual.isBuiltIn()).isFalse();
   }
 
+  @Test
+  public void response_contains_quality_gate() {
+    userSession.addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
+    QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+
+    QualityGate result = ws.newRequest()
+      .setParam(PARAM_ID, qualityGate.getId().toString())
+      .setParam(PARAM_NAME, "new-name")
+      .executeProtobuf(QualityGate.class);
+
+    assertThat(result.getId()).isNotEqualTo(qualityGate.getId());
+    assertThat(result.getName()).isEqualTo("new-name");
+  }
+
   @Test
   public void fail_when_id_parameter_is_missing() {
     userSession.addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());