ContractDeserializer extends JsonDeserializer<Contract> { @Override public Contract deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectCodec oc = parser.getCodec(); ObjectNode treeNode = oc.readTree(parser); Contract contract = new Contract(); parseNodeContractInput(childInput(treeNode), contract); return contract; }  @Override Contract deserialize(JsonParser parser, DeserializationContext ctxt); @Override Class<?> handledType();  }
@Test public void deserialize_a_serialized_contract_json_document_into_a_contract_object() throws Exception { Contract aSimpleContract = aSimpleContract(); byte[] serializedContract = new ObjectMapper().writeValueAsBytes(aSimpleContract); ContractDeserializer contractDeserializer = new ContractDeserializer(); Contract contract = contractDeserializer.deserialize(new JsonFactory(new ObjectMapper()).createParser(serializedContract), null); assertThat(contract.getInput()).extracting("name", "type").containsExactly( tuple("name", String.class.getName()), tuple("isValid", Boolean.class.getName()), tuple("ticket", NodeContractInput.class.getName())); assertThat(find(contract.getInput(), instanceOf(NodeContractInput.class)).getInput()).extracting("name", "type").containsExactly( tuple("title", String.class.getName()), tuple("creationDate", Date.class.getName()), tuple("creationLocalDate", LocalDate.class.getName()), tuple("creationLocalDateTime", LocalDateTime.class.getName()), tuple("creationOffsetDateTime", OffsetDateTime.class.getName()), tuple("updateTime", Long.class.getName())); }
@Test public void should_handle_serialization_of_a_contract_with_data_references() throws Exception { Contract aSimpleContractWithDataRef = aSimpleContractWithDataRef(EditMode.EDIT); byte[] serializedContract = new ObjectMapper().writeValueAsBytes(aSimpleContractWithDataRef); ContractDeserializer contractDeserializer = new ContractDeserializer(); Contract contract = contractDeserializer.deserialize(new JsonFactory(new ObjectMapper()).createParser(serializedContract), null); assertThat(contract.getInput()).extracting("name", "type","dataReference").containsExactly( tuple("employeeInput", NodeContractInput.class.getName(),new BusinessDataReference("employee","org.test.Employee",RelationType.COMPOSITION,LoadingType.EAGER))); assertThat(find(contract.getInput(), instanceOf(NodeContractInput.class)).getInput()).extracting("name", "type").containsExactly( tuple("firstName", String.class.getName()), tuple("lastName", String.class.getName()), tuple("birthDate", LocalDate.class.getName()), tuple("manager", NodeContractInput.class.getName()), tuple("addresses", NodeContractInput.class.getName())); }
@Test public void deserialize_an_contact_with_an_input_without_name() throws Exception { Contract anEmptyContract = aContract() .withInput(aContractInput(null).withType(null).withDescription(null).build()).build(); byte[] serializedContract = new ObjectMapper().writeValueAsBytes(anEmptyContract); ContractDeserializer contractDeserializer = new ContractDeserializer(); Contract contract = contractDeserializer.deserialize(new JsonFactory(new ObjectMapper()).createParser(serializedContract), null); assertThat(contract.getInput()).extracting("name", "type", "description", "mandatory", "multiple", "input") .contains(tuple("", String.class.getName(), null, false, false, newArrayList())); }