Search in sources :

Example 6 with Schema

use of org.apache.felix.schematizer.Schema in project felix by apache.

the class SchemaTest method testEmbeddedValues.

@Test
public void testEmbeddedValues() {
    Schema s = schematizer.schematize("MyDTO", new TypeReference<MyDTO>() {
    }).get("MyDTO");
    assertNotNull(s);
    MyEmbeddedDTO embedded = new MyEmbeddedDTO();
    embedded.alpha = Alpha.A;
    embedded.marco = "mmmm";
    embedded.polo = 66;
    MyDTO dto = new MyDTO();
    dto.ping = "lalala";
    dto.pong = Long.MIN_VALUE;
    dto.count = MyDTO.Count.ONE;
    dto.embedded = embedded;
    assertEquals("lalala", s.valuesAt("/ping", dto).iterator().next());
    assertEquals(Long.MIN_VALUE, s.valuesAt("/pong", dto).iterator().next());
    assertEquals(MyDTO.Count.ONE, s.valuesAt("/count", dto).iterator().next());
    assertNotNull(s.valuesAt("/embedded", dto));
    Object embeddedObject = s.valuesAt("/embedded", dto).iterator().next();
    assertTrue(embeddedObject instanceof MyEmbeddedDTO);
    assertEquals(Alpha.A, s.valuesAt("/embedded/alpha", dto).iterator().next());
    assertEquals("mmmm", s.valuesAt("/embedded/marco", dto).iterator().next());
    assertEquals(66L, s.valuesAt("/embedded/polo", dto).iterator().next());
}
Also used : Schema(org.apache.felix.schematizer.Schema) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

Example 7 with Schema

use of org.apache.felix.schematizer.Schema in project felix by apache.

the class SchematizerServiceTest method testSchematizeToMap.

@Test
public void testSchematizeToMap() {
    Schema s = schematizer.schematize("MyDTO", new TypeReference<MyDTO>() {
    }).get("MyDTO");
    Map<String, Node.DTO> map = s.toMap();
    testMapValues(map);
}
Also used : Schema(org.apache.felix.schematizer.Schema) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

Example 8 with Schema

use of org.apache.felix.schematizer.Schema in project felix by apache.

the class SchematizerServiceTest method testVisitor.

@Test
public void testVisitor() {
    Schema s = schematizer.schematize("MyDTO", new TypeReference<MyDTO>() {
    }).get("MyDTO");
    StringBuilder sb = new StringBuilder();
    s.visit(n -> sb.append("::").append(n.name()));
    assertEquals("::::count::embedded::alpha::marco::polo::ping::pong", sb.toString());
}
Also used : Schema(org.apache.felix.schematizer.Schema) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

Example 9 with Schema

use of org.apache.felix.schematizer.Schema in project felix by apache.

the class SchematizerServiceTest method testTypeRules.

@Test
public void testTypeRules() {
    Schema s = schematizer.type("MyDTO", "/embedded", new TypeReference<MyEmbeddedDTO2<String>>() {
    }).type("MyDTO", "/embedded/value", String.class).schematize("MyDTO", new TypeReference<MyDTO3<MyEmbeddedDTO2<String>>>() {
    }).get("MyDTO");
    assertNotNull(s);
    Node embeddedNode = s.nodeAtPath("/embedded/value");
    assertTrue(!"ERROR".equals(embeddedNode.name()));
    Node parentNode = s.parentOf(embeddedNode);
    assertTrue(!"ERROR".equals(parentNode.name()));
    Node grandparentNode = s.parentOf(parentNode);
    assertTrue(!"ERROR".equals(grandparentNode.name()));
    assertEquals("/", grandparentNode.absolutePath());
}
Also used : Schema(org.apache.felix.schematizer.Schema) Node(org.apache.felix.schematizer.Node) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

Example 10 with Schema

use of org.apache.felix.schematizer.Schema in project felix by apache.

the class SchemaBasedConverter method convertToCollection.

@SuppressWarnings({ "rawtypes", "unchecked" })
private <U, V extends Collection<U>> V convertToCollection(Class<U> targetCls, Class<V> collectionClass, Collection sourceCollection, Schema schema, String path) {
    try {
        V targetCollection = instantiateCollection(collectionClass);
        sourceCollection.stream().map(obj -> convertCollectionItemToObject(obj, targetCls, schema, path)).forEach(u -> targetCollection.add((U) u));
        return targetCollection;
    } catch (Exception e) {
        throw new ConversionException("Cannot create DTO " + targetCls, e);
    }
}
Also used : Util.asDTO(org.apache.felix.schematizer.impl.Util.asDTO) Converter(org.osgi.util.converter.Converter) ConverterFunction(org.osgi.util.converter.ConverterFunction) TargetRule(org.osgi.util.converter.TargetRule) Collection(java.util.Collection) TypeReference(org.osgi.util.converter.TypeReference) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) Schema(org.apache.felix.schematizer.Schema) ConversionException(org.osgi.util.converter.ConversionException) List(java.util.List) Node(org.apache.felix.schematizer.Node) DTO(org.osgi.dto.DTO) Type(java.lang.reflect.Type) Map(java.util.Map) Converters(org.osgi.util.converter.Converters) Util.rawClassOf(org.apache.felix.schematizer.impl.Util.rawClassOf) ConversionException(org.osgi.util.converter.ConversionException) ConversionException(org.osgi.util.converter.ConversionException)

Aggregations

Schema (org.apache.felix.schematizer.Schema)12 TypeReference (org.osgi.util.converter.TypeReference)12 Test (org.junit.Test)11 Node (org.apache.felix.schematizer.Node)6 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Field (java.lang.reflect.Field)1 Type (java.lang.reflect.Type)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Util.asDTO (org.apache.felix.schematizer.impl.Util.asDTO)1 Util.rawClassOf (org.apache.felix.schematizer.impl.Util.rawClassOf)1 Ignore (org.junit.Ignore)1 DTO (org.osgi.dto.DTO)1 ConversionException (org.osgi.util.converter.ConversionException)1 Converter (org.osgi.util.converter.Converter)1 ConverterFunction (org.osgi.util.converter.ConverterFunction)1 Converters (org.osgi.util.converter.Converters)1 TargetRule (org.osgi.util.converter.TargetRule)1