From: Sébastien Lesaint Date: Thu, 19 Oct 2017 11:58:14 +0000 (+0200) Subject: SONAR-9940 support commit invalid license api/editions/apply_license X-Git-Tag: 6.7-RC1~50 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=70c38b312ff6837bf2e470926a1305080f7b4e5a;p=sonarqube.git SONAR-9940 support commit invalid license api/editions/apply_license --- 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