package org.sonar.server.license.ws;
+import java.util.Collection;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
+import org.sonar.core.util.stream.Collectors;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.property.PropertyDto;
+import org.sonar.server.setting.ws.Setting;
+import org.sonar.server.setting.ws.SettingsFinder;
import org.sonar.server.user.UserSession;
import org.sonar.server.ws.WsAction;
import org.sonarqube.ws.Licenses;
import org.sonarqube.ws.Licenses.ListWsResponse;
-import static com.google.common.collect.Sets.newHashSet;
+import static com.google.common.base.Strings.isNullOrEmpty;
import static org.sonar.api.CoreProperties.PERMANENT_SERVER_ID;
import static org.sonar.api.PropertyType.LICENSE;
import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
-import static org.sonar.core.util.stream.Collectors.toSet;
import static org.sonar.core.util.stream.Collectors.uniqueIndex;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.license.LicensesWsParameters.ACTION_LIST;
private final UserSession userSession;
private final PropertyDefinitions definitions;
private final DbClient dbClient;
+ private final SettingsFinder settingsFinder;
- public ListAction(UserSession userSession, PropertyDefinitions definitions, DbClient dbClient) {
+ public ListAction(UserSession userSession, PropertyDefinitions definitions, DbClient dbClient, SettingsFinder settingsFinder) {
this.userSession = userSession;
this.definitions = definitions;
this.dbClient = dbClient;
+ this.settingsFinder = settingsFinder;
}
@Override
}
private ListWsResponse doHandle(DbSession dbSession) {
- Set<String> licenseSettingsKeys = definitions.getAll().stream()
+ Map<String, PropertyDefinition> licenseDefinitionsByKeys = definitions.getAll().stream()
.filter(definition -> LICENSE.equals(definition.type()))
- .map(PropertyDefinition::key)
- .collect(toSet());
- Set<String> settingsKeys = newHashSet(licenseSettingsKeys);
+ .collect(Collectors.uniqueIndex(PropertyDefinition::key, Function.identity()));
+ Set<String> settingsKeys = new HashSet<>(licenseDefinitionsByKeys.keySet());
settingsKeys.add(PERMANENT_SERVER_ID);
- List<PropertyDto> properties = dbClient.propertiesDao().selectGlobalPropertiesByKeys(dbSession, settingsKeys);
- return new ListResponseBuilder(licenseSettingsKeys, properties).build();
+ List<Setting> settings = settingsFinder.loadGlobalSettings(dbSession, settingsKeys);
+ return new ListResponseBuilder(licenseDefinitionsByKeys, settings).build();
}
private static class ListResponseBuilder {
private final Optional<String> serverId;
- private final Map<String, PropertyDto> licenseSettingsByKey;
- private final Set<String> licenseSettingsKeys;
+ private final Map<String, Setting> licenseSettingsByKey;
+ private final Collection<PropertyDefinition> licenseDefinitions;
- ListResponseBuilder(Set<String> licenseSettingsKeys, List<PropertyDto> properties) {
- this.serverId = getServerId(properties);
- this.licenseSettingsKeys = licenseSettingsKeys;
- this.licenseSettingsByKey = properties.stream().collect(uniqueIndex(PropertyDto::getKey, Function.identity()));
+ ListResponseBuilder(Map<String, PropertyDefinition> licenseDefinitionsByKeys, List<Setting> settings) {
+ this.serverId = getServerId(settings);
+ this.licenseDefinitions = licenseDefinitionsByKeys.values();
+ this.licenseSettingsByKey = settings.stream().collect(uniqueIndex(Setting::getKey, Function.identity()));
}
ListWsResponse build() {
ListWsResponse.Builder wsResponse = ListWsResponse.newBuilder();
- licenseSettingsKeys.forEach(key -> wsResponse.addLicenses(buildLicense(key, licenseSettingsByKey.get(key))));
+ licenseDefinitions.forEach(def -> wsResponse.addLicenses(buildLicense(def, licenseSettingsByKey.get(def.key()))));
return wsResponse.build();
}
- private Licenses.License buildLicense(String key, @Nullable PropertyDto setting) {
- Licenses.License.Builder licenseBuilder = Licenses.License.newBuilder().setKey(key);
+ private Licenses.License buildLicense(PropertyDefinition definition, @Nullable Setting setting) {
+ Licenses.License.Builder licenseBuilder = Licenses.License.newBuilder()
+ .setKey(definition.key());
+ String name = definition.name();
+ if (!isNullOrEmpty(name)) {
+ licenseBuilder.setName(name);
+ }
if (setting != null) {
License license = License.readBase64(setting.getValue());
licenseBuilder.setValue(setting.getValue());
return licenseBuilder.build();
}
- private static void setProduct(Licenses.License.Builder licenseBuilder, License license, PropertyDto setting) {
+ private static void setProduct(Licenses.License.Builder licenseBuilder, License license, Setting setting) {
String product = license.getProduct();
if (product != null) {
licenseBuilder.setProduct(product);
}
}
- private static Optional<String> getServerId(List<PropertyDto> propertyDtos) {
- Optional<PropertyDto> propertyDto = propertyDtos.stream().filter(setting -> setting.getKey().equals(PERMANENT_SERVER_ID)).findFirst();
- return propertyDto.isPresent() ? Optional.of(propertyDto.get().getValue()) : Optional.empty();
+ private static Optional<String> getServerId(List<Setting> settings) {
+ Optional<Setting> setting = settings.stream().filter(s -> s.getKey().equals(PERMANENT_SERVER_ID)).findFirst();
+ return setting.isPresent() ? Optional.of(setting.get().getValue()) : Optional.empty();
}
}
}
import org.sonar.db.DbTester;
import org.sonar.db.property.PropertyDbTester;
import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.setting.ws.SettingsFinder;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.TestRequest;
import org.sonar.server.ws.WsActionTester;
public class ListActionTest {
private static final String LICENSE_KEY_SAMPLE = "sonar.governance.license.secured";
+ private static final String LICENSE_NAME_SAMPLE = "Governance";
private static final String ORGANISATION_SAMPLE = "SonarSource";
private static final String SERVER_ID_SAMPLE = "12345";
private static final String PRODUCT_SAMPLE = "governance";
DbClient dbClient = db.getDbClient();
PropertyDbTester propertyDb = new PropertyDbTester(db);
PropertyDefinitions definitions = new PropertyDefinitions();
+ SettingsFinder settingsFinder = new SettingsFinder(dbClient, definitions);
- WsActionTester ws = new WsActionTester(new ListAction(userSession, definitions, dbClient));
+ WsActionTester ws = new WsActionTester(new ListAction(userSession, definitions, dbClient, settingsFinder));
@Test
public void return_licenses() throws Exception {
setUserAsSystemAdmin();
addServerIdSettings("12345");
String data = createBase64License("SonarSource", "governance", "12345", "2099-01-01", "PRODUCTION", ImmutableMap.of("other", "value"));
- addLicenseSetting("sonar.governance.license.secured", data);
+ addLicenseSetting("sonar.governance.license.secured", "Governance", data);
ListWsResponse result = executeRequest();
assertThat(result.getLicensesList()).hasSize(1);
Licenses.License license = result.getLicenses(0);
assertThat(license.getKey()).isEqualTo("sonar.governance.license.secured");
+ assertThat(license.getName()).isEqualTo("Governance");
assertThat(license.getValue()).isEqualTo(data);
assertThat(license.getProduct()).isEqualTo("governance");
assertThat(license.getOrganization()).isEqualTo("SonarSource");
}
@Test
- public void return_license_with_minimal_info() throws Exception {
+ public void return_information_when_no_licence_set() throws Exception {
setUserAsSystemAdmin();
addServerIdSettings(SERVER_ID_SAMPLE);
- addLicenseSetting(LICENSE_KEY_SAMPLE, toBase64(""));
+ addLicenseSetting(LICENSE_KEY_SAMPLE, null, toBase64(""));
ListWsResponse result = executeRequest();
assertThat(result.getLicensesList()).hasSize(1);
Licenses.License license = result.getLicenses(0);
assertThat(license.getKey()).isEqualTo(LICENSE_KEY_SAMPLE);
+ assertThat(license.hasName()).isFalse();
assertThat(license.getValue()).isEmpty();
assertThat(license.hasProduct()).isFalse();
assertThat(license.hasOrganization()).isFalse();
public void return_license_with_bad_product() throws Exception {
setUserAsSystemAdmin();
addServerIdSettings(SERVER_ID_SAMPLE);
- addLicenseSetting(LICENSE_KEY_SAMPLE, createBase64License(ORGANISATION_SAMPLE, "Other", SERVER_ID_SAMPLE, EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap()));
+ addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE,
+ createBase64License(ORGANISATION_SAMPLE, "Other", SERVER_ID_SAMPLE, EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap()));
ListWsResponse result = executeRequest();
public void return_license_with_bad_server_id() throws Exception {
setUserAsSystemAdmin();
addServerIdSettings(SERVER_ID_SAMPLE);
- addLicenseSetting(LICENSE_KEY_SAMPLE, createBase64License(ORGANISATION_SAMPLE, PRODUCT_SAMPLE, "Other", EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap()));
+ addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE,
+ createBase64License(ORGANISATION_SAMPLE, PRODUCT_SAMPLE, "Other", EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap()));
ListWsResponse result = executeRequest();
@Test
public void does_not_return_invalid_server_id_when_all_servers_accepted_and_no_server_id_setting() throws Exception {
setUserAsSystemAdmin();
- addLicenseSetting(LICENSE_KEY_SAMPLE, createBase64License(ORGANISATION_SAMPLE, PRODUCT_SAMPLE, "*", EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap()));
+ addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE,
+ createBase64License(ORGANISATION_SAMPLE, PRODUCT_SAMPLE, "*", EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap()));
ListWsResponse result = executeRequest();
public void return_license_when_all_servers_are_accepted() throws Exception {
setUserAsSystemAdmin();
addServerIdSettings(SERVER_ID_SAMPLE);
- addLicenseSetting(LICENSE_KEY_SAMPLE, createBase64License(ORGANISATION_SAMPLE, PRODUCT_SAMPLE, "*", EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap()));
+ addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE,
+ createBase64License(ORGANISATION_SAMPLE, PRODUCT_SAMPLE, "*", EXPIRATION_SAMPLE, TYPE_SAMPLE, Collections.emptyMap()));
ListWsResponse result = executeRequest();
public void return_license_when_expired() throws Exception {
setUserAsSystemAdmin();
addServerIdSettings(SERVER_ID_SAMPLE);
- addLicenseSetting(LICENSE_KEY_SAMPLE,
+ addLicenseSetting(LICENSE_KEY_SAMPLE, LICENSE_NAME_SAMPLE,
createBase64License(ORGANISATION_SAMPLE, PRODUCT_SAMPLE, SERVER_ID_SAMPLE, "2010-01-01", TYPE_SAMPLE, Collections.emptyMap()));
ListWsResponse result = executeRequest();
public void test_example_json_response() {
setUserAsSystemAdmin();
addServerIdSettings("12345");
- addLicenseSetting("sonar.governance.license.secured", createBase64License("SonarSource", "governance", "12345", "2099-01-01", "PRODUCTION", ImmutableMap.of("other", "value")));
- addLicenseSetting("sonar.devcockpit.license.secured", createBase64License("Unknown", "other", "54321", "2010-01-01", "EVALUATION", Collections.emptyMap()));
+ addLicenseSetting("sonar.governance.license.secured", "Governance",
+ createBase64License("SonarSource", "governance", "12345", "2099-01-01", "PRODUCTION", ImmutableMap.of("other", "value")));
+ addLicenseSetting("sonar.devcockpit.license.secured", "Dev Cockpit", createBase64License("Unknown", "other", "54321", "2010-01-01", "EVALUATION", Collections.emptyMap()));
String result = ws.newRequest()
.setMediaType(JSON)
userSession.login("admin").setGlobalPermissions(SYSTEM_ADMIN);
}
- private void addLicenseSetting(String key, String value) {
- definitions.addComponent(PropertyDefinition.builder(key).type(LICENSE).build());
+ private void addLicenseSetting(String key, @Nullable String name, String value) {
+ definitions.addComponent(PropertyDefinition.builder(key).name(name).type(LICENSE).build());
propertyDb.insertProperties(newGlobalPropertyDto().setKey(key).setValue(value));
}