Licenses { public String getLicense(Edition edition, Version version) { if (!version.isGreaterThanOrEquals(7, 9)) { throw new IllegalArgumentException(String.format("Commercial licenses of SonarQube %s are no longer supported", version)); } return licensesPerEdition.computeIfAbsent(edition, e -> { String filename; switch (e) { case DEVELOPER: filename = "de.txt"; break; case ENTERPRISE: filename = "ee.txt"; break; case DATACENTER: filename = "dce.txt"; break; default: throw new IllegalStateException("License does not exist for edition " + e); } return download(baseUrl + "master/edition_testing/" + filename); }); } Licenses(Configuration configuration, String baseUrl);  Licenses(Configuration configuration); String getLicense(Edition edition, Version version);  }
@Test public void getLicense_should_fail_if_version_less_than_7_9() throws Exception { Licenses underTest = newLicenses(true); Throwable thrown = catchThrowable(() -> underTest.getLicense(Edition.DEVELOPER, Version.create("7.2.0.10000"))); assertThat(thrown).hasMessage("Commercial licenses of SonarQube 7.2.0.10000 are no longer supported").isInstanceOf(IllegalArgumentException.class); thrown = catchThrowable(() -> underTest.getLicense(Edition.DEVELOPER, Version.create("6.7"))); assertThat(thrown).hasMessage("Commercial licenses of SonarQube 6.7 are no longer supported").isInstanceOf(IllegalArgumentException.class); }
@Test public void download_edition_license_and_remove_header_if_present() { Licenses underTest = newLicenses(true); github.enqueue(new MockResponse().setBody("-----\nheader\n----\nabcde\n\r\n")); Version version = Version.create("7.9.0.10000"); assertThat(underTest.getLicense(Edition.DEVELOPER, version)).isEqualTo("abcde"); }
@Test public void fail_in_license_not_found() { github.enqueue(new MockResponse().setResponseCode(404)); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Fail to download license. URL [http: newLicenses(true).getLicense(Edition.DEVELOPER, Version.create("8.0.0.10000")); }
@Test public void fail_if_connection_failure() throws Exception { github.shutdown(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Can not call http: newLicenses(true).getLicense(Edition.DEVELOPER, Version.create("8.0.0.10000")); }
@Test public void fail_if_github_token_is_not_defined() { expectedException.expect(RuntimeException.class); expectedException.expectMessage("Please provide your GitHub token with the property github.token or the env variable GITHUB_TOKEN"); newLicenses(false).getLicense(Edition.DEVELOPER, Version.create("8.0.0.10000")); }