use of ninja.exceptions.BadRequestException in project ninja by ninjaframework.
the class BodyParserEngineXmlTest method testInvalidXmlMissingRootBody.
@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);
}
use of ninja.exceptions.BadRequestException in project ninja by ninjaframework.
the class BodyParserEngineXmlTest method testEmptyXmlBody.
@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);
}
use of ninja.exceptions.BadRequestException in project ninja by ninjaframework.
the class BodyParserEngineXmlTest method testInvalidXmlBadCloseBody.
@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);
}
use of ninja.exceptions.BadRequestException in project ninja by ninjaframework.
the class BodyParserEngineJsonTest method testJsonBodyWithFullSpacesAndEndOfLines.
@Test
public void testJsonBodyWithFullSpacesAndEndOfLines() {
final String jsonDocument = String.format(" \n\n\n { \n \"firstName\" \n : \"%s\", \"lastName\"\n : \"%s\", \"birthYear\":%d,\n \"lastSeen\":\"%s\"} ", BodyParserEngineJsonTest.DATA_FIRSTNAME, BodyParserEngineJsonTest.DATA_LASTNAME, BodyParserEngineJsonTest.DATA_BIRTHYEAR, BodyParserEngineJsonTest.DATA_LASTSEEN);
final InputStream is = new ByteArrayInputStream(jsonDocument.getBytes());
final ObjectMapper jsonObjMapper = new ObjectMapper();
final BodyParserEngineJson bodyParserEngineJson = new BodyParserEngineJson(jsonObjMapper);
SimpleTestForm testForm = null;
try {
Mockito.when(context.getInputStream()).thenReturn(is);
} catch (IOException ignore) {
}
try {
testForm = bodyParserEngineJson.invoke(context, SimpleTestForm.class);
} catch (BadRequestException ignore) {
} finally {
try {
is.close();
} catch (IOException ignore) {
}
}
final Calendar cal = Calendar.getInstance();
final SimpleDateFormat dateFormat = new SimpleDateFormat(BodyParserEngineJsonTest.PARSER_DATEFORMAT);
dateFormat.setTimeZone(TimeZone.getTimeZone(BodyParserEngineJsonTest.PARSER_DATETZ));
try {
cal.setTime(dateFormat.parse(BodyParserEngineJsonTest.DATA_LASTSEEN));
} catch (ParseException ignore) {
}
cal.setTimeZone(TimeZone.getTimeZone(BodyParserEngineJsonTest.PARSER_DATETZ));
assertTrue(testForm != null);
assertThat(testForm.firstName, equalTo(BodyParserEngineJsonTest.DATA_FIRSTNAME));
assertThat(testForm.lastName, equalTo(BodyParserEngineJsonTest.DATA_LASTNAME));
assertThat(testForm.birthYear, CoreMatchers.equalTo(BodyParserEngineJsonTest.DATA_BIRTHYEAR));
assertTrue(testForm.lastSeen != null);
assertTrue(testForm.lastSeen.compareTo(cal) == 0);
}
use of ninja.exceptions.BadRequestException in project ninja by ninjaframework.
the class BodyParserEngineJsonTest method testEmptyJsonBody.
@Test
public void testEmptyJsonBody() {
final String jsonDocument = "";
final InputStream is = new ByteArrayInputStream(jsonDocument.getBytes());
final ObjectMapper jsonObjMapper = new ObjectMapper();
final BodyParserEngineJson bodyParserEngineJson = new BodyParserEngineJson(jsonObjMapper);
boolean badRequestThrown = false;
try {
Mockito.when(context.getInputStream()).thenReturn(is);
} catch (IOException ignore) {
}
try {
bodyParserEngineJson.invoke(context, SimpleTestForm.class);
} catch (BadRequestException ignore) {
badRequestThrown = true;
} finally {
try {
is.close();
} catch (IOException ignore) {
}
}
assertTrue(badRequestThrown);
}
Aggregations