Search in sources :

Example 1 with ModelCollector

use of com.disney.groovity.model.ModelCollector in project groovity by disney.

the class TestModel method testTypes.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testTypes() throws Exception {
    HashMap main = new HashMap<>();
    main.put("longChange", 100l);
    main.put("intChange", (short) 100);
    HashMap second = new HashMap();
    second.put("double", 3.0);
    main.put("second", second);
    Map third = new LinkedHashMap<>();
    third.put("x", "y");
    third.put("a", "b");
    third.put("longChange", 20l);
    second.put("third", third);
    HashMap features = new HashMap();
    features.put("clowns", 5);
    Map menu = new ConcurrentHashMap();
    menu.put("appetizer", "pretzels");
    menu.put("mainCourse", "pizza");
    features.put("menu", menu);
    Map place = new HashMap();
    place.put("name", "State Fairground");
    place.put("latitude", 1234);
    place.put("longitude", 5678);
    RealEvent event = new RealEvent();
    event.time = new Date(1000000000);
    event.name = "State Fair";
    event.put("place", place);
    event.description = "State Fun";
    event.features = features;
    second.put("event", event);
    ModelCollector mc = new ModelCollector();
    mc.setFilters(new ModelFilter[] { ModelFilter.transform(Short.class, o -> {
        return o * 13;
    }), ModelFilter.transform(Long.class, o -> {
        return o * 17;
    }), ModelFilter.transform(Number.class, o -> {
        return o.doubleValue() * 2;
    }), ModelFilter.exclude(LinkedHashMap.class, "longChange"), ModelFilter.add(LinkedHashMap.class, "ordered", o -> {
        return o.size();
    }), ModelFilter.collapse(RealEvent.class, "features"), ModelFilter.copy(RealEvent.class, "name", "allcaps", name -> {
        return name.toString().toUpperCase();
    }), ModelFilter.include(RealPlace.class, "name", "shortName"), ModelFilter.promote(ConcurrentHashMap.class, "mainCourse"), ModelFilter.rename(RealEvent.class, "description", "info"), ModelFilter.invoke(Date.class, "getTime", ""), ModelFilter.invoke(RealPlace.class, "getName", "shortName", 1, 11), ModelFilter.transform(RealPlace.class, "name", o -> {
        return o.toString().concat("...");
    }) });
    mc.visit(main);
    Map expected = new HashMap<>();
    expected.put("longChange", 3400.0);
    expected.put("intChange", 2600.0);
    HashMap expectedSecond = new HashMap();
    expectedSecond.put("double", 6.0);
    expected.put("second", expectedSecond);
    Map expectedThird = new HashMap();
    expectedThird.put("x", "y");
    expectedThird.put("a", "b");
    expectedThird.put("ordered", 6.0);
    expectedSecond.put("third", expectedThird);
    Map expectedEvent = new HashMap<>();
    expectedEvent.put("allcaps", "STATE FAIR");
    expectedEvent.put("name", "State Fair");
    expectedEvent.put("info", "State Fun");
    expectedEvent.put("time", 1000000000l);
    expectedEvent.put("clowns", 10.0);
    expectedEvent.put("menu", "pizza");
    Map expectedPlace = new HashMap<>();
    expectedPlace.put("name", "State Fairground...");
    expectedPlace.put("shortName", "tate Fairg");
    expectedEvent.put("place", expectedPlace);
    expectedSecond.put("event", expectedEvent);
    Assert.assertEquals(expected, mc.getCollected());
}
Also used : Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GroovySystem(groovy.lang.GroovySystem) Date(java.util.Date) ModelFilter(com.disney.groovity.model.ModelFilter) ObjectInputStream(java.io.ObjectInputStream) Closure(groovy.lang.Closure) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ModelConsumer(com.disney.groovity.model.ModelConsumer) LinkedHashMap(java.util.LinkedHashMap) MetaBeanProperty(groovy.lang.MetaBeanProperty) ReflectionCache(org.codehaus.groovy.reflection.ReflectionCache) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) ModelCollector(com.disney.groovity.model.ModelCollector) ObjectOutputStream(java.io.ObjectOutputStream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test) CachedClass(org.codehaus.groovy.reflection.CachedClass) MetaMethod(groovy.lang.MetaMethod) MetaClassImpl(groovy.lang.MetaClassImpl) ModelJsonWriter(com.disney.groovity.model.ModelJsonWriter) Modifier(java.lang.reflect.Modifier) Assert(org.junit.Assert) Model(com.disney.groovity.model.Model) ModelCollector(com.disney.groovity.model.ModelCollector) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 2 with ModelCollector

use of com.disney.groovity.model.ModelCollector in project groovity by disney.

the class TestModel method testModel.

@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
@Test
public void testModel() throws Exception {
    HashMap place = new HashMap<>();
    place.put("name", "State Fairground");
    place.put("latitude", 1234);
    place.put("longitude", 5678);
    HashMap features = new HashMap();
    features.put("clowns", 5);
    HashMap menu = new HashMap();
    menu.put("appetizer", "pretzels");
    menu.put("mainCourse", "pizza");
    features.put("menu", menu);
    RealEvent event = new RealEvent();
    event.name = "State Fair";
    event.put("place", place);
    event.description = "State Fun";
    event.features = features;
    Exception e = null;
    try {
        event.put("time", "last week");
    } catch (IllegalArgumentException iae) {
        e = iae;
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(event);
    oos.close();
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    Object o = ois.readObject();
    Assert.assertEquals(((Model) o).map(), event.map());
    Assert.assertNotNull("Expected IllegalArgumentException, none was thrown", e);
    event.put("time", 1000000000000l);
    ModelCollector collector = new ModelCollector();
    collector.setFilters(new ModelFilter[] { ModelFilter.invoke("", "reverseName", "reversedName", 2, 8), ModelFilter.exclude("description"), ModelFilter.copy("name", "allcaps", name -> {
        return name.toString().toUpperCase();
    }), ModelFilter.promote("place.name"), ModelFilter.invoke("place", "reverse", ""), ModelFilter.collapse("features.menu"), ModelFilter.include("*", "features.mainCourse", "features.clowns", "features.bands"), ModelFilter.transform("time", date -> {
        return ((Date) date).getTime();
    }), ModelFilter.rename("time", "date"), ModelFilter.add("features.bands", ev -> {
        return Arrays.asList("Foo", "Bar");
    }), ModelFilter.custom(new Closure(null) {

        @SuppressWarnings("unused")
        public void doCall(String k, Object v, ModelConsumer c) {
            if (k.equals("clowns")) {
                c.call(k, v.toString());
            } else {
                c.call(k, v);
            }
        }
    }) });
    HashMap expected = new HashMap<>();
    expected.put("name", "State Fair");
    expected.put("reversedName", "aF eta");
    expected.put("allcaps", "STATE FAIR");
    expected.put("date", 1000000000000l);
    expected.put("place", "dnuorgriaF etatS");
    HashMap expectedFeatures = new HashMap();
    expectedFeatures.put("mainCourse", "pizza");
    expectedFeatures.put("clowns", "5");
    expectedFeatures.put("bands", Arrays.asList("Foo", "Bar"));
    expected.put("features", expectedFeatures);
    collector.visit(event);
    Assert.assertEquals(expected, collector.getCollected());
    HashMap firstLevel = new HashMap();
    HashMap secondLevelA = new HashMap();
    HashMap secondLevelB = new HashMap();
    secondLevelA.put("name", "foo");
    secondLevelB.put("name", "bar");
    firstLevel.put("a", secondLevelA);
    firstLevel.put("b", secondLevelB);
    ModelCollector acc = new ModelCollector();
    acc.setFilters(new ModelFilter[] { ModelFilter.include("a.name") });
    acc.visit(firstLevel);
    HashMap exacc = new HashMap<>();
    exacc.put("a", secondLevelA);
    Assert.assertEquals(exacc, acc.getCollected());
    ModelCollector acc2 = new ModelCollector();
    acc2.setFilters(new ModelFilter[] { ModelFilter.exclude("a.name") });
    acc2.visit(firstLevel);
    HashMap exacc2 = new HashMap<>();
    exacc2.put("b", secondLevelB);
    exacc2.put("a", new HashMap<>());
    Assert.assertEquals(exacc2, acc2.getCollected());
}
Also used : Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GroovySystem(groovy.lang.GroovySystem) Date(java.util.Date) ModelFilter(com.disney.groovity.model.ModelFilter) ObjectInputStream(java.io.ObjectInputStream) Closure(groovy.lang.Closure) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ModelConsumer(com.disney.groovity.model.ModelConsumer) LinkedHashMap(java.util.LinkedHashMap) MetaBeanProperty(groovy.lang.MetaBeanProperty) ReflectionCache(org.codehaus.groovy.reflection.ReflectionCache) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) ModelCollector(com.disney.groovity.model.ModelCollector) ObjectOutputStream(java.io.ObjectOutputStream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test) CachedClass(org.codehaus.groovy.reflection.CachedClass) MetaMethod(groovy.lang.MetaMethod) MetaClassImpl(groovy.lang.MetaClassImpl) ModelJsonWriter(com.disney.groovity.model.ModelJsonWriter) Modifier(java.lang.reflect.Modifier) Assert(org.junit.Assert) Model(com.disney.groovity.model.Model) Closure(groovy.lang.Closure) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ModelCollector(com.disney.groovity.model.ModelCollector) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInputStream(java.io.ObjectInputStream) ModelConsumer(com.disney.groovity.model.ModelConsumer) Test(org.junit.Test)

Aggregations

Model (com.disney.groovity.model.Model)2 ModelCollector (com.disney.groovity.model.ModelCollector)2 ModelConsumer (com.disney.groovity.model.ModelConsumer)2 ModelFilter (com.disney.groovity.model.ModelFilter)2 ModelJsonWriter (com.disney.groovity.model.ModelJsonWriter)2 Closure (groovy.lang.Closure)2 GroovySystem (groovy.lang.GroovySystem)2 MetaBeanProperty (groovy.lang.MetaBeanProperty)2 MetaClassImpl (groovy.lang.MetaClassImpl)2 MetaMethod (groovy.lang.MetaMethod)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Modifier (java.lang.reflect.Modifier)2 Arrays (java.util.Arrays)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2