HeadArraySchema extends AbstractSchema { @Override public PactRecord jsonToRecord(final IJsonNode value, PactRecord target, final EvaluationContext context) { IArrayNode others; if (target == null) { target = new PactRecord(this.headSize + 1); others = new ArrayNode(); target.setField(this.headSize, SopremoUtil.wrap(others)); } else { others = (IArrayNode) SopremoUtil.unwrap(target.getField(this.headSize, JsonNodeWrapper.class)); others.clear(); } IJsonNode arrayElement; for (int i = 0; i < this.headSize; i++) { arrayElement = ((IArrayNode) value).get(i); if (!arrayElement.isMissing()) target.setField(i, SopremoUtil.wrap(arrayElement)); else target.setNull(i); } if (this.getHeadSize() < ((IArrayNode) value).size()) for (int i = this.headSize; i < ((IArrayNode) value).size(); i++) others.add(((IArrayNode) value).get(i)); return target; } HeadArraySchema(final int headSize); int getHeadSize(); @Override IntSet indicesOf(final EvaluationExpression expression); @Override PactRecord jsonToRecord(final IJsonNode value, PactRecord target, final EvaluationContext context); @Override IJsonNode recordToJson(final PactRecord record, IJsonNode target);  }
@Test public void shouldConvertFromJsonToRecord() { final HeadArraySchema schema = new HeadArraySchema(2); final IArrayNode array = new ArrayNode(); array.add(IntNode.valueOf(1)); final PactRecord result = schema.jsonToRecord(array, null, null); final PactRecord expected = new PactRecord(3); expected.setField(0, SopremoUtil.wrap(IntNode.valueOf(1))); expected.setField(2, SopremoUtil.wrap(new ArrayNode())); Assert.assertTrue(PactRecordEqualer.recordsEqual(expected, result, schema.getPactSchema())); }