Search in sources :

Example 1 with Schema

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

the class SchematizerServiceTest method testSchemaFromMap.

@SuppressWarnings("unused")
@Test
@Ignore("Probably no longer necessary...")
public void testSchemaFromMap() {
    Schema s1 = schematizer.schematize("MyDTO", new TypeReference<MyDTO>() {
    }).get("MyDTO");
    Map<String, Node.DTO> map = s1.toMap();
// Schema s2 = schematizer.from("MyDTO", map);
// testSchema(s2);
}
Also used : Schema(org.apache.felix.schematizer.Schema) TypeReference(org.osgi.util.converter.TypeReference) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with Schema

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

the class SchematizerServiceTest method testSchematizeDTOWithAnnotatedColletion.

@Test
public void testSchematizeDTOWithAnnotatedColletion() {
    Schema s = schematizer.schematize("MyDTO4", new TypeReference<MyDTO4>() {
    }).get("MyDTO4");
    assertNotNull(s);
    Node root = s.rootNode();
    assertNodeEquals("", "/", false, MyDTO4.class, false, root);
    assertEquals(4, root.children().size());
    Node pingNode = root.children().get("/ping");
    assertNodeEquals("ping", "/ping", false, String.class, true, pingNode);
    Node pongNode = root.children().get("/pong");
    assertNodeEquals("pong", "/pong", false, Long.class, true, pongNode);
    Node countNode = root.children().get("/count");
    assertNodeEquals("count", "/count", false, MyDTO4.Count.class, true, countNode);
    Node embeddedNode = root.children().get("/embedded");
    assertEquals(3, embeddedNode.children().size());
    assertNodeEquals("embedded", "/embedded", true, MyEmbeddedDTO.class, true, embeddedNode);
    Node marcoNode = embeddedNode.children().get("/marco");
    assertNodeEquals("marco", "/embedded/marco", false, String.class, true, marcoNode);
    Node poloNode = embeddedNode.children().get("/polo");
    assertNodeEquals("polo", "/embedded/polo", false, Long.class, true, poloNode);
    Node alphaNode = embeddedNode.children().get("/alpha");
    assertNodeEquals("alpha", "/embedded/alpha", false, MyEmbeddedDTO.Alpha.class, true, alphaNode);
    Node sRoot = s.nodeAtPath("/");
    assertNodeEquals("", "/", false, MyDTO4.class, false, sRoot);
    Node sPingNode = s.nodeAtPath("/ping");
    assertNodeEquals("ping", "/ping", false, String.class, true, sPingNode);
    Node sPongNode = s.nodeAtPath("/pong");
    assertNodeEquals("pong", "/pong", false, Long.class, true, sPongNode);
    Node sCountNode = s.nodeAtPath("/count");
    assertNodeEquals("count", "/count", false, MyDTO4.Count.class, true, sCountNode);
    Node sEmbeddedNode = s.nodeAtPath("/embedded");
    assertNodeEquals("embedded", "/embedded", true, MyEmbeddedDTO.class, true, sEmbeddedNode);
    Node sMarcoNode = s.nodeAtPath("/embedded/marco");
    assertNodeEquals("marco", "/embedded/marco", false, String.class, true, sMarcoNode);
    Node sPoloNode = s.nodeAtPath("/embedded/polo");
    assertNodeEquals("polo", "/embedded/polo", false, Long.class, true, sPoloNode);
    Node sAlphaNode = s.nodeAtPath("/embedded/alpha");
    assertNodeEquals("alpha", "/embedded/alpha", false, MyEmbeddedDTO.Alpha.class, true, sAlphaNode);
}
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 3 with Schema

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

the class SchematizerServiceTest method testSchematizeDTOWithColletion.

@Test
public void testSchematizeDTOWithColletion() {
    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 root = s.rootNode();
    assertNodeEquals("", "/", false, new TypeReference<MyDTO3<MyEmbeddedDTO2<String>>>() {
    }.getType(), false, root);
    assertEquals(4, root.children().size());
    Node pingNode = root.children().get("/ping");
    assertNodeEquals("ping", "/ping", false, String.class, true, pingNode);
    Node pongNode = root.children().get("/pong");
    assertNodeEquals("pong", "/pong", false, Long.class, true, pongNode);
    Node countNode = root.children().get("/count");
    assertNodeEquals("count", "/count", false, MyDTO3.Count.class, true, countNode);
    Node embeddedNode = root.children().get("/embedded");
    assertEquals(1, embeddedNode.children().size());
    assertNodeEquals("embedded", "/embedded", true, new TypeReference<MyEmbeddedDTO2<String>>() {
    }.getType(), true, embeddedNode);
    Node valueNode = embeddedNode.children().get("/value");
    assertNodeEquals("value", "/embedded/value", false, String.class, true, valueNode);
    Node sRoot = s.nodeAtPath("/");
    assertNodeEquals("", "/", false, new TypeReference<MyDTO3<MyEmbeddedDTO2<String>>>() {
    }.getType(), false, sRoot);
    Node sPingNode = s.nodeAtPath("/ping");
    assertNodeEquals("ping", "/ping", false, String.class, true, sPingNode);
    Node sPongNode = s.nodeAtPath("/pong");
    assertNodeEquals("pong", "/pong", false, Long.class, true, sPongNode);
    Node sCountNode = s.nodeAtPath("/count");
    assertNodeEquals("count", "/count", false, MyDTO3.Count.class, true, sCountNode);
    Node sEmbeddedNode = s.nodeAtPath("/embedded");
    assertNodeEquals("embedded", "/embedded", true, new TypeReference<MyEmbeddedDTO2<String>>() {
    }.getType(), true, sEmbeddedNode);
    Node sValueNode = s.nodeAtPath("/embedded/value");
    assertNodeEquals("value", "/embedded/value", false, String.class, true, sValueNode);
}
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 4 with Schema

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

the class SchemaTest method testNullValues.

@Test
public void testNullValues() {
    Schema s = schematizer.schematize("MyDTO", new TypeReference<MyDTO3<MyEmbeddedDTO2<String>>>() {
    }).get("MyDTO");
    assertNotNull(s);
    MyEmbeddedDTO2<String> embedded1 = new MyEmbeddedDTO2<>();
    MyEmbeddedDTO2<String> embedded2 = new MyEmbeddedDTO2<>();
    MyEmbeddedDTO2<String> embedded3 = new MyEmbeddedDTO2<>();
    MyDTO3<MyEmbeddedDTO2<String>> dto = new MyDTO3<>();
    dto.ping = "lalala";
    dto.pong = Long.MIN_VALUE;
    dto.count = MyDTO3.Count.ONE;
    dto.embedded = new ArrayList<>();
    dto.embedded.add(embedded1);
    dto.embedded.add(embedded2);
    dto.embedded.add(embedded3);
    assertEquals("lalala", s.valuesAt("/ping", dto).iterator().next());
    assertEquals(Long.MIN_VALUE, s.valuesAt("/pong", dto).iterator().next());
    assertEquals(MyDTO3.Count.ONE, s.valuesAt("/count", dto).iterator().next());
    assertNotNull(s.valuesAt("/embedded", dto));
    assertListEquals(Arrays.asList(new String[] { null, null, null }), s.valuesAt("/embedded/value", dto));
}
Also used : Schema(org.apache.felix.schematizer.Schema) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

Example 5 with Schema

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

the class SchemaTest method testValues.

@Test
public void testValues() {
    final Schema s = schematizer.schematize("MyDTO", new TypeReference<MyDTO3<MyEmbeddedDTO2<String>>>() {
    }).get("MyDTO");
    assertNotNull(s);
    MyEmbeddedDTO2<String> embedded1 = new MyEmbeddedDTO2<>();
    embedded1.value = "value1";
    MyEmbeddedDTO2<String> embedded2 = new MyEmbeddedDTO2<>();
    embedded2.value = "value2";
    MyEmbeddedDTO2<String> embedded3 = new MyEmbeddedDTO2<>();
    embedded3.value = "value3";
    MyDTO3<MyEmbeddedDTO2<String>> dto = new MyDTO3<>();
    dto.ping = "lalala";
    dto.pong = Long.MIN_VALUE;
    dto.count = MyDTO3.Count.ONE;
    dto.embedded = new ArrayList<>();
    dto.embedded.add(embedded1);
    dto.embedded.add(embedded2);
    dto.embedded.add(embedded3);
    assertEquals("lalala", s.valuesAt("/ping", dto).iterator().next());
    assertEquals(Long.MIN_VALUE, s.valuesAt("/pong", dto).iterator().next());
    assertEquals(MyDTO3.Count.ONE, s.valuesAt("/count", dto).iterator().next());
    assertNotNull(s.valuesAt("/embedded", dto));
    Object embeddedList = s.valuesAt("/embedded", dto).iterator().next();
    assertNotNull(embeddedList);
    assertTrue(embeddedList instanceof List);
    assertFalse(((List<?>) embeddedList).isEmpty());
    Object embeddedObject = ((List<?>) embeddedList).get(0);
    assertTrue(embeddedObject instanceof MyEmbeddedDTO2);
    assertListEquals(Arrays.asList(new String[] { "value1", "value2", "value3" }), s.valuesAt("/embedded/value", dto));
}
Also used : Schema(org.apache.felix.schematizer.Schema) List(java.util.List) ArrayList(java.util.ArrayList) TypeReference(org.osgi.util.converter.TypeReference) Test(org.junit.Test)

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