aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-10-19 13:58:14 +0200
committerGrégoire Aubert <gregoire.aubert@sonarsource.com>2017-10-23 08:01:13 -0700
commit70c38b312ff6837bf2e470926a1305080f7b4e5a (patch)
tree355bdc236d43e53e9384d9c0b0a0bf1d3b8bba5d
parent069ffe97ecbdf004348e679f97eb79b1135abe6d (diff)
downloadsonarqube-70c38b312ff6837bf2e470926a1305080f7b4e5a.tar.gz
sonarqube-70c38b312ff6837bf2e470926a1305080f7b4e5a.zip
SONAR-9940 support commit invalid license api/editions/apply_license
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/edition/ws/ApplyLicenseAction.java10
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/edition/ws/ApplyLicenseActionTest.java26
2 files changed, 33 insertions, 3 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/edition/ws/ApplyLicenseAction.java b/server/sonar-server/src/main/java/org/sonar/server/edition/ws/ApplyLicenseAction.java
index de7f62f6d4a..cf37a01ba5a 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/edition/ws/ApplyLicenseAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/edition/ws/ApplyLicenseAction.java
@@ -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() {
diff --git a/server/sonar-server/src/test/java/org/sonar/server/edition/ws/ApplyLicenseActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/edition/ws/ApplyLicenseActionTest.java
index c05b5e75ec2..181a980a6fd 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/edition/ws/ApplyLicenseActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/edition/ws/ApplyLicenseActionTest.java
@@ -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