ObjectSchema extends AbstractSchema { @Override public PactRecord jsonToRecord(final IJsonNode value, PactRecord target, final EvaluationContext context) { IObjectNode others; if (target == null || target.getNumFields() < this.mappings.size() + 1) { target = new PactRecord(this.mappings.size() + 1); for (int i = 0; i < this.mappings.size(); i++) target.setField(i, new JsonNodeWrapper(MissingNode.getInstance())); target.setField(this.mappings.size(), new JsonNodeWrapper(others = new ObjectNode())); } else { final JsonNodeWrapper wrappedField = target.getField(target.getNumFields() - 1, JsonNodeWrapper.class); others = wrappedField.getValue(IObjectNode.class); others.clear(); target.setField(target.getNumFields() - 1, wrappedField); } final IObjectNode object = (IObjectNode) value; for (int i = 0; i < this.mappings.size(); i++) { final IJsonNode node = object.get(this.mappings.get(i)); final JsonNodeWrapper wrappedField = target.getField(i, JsonNodeWrapper.class); wrappedField.setValue(node); target.setField(i, wrappedField); } for (final Entry<String, IJsonNode> entry : object) if (!this.mappings.contains(entry.getKey())) others.put(entry.getKey(), entry.getValue()); return target; } ObjectSchema(final List<ObjectAccess> mappings);  ObjectSchema(final String... mappings); @Override boolean equals(final Object obj); List<String> getMappings(); int getMappingSize(); @Override int hashCode(); int hasMapping(final String key); @Override IntSet indicesOf(final EvaluationExpression expression); @Override PactRecord jsonToRecord(final IJsonNode value, PactRecord target, final EvaluationContext context); @Override IJsonNode recordToJson(final PactRecord record, final IJsonNode target); @Override String toString();  }
@Test public void shouldConvertFromJsonToRecord() { final ObjectSchema schema = new ObjectSchema("firstname", "lastname"); final ObjectNode object = new ObjectNode(); object.put("firstname", TextNode.valueOf("testfn")) .put("lastname", TextNode.valueOf("testln")); final PactRecord result = schema.jsonToRecord(object, null, null); final PactRecord expected = new PactRecord(); expected.setField(0, new JsonNodeWrapper(TextNode.valueOf("testfn"))); expected.setField(1, new JsonNodeWrapper(TextNode.valueOf("testln"))); expected.setField(2, new JsonNodeWrapper(new ObjectNode())); Assert.assertTrue(PactRecordEqualer.recordsEqual(expected, result, schema.getPactSchema())); }
@SuppressWarnings("unchecked") @Test public void shouldReturnObjectAsRecordWithMissingSchema() { final ObjectSchema schema = new ObjectSchema(); final ObjectNode object = new ObjectNode().put("firstname", TextNode.valueOf("testfn")) .put("lastname", TextNode.valueOf("testln")); final PactRecord result = schema.jsonToRecord(object, null, null); final PactRecord expected = new PactRecord(); expected.setField(0, new JsonNodeWrapper(object)); Assert.assertTrue(PactRecordEqualer.recordsEqual(expected, result, new Class[] { JsonNodeWrapper.class })); }
@Test public void shouldUseRecordTarget() { final ObjectSchema schema = new ObjectSchema("firstname", "lastname"); final ObjectNode object = new ObjectNode().put("firstname", TextNode.valueOf("testfn")) .put("lastname", TextNode.valueOf("testln")); final PactRecord target = schema.jsonToRecord(object, null, null); final PactRecord result = schema.jsonToRecord(object, target, null); Assert.assertSame(target, result); }