From 380816fec9f40f8d2c25c3f08be2d783b7293226 Mon Sep 17 00:00:00 2001
From: Teryk Bellahsene <teryk.bellahsene@sonarsource.com>
Date: Thu, 1 Jun 2017 17:27:41 +0200
Subject: SONAR-9303 IT to test built-in profile cannot be modified

---
 .../qualityprofile/ActivateRuleWsRequest.java      |  3 +-
 .../ws/client/qualityprofile/CopyRequest.java      | 41 ++++++++++++++++++++++
 .../ws/client/qualityprofile/DeleteRequest.java    | 33 +++++++++++++++++
 .../qualityprofile/QualityProfileWsParameters.java |  5 +++
 .../qualityprofile/QualityProfilesService.java     | 28 +++++++++++++++
 .../client/qualityprofile/SetDefaultRequest.java   | 33 +++++++++++++++++
 .../src/main/protobuf/ws-qualityprofiles.proto     | 11 ++++++
 .../qualityprofile/QualityProfilesServiceTest.java | 40 ++++++++++++++++++++-
 8 files changed, 192 insertions(+), 2 deletions(-)
 create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/CopyRequest.java
 create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/DeleteRequest.java
 create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/SetDefaultRequest.java

(limited to 'sonar-ws/src')

diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/ActivateRuleWsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/ActivateRuleWsRequest.java
index 0bd921bc3d8..368f8512018 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/ActivateRuleWsRequest.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/ActivateRuleWsRequest.java
@@ -81,8 +81,9 @@ public class ActivateRuleWsRequest {
     private Builder() {
     }
 
-    public void setOrganization(@Nullable String organization) {
+    public Builder setOrganization(@Nullable String organization) {
       this.organization = Optional.ofNullable(organization);
+      return this;
     }
 
     public Builder setParams(@Nullable String params) {
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/CopyRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/CopyRequest.java
new file mode 100644
index 00000000000..39fdb48a3c9
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/CopyRequest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.ws.client.qualityprofile;
+
+import static java.util.Objects.requireNonNull;
+
+public class CopyRequest {
+  private final String fromKey;
+  private final String toName;
+
+  public CopyRequest(String fromKey, String toName) {
+    this.fromKey = requireNonNull(fromKey);
+    this.toName = requireNonNull(toName);
+  }
+
+  public String getFromKey() {
+    return fromKey;
+  }
+
+  public String getToName() {
+    return toName;
+  }
+}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/DeleteRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/DeleteRequest.java
new file mode 100644
index 00000000000..3eaa8e287ae
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/DeleteRequest.java
@@ -0,0 +1,33 @@
+/*
+ * 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.ws.client.qualityprofile;
+
+public class DeleteRequest {
+  private final String profileKey;
+
+  public DeleteRequest(String profileKey) {
+    this.profileKey = profileKey;
+  }
+
+  public String getProfileKey() {
+    return profileKey;
+  }
+}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java
index 5c25a3cdc92..c054f13eecc 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfileWsParameters.java
@@ -42,6 +42,9 @@ public class QualityProfileWsParameters {
   public static final String ACTION_ADD_PROJECT = "add_project";
   public static final String ACTION_REMOVE_PROJECT = "remove_project";
   public static final String ACTION_CREATE = "create";
+  public static final String ACTION_COPY = "copy";
+  public static final String ACTION_SET_DEFAULT = "set_default";
+  public static final String ACTION_DELETE = "delete";
 
   public static final String PARAM_ORGANIZATION = "organization";
   public static final String PARAM_DEFAULTS = "defaults";
@@ -50,6 +53,8 @@ public class QualityProfileWsParameters {
   public static final String PARAM_PROFILE_KEY = "profileKey";
   public static final String PARAM_PROJECT_KEY = "projectKey";
   public static final String PARAM_PROJECT_UUID = "projectUuid";
+  public static final String PARAM_FROM_KEY = "fromKey";
+  public static final String PARAM_TO_NAME = "toName";
 
   private QualityProfileWsParameters() {
     // Only static stuff
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java
index 9d3cf61c216..5fe3a705b16 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesService.java
@@ -20,6 +20,7 @@
 package org.sonarqube.ws.client.qualityprofile;
 
 import org.sonarqube.ws.MediaTypes;
+import org.sonarqube.ws.QualityProfiles.CopyWsResponse;
 import org.sonarqube.ws.QualityProfiles.CreateWsResponse;
 import org.sonarqube.ws.QualityProfiles.SearchWsResponse;
 import org.sonarqube.ws.client.BaseService;
@@ -29,19 +30,24 @@ import org.sonarqube.ws.client.WsConnector;
 
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_ACTIVATE_RULE;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_ADD_PROJECT;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_COPY;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_CREATE;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_DELETE;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_REMOVE_PROJECT;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_RESTORE;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_SEARCH;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ACTION_SET_DEFAULT;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.ActivateActionParameters;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.CONTROLLER_QUALITY_PROFILES;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_DEFAULTS;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_FROM_KEY;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_ORGANIZATION;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE_KEY;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE_NAME;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT_KEY;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT_UUID;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TO_NAME;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.RestoreActionParameters.PARAM_BACKUP;
 
 public class QualityProfilesService extends BaseService {
@@ -104,4 +110,26 @@ public class QualityProfilesService extends BaseService {
       .setParam(PARAM_PROFILE_NAME, request.getProfileName());
     return call(postRequest, CreateWsResponse.parser());
   }
+
+  public CopyWsResponse copy(CopyRequest request) {
+    PostRequest postRequest = new PostRequest(path(ACTION_COPY))
+      .setParam(PARAM_FROM_KEY, request.getFromKey())
+      .setParam(PARAM_TO_NAME, request.getToName());
+
+    return call(postRequest, CopyWsResponse.parser());
+  }
+
+  public void setDefault(SetDefaultRequest request) {
+    PostRequest postRequest = new PostRequest(path(ACTION_SET_DEFAULT))
+      .setParam(PARAM_PROFILE_KEY, request.getProfileKey());
+
+    call(postRequest);
+  }
+
+  public void delete(DeleteRequest request) {
+    PostRequest postRequest = new PostRequest(path(ACTION_DELETE))
+      .setParam(PARAM_PROFILE_KEY, request.getProfileKey());
+
+    call(postRequest);
+  }
 }
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/SetDefaultRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/SetDefaultRequest.java
new file mode 100644
index 00000000000..fd11fd24e92
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/SetDefaultRequest.java
@@ -0,0 +1,33 @@
+/*
+ * 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.ws.client.qualityprofile;
+
+public class SetDefaultRequest {
+  private final String profileKey;
+
+  public SetDefaultRequest(String profileKey) {
+    this.profileKey = profileKey;
+  }
+
+  public String getProfileKey() {
+    return profileKey;
+  }
+}
diff --git a/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto b/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto
index 9c5c9acaeb0..a6f2df7b39a 100644
--- a/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto
+++ b/sonar-ws/src/main/protobuf/ws-qualityprofiles.proto
@@ -88,3 +88,14 @@ message InheritanceWsResponse {
     optional bool isBuiltIn = 6;
   }
 }
+
+// WS api/qualityprofiles/copy
+message CopyWsResponse {
+  optional string key = 1;
+  optional string name = 2;
+  optional string language = 3;
+  optional string languageName = 4;
+  optional bool isDefault = 5;
+  optional bool isInherited = 6;
+  optional string parentKey = 7;
+}
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesServiceTest.java
index b50ee4a6259..411d4662779 100644
--- a/sonar-ws/src/test/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesServiceTest.java
+++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesServiceTest.java
@@ -29,9 +29,12 @@ import org.sonarqube.ws.client.WsConnector;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_DEFAULTS;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_FROM_KEY;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE_KEY;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROFILE_NAME;
 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_PROJECT_KEY;
+import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TO_NAME;
 
 public class QualityProfilesServiceTest {
 
@@ -51,6 +54,7 @@ public class QualityProfilesServiceTest {
 
     assertThat(serviceTester.getGetParser()).isSameAs(QualityProfiles.SearchWsResponse.parser());
     serviceTester.assertThat(getRequest)
+      .hasPath("search")
       .hasParam(PARAM_DEFAULTS, true)
       .hasParam(PARAM_PROJECT_KEY, "project")
       .hasParam(PARAM_LANGUAGE, "language")
@@ -67,6 +71,7 @@ public class QualityProfilesServiceTest {
       .build());
 
     serviceTester.assertThat(serviceTester.getPostRequest())
+      .hasPath("add_project")
       .hasParam(PARAM_LANGUAGE, "xoo")
       .hasParam(PARAM_PROFILE_NAME, "Sonar Way")
       .hasParam(PARAM_PROJECT_KEY, "sample")
@@ -82,6 +87,7 @@ public class QualityProfilesServiceTest {
       .build());
 
     serviceTester.assertThat(serviceTester.getPostRequest())
+      .hasPath("remove_project")
       .hasParam(PARAM_LANGUAGE, "xoo")
       .hasParam(PARAM_PROFILE_NAME, "Sonar Way")
       .hasParam(PARAM_PROJECT_KEY, "sample")
@@ -89,15 +95,47 @@ public class QualityProfilesServiceTest {
   }
 
   @Test
-  public void create_project() throws Exception {
+  public void create() throws Exception {
     underTest.create(CreateRequest.builder()
       .setLanguage("xoo")
       .setProfileName("Sonar Way")
       .build());
 
     serviceTester.assertThat(serviceTester.getPostRequest())
+      .hasPath("create")
       .hasParam(PARAM_LANGUAGE, "xoo")
       .hasParam(PARAM_PROFILE_NAME, "Sonar Way")
       .andNoOtherParam();
   }
+
+  @Test
+  public void copy() throws Exception {
+    underTest.copy(new CopyRequest("fromKey", "My Sonar Way"));
+
+    serviceTester.assertThat(serviceTester.getPostRequest())
+      .hasPath("copy")
+      .hasParam(PARAM_FROM_KEY, "fromKey")
+      .hasParam(PARAM_TO_NAME, "My Sonar Way")
+      .andNoOtherParam();
+  }
+
+  @Test
+  public void set_default() {
+    underTest.setDefault(new SetDefaultRequest("sample"));
+
+    serviceTester.assertThat(serviceTester.getPostRequest())
+      .hasPath("set_default")
+      .hasParam(PARAM_PROFILE_KEY, "sample")
+      .andNoOtherParam();
+  }
+
+  @Test
+  public void delete() {
+    underTest.delete(new DeleteRequest("sample"));
+
+    serviceTester.assertThat(serviceTester.getPostRequest())
+      .hasPath("delete")
+      .hasParam(PARAM_PROFILE_KEY, "sample")
+      .andNoOtherParam();
+  }
 }
-- 
cgit v1.2.3