XstreamRecordMapper implements RecordMapper<String, P> { @Override @SuppressWarnings(value = "unchecked") public Record<P> processRecord(final Record<String> record) { P unmarshalledObject = (P) xStream.fromXML(record.getPayload()); return new GenericRecord<>(record.getHeader(), unmarshalledObject); } XstreamRecordMapper(final XStream xStream); @Override @SuppressWarnings(value = "unchecked") Record<P> processRecord(final Record<String> record);  }
@Test public void testValidXmlPersonMapping() throws Exception { when(xmlRecord.getPayload()).thenReturn(getXmlFromFile("/person.xml")); Record<Person> actual = xmlRecordMapper.processRecord(xmlRecord); Person person = actual.getPayload(); assertThat(actual.getHeader()).isEqualTo(header); assertThat(person).isNotNull(); assertThat(person.getId()).isEqualTo(1); assertThat(person.getFirstName()).isEqualTo("foo"); assertThat(person.getLastName()).isEqualTo("bar"); assertThat(person.isMarried()).isTrue(); }
@Test public void testEmptyXmlPersonMapping() throws Exception { when(xmlRecord.getPayload()).thenReturn("<person/>"); Record<Person> actual = xmlRecordMapper.processRecord(xmlRecord); Person person = actual.getPayload(); assertThat(actual.getHeader()).isEqualTo(header); assertThat(person.getId()).isEqualTo(0); assertThat(person.getFirstName()).isNull(); assertThat(person.getLastName()).isNull(); assertThat(person.isMarried()).isFalse(); }
@Test public void testPartialXmlPersonMapping() throws Exception { when(xmlRecord.getPayload()).thenReturn(getXmlFromFile("/person-partial.xml")); Record<Person> actual = xmlRecordMapper.processRecord(xmlRecord); Person person = actual.getPayload(); assertThat(actual.getHeader()).isEqualTo(header); assertThat(person).isNotNull(); assertThat(person.getId()).isEqualTo(1); assertThat(person.getFirstName()).isEqualTo("foo"); assertThat(person.getLastName()).isNull(); assertThat(person.isMarried()).isFalse(); }
@Test(expected = Exception.class) public void testInvalidXmlPersonMapping() throws Exception { when(xmlRecord.getPayload()).thenReturn(getXmlFromFile("/person-invalid.xml")); xmlRecordMapper.processRecord(xmlRecord); }