ObjectCreation extends EvaluationExpression { @Override public IJsonNode evaluate(final IJsonNode node, final IJsonNode target, final EvaluationContext context) { final ObjectNode targetObject = SopremoUtil.reinitializeTarget(target, ObjectNode.class); for (final Mapping<?> mapping : this.mappings) mapping.evaluate(node, targetObject, context); return targetObject; } ObjectCreation();  ObjectCreation(final List<Mapping<?>> mappings);  ObjectCreation(final FieldAssignment... mappings); void addMapping(final Mapping<?> mapping); void addMapping(final String target, final EvaluationExpression expression); void addMapping(final EvaluationExpression target, final EvaluationExpression expression); @Override boolean equals(final Object obj); @Override IJsonNode evaluate(final IJsonNode node, final IJsonNode target, final EvaluationContext context); @Override ObjectCreation clone(); @Override @SuppressWarnings({ "rawtypes", "unchecked" }) EvaluationExpression transformRecursively(final TransformFunction function); Mapping<?> getMapping(final int index); List<Mapping<?>> getMappings(); int getMappingSize(); @Override int hashCode(); @Override void toString(final StringBuilder builder); static final ObjectCreation CONCATENATION; }
@Test public void shouldCreateObjectAsIntended() { final IJsonNode result = new ObjectCreation(new ObjectCreation.FieldAssignment("name", new ConstantExpression( TextNode.valueOf("testperson"))), new ObjectCreation.FieldAssignment("age", new ConstantExpression( IntNode.valueOf(30)))).evaluate(IntNode.valueOf(0), null, this.context); Assert.assertEquals(createObjectNode("name", "testperson", "age", 30), result); }
@Test public void shouldEvaluateMappings() { final ObjectCreation.FieldAssignment mapping = new ObjectCreation.FieldAssignment("testname", new InputSelection(0)); final ObjectNode result = createObjectNode("fieldname", "test"); mapping.evaluate(createArrayNode("1", "2"), result, this.context); mapping.evaluate(createArrayNode("3", "4"), result, this.context); Assert.assertEquals(createObjectNode("fieldname", "test", "testname", "3"), result); }
@Test public void shouldReuseTargetIfCorrectType() { final ObjectNode target = new ObjectNode(); final ObjectCreation object = new ObjectCreation(new ObjectCreation.FieldAssignment("name", new ConstantExpression( TextNode.valueOf("testperson"))), new ObjectCreation.FieldAssignment("age", new ConstantExpression( IntNode.valueOf(30)))); final IJsonNode result = object.evaluate(IntNode.valueOf(0), target, this.context); Assert.assertSame(target, result); }
@Test public void shouldNotReuseTargetWithWrongType() { final ArrayNode target = new ArrayNode(); final ObjectCreation object = new ObjectCreation(new ObjectCreation.FieldAssignment("name", new ConstantExpression( TextNode.valueOf("testperson"))), new ObjectCreation.FieldAssignment("age", new ConstantExpression( IntNode.valueOf(30)))); final IJsonNode result = object.evaluate(IntNode.valueOf(0), target, this.context); Assert.assertNotSame(target, result); }