Search in sources :

Example 46 with Property

use of org.ff4j.property.Property in project ff4j by ff4j.

the class CacheProxyTest method testCacheProxyManagerProperty.

@Test
public void testCacheProxyManagerProperty() {
    FF4jCacheProxy proxy = new FF4jCacheProxy();
    proxy.setTargetPropertyStore(new InMemoryPropertyStore());
    proxy.setTargetFeatureStore(new InMemoryFeatureStore());
    proxy.setCacheManager(new InMemoryCacheManager());
    Assert.assertTrue(proxy.isEmpty());
    proxy.create(new Feature("a"));
    Assert.assertFalse(proxy.isEmpty());
    proxy.createProperty(new PropertyString("p1", "v1"));
    Property<?> p1 = proxy.readProperty("p1");
    proxy.readProperty("p1");
    proxy.getTargetPropertyStore().createProperty(new PropertyString("p2"));
    proxy.readProperty("p2");
    proxy.updateProperty("p1", "v2");
    proxy.updateProperty(p1);
    Assert.assertFalse(proxy.isEmpty());
    Assert.assertFalse(proxy.listPropertyNames().isEmpty());
    proxy.deleteProperty("p1");
    proxy.clear();
    Set<Property<?>> setOfProperty = new HashSet<Property<?>>();
    setOfProperty.add(new PropertyLogLevel("a", LogLevel.INFO));
    setOfProperty.add(new PropertyLogLevel("titi1", LogLevel.INFO));
    proxy.importProperties(setOfProperty);
    // Already in cache, but not same value
    proxy.createProperty(new PropertyString("cacheNStore", "cacheNStore"));
    proxy.readProperty("cacheNStore", p1);
    // Not in cache, but in store, but not same default value
    proxy.getTargetPropertyStore().createProperty(new PropertyString("p4", "v4"));
    proxy.readProperty("p1", p1);
    proxy.readProperty("p1", p1);
    // Nowhere, return default
    proxy.readProperty("p2", new PropertyString("p2"));
    proxy.readProperty("p1", new PropertyString("p3"));
}
Also used : PropertyLogLevel(org.ff4j.property.PropertyLogLevel) PropertyString(org.ff4j.property.PropertyString) InMemoryPropertyStore(org.ff4j.property.store.InMemoryPropertyStore) InMemoryCacheManager(org.ff4j.cache.InMemoryCacheManager) InMemoryFeatureStore(org.ff4j.store.InMemoryFeatureStore) FF4jCacheProxy(org.ff4j.cache.FF4jCacheProxy) Feature(org.ff4j.core.Feature) Property(org.ff4j.property.Property) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 47 with Property

use of org.ff4j.property.Property in project ff4j by ff4j.

the class AbstractPropertyStoreJunitTest method importPropertiesOK.

/**
 * TDD.
 */
@Test
public void importPropertiesOK() {
    // Given
    Assert.assertNotNull(testedStore);
    Assert.assertFalse(testedStore.existProperty("titi1"));
    Assert.assertFalse(testedStore.existProperty("titi2"));
    Assert.assertTrue(testedStore.existProperty("a"));
    // When
    Set<Property<?>> setOfProperty = new HashSet<Property<?>>();
    setOfProperty.add(new PropertyLogLevel("a", LogLevel.INFO));
    setOfProperty.add(new PropertyLogLevel("titi1", LogLevel.INFO));
    setOfProperty.add(new PropertyLogLevel("titi2", LogLevel.INFO));
    testedStore.importProperties(setOfProperty);
    // Then
    Assert.assertTrue(testedStore.existProperty("titi1"));
    Assert.assertTrue(testedStore.existProperty("titi2"));
    Assert.assertTrue(testedStore.existProperty("a"));
}
Also used : PropertyLogLevel(org.ff4j.property.PropertyLogLevel) Property(org.ff4j.property.Property) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 48 with Property

use of org.ff4j.property.Property in project ff4j by ff4j.

the class InMemoryPropertiesStoreTest method testInitStores.

@Test
public void testInitStores() {
    new InMemoryPropertyStore(new HashMap<String, Property<?>>());
    InputStream in = getClass().getClassLoader().getResourceAsStream("ff4j.xml");
    new InMemoryPropertyStore(in);
}
Also used : InMemoryPropertyStore(org.ff4j.property.store.InMemoryPropertyStore) InputStream(java.io.InputStream) PropertyString(org.ff4j.property.PropertyString) Property(org.ff4j.property.Property) Test(org.junit.Test)

Example 49 with Property

use of org.ff4j.property.Property in project ff4j by ff4j.

the class PropertyStoreHttp method readAllProperties.

/**
 * {@inheritDoc}
 */
public Map<String, Property<?>> readAllProperties() {
    Response cRes = ClientHttpUtils.invokeGetMethod(getStore(), authorization);
    if (Status.OK.getStatusCode() != cRes.getStatus()) {
        throw new PropertyAccessException("Cannot read properties, an HTTP error " + cRes.getStatus() + OCCURED);
    }
    String resEntity = cRes.readEntity(String.class);
    Property<?>[] pArray = PropertyJsonParser.parsePropertyArray(resEntity);
    Map<String, Property<?>> properties = new HashMap<String, Property<?>>();
    for (Property<?> pName : pArray) {
        properties.put(pName.getName(), pName);
    }
    return properties;
}
Also used : Response(javax.ws.rs.core.Response) HashMap(java.util.HashMap) PropertyAccessException(org.ff4j.exception.PropertyAccessException) Property(org.ff4j.property.Property)

Example 50 with Property

use of org.ff4j.property.Property in project ff4j by ff4j.

the class PropertiesParser method parseProperties.

/**
 * Parse properties:
 *
 * ff4j.properties.0.name=...
 * ff4j.properties.0.type=...
 * ff4j.properties.0.description=...
 * ff4j.properties.0.value=...
 * ff4j.properties.0.fixedValues=...
 *
 * ff4j.features.0.custom-properties.0.name=...
 * ff4j.features.0.custom-properties.0.type=...
 * ff4j.features.0.custom-properties.0.description=...
 * ff4j.features.0.custom-properties.0.value=...
 * ff4j.features.0.custom-properties.0.fixedValues=...
 */
private Map<String, Property<?>> parseProperties(String prefix, Map<String, String> mapConfigProperties) {
    Map<String, Property<?>> result = new HashMap<>();
    int idx = 0;
    String currentPropertyKey = prefix + "." + idx;
    while (mapConfigProperties.containsKey(currentPropertyKey + "." + PROPERTY_PARAMNAME)) {
        assertKeyNotEmpty(mapConfigProperties, currentPropertyKey + "." + PROPERTY_PARAMNAME);
        String name = mapConfigProperties.get(currentPropertyKey + "." + PROPERTY_PARAMNAME);
        assertKeyNotEmpty(mapConfigProperties, currentPropertyKey + "." + PROPERTY_PARAMVALUE);
        String strValue = mapConfigProperties.get(currentPropertyKey + "." + PROPERTY_PARAMVALUE);
        Property<?> ap = new PropertyString(name, strValue);
        String optionalType = mapConfigProperties.get(currentPropertyKey + "." + PROPERTY_PARAMTYPE);
        // If specific type defined ?
        if (null != optionalType) {
            // Substitution if relevant (e.g. 'int' -> 'org.ff4j.property.PropertyInt')
            optionalType = MappingUtil.mapPropertyType(optionalType);
            try {
                // Constructor (String, String) is mandatory in Property interface
                Constructor<?> constr = Class.forName(optionalType).getConstructor(String.class, String.class);
                ap = (Property<?>) constr.newInstance(name, strValue);
            } catch (Exception e) {
                throw new IllegalArgumentException("Cannot instantiate '" + optionalType + "' check default constructor", e);
            }
        }
        // Description
        String description = mapConfigProperties.get(currentPropertyKey + "." + PROPERTY_PARAMDESCRIPTION);
        if (null != description) {
            ap.setDescription(description);
        }
        // Fixed Values
        String strFixedValues = mapConfigProperties.get(currentPropertyKey + "." + PROPERTY_PARAMFIXED_VALUES);
        if (null != strFixedValues && !"".equals(strFixedValues)) {
            Arrays.asList(strValue.split(",")).stream().map(String::trim).forEach(ap::add2FixedValueFromString);
        }
        // Check fixed value
        if (ap.getFixedValues() != null && !ap.getFixedValues().isEmpty() && !ap.getFixedValues().contains(ap.getValue())) {
            throw new IllegalArgumentException("Cannot create property <" + ap.getName() + "> invalid value <" + ap.getValue() + "> expected one of " + ap.getFixedValues());
        }
        result.put(ap.getName(), ap);
        // ff4j.properties.X
        currentPropertyKey = prefix + "." + ++idx;
    }
    return result;
}
Also used : PropertyString(org.ff4j.property.PropertyString) HashMap(java.util.HashMap) PropertyString(org.ff4j.property.PropertyString) Property(org.ff4j.property.Property) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Property (org.ff4j.property.Property)56 HashMap (java.util.HashMap)20 PropertyString (org.ff4j.property.PropertyString)20 Test (org.junit.Test)20 Feature (org.ff4j.core.Feature)16 Map (java.util.Map)11 InputStream (java.io.InputStream)9 XmlParser (org.ff4j.conf.XmlParser)7 InMemoryPropertyStore (org.ff4j.property.store.InMemoryPropertyStore)7 LinkedHashMap (java.util.LinkedHashMap)6 XmlConfig (org.ff4j.conf.XmlConfig)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ArrayList (java.util.ArrayList)4 FeatureStore (org.ff4j.core.FeatureStore)4 PropertyAccessException (org.ff4j.exception.PropertyAccessException)4 PropertyStore (org.ff4j.property.store.PropertyStore)4 IOException (java.io.IOException)3 Date (java.util.Date)3 HashSet (java.util.HashSet)3 FF4jCacheProxy (org.ff4j.cache.FF4jCacheProxy)3