XmlRecordMapper implements RecordMapper<String, P> { @Override @SuppressWarnings(value = "unchecked") public Record<P> processRecord(final Record<String> record) throws Exception { P unmarshalledObject = (P) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(record.getPayload().getBytes())); return new GenericRecord<>(record.getHeader(), unmarshalledObject); } XmlRecordMapper(final Class<P> type);  XmlRecordMapper(final Class<P> type, final Path xsd);  XmlRecordMapper(final Unmarshaller unmarshaller); @Override @SuppressWarnings(value = "unchecked") Record<P> processRecord(final Record<String> record);  }
@Test public void testValidXmlPersonMapping() throws Exception { xmlRecord = new XmlRecord(header, getXmlFromFile("/person.xml")); Record<Person> mappedRecord = xmlRecordMapper.processRecord(xmlRecord); Person person = mappedRecord.getPayload(); assertThat(person).isNotNull(); assertThat(person.getId()).isEqualTo(1); assertThat(person.getFirstName()).isEqualTo("foo"); assertThat(person.getLastName()).isEqualTo("bar"); assertThat(person.getBirthDate()).isEqualTo(new SimpleDateFormat("yyyy-MM-dd").parse("2000-01-01")); assertThat(person.isMarried()).isTrue(); }
@Test public void testEmptyXmlPersonMapping() throws Exception { xmlRecord = new XmlRecord(header, "<person/>"); Record<Person> mappedRecord = xmlRecordMapper.processRecord(xmlRecord); Person person = mappedRecord.getPayload(); assertThat(person.getId()).isEqualTo(0); assertThat(person.getFirstName()).isNull(); assertThat(person.getLastName()).isNull(); assertThat(person.getBirthDate()).isNull(); assertThat(person.isMarried()).isFalse(); }
@Test public void testPartialXmlPersonMapping() throws Exception { xmlRecord = new XmlRecord(header, getXmlFromFile("/person-partial.xml")); Record<Person> mappedRecord = xmlRecordMapper.processRecord(xmlRecord); Person person = mappedRecord.getPayload(); assertThat(person).isNotNull(); assertThat(person.getId()).isEqualTo(1); assertThat(person.getFirstName()).isEqualTo("foo"); assertThat(person.getLastName()).isNull(); assertThat(person.getBirthDate()).isNull(); assertThat(person.isMarried()).isFalse(); }
@Test public void testMappingWithEscapedXmlSpecialCharacter() throws Exception { xmlRecord = new XmlRecord(header, "<website name='google' url='http: XmlRecordMapper<Website> xmlRecordMapper = new XmlRecordMapper<>(Website.class); Record<Website> mappedRecord = xmlRecordMapper.processRecord(xmlRecord); Website website = mappedRecord.getPayload(); assertThat(website).isNotNull(); assertThat(website.getName()).isEqualTo("google"); assertThat(website.getUrl()).isEqualTo("http: xmlRecord = new XmlRecord(header, "<website name='l&apos;equipe' url='http: mappedRecord = xmlRecordMapper.processRecord(xmlRecord); website = mappedRecord.getPayload(); assertThat(website).isNotNull(); assertThat(website.getName()).isEqualTo("l'equipe"); assertThat(website.getUrl()).isEqualTo("http: }
@Test(expected = Exception.class) public void testMappingWithUnescapedXmlSpecialCharacter() throws Exception { xmlRecord = new XmlRecord(header, "<website name='google' url='http: XmlRecordMapper<Website> xmlRecordMapper = new XmlRecordMapper<>(Website.class); xmlRecordMapper.processRecord(xmlRecord); }
@Test(expected = Exception.class) public void testInvalidXmlPersonMapping() throws Exception { xmlRecord = new XmlRecord(header, getXmlFromFile("/person-invalid.xml")); xmlRecordMapper.processRecord(xmlRecord); }
@Test(expected = Exception.class) public void testMappingOfInvalidXmlAccordingToXsd() throws Exception { xmlRecord = new XmlRecord(header, getXmlFromFile("/person-invalid-xsd.xml")); xmlRecordMapper = new XmlRecordMapper<>(Person.class, Paths.get("src/test/resources/person.xsd")); xmlRecordMapper.processRecord(xmlRecord); }