use of ninja.exceptions.BadRequestException in project ninja by ninjaframework.
the class BodyParserEngineXmlTest method testValidXmlBody.
@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);
}
use of ninja.exceptions.BadRequestException in project ninja by ninjaframework.
the class NinjaDefaultTest method testThatGetBadRequestContentNegotiation.
@Test
public void testThatGetBadRequestContentNegotiation() {
Result result = ninjaDefault.getBadRequestResult(contextImpl, new BadRequestException("not important"));
assertThat(result.getContentType(), equalTo(null));
assertThat(result.supportedContentTypes().size(), equalTo(3));
}
use of ninja.exceptions.BadRequestException in project ninja by ninjaframework.
the class NinjaDefaultTest method testOnRouteRequestWhenOnBadRequest.
@Test
public void testOnRouteRequestWhenOnBadRequest() {
FilterChain filterChain = Mockito.mock(FilterChain.class);
Mockito.when(route.getFilterChain()).thenReturn(filterChain);
BadRequestException badRequest = new BadRequestException("That's a BadRequest that should be handled by onBadRequest");
Mockito.when(filterChain.next(contextImpl)).thenThrow(badRequest);
ninjaDefault.onRouteRequest(contextImpl);
verify(ninjaDefault).getBadRequestResult(contextImpl, badRequest);
}
use of ninja.exceptions.BadRequestException in project ninja by ninjaframework.
the class InstrumentedNinja method onRouteRequest.
@Override
@Timed
public void onRouteRequest(Context.Impl context) {
activeRequests.inc();
String httpMethod = context.getMethod();
Route route = router.getRouteFor(httpMethod, context.getRequestPath());
context.setRoute(route);
if (route != null) {
allRequestsMeter.mark();
try {
Result result = route.getFilterChain().next(context);
resultHandler.handleResult(result, context);
} catch (Exception exception) {
if (exception instanceof BadRequestException) {
badRequests.mark();
} else {
internalServerErrors.mark();
}
Result result = onException(context, exception);
renderErrorResultAndCatchAndLogExceptions(result, context);
}
} else {
// throw a 404 "not found" because we did not find the route
routesNotFound.mark();
Result result = getNotFoundResult(context);
renderErrorResultAndCatchAndLogExceptions(result, context);
}
activeRequests.dec();
}
use of ninja.exceptions.BadRequestException in project ninja by ninjaframework.
the class BodyParserEngineXmlTest method testXmlBodyWithMissingVariables.
@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);
}
Aggregations