ObjectSchema extends AbstractSchema { @Override public IJsonNode recordToJson(final PactRecord record, final IJsonNode target) { if (this.mappings.size() + 1 != record.getNumFields()) throw new IllegalStateException("Schema does not match to record!"); final IObjectNode targetObject; if (target == null) targetObject = new ObjectNode(); else { targetObject = (IObjectNode) target; targetObject.clear(); } for (int i = 0; i < this.mappings.size(); i++) if (record.getField(i, JsonNodeWrapper.class) != null) targetObject.put(this.mappings.get(i), record.getField(i, JsonNodeWrapper.class).getValue()); targetObject.putAll(record.getField(this.mappings.size(), JsonNodeWrapper.class).getValue(IObjectNode.class)); return targetObject; } 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 shouldConvertFromRecordToJson() { final ObjectSchema schema = new ObjectSchema("firstname", "lastname"); final PactRecord record = new PactRecord(); record.setField(0, new JsonNodeWrapper(TextNode.valueOf("testfn"))); record.setField(1, new JsonNodeWrapper(TextNode.valueOf("testln"))); record.setField(2, new JsonNodeWrapper(new ObjectNode())); final IJsonNode result = schema.recordToJson(record, null); final IJsonNode expected = new ObjectNode().put("firstname", TextNode.valueOf("testfn")) .put("lastname", TextNode.valueOf("testln")); Assert.assertEquals(expected, result); }
@Test public void shouldReturnObjectNodeWithMissingSchema() { final ObjectSchema schema = new ObjectSchema(); final PactRecord record = new PactRecord(); final ObjectNode object = new ObjectNode().put("firstName", TextNode.valueOf("Hans")) .put("lastName", TextNode.valueOf("Maier")).put("age", IntNode.valueOf(23)); record.setField(0, new JsonNodeWrapper(object)); final IJsonNode result = schema.recordToJson(record, null); Assert.assertEquals(object, result); }
@Test(expected = IllegalStateException.class) public void shouldThrowExceptionWhenSchemaAndRecordDontMatch() { final ObjectSchema schema = new ObjectSchema("firstname", "lastname"); final PactRecord record = new PactRecord(); record.setField(0, new JsonNodeWrapper(TextNode.valueOf("testfn"))); schema.recordToJson(record, null); }
@Test public void shouldUseJsonNodeTarget() { final ObjectSchema schema = new ObjectSchema("firstname", "lastname"); final PactRecord record = new PactRecord(); record.setField(0, new JsonNodeWrapper(TextNode.valueOf("testfn"))); record.setField(1, new JsonNodeWrapper(TextNode.valueOf("testln"))); record.setField(2, new JsonNodeWrapper(new ObjectNode())); final IJsonNode target = new ObjectNode(); final IJsonNode result = schema.recordToJson(record, target); Assert.assertSame(target, result); }