]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9940 support commit invalid license api/editions/apply_license
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 19 Oct 2017 11:58:14 +0000 (13:58 +0200)
committerGrégoire Aubert <gregoire.aubert@sonarsource.com>
Mon, 23 Oct 2017 15:01:13 +0000 (08:01 -0700)
server/sonar-server/src/main/java/org/sonar/server/edition/ws/ApplyLicenseAction.java
server/sonar-server/src/test/java/org/sonar/server/edition/ws/ApplyLicenseActionTest.java

index de7f62f6d4a102dc9d71e43c7d15d4a00decdc8f..cf37a01ba5adaa1b72bdefeeff2e5c0c195d06f4 100644 (file)
@@ -24,6 +24,7 @@ import javax.annotation.Nullable;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.log.Loggers;
 import org.sonar.server.edition.EditionManagementState;
 import org.sonar.server.edition.License;
 import org.sonar.server.edition.MutableEditionManagementState;
@@ -105,8 +106,13 @@ public class ApplyLicenseAction implements EditionsWsAction {
   }
 
   private void setLicenseWithoutInstall(License newLicense) {
-    editionManagementState.newEditionWithoutInstall(newLicense.getEditionKey());
-    licenseCommit.update(newLicense.getContent());
+    try {
+      licenseCommit.update(newLicense.getContent());
+      editionManagementState.newEditionWithoutInstall(newLicense.getEditionKey());
+    } catch (IllegalArgumentException e) {
+      Loggers.get(ApplyLicenseAction.class).error("Failed to commit license", e);
+      throw BadRequestException.create(e.getMessage());
+    }
   }
 
   private WsEditions.StatusResponse buildResponse() {
index c05b5e75ec28d1582446278e19926bd96e9de8f3..181a980a6fd0ddba9d1931e890f64e8bc6f19690 100644 (file)
@@ -57,6 +57,7 @@ import org.sonarqube.ws.WsEditions.StatusResponse;
 import static java.util.Collections.singleton;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
@@ -225,15 +226,38 @@ public class ApplyLicenseActionTest {
     userSessionRule.logIn().setSystemAdministrator();
     setPendingLicense(NONE);
     when(editionInstaller.requiresInstallationChange(singleton("plugin1"))).thenReturn(false);
+    String base64License = createLicenseParam(PENDING_EDITION_NAME, "plugin1");
     when(webServer.isStandalone()).thenReturn(true);
 
     TestRequest request = actionTester.newRequest()
       .setMediaType(MediaTypes.PROTOBUF)
-      .setParam(PARAM_LICENSE, createLicenseParam(PENDING_EDITION_NAME, "plugin1"));
+      .setParam(PARAM_LICENSE, base64License);
 
     TestResponse response = request.execute();
+
     assertResponse(response, PENDING_EDITION_NAME, "", NONE);
     verify(mutableEditionManagementState).newEditionWithoutInstall(PENDING_EDITION_NAME);
+    verify(licenseCommit).update(base64License);
+  }
+
+  @Test
+  public void execute_throws_BadRequestException_if_license_validation_fails_when_there_is_no_need_to_install() throws IOException {
+    userSessionRule.logIn().setSystemAdministrator();
+    setPendingLicense(NONE, null);
+    when(editionInstaller.requiresInstallationChange(singleton("plugin1"))).thenReturn(false);
+    String base64License = createLicenseParam(PENDING_EDITION_NAME, "plugin1");
+    TestRequest request = actionTester.newRequest()
+      .setMediaType(MediaTypes.PROTOBUF)
+      .setParam(PARAM_LICENSE, base64License);
+    IllegalArgumentException fakeValidationError = new IllegalArgumentException("Faking failed validation of license on update call");
+    doThrow(fakeValidationError)
+      .when(licenseCommit)
+      .update(base64License);
+
+    expectedException.expect(BadRequestException.class);
+    expectedException.expectMessage(fakeValidationError.getMessage());
+
+    request.execute();
   }
 
   @Test