use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.
the class LookupTableTypeTest method testStringLookup.
/**
* Test if a simple lookup table containing only string values is the same
* when converted to DOM and back again.
*/
@Test
public void testStringLookup() {
Map<Value, Value> values = createStringLookup();
LookupTable org = new LookupTableImpl(values);
// convert to DOM
Element fragment = HaleIO.getComplexElement(org);
// convert back
LookupTable conv = HaleIO.getComplexValue(fragment, LookupTable.class, null);
checkTable(conv, values);
}
use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.
the class LookupTableTypeTest method testComplexLookup.
/**
* Test if a lookup table containing only complex values is the same when
* converted to DOM and back again.
*/
@Test
public void testComplexLookup() {
Map<Value, Value> values2 = createComplexLookup();
LookupTable org2 = new LookupTableImpl(values2);
// convert to DOM
Element fragment = HaleIO.getComplexElement(org2);
// convert back
LookupTable conv = HaleIO.getComplexValue(fragment, LookupTable.class, null);
checkTable(conv, values2);
}
use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.
the class AbstractValueConstraintFactoryTest method storeRestoreTest.
/**
* Test storing a constraint, restoring it and compare both.
*
* @param constraint the constraint to store
* @param typeIndex the type index (as context for storing/restoring),
* <code>null</code> for an empty index
* @param constraintDef the definition the constraint is associated to (as
* context for storing/restoring), for <code>null</code> the
* method will try to generate a default definition based on the
* constraint type
* @throws Exception if an error occurs during storing, restoring or
* comparing the constraint
*/
@SuppressWarnings("unchecked")
protected void storeRestoreTest(T constraint, Map<TypeDefinition, Value> typeIndex, Definition<?> constraintDef) throws Exception {
// conversion service may be needed for Value conversions
TestUtil.startConversionService();
ValueConstraintFactoryDescriptor desc = ValueConstraintExtension.INSTANCE.getForConstraint(constraint);
// provide defaults for null parameters
if (typeIndex == null) {
typeIndex = new HashMap<>();
}
if (constraintDef == null) {
constraintDef = getDefaultConstraintDefinition(constraint.getClass());
}
@SuppressWarnings("rawtypes") ValueConstraintFactory factory = desc.getFactory();
Value val = factory.store(constraint, new MapTypeReferenceBuilder(typeIndex));
T read;
if (val != null) {
// to DOM
NSDOMBuilder builder = NSDOMBuilder.newBuilder(new HashMap<String, String>());
Element elem = DOMValueUtil.valueTag(builder, "test", val);
// from DOM
Value res = DOMValueUtil.fromTag(elem);
// bimap for reverse index
BiMap<TypeDefinition, Value> types = HashBiMap.create(typeIndex);
read = (T) factory.restore(res, constraintDef, new MapTypeResolver(types.inverse()), new OsgiClassResolver());
} else {
// fall back to default constraint
Class<?> constraintType = ConstraintUtil.getConstraintType(constraint.getClass());
read = (T) ConstraintUtil.getDefaultConstraint(constraintType, constraintDef);
}
compare(constraint, read);
}
use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.
the class ElementTypeFactory method store.
@Override
public Value store(ElementType constraint, TypeReferenceBuilder typeIndex) throws Exception {
ValueProperties props = new ValueProperties();
// type definition
if (constraint.getDefinition() != null) {
Optional<Value> ref = typeIndex.createReference(constraint.getDefinition());
if (ref.isPresent()) {
props.put(P_TYPE, ref.get());
}
}
// binding
String className = constraint.getBinding().getName();
props.put(P_BINDING, Value.of(className));
return props.toValue();
}
use of eu.esdihumboldt.hale.common.core.io.Value in project hale by halestudio.
the class EnumerationValidatorFactory method restore.
@Override
public Validator restore(Value value) throws Exception {
ValueList list = value.as(ValueList.class);
List<String> values = new ArrayList<>();
for (Value val : list) {
String str = val.as(String.class);
if (str != null) {
values.add(str);
} else {
throw new IllegalStateException("Enumeration value for validator could not be read");
}
}
return new EnumerationValidator(values);
}
Aggregations