/** * The definition related to the specified property. It may * be empty. * * @since 6.1 */ public Optional<PropertyDefinition> getDefinition(String key) { return Optional.ofNullable(definitions.get(key)); }
private void assertProperties(PropertyDefinitions definitions) { assertThat(definitions.get("foo").name()).isEqualTo("Foo"); assertThat(definitions.get("one").name()).isEqualTo("One"); assertThat(definitions.get("two").name()).isEqualTo("Two"); assertThat(definitions.get("unknown")).isNull(); assertThat(definitions.getDefaultValue("foo")).isNull(); assertThat(definitions.getDefaultValue("two")).isEqualTo("2"); assertThat(definitions.getAll().size()).isEqualTo(3); }
private void assertPropertyDefinitions(ComponentContainer container, String... keys) { PropertyDefinitions propertyDefinitions = container.getComponentByType(PropertyDefinitions.class); if (keys.length == 0) { assertThat(propertyDefinitions.getAll()).isEmpty(); } else { for (String key : keys) { assertThat(propertyDefinitions.get(key)).isNotNull(); } } }
private Set<String> getPropertySetKeys(List<PropertyDto> properties) { return properties.stream() .filter(propertyDto -> propertyDefinitions.get(propertyDto.getKey()) != null) .filter(propertyDto -> propertyDefinitions.get(propertyDto.getKey()).type().equals(PROPERTY_SET)) .flatMap(propertyDto -> extractPropertySetKeys(propertyDto, propertyDefinitions.get(propertyDto.getKey())).stream()) .collect(Collectors.toSet()); }
private void assertPropertyDefinitions(ComponentContainer container, CoreExtension coreExtension, PropertyDefinition... definitions) { PropertyDefinitions propertyDefinitions = container.getComponentByType(PropertyDefinitions.class); if (definitions.length == 0) { assertThat(propertyDefinitions.getAll()).isEmpty(); } else { for (PropertyDefinition definition : definitions) { PropertyDefinition actual = propertyDefinitions.get(definition.key()); assertThat(actual.category()).isEqualTo(definition.category() == null ? coreExtension.getName() : definition.category()); } } }
@CheckForNull public String getDefaultValue(String key) { PropertyDefinition def = get(key); if (def == null) { return null; } return StringUtils.defaultIfEmpty(def.defaultValue(), null); }
private List<String> getKeys(ResetRequest request) { return new ArrayList<>(request.getKeys().stream() .map(key -> { PropertyDefinition definition = definitions.get(key); return definition != null ? definition.key() : key; }) .collect(MoreCollectors.toSet())); }
private void validate(SetRequest request) { PropertyDefinition definition = propertyDefinitions.get(request.getKey()); if (definition == null) { return; } checkSingleOrMultiValue(request, definition); }
public String getDeprecatedKey(String key) { PropertyDefinition def = get(key); if (def == null) { return null; } return StringUtils.defaultIfEmpty(def.deprecatedKey(), null); } }
public Consumer<SettingData> scope() { return data -> { PropertyDefinition definition = definitions.get(data.key); checkRequest(data.component != null || definition == null || definition.global() || isGlobal(definition), "Setting '%s' cannot be global", data.key); }; }
private String persistedKey(SetRequest request) { PropertyDefinition definition = propertyDefinitions.get(request.getKey()); // handles deprecated key but persist the new key return definition == null ? request.getKey() : definition.key(); }
@Override public Optional<String> get(String key) { String effectiveKey = definitions.validKey(key); PropertyDefinition def = definitions.get(effectiveKey); if (def != null && (def.multiValues() || !def.fields().isEmpty())) { LOG.warn("Access to the multi-values/property set property '{}' should be made using 'getStringArray' method. The SonarQube plugin using this property should be updated.", key); } return getInternal(effectiveKey); }
private void delete(DbSession dbSession, String settingKey, Optional<ComponentDto> componentDto) { PropertyDefinition definition = definitions.get(settingKey); if (definition == null || !definition.type().equals(PropertyType.PROPERTY_SET)) { deleteSetting(dbSession, settingKey, componentDto); } else { deletePropertySet(dbSession, settingKey, definition, componentDto); } }
public Consumer<SettingData> qualifier() { return data -> { String qualifier = data.component == null ? "" : data.component.qualifier(); PropertyDefinition definition = definitions.get(data.key); checkRequest(checkComponentScopeAndQualifier(data, definition), "Setting '%s' cannot be set on a %s", data.key, i18n.message(Locale.ENGLISH, "qualifier." + qualifier, null)); }; }
@Test public void shouldDeclareComponentProperties() { ComponentContainer container = new ComponentContainer(); container.addSingleton(ComponentWithProperty.class); PropertyDefinitions propertyDefinitions = container.getComponentByType(PropertyDefinitions.class); assertThat(propertyDefinitions.get("foo")).isNotNull(); assertThat(propertyDefinitions.get("foo").defaultValue()).isEqualTo("bar"); }
private static String obfuscateValue(PropertyDefinitions definitions, String key, String value) { PropertyDefinition def = definitions.get(key); if (def != null && def.type() == PropertyType.PASSWORD) { return PASSWORD_VALUE; } if (endsWithIgnoreCase(key, ".secured") || containsIgnoreCase(key, "password") || containsIgnoreCase(key, "passcode") || AUTH_JWT_SECRET.getKey().equals(key)) { return PASSWORD_VALUE; } return abbreviate(value, MAX_VALUE_LENGTH); } }
@Test public void shouldDeclareExtensionWhenAdding() { ComponentContainer container = new ComponentContainer(); PluginInfo plugin = mock(PluginInfo.class); container.addExtension(plugin, ComponentWithProperty.class); PropertyDefinitions propertyDefinitions = container.getComponentByType(PropertyDefinitions.class); assertThat(propertyDefinitions.get("foo")).isNotNull(); assertThat(container.getComponentByType(ComponentWithProperty.class)).isNotNull(); assertThat(container.getComponentByKey(ComponentWithProperty.class)).isNotNull(); }
@Test public void shouldDeclareExtensionWithoutAddingIt() { ComponentContainer container = new ComponentContainer(); PluginInfo plugin = mock(PluginInfo.class); container.declareExtension(plugin, ComponentWithProperty.class); PropertyDefinitions propertyDefinitions = container.getComponentByType(PropertyDefinitions.class); assertThat(propertyDefinitions.get("foo")).isNotNull(); assertThat(container.getComponentByType(ComponentWithProperty.class)).isNull(); }
private void setValue(PropertyDto propertyDto, Property property) { String value = propertyDto.getValue(); property.setValue(value); PropertyDefinition definition = propertyDefinitions.get(propertyDto.getKey()); if (definition != null && (definition.multiValues() || definition.type().equals(PROPERTY_SET))) { property.setValues(createValues(value)); } }
@Test public void get_throws_NPE_if_key_is_null() { PropertyDefinitions underTest = new PropertyDefinitions(); expectedException.expect(NullPointerException.class); expectedException.expectMessage("key can't be null"); underTest.get(null); }