MailMarshaller extends ObjectMapper implements Marshaller, Unmarshaller { @Override public Object unmarshal(Source source) { if (type.equalsIgnoreCase(MessageType.XML.name())) { try { return jaxbDelegate.unmarshal(source); } catch (XmlMappingException e) { if (source instanceof StreamSource) { for (Class<?> type : Arrays.asList(AcceptRequest.class, AcceptResponse.class, MailRequest.class, MailResponse.class)) { try { return readValue(((StreamSource) source).getReader(), type); } catch (JsonParseException | JsonMappingException e2) { continue; } catch (IOException io) { log.warn("Unable to read mail JSON object from source", io); throw e; } } } throw e; } } else if (type.equalsIgnoreCase(MessageType.JSON.name())) { for (Class<?> type : Arrays.asList(AcceptRequest.class, AcceptResponse.class, MailRequest.class, MailResponse.class)) { try { return readValue(((StreamSource) source).getReader(), type); } catch (JsonParseException | JsonMappingException e2) { continue; } catch (IOException io) { throw new CitrusRuntimeException("Unable to read mail JSON object from source", io); } } throw new CitrusRuntimeException("Failed to read mail JSON object from source:" + source); } else { throw new CitrusRuntimeException("Unsupported mail marshaller type: " + type); } } MailMarshaller(); @Override boolean supports(Class<?> clazz); @Override Object unmarshal(Source source); @Override void marshal(Object graph, Result result); String getType(); void setType(String type);  }
@Test(dataProvider = "mailSourceProvider") public void testUnmarshalMail(String requestSource, String responseSource) throws Exception { MailRequest request = (MailRequest) mailMarshaller.unmarshal(new StringSource(FileUtils.readToString(new ClassPathResource(requestSource)))); MailResponse response = (MailResponse) mailMarshaller.unmarshal(new StringSource(FileUtils.readToString(new ClassPathResource(responseSource)))); Assert.assertEquals(request.getFrom(), "foo@mail.com"); Assert.assertEquals(request.getTo(), "bar@mail.com,copy@mail.com"); Assert.assertEquals(response.getCode(), 250); Assert.assertEquals(response.getMessage(), "OK"); }
@Test(dataProvider = "acceptSourceProvider") public void testUnmarshalAccept(String requestSource, String responseSource) throws Exception { AcceptRequest request = (AcceptRequest) mailMarshaller.unmarshal(new StringSource(FileUtils.readToString(new ClassPathResource(requestSource)))); AcceptResponse response = (AcceptResponse) mailMarshaller.unmarshal(new StringSource(FileUtils.readToString(new ClassPathResource(responseSource)))); Assert.assertEquals(request.getFrom(), "foo@mail.com"); Assert.assertTrue(response.isAccept()); }