Search in sources :

Example 6 with AttributeMetadata

use of io.airlift.configuration.ConfigurationMetadata.AttributeMetadata in project airlift by airlift.

the class ConfigAssertions method assertAttributesNotEqual.

private static <T> void assertAttributesNotEqual(ConfigurationMetadata<T> metadata, T actual, T expected) {
    for (AttributeMetadata attribute : metadata.getAttributes().values()) {
        Method getter = attribute.getGetter();
        if (getter == null) {
            continue;
        }
        Object actualAttributeValue = invoke(actual, getter);
        Object expectedAttributeValue = invoke(expected, getter);
        if (Objects.deepEquals(actualAttributeValue, expectedAttributeValue)) {
            throw new AssertionError("Attribute value matches the default: " + attribute.getName());
        }
    }
}
Also used : AttributeMetadata(io.airlift.configuration.ConfigurationMetadata.AttributeMetadata) Method(java.lang.reflect.Method)

Example 7 with AttributeMetadata

use of io.airlift.configuration.ConfigurationMetadata.AttributeMetadata in project airlift by airlift.

the class ConfigAssertions method assertFullMapping.

public static <T> void assertFullMapping(Map<String, String> properties, T expected) {
    requireNonNull(properties, "properties");
    requireNonNull(expected, "expected");
    Class<T> configClass = getClass(expected);
    ConfigurationMetadata<T> metadata = ConfigurationMetadata.getValidConfigurationMetadata(configClass);
    // verify all supplied properties are supported and not deprecated
    assertPropertiesSupported(metadata, properties.keySet(), false);
    // verify that every (non-deprecated) property is tested
    Set<String> nonDeprecatedProperties = new TreeSet<>();
    for (AttributeMetadata attribute : metadata.getAttributes().values()) {
        if (attribute.getInjectionPoint().getProperty() != null) {
            nonDeprecatedProperties.add(attribute.getInjectionPoint().getProperty());
        }
    }
    if (!properties.keySet().equals(nonDeprecatedProperties)) {
        Set<String> untestedProperties = new TreeSet<>(nonDeprecatedProperties);
        untestedProperties.removeAll(properties.keySet());
        throw new AssertionError("Untested properties " + untestedProperties);
    }
    // verify that none of the values are the same as a default for the configuration
    T actual = newInstance(configClass, properties);
    T defaultInstance = newDefaultInstance(configClass);
    assertAttributesNotEqual(metadata, actual, defaultInstance);
    // verify that a configuration object created from the properties is equivalent to the expected object
    assertAttributesEqual(metadata, actual, expected);
}
Also used : TreeSet(java.util.TreeSet) AttributeMetadata(io.airlift.configuration.ConfigurationMetadata.AttributeMetadata)

Example 8 with AttributeMetadata

use of io.airlift.configuration.ConfigurationMetadata.AttributeMetadata in project airlift by airlift.

the class ConfigAssertions method assertAttributesEqual.

private static <T> void assertAttributesEqual(ConfigurationMetadata<T> metadata, T actual, T expected) {
    for (AttributeMetadata attribute : metadata.getAttributes().values()) {
        Method getter = attribute.getGetter();
        if (getter == null) {
            continue;
        }
        Object actualAttributeValue = invoke(actual, getter);
        Object expectedAttributeValue = invoke(expected, getter);
        if (!Objects.deepEquals(actualAttributeValue, expectedAttributeValue)) {
            throw new AssertionError(notEquals(attribute.getName(), actualAttributeValue, expectedAttributeValue));
        }
    }
}
Also used : AttributeMetadata(io.airlift.configuration.ConfigurationMetadata.AttributeMetadata) Method(java.lang.reflect.Method)

Example 9 with AttributeMetadata

use of io.airlift.configuration.ConfigurationMetadata.AttributeMetadata in project airlift by airlift.

the class ConfigAssertions method assertDeprecatedEquivalence.

@SafeVarargs
public static <T> void assertDeprecatedEquivalence(Class<T> configClass, Map<String, String> currentProperties, Map<String, String>... oldPropertiesList) {
    requireNonNull(configClass, "configClass");
    requireNonNull(currentProperties, "currentProperties");
    requireNonNull(oldPropertiesList, "oldPropertiesList");
    ConfigurationMetadata<T> metadata = ConfigurationMetadata.getValidConfigurationMetadata(configClass);
    // verify all current properties are supported and not deprecated
    assertPropertiesSupported(metadata, currentProperties.keySet(), false);
    // verify all old properties are supported (deprecation allowed)
    for (Map<String, String> evenOlderProperties : oldPropertiesList) {
        assertPropertiesSupported(metadata, evenOlderProperties.keySet(), true);
    }
    // verify that all deprecated properties are tested
    Set<String> knownDeprecatedProperties = new TreeSet<>();
    for (AttributeMetadata attribute : metadata.getAttributes().values()) {
        for (ConfigurationMetadata.InjectionPointMetaData deprecated : attribute.getLegacyInjectionPoints()) {
            knownDeprecatedProperties.add(deprecated.getProperty());
        }
    }
    Set<String> suppliedDeprecatedProperties = new TreeSet<>();
    for (Map<String, String> evenOlderProperties : oldPropertiesList) {
        suppliedDeprecatedProperties.addAll(evenOlderProperties.keySet());
    }
    if (!suppliedDeprecatedProperties.containsAll(knownDeprecatedProperties)) {
        Set<String> untestedDeprecatedProperties = new TreeSet<>(knownDeprecatedProperties);
        untestedDeprecatedProperties.removeAll(suppliedDeprecatedProperties);
        throw new AssertionError("Untested deprecated properties: " + untestedDeprecatedProperties);
    }
    // verify property sets create equivalent configurations
    T currentConfiguration = newInstance(configClass, currentProperties);
    for (Map<String, String> evenOlderProperties : oldPropertiesList) {
        T evenOlderConfiguration = newInstance(configClass, evenOlderProperties);
        assertAttributesEqual(metadata, currentConfiguration, evenOlderConfiguration);
    }
}
Also used : TreeSet(java.util.TreeSet) AttributeMetadata(io.airlift.configuration.ConfigurationMetadata.AttributeMetadata) ConfigurationMetadata(io.airlift.configuration.ConfigurationMetadata)

Example 10 with AttributeMetadata

use of io.airlift.configuration.ConfigurationMetadata.AttributeMetadata in project airlift by airlift.

the class TestConfigurationMetadata method verifyMetaData.

private void verifyMetaData(ConfigurationMetadata<?> metadata, Class<?> configClass, String description, boolean securitySensitive, boolean hidden, Map<String, Set<String>> attributeProperties) throws Exception {
    assertEquals(metadata.getConfigClass(), configClass);
    if (metadata.getConstructor() != null) {
        assertEquals(metadata.getConstructor(), configClass.getDeclaredConstructor());
    } else {
        try {
            configClass.getDeclaredConstructor();
            fail(String.format("Expected configClass [%s] not to have a constructor", configClass.getName()));
        } catch (NoSuchMethodException expected) {
        }
    }
    assertEquals(metadata.getAttributes().size(), attributeProperties.keySet().size());
    for (String name : attributeProperties.keySet()) {
        AttributeMetadata attribute = metadata.getAttributes().get(name);
        assertEquals(attribute.getConfigClass(), configClass);
        Set<String> namesToTest = new HashSet<>();
        namesToTest.add(attribute.getInjectionPoint().getProperty());
        for (ConfigurationMetadata.InjectionPointMetaData legacyInjectionPoint : attribute.getLegacyInjectionPoints()) {
            namesToTest.add(legacyInjectionPoint.getProperty());
        }
        assertEquals(namesToTest, attributeProperties.get(name));
        assertEquals(attribute.getDescription(), description);
        assertEquals(attribute.isSecuritySensitive(), securitySensitive);
        assertEquals(attribute.isHidden(), hidden);
    }
}
Also used : AttributeMetadata(io.airlift.configuration.ConfigurationMetadata.AttributeMetadata) HashSet(java.util.HashSet)

Aggregations

AttributeMetadata (io.airlift.configuration.ConfigurationMetadata.AttributeMetadata)10 Method (java.lang.reflect.Method)5 TreeSet (java.util.TreeSet)5 HashSet (java.util.HashSet)3 Sets.newConcurrentHashSet (com.google.common.collect.Sets.newConcurrentHashSet)2 ConfigurationMetadata (io.airlift.configuration.ConfigurationMetadata)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 LOWER_CAMEL (com.google.common.base.CaseFormat.LOWER_CAMEL)1 UPPER_CAMEL (com.google.common.base.CaseFormat.UPPER_CAMEL)1 Splitter (com.google.common.base.Splitter)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 ListMultimap (com.google.common.collect.ListMultimap)1