ArrayMerger extends EvaluationExpression { @Override public IJsonNode evaluate(final IJsonNode node) { this.result.clear(); for (final IJsonNode nextNode : (IArrayNode<?>) node) if (nextNode != NullNode.getInstance()) { final IArrayNode<?> array = (IArrayNode<?>) nextNode; for (int index = 0; index < array.size(); index++) if (this.result.size() <= index) this.result.add(array.get(index)); else if (this.isNull(this.result.get(index)) && !this.isNull(array.get(index))) this.result.set(index, array.get(index)); } return this.result; }  @Override void appendAsString(final Appendable appendable); @Override IJsonNode evaluate(final IJsonNode node);  }
@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)))); Assert.assertEquals(createArrayNode(createObjectNode("fieldName", 1), createObjectNode("fieldName", 2), createObjectNode("fieldName", 3)), 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)))); 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())); Assert.assertEquals(createArrayNode(createObjectNode("fieldName", 1), createObjectNode("fieldName", 2), createObjectNode("fieldName", 3), createObjectNode("fieldName", 4), createObjectNode("fieldName", 5)), result); }
@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)))); Assert.assertEquals(createArrayNode(createObjectNode("fieldName", 1), createObjectNode("fieldName", 2), createObjectNode("fieldName", 3), createObjectNode("fieldName", 4), createObjectNode("fieldName", 5)), result); }
@Test public void shouldReuseTarget() { final IArrayNode<?> firstArray = createArrayNode(null, IntNode.valueOf(2), IntNode.valueOf(3)); final IArrayNode<?> secondArray = createArrayNode(IntNode.valueOf(1)); final ArrayMerger arrayMerger = new ArrayMerger(); final IJsonNode result1 = arrayMerger.evaluate(createArrayNode(firstArray, secondArray)); final IJsonNode result2 = arrayMerger.evaluate(createArrayNode(firstArray, secondArray)); Assert.assertEquals(createArrayNode(IntNode.valueOf(1), IntNode.valueOf(2), IntNode.valueOf(3)), result2); Assert.assertSame(result1, result2); }