]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14372 move Azure alm settings WS
authorZipeng WU <zipeng.wu@sonarsource.com>
Fri, 29 Jan 2021 11:16:34 +0000 (12:16 +0100)
committersonartech <sonartech@sonarsource.com>
Thu, 4 Feb 2021 20:07:07 +0000 (20:07 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/AlmSettingsWsModule.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/CreateAzureAction.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateAzureAction.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/AlmSettingsWsModuleTest.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/CreateAzureActionTest.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/UpdateAzureActionTest.java [new file with mode: 0644]
server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java

diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/AlmSettingsWsModule.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/AlmSettingsWsModule.java
new file mode 100644 (file)
index 0000000..ca593a0
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.sonar.server.almsettings.ws;
+
+import org.sonar.core.platform.Module;
+
+public class AlmSettingsWsModule extends Module {
+  @Override
+  protected void configureModule() {
+    add(
+      AlmSettingsWs.class,
+      AlmSettingsSupport.class,
+      DeleteAction.class,
+      ListAction.class,
+      ListDefinitionsAction.class,
+      //Azure alm settings,
+      CreateAzureAction.class,
+      UpdateAzureAction.class
+    );
+  }
+}
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/CreateAzureAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/CreateAzureAction.java
new file mode 100644 (file)
index 0000000..077096a
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.sonar.server.almsettings.ws;
+
+import org.sonar.api.server.ws.Change;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.server.user.UserSession;
+
+import static org.sonar.db.alm.setting.ALM.AZURE_DEVOPS;
+import static org.sonar.server.projectlink.ws.ProjectLinksWsParameters.PARAM_URL;
+
+public class CreateAzureAction implements AlmSettingsWsAction {
+
+  private static final String PARAM_KEY = "key";
+  private static final String PARAM_PERSONAL_ACCESS_TOKEN = "personalAccessToken";
+
+  private final DbClient dbClient;
+  private UserSession userSession;
+  private final AlmSettingsSupport almSettingsSupport;
+
+  public CreateAzureAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) {
+    this.dbClient = dbClient;
+    this.userSession = userSession;
+    this.almSettingsSupport = almSettingsSupport;
+  }
+
+  @Override
+  public void define(WebService.NewController context) {
+    WebService.NewAction action = context.createAction("create_azure")
+      .setDescription("Create Azure ALM instance Setting. <br/>" +
+        "Requires the 'Administer System' permission")
+      .setPost(true)
+      .setSince("8.1")
+      .setChangelog(new Change("8.6", "Parameter 'URL' was added"))
+      .setHandler(this);
+
+    action.createParam(PARAM_KEY)
+      .setRequired(true)
+      .setMaximumLength(200)
+      .setDescription("Unique key of the Azure Devops instance setting");
+    action.createParam(PARAM_PERSONAL_ACCESS_TOKEN)
+      .setRequired(true)
+      .setMaximumLength(2000)
+      .setDescription("Azure Devops personal access token");
+    action.createParam(PARAM_URL)
+      .setRequired(true)
+      .setMaximumLength(2000)
+      .setDescription("Azure API URL");
+  }
+
+  @Override
+  public void handle(Request request, Response response) {
+    userSession.checkIsSystemAdministrator();
+    doHandle(request);
+    response.noContent();
+  }
+
+  private void doHandle(Request request) {
+    String key = request.mandatoryParam(PARAM_KEY);
+    String pat = request.mandatoryParam(PARAM_PERSONAL_ACCESS_TOKEN);
+    String url = request.mandatoryParam(PARAM_URL);
+
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      almSettingsSupport.checkAlmMultipleFeatureEnabled(AZURE_DEVOPS);
+      almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, key);
+      dbClient.almSettingDao().insert(dbSession, new AlmSettingDto()
+        .setAlm(AZURE_DEVOPS)
+        .setKey(key)
+        .setPersonalAccessToken(pat)
+        .setUrl(url));
+      dbSession.commit();
+    }
+  }
+
+}
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateAzureAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almsettings/ws/UpdateAzureAction.java
new file mode 100644 (file)
index 0000000..036122a
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.sonar.server.almsettings.ws;
+
+import org.sonar.api.server.ws.Change;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.server.user.UserSession;
+
+import static org.apache.commons.lang.StringUtils.isNotBlank;
+import static org.sonar.server.projectlink.ws.ProjectLinksWsParameters.PARAM_URL;
+
+public class UpdateAzureAction implements AlmSettingsWsAction {
+
+  private static final String PARAM_KEY = "key";
+  private static final String PARAM_NEW_KEY = "newKey";
+  private static final String PARAM_PERSONAL_ACCESS_TOKEN = "personalAccessToken";
+
+  private final DbClient dbClient;
+  private UserSession userSession;
+  private final AlmSettingsSupport almSettingsSupport;
+
+  public UpdateAzureAction(DbClient dbClient, UserSession userSession, AlmSettingsSupport almSettingsSupport) {
+    this.dbClient = dbClient;
+    this.userSession = userSession;
+    this.almSettingsSupport = almSettingsSupport;
+  }
+
+  @Override
+  public void define(WebService.NewController context) {
+    WebService.NewAction action = context.createAction("update_azure")
+      .setDescription("Update Azure ALM instance Setting. <br/>" +
+        "Requires the 'Administer System' permission")
+      .setPost(true)
+      .setSince("8.1")
+      .setChangelog(new Change("8.6", "Parameter 'URL' was added"),
+        new Change("8.7", String.format("Parameter '%s' is no longer required", PARAM_PERSONAL_ACCESS_TOKEN)))
+      .setHandler(this);
+
+    action.createParam(PARAM_KEY)
+      .setRequired(true)
+      .setMaximumLength(200)
+      .setDescription("Unique key of the Azure instance setting");
+    action.createParam(PARAM_NEW_KEY)
+      .setRequired(false)
+      .setMaximumLength(200)
+      .setDescription("Optional new value for an unique key of the Azure Devops instance setting");
+    action.createParam(PARAM_PERSONAL_ACCESS_TOKEN)
+      .setRequired(false)
+      .setMaximumLength(2000)
+      .setDescription("Azure Devops personal access token");
+    action.createParam(PARAM_URL)
+      .setRequired(true)
+      .setMaximumLength(2000)
+      .setDescription("Azure API URL");
+  }
+
+  @Override
+  public void handle(Request request, Response response) throws Exception {
+    userSession.checkIsSystemAdministrator();
+    doHandle(request);
+    response.noContent();
+  }
+
+  private void doHandle(Request request) {
+    String key = request.mandatoryParam(PARAM_KEY);
+    String newKey = request.param(PARAM_NEW_KEY);
+    String pat = request.param(PARAM_PERSONAL_ACCESS_TOKEN);
+    String url = request.mandatoryParam(PARAM_URL);
+
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      AlmSettingDto almSettingDto = almSettingsSupport.getAlmSetting(dbSession, key);
+      if (isNotBlank(newKey) && !newKey.equals(key)) {
+        almSettingsSupport.checkAlmSettingDoesNotAlreadyExist(dbSession, newKey);
+      }
+      dbClient.almSettingDao().update(dbSession, almSettingDto
+        .setKey(isNotBlank(newKey) ? newKey : key)
+        .setPersonalAccessToken(isNotBlank(pat) ? pat : almSettingDto.getPersonalAccessToken())
+        .setUrl(url));
+      dbSession.commit();
+    }
+  }
+
+}
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/AlmSettingsWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/AlmSettingsWsModuleTest.java
new file mode 100644 (file)
index 0000000..39e1a86
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.sonar.server.almsettings.ws;
+
+import org.junit.Test;
+import org.sonar.core.platform.ComponentContainer;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.core.platform.ComponentContainer.COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER;
+
+public class AlmSettingsWsModuleTest {
+
+  @Test
+  public void verify_count_of_added_components() {
+    ComponentContainer container = new ComponentContainer();
+    new AlmSettingsWsModule().configure(container);
+    assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 7);
+  }
+
+}
\ No newline at end of file
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/CreateAzureActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/CreateAzureActionTest.java
new file mode 100644 (file)
index 0000000..ad9fb29
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.sonar.server.almsettings.ws;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbTester;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.db.user.UserDto;
+import org.sonar.server.almsettings.MultipleAlmFeatureProvider;
+import org.sonar.server.component.ComponentFinder;
+import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.groups.Tuple.tuple;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class CreateAzureActionTest {
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone();
+  @Rule
+  public DbTester db = DbTester.create();
+
+  private MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class);
+
+  private WsActionTester ws = new WsActionTester(new CreateAzureAction(db.getDbClient(), userSession,
+    new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
+      multipleAlmFeatureProvider)));
+
+  @Before
+  public void before() {
+    when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
+  }
+
+  @Test
+  public void create() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+
+    ws.newRequest()
+      .setParam("key", "Azure Server - Dev Team")
+      .setParam("personalAccessToken", "98765432100")
+      .setParam("url", "https://ado.sonarqube.com/")
+      .execute();
+
+    assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
+      .extracting(AlmSettingDto::getKey, AlmSettingDto::getPersonalAccessToken, AlmSettingDto::getUrl)
+      .containsOnly(tuple("Azure Server - Dev Team", "98765432100", "https://ado.sonarqube.com/"));
+  }
+
+  @Test
+  public void fail_when_key_is_already_used() {
+    when(multipleAlmFeatureProvider.enabled()).thenReturn(true);
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+    AlmSettingDto azureAlmSetting = db.almSettings().insertAzureAlmSetting();
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage(String.format("An ALM setting with key '%s' already exist", azureAlmSetting.getKey()));
+
+    ws.newRequest()
+      .setParam("key", azureAlmSetting.getKey())
+      .setParam("personalAccessToken", "98765432100")
+      .setParam("url", "https://ado.sonarqube.com/")
+      .execute();
+  }
+
+  @Test
+  public void fail_when_no_multiple_instance_allowed() {
+    when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+    db.almSettings().insertAzureAlmSetting();
+
+    expectedException.expect(BadRequestException.class);
+    expectedException.expectMessage("A AZURE_DEVOPS setting is already defined");
+
+    ws.newRequest()
+      .setParam("key", "key")
+      .setParam("personalAccessToken", "98765432100")
+      .setParam("url", "https://ado.sonarqube.com/")
+      .execute();
+  }
+
+  @Test
+  public void fail_when_missing_administer_system_permission() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user);
+
+    expectedException.expect(ForbiddenException.class);
+
+    ws.newRequest()
+      .setParam("key", "Azure Server - Dev Team")
+      .setParam("personalAccessToken", "98765432100")
+      .execute();
+  }
+
+  @Test
+  public void definition() {
+    WebService.Action def = ws.getDef();
+
+    assertThat(def.since()).isEqualTo("8.1");
+    assertThat(def.isPost()).isTrue();
+    assertThat(def.params())
+      .extracting(WebService.Param::key, WebService.Param::isRequired)
+      .containsExactlyInAnyOrder(tuple("key", true), tuple("personalAccessToken", true), tuple("url", true));
+  }
+}
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/UpdateAzureActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almsettings/ws/UpdateAzureActionTest.java
new file mode 100644 (file)
index 0000000..9d49088
--- /dev/null
@@ -0,0 +1,166 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.sonar.server.almsettings.ws;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbTester;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.db.user.UserDto;
+import org.sonar.server.almsettings.MultipleAlmFeatureProvider;
+import org.sonar.server.component.ComponentFinder;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.WsActionTester;
+
+import static java.lang.String.format;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.groups.Tuple.tuple;
+import static org.mockito.Mockito.mock;
+
+public class UpdateAzureActionTest {
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone();
+  @Rule
+  public DbTester db = DbTester.create();
+
+  private static String AZURE_URL = "https://ado.sonarqube.com/";
+
+  private WsActionTester ws = new WsActionTester(new UpdateAzureAction(db.getDbClient(), userSession,
+    new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
+      mock(MultipleAlmFeatureProvider.class))));
+
+  @Test
+  public void update() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+
+    AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting();
+
+    ws.newRequest()
+      .setParam("key", almSettingDto.getKey())
+      .setParam("personalAccessToken", "10987654321")
+      .setParam("url", AZURE_URL)
+      .execute();
+    assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
+      .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getPersonalAccessToken)
+      .containsOnly(tuple(almSettingDto.getKey(), AZURE_URL, "10987654321"));
+  }
+
+  @Test
+  public void update_with_new_key() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+
+    AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting();
+
+    ws.newRequest()
+      .setParam("key", almSettingDto.getKey())
+      .setParam("newKey", "Azure Server - Infra Team")
+      .setParam("personalAccessToken", "0123456789")
+      .setParam("url", AZURE_URL)
+      .execute();
+    assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
+      .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getPersonalAccessToken)
+      .containsOnly(tuple("Azure Server - Infra Team", AZURE_URL, "0123456789"));
+  }
+
+  @Test
+  public void update_without_pat() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+
+    AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting();
+
+    ws.newRequest()
+      .setParam("key", almSettingDto.getKey())
+      .setParam("url", AZURE_URL)
+      .execute();
+    assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
+      .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getPersonalAccessToken)
+      .containsOnly(tuple(almSettingDto.getKey(), AZURE_URL, almSettingDto.getPersonalAccessToken()));
+  }
+
+  @Test
+  public void fail_when_key_does_not_match_existing_alm_setting() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+
+    expectedException.expect(NotFoundException.class);
+    expectedException.expectMessage("ALM setting with key 'unknown' cannot be found");
+
+    ws.newRequest()
+      .setParam("key", "unknown")
+      .setParam("personalAccessToken", "0123456789")
+      .setParam("url", AZURE_URL)
+      .execute();
+  }
+
+  @Test
+  public void fail_when_new_key_matches_existing_alm_setting() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).setSystemAdministrator();
+    AlmSettingDto almSetting1 = db.almSettings().insertAzureAlmSetting();
+    AlmSettingDto almSetting2 = db.almSettings().insertAzureAlmSetting();
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage(format("An ALM setting with key '%s' already exists", almSetting2.getKey()));
+
+    ws.newRequest()
+      .setParam("key", almSetting1.getKey())
+      .setParam("newKey", almSetting2.getKey())
+      .setParam("personalAccessToken", "0123456789")
+      .setParam("url", AZURE_URL)
+      .execute();
+  }
+
+  @Test
+  public void fail_when_missing_administer_system_permission() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user);
+    AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting();
+
+    expectedException.expect(ForbiddenException.class);
+
+    ws.newRequest()
+      .setParam("key", almSettingDto.getKey())
+      .setParam("newKey", "Azure Server - Infra Team")
+      .setParam("personalAccessToken", "0123456789")
+      .execute();
+  }
+
+  @Test
+  public void definition() {
+    WebService.Action def = ws.getDef();
+
+    assertThat(def.since()).isEqualTo("8.1");
+    assertThat(def.isPost()).isTrue();
+    assertThat(def.params())
+      .extracting(WebService.Param::key, WebService.Param::isRequired)
+      .containsExactlyInAnyOrder(tuple("key", true), tuple("newKey", false), tuple("personalAccessToken", false), tuple("url", true));
+  }
+
+}
index a2e1dafc76d4e2b6671e9c861276d8befff4d177..039039fc8f089887fb9386020cad4760f3b0be44 100644 (file)
@@ -48,11 +48,7 @@ import org.sonar.core.platform.PlatformEditionProvider;
 import org.sonar.server.almintegration.ws.AlmIntegrationsWSModule;
 import org.sonar.server.almintegration.ws.ImportHelper;
 import org.sonar.server.almsettings.MultipleAlmFeatureProvider;
-import org.sonar.server.almsettings.ws.AlmSettingsSupport;
-import org.sonar.server.almsettings.ws.AlmSettingsWs;
-import org.sonar.server.almsettings.ws.DeleteAction;
-import org.sonar.server.almsettings.ws.ListAction;
-import org.sonar.server.almsettings.ws.ListDefinitionsAction;
+import org.sonar.server.almsettings.ws.AlmSettingsWsModule;
 import org.sonar.server.authentication.AuthenticationModule;
 import org.sonar.server.authentication.DefaultAdminCredentialsVerifierNotificationHandler;
 import org.sonar.server.authentication.DefaultAdminCredentialsVerifierNotificationTemplate;
@@ -508,11 +504,7 @@ public class PlatformLevel4 extends PlatformLevel {
       AlmIntegrationsWSModule.class,
 
       // ALM settings
-      AlmSettingsWs.class,
-      AlmSettingsSupport.class,
-      DeleteAction.class,
-      ListAction.class,
-      ListDefinitionsAction.class,
+      AlmSettingsWsModule.class,
 
       // Branch
       BranchFeatureProxyImpl.class,