@Test public void shouldCreateArrayWithListAsParam() { final List<EvaluationExpression> list = new ArrayList<EvaluationExpression>(); list.add(new ConstantExpression(IntNode.valueOf(0))); list.add(EvaluationExpression.VALUE); final IJsonNode result = new ArrayCreation(list).evaluate(IntNode.valueOf(42)); Assert.assertEquals(createArrayNode(IntNode.valueOf(0), IntNode.valueOf(42)), result); }
@Test public void shouldReuseTarget() { final ArrayCreation arrayCreation = new ArrayCreation(new ConstantExpression(IntNode.valueOf(42))); final IJsonNode result1 = arrayCreation.evaluate(IntNode.valueOf(42)); final IJsonNode result2 = arrayCreation.evaluate(IntNode.valueOf(42)); Assert.assertEquals(new ArrayNode<IJsonNode>(IntNode.valueOf(42)), result1); Assert.assertSame(result1, result2); Assert.assertEquals(new ArrayNode<IJsonNode>(IntNode.valueOf(42)), result2); }
@Test public void shouldCreateGeneralSchema() { final List<EvaluationExpression> expressions = new ArrayList<EvaluationExpression>(); expressions.add(new ConstantExpression(IntNode.valueOf(42))); expressions.add( new ArithmeticExpression( new ArrayAccess(0), ArithmeticOperator.ADDITION, new ArrayAccess(1))); final Schema schema = this.factory.create(expressions); Assert.assertTrue(schema instanceof GeneralSchema); }
@Override protected void map(final IJsonNode value, JsonCollector<IJsonNode> out) { final IJsonNode target = this.arrayPath.evaluate(value); if (!(target instanceof IArrayNode<?>)) throw new EvaluationException("Cannot split non-array"); final IArrayNode<?> array = (IArrayNode<?>) target; int index = 0; IntNode indexNode = IntNode.valueOf(0); IArrayNode<IJsonNode> contextNode = JsonUtil.asArray(NullNode.getInstance(), indexNode, array, value); for (IJsonNode element : array) { contextNode.set(0, element); indexNode.setValue(index); out.collect(this.splitProjection.evaluate(contextNode)); index++; } }
@Override protected ArrayCreation createDefaultInstance(final int index) { return new ArrayCreation(new ConstantExpression(IntNode.valueOf(index))); }