ArrayMerger extends EvaluationExpression { @Override public IJsonNode evaluate(final IJsonNode node, final IJsonNode target, final EvaluationContext context) { final ArrayNode targetArray = SopremoUtil.reinitializeTarget(target, ArrayNode.class); for (final IJsonNode nextNode : (IArrayNode) node) if (nextNode != NullNode.getInstance()) { final IArrayNode array = (IArrayNode) nextNode; for (int index = 0; index < array.size(); index++) if (targetArray.size() <= index) targetArray.add(array.get(index)); else if (this.isNull(targetArray.get(index)) && !this.isNull(array.get(index))) targetArray.set(index, array.get(index)); } return targetArray; }  @Override IJsonNode evaluate(final IJsonNode node, final IJsonNode target, final EvaluationContext context); @Override void toString(final StringBuilder builder);  }
@Test public void shouldMergeOneArray() { final IJsonNode result = new ArrayMerger().evaluate( createArrayNode(createArrayNode(createObjectNode("fieldName", 1), createObjectNode("fieldName", 2), createObjectNode("fieldName", 3), createObjectNode("fieldName", 4), createObjectNode("fieldName", 5))), null, this.context); Assert.assertEquals(createArrayNode(createObjectNode("fieldName", 1), createObjectNode("fieldName", 2), createObjectNode("fieldName", 3), createObjectNode("fieldName", 4), createObjectNode("fieldName", 5)), result); }
@Test public void shouldMergeEmptyArray() { final IJsonNode result = new ArrayMerger().evaluate( createArrayNode(createArrayNode(createObjectNode("fieldName", 1), createObjectNode("fieldName", 2), createObjectNode("fieldName", 3), createObjectNode("fieldName", 4), createObjectNode("fieldName", 5)), createArrayNode()), null, this.context); Assert.assertEquals(createArrayNode(createObjectNode("fieldName", 1), createObjectNode("fieldName", 2), createObjectNode("fieldName", 3), createObjectNode("fieldName", 4), createObjectNode("fieldName", 5)), result); }
@Test public void shouldFillNullValuesWithValuesFromOtherArrays() { final IJsonNode result = new ArrayMerger().evaluate( createArrayNode(createArrayNode(null, createObjectNode("fieldName", 2), createObjectNode("fieldName", 3), createObjectNode("fieldName", 4), createObjectNode("fieldName", 5)), createArrayNode(createObjectNode("fieldName", 1))), null, this.context); Assert.assertEquals(createArrayNode(createObjectNode("fieldName", 1), createObjectNode("fieldName", 2), createObjectNode("fieldName", 3), createObjectNode("fieldName", 4), createObjectNode("fieldName", 5)), result); }
@Test public void shouldFillNullNodesWithValuesFromOtherArrays() { final IJsonNode result = new ArrayMerger().evaluate( createArrayNode( createArrayNode(NullNode.getInstance(), createObjectNode("fieldName", 2), NullNode.getInstance()), createArrayNode(createObjectNode("fieldName", 1)), createArrayNode(null, null, createObjectNode("fieldName", 3))), null, this.context); Assert.assertEquals(createArrayNode(createObjectNode("fieldName", 1), createObjectNode("fieldName", 2), createObjectNode("fieldName", 3)), result); }
@Test public void shouldReuseTarget() { final IJsonNode target = new ArrayNode(); final IArrayNode firstArray = createArrayNode(null, IntNode.valueOf(2), IntNode.valueOf(3)); final IArrayNode secondArray = createArrayNode(IntNode.valueOf(1)); final IJsonNode result = new ArrayMerger().evaluate(createArrayNode(firstArray, secondArray), target, this.context); Assert.assertEquals(createArrayNode(IntNode.valueOf(1), IntNode.valueOf(2), IntNode.valueOf(3)), result); Assert.assertSame(target, result); }
@Test public void shouldNotReuseTargetIfWrongType() { final IJsonNode target = new ObjectNode(); final IArrayNode firstArray = createArrayNode(null, IntNode.valueOf(2), IntNode.valueOf(3)); final IArrayNode secondArray = createArrayNode(IntNode.valueOf(1)); final IJsonNode result = new ArrayMerger().evaluate(createArrayNode(firstArray, secondArray), target, this.context); Assert.assertEquals(createArrayNode(IntNode.valueOf(1), IntNode.valueOf(2), IntNode.valueOf(3)), result); Assert.assertNotSame(target, result); }