Search in sources :

Example 6 with BadRequestException

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Calendar(java.util.Calendar) BadRequestException(ninja.exceptions.BadRequestException) IOException(java.io.IOException) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) Test(org.junit.Test)

Example 7 with BadRequestException

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));
}
Also used : BadRequestException(ninja.exceptions.BadRequestException) Test(org.junit.Test)

Example 8 with BadRequestException

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);
}
Also used : BadRequestException(ninja.exceptions.BadRequestException) Test(org.junit.Test)

Example 9 with BadRequestException

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();
}
Also used : BadRequestException(ninja.exceptions.BadRequestException) Route(ninja.Route) BadRequestException(ninja.exceptions.BadRequestException) Result(ninja.Result)

Example 10 with BadRequestException

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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BadRequestException(ninja.exceptions.BadRequestException) IOException(java.io.IOException) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) Test(org.junit.Test)

Aggregations

BadRequestException (ninja.exceptions.BadRequestException)17 Test (org.junit.Test)16 ByteArrayInputStream (java.io.ByteArrayInputStream)10 IOException (java.io.IOException)10 InputStream (java.io.InputStream)10 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)5 ParseException (java.text.ParseException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Calendar (java.util.Calendar)3 Optional (java.util.Optional)1 Result (ninja.Result)1 Route (ninja.Route)1 DiagnosticError (ninja.diagnostics.DiagnosticError)1 Message (ninja.utils.Message)1