BodyParserEngineXml implements BodyParserEngine { public <T> T invoke(Context context, Class<T> classOfT) { try { return xmlMapper.readValue(context.getInputStream(), classOfT); } catch (JsonParseException | JsonMappingException e) { throw new BadRequestException("Error parsing incoming Xml", e); } catch (IOException e) { throw new BadRequestException("Invalid Xml document", e); } } @Inject  BodyParserEngineXml(XmlMapper xmlMapper); T invoke(Context context, Class<T> classOfT); String getContentType();  }
@Test public void testValidXmlBody() { final String xmlDocument = String.format("<form><firstName>%s</firstName><lastName>%s</lastName><birthYear>%d</birthYear><lastSeen>%s</lastSeen></form>", BodyParserEngineXmlTest.DATA_FIRSTNAME, BodyParserEngineXmlTest.DATA_LASTNAME, BodyParserEngineXmlTest.DATA_BIRTHYEAR, BodyParserEngineXmlTest.DATA_LASTSEEN); final InputStream is = new ByteArrayInputStream(xmlDocument.getBytes()); final XmlMapper xmlObjMapper = new XmlMapper(); final BodyParserEngineXml bodyParserEngineXml = new BodyParserEngineXml(xmlObjMapper); SimpleTestForm testForm = null; try { Mockito.when(context.getInputStream()).thenReturn(is); } catch (IOException ignore) { } try { testForm = bodyParserEngineXml.invoke(context, SimpleTestForm.class); } catch (BadRequestException ignore) { } finally { try { is.close(); } catch (IOException ignore) { } } final Calendar cal = Calendar.getInstance(); final SimpleDateFormat dateFormat = new SimpleDateFormat(BodyParserEngineXmlTest.PARSER_DATEFORMAT); dateFormat.setTimeZone(TimeZone.getTimeZone(BodyParserEngineXmlTest.PARSER_DATETZ)); try { cal.setTime(dateFormat.parse(BodyParserEngineXmlTest.DATA_LASTSEEN)); } catch (ParseException ignore) { } cal.setTimeZone(TimeZone.getTimeZone(BodyParserEngineXmlTest.PARSER_DATETZ)); assertTrue(testForm != null); assertThat(testForm.firstName, equalTo(BodyParserEngineXmlTest.DATA_FIRSTNAME)); assertThat(testForm.lastName, equalTo(BodyParserEngineXmlTest.DATA_LASTNAME)); assertThat(testForm.birthYear, CoreMatchers.equalTo(BodyParserEngineXmlTest.DATA_BIRTHYEAR)); assertTrue(testForm.lastSeen != null); assertTrue(testForm.lastSeen.compareTo(cal) == 0); }
@Test public void testXmlBodyWithMissingVariables() { final String xmlDocument = String.format("<form><firstName>%s</firstName><lastName>%s</lastName></form>", BodyParserEngineXmlTest.DATA_FIRSTNAME, BodyParserEngineXmlTest.DATA_LASTNAME); final InputStream is = new ByteArrayInputStream(xmlDocument.getBytes()); final XmlMapper xmlObjMapper = new XmlMapper(); final BodyParserEngineXml bodyParserEngineXml = new BodyParserEngineXml(xmlObjMapper); SimpleTestForm testForm = null; try { Mockito.when(context.getInputStream()).thenReturn(is); } catch (IOException ignore) { } try { testForm = bodyParserEngineXml.invoke(context, SimpleTestForm.class); } catch (BadRequestException ignore) { } finally { try { is.close(); } catch (IOException ignore) { } } assertTrue(testForm != null); assertThat(testForm.firstName, equalTo(BodyParserEngineXmlTest.DATA_FIRSTNAME)); assertThat(testForm.lastName, equalTo(BodyParserEngineXmlTest.DATA_LASTNAME)); assertTrue(testForm.birthYear == null); assertTrue(testForm.lastSeen == null); }
@Test public void testEmptyXmlBody() { final String xmlDocument = ""; final InputStream is = new ByteArrayInputStream(xmlDocument.getBytes()); final XmlMapper xmlObjMapper = new XmlMapper(); final BodyParserEngineXml bodyParserEngineXml = new BodyParserEngineXml(xmlObjMapper); boolean badRequestThrown = false; try { Mockito.when(context.getInputStream()).thenReturn(is); } catch (IOException ignore) { } try { bodyParserEngineXml.invoke(context, SimpleTestForm.class); } catch (BadRequestException ignore) { badRequestThrown = true; } finally { try { is.close(); } catch (IOException ignore) { } } assertTrue(badRequestThrown); }
@Test public void testInvalidXmlBadCloseBody() { final String xmlDocument = String.format("<form><firstName>%s</firstName><lastName>%s</lastName><birthYear>%d</birthYear><lastSeen>%s</lastSeen></>", BodyParserEngineXmlTest.DATA_FIRSTNAME, BodyParserEngineXmlTest.DATA_LASTNAME, BodyParserEngineXmlTest.DATA_BIRTHYEAR, BodyParserEngineXmlTest.DATA_LASTSEEN); final InputStream is = new ByteArrayInputStream(xmlDocument.getBytes()); final XmlMapper xmlObjMapper = new XmlMapper(); final BodyParserEngineXml bodyParserEngineXml = new BodyParserEngineXml(xmlObjMapper); boolean badRequestThrown = false; try { Mockito.when(context.getInputStream()).thenReturn(is); } catch (IOException ignore) { } try { bodyParserEngineXml.invoke(context, SimpleTestForm.class); } catch (BadRequestException ignore) { badRequestThrown = true; } finally { try { is.close(); } catch (IOException ignore) { } } assertTrue(badRequestThrown); }
@Test public void testInvalidXmlMissingRootBody() { final String xmlDocument = String.format("<firstName>%s</firstName><lastName>%s</lastName><birthYear>%d</birthYear><lastSeen>%s</lastSeen>", BodyParserEngineXmlTest.DATA_FIRSTNAME, BodyParserEngineXmlTest.DATA_LASTNAME, BodyParserEngineXmlTest.DATA_BIRTHYEAR, BodyParserEngineXmlTest.DATA_LASTSEEN); final InputStream is = new ByteArrayInputStream(xmlDocument.getBytes()); final XmlMapper xmlObjMapper = new XmlMapper(); final BodyParserEngineXml bodyParserEngineXml = new BodyParserEngineXml(xmlObjMapper); boolean badRequestThrown = false; try { Mockito.when(context.getInputStream()).thenReturn(is); } catch (IOException ignore) { } try { bodyParserEngineXml.invoke(context, SimpleTestForm.class); } catch (BadRequestException ignore) { badRequestThrown = true; } finally { try { is.close(); } catch (IOException ignore) { } } assertTrue(badRequestThrown); }