*/
package org.sonar.server.platform.monitoring;
-import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Optional;
import java.util.TreeMap;
import org.sonar.api.PropertyType;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.config.internal.Settings;
import org.sonar.api.server.ServerSide;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.newcodeperiod.NewCodePeriodDto;
import org.sonar.process.systeminfo.Global;
import org.sonar.process.systeminfo.SystemInfoSection;
import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
+import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo.Section.Builder;
import static org.apache.commons.lang.StringUtils.abbreviate;
import static org.apache.commons.lang.StringUtils.containsIgnoreCase;
@ServerSide
public class SettingsSection implements SystemInfoSection, Global {
-
private static final int MAX_VALUE_LENGTH = 500;
private static final String PASSWORD_VALUE = "xxxxxxxx";
+
+ private final DbClient dbClient;
private final Settings settings;
- public SettingsSection(Settings settings) {
+ public SettingsSection(DbClient dbClient, Settings settings) {
+ this.dbClient = dbClient;
this.settings = settings;
}
@Override
public ProtobufSystemInfo.Section toProtobuf() {
- ProtobufSystemInfo.Section.Builder protobuf = ProtobufSystemInfo.Section.newBuilder();
+ Builder protobuf = ProtobufSystemInfo.Section.newBuilder();
protobuf.setName("Settings");
PropertyDefinitions definitions = settings.getDefinitions();
TreeMap<String, String> orderedProps = new TreeMap<>(settings.getProperties());
- for (Map.Entry<String, String> prop : orderedProps.entrySet()) {
- String key = prop.getKey();
- String value = obfuscateValue(definitions, key, prop.getValue());
- setAttribute(protobuf, key, value);
+ for (Entry<String, String> prop : orderedProps.entrySet()) {
+ includeSetting(protobuf, definitions, prop);
}
+ addDefaultNewCodeDefinition(protobuf);
return protobuf.build();
}
+ private static void includeSetting(Builder protobuf, PropertyDefinitions definitions, Entry<String, String> prop) {
+ String key = prop.getKey();
+ String value = obfuscateValue(definitions, key, prop.getValue());
+ setAttribute(protobuf, key, value);
+ }
+
+ private void addDefaultNewCodeDefinition(Builder protobuf) {
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ Optional<NewCodePeriodDto> period = dbClient.newCodePeriodDao().selectGlobal(dbSession);
+ setAttribute(protobuf, "Default New Code Definition", parseDefaultNewCodeDefinition(period));
+ }
+ }
+
private static String obfuscateValue(PropertyDefinitions definitions, String key, String value) {
PropertyDefinition def = definitions.get(key);
if (def != null && def.type() == PropertyType.PASSWORD) {
}
return abbreviate(value, MAX_VALUE_LENGTH);
}
+
+ private static String parseDefaultNewCodeDefinition(Optional<NewCodePeriodDto> period) {
+ if (!period.isPresent()) {
+ return "PREVIOUS_VERSION";
+ }
+
+ if (period.get().getValue() == null) {
+ return period.get().getType().name();
+ }
+
+ return period.get().getType().name() + ": " + period.get().getValue();
+ }
}
*/
package org.sonar.server.platform.monitoring;
+import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.PropertyType;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.config.internal.Settings;
import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+import org.sonar.db.newcodeperiod.NewCodePeriodType;
import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
import static org.apache.commons.lang.StringUtils.repeat;
private static final String PASSWORD_PROPERTY = "sonar.password";
+ @Rule
+ public DbTester dbTester = DbTester.create(System2.INSTANCE);
+
private PropertyDefinitions defs = new PropertyDefinitions(System2.INSTANCE, PropertyDefinition.builder(PASSWORD_PROPERTY).type(PropertyType.PASSWORD).build());
private Settings settings = new MapSettings(defs);
- private SettingsSection underTest = new SettingsSection(settings);
+ private SettingsSection underTest = new SettingsSection(dbTester.getDbClient(), settings);
@Test
public void return_properties_and_sort_by_key() {
ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
assertThatAttributeIs(protobuf, "bar", "bar value");
assertThatAttributeIs(protobuf, "foo", "foo value");
+ assertThatAttributeIs(protobuf, "Default New Code Definition", "PREVIOUS_VERSION");
// keys are ordered alphabetically
assertThat(protobuf.getAttributesList())
.extracting(ProtobufSystemInfo.Attribute::getKey)
- .containsExactly("bar", "foo");
+ .containsExactly("bar", "foo", "Default New Code Definition");
+ }
+
+ @Test
+ public void return_default_new_code_definition_with_no_specified_value() {
+ dbTester.newCodePeriods().insert(NewCodePeriodType.PREVIOUS_VERSION,null);
+
+ ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
+ assertThatAttributeIs(protobuf, "Default New Code Definition", "PREVIOUS_VERSION");
+ }
+
+ @Test
+ public void return_default_new_code_definition_with_specified_value() {
+ dbTester.newCodePeriods().insert(NewCodePeriodType.NUMBER_OF_DAYS,"30");
+
+ ProtobufSystemInfo.Section protobuf = underTest.toProtobuf();
+ assertThatAttributeIs(protobuf, "Default New Code Definition", "NUMBER_OF_DAYS: 30");
}
@Test