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"));
}
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"));
}
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);
}
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;
}
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;
}
Aggregations