TailArraySchema extends AbstractSchema { @Override public PactRecord jsonToRecord(final IJsonNode value, PactRecord target, final EvaluationContext context) { IArrayNode others; if (target == null || target.getNumFields() != 1) { target = new PactRecord(this.getTailSize() + 1); others = new ArrayNode(); target.setField(0, SopremoUtil.wrap(others)); } else { others = (IArrayNode) SopremoUtil.unwrap(target.getField(0, JsonNodeWrapper.class)); others.clear(); } IJsonNode arrayElement; final int arraySize = ((IArrayNode) value).size(); for (int i = 1; i <= this.getTailSize(); i++) { arrayElement = ((IArrayNode) value).get(arraySize - i); if (!arrayElement.isMissing()) target.setField(this.getTailSize() - i + 1, SopremoUtil.wrap(arrayElement)); else target.setNull(this.getTailSize() - i + 1); } if (this.getTailSize() < arraySize) for (int i = 0; i < ((IArrayNode) value).size() - this.getTailSize(); i++) others.add(((IArrayNode) value).get(i)); return target; } TailArraySchema(final int tailSize); int getTailSize(); @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 TailArraySchema schema = new TailArraySchema(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(2, SopremoUtil.wrap(IntNode.valueOf(1))); expected.setField(0, SopremoUtil.wrap(new ArrayNode())); Assert.assertTrue(PactRecordEqualer.recordsEqual(expected, result, schema.getPactSchema())); }
@Test public void shouldConvertFromJsonToRecordWithOthers() { final TailArraySchema schema = new TailArraySchema(2); final IArrayNode array = new ArrayNode(IntNode.valueOf(1), IntNode.valueOf(2), IntNode.valueOf(3), IntNode.valueOf(4), IntNode.valueOf(5)); final PactRecord result = schema.jsonToRecord(array, null, null); final PactRecord expected = new PactRecord(3); expected.setField(1, SopremoUtil.wrap(IntNode.valueOf(4))); expected.setField(2, SopremoUtil.wrap(IntNode.valueOf(5))); expected.setField(0, SopremoUtil.wrap(new ArrayNode(IntNode.valueOf(1), IntNode.valueOf(2), IntNode.valueOf(3)))); Assert.assertTrue(PactRecordEqualer.recordsEqual(expected, result, schema.getPactSchema())); }
@Test public void shouldConvertFromJsonToRecordWithoutOthers() { final TailArraySchema schema = new TailArraySchema(2); final IArrayNode array = new ArrayNode(IntNode.valueOf(1), IntNode.valueOf(2)); final PactRecord result = schema.jsonToRecord(array, null, null); final PactRecord expected = new PactRecord(3); expected.setField(1, SopremoUtil.wrap(IntNode.valueOf(1))); expected.setField(2, SopremoUtil.wrap(IntNode.valueOf(2))); expected.setField(0, SopremoUtil.wrap(new ArrayNode())); Assert.assertTrue(PactRecordEqualer.recordsEqual(expected, result, schema.getPactSchema())); }