Search in sources :

Example 11 with BadRequestException

use of ninja.exceptions.BadRequestException in project ninja by ninjaframework.

the class BodyParserEngineJsonTest method testJsonBodyWithMissingVariables.

@Test
public void testJsonBodyWithMissingVariables() {
    final String jsonDocument = String.format("{\"firstName\":\"%s\", \"lastName\":\"%s\"}", BodyParserEngineJsonTest.DATA_FIRSTNAME, BodyParserEngineJsonTest.DATA_LASTNAME);
    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) {
        }
    }
    assertTrue(testForm != null);
    assertThat(testForm.firstName, equalTo(BodyParserEngineJsonTest.DATA_FIRSTNAME));
    assertThat(testForm.lastName, equalTo(BodyParserEngineJsonTest.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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 12 with BadRequestException

use of ninja.exceptions.BadRequestException in project ninja by ninjaframework.

the class BodyParserEngineJsonTest method testInvalidJsonBody.

@Test
public void testInvalidJsonBody() {
    final String jsonDocument = String.format("{\"firstName\":\"%s\", \"lastName\":\"%s\", \"birthYear\":%d, \"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);
    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);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BadRequestException(ninja.exceptions.BadRequestException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 13 with BadRequestException

use of ninja.exceptions.BadRequestException in project ninja by ninjaframework.

the class BodyParserEngineJsonTest method testValidJsonBody.

@Test
public void testValidJsonBody() {
    final String jsonDocument = String.format("{\"firstName\":\"%s\", \"lastName\":\"%s\", \"birthYear\":%d, \"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);
}
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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 14 with BadRequestException

use of ninja.exceptions.BadRequestException in project ninja by ninjaframework.

the class NinjaDefaultTest method testThatGetBadRequestDoesFallsBackToHtml.

@Test
public void testThatGetBadRequestDoesFallsBackToHtml() {
    Result result = ninjaDefault.getBadRequestResult(contextImpl, new BadRequestException("not important"));
    assertThat(result.fallbackContentType().get(), equalTo(Result.TEXT_HTML));
}
Also used : BadRequestException(ninja.exceptions.BadRequestException) Test(org.junit.Test)

Example 15 with BadRequestException

use of ninja.exceptions.BadRequestException in project ninja by ninjaframework.

the class NinjaDefaultTest method testGetBadRequest.

@Test
public void testGetBadRequest() {
    when(ninjaProperties.getWithDefault(ArgumentMatchers.eq(NinjaConstant.LOCATION_VIEW_HTML_BAD_REQUEST_KEY), ArgumentMatchers.eq(NinjaConstant.LOCATION_VIEW_FTL_HTML_BAD_REQUEST))).thenReturn(NinjaConstant.LOCATION_VIEW_FTL_HTML_BAD_REQUEST);
    BadRequestException exception = new BadRequestException("not important");
    // real test:
    Result result = ninjaDefault.getBadRequestResult(contextImpl, exception);
    assertThat(result.getStatusCode(), equalTo(Result.SC_400_BAD_REQUEST));
    assertThat(result.getTemplate(), equalTo(NinjaConstant.LOCATION_VIEW_FTL_HTML_BAD_REQUEST));
    assertTrue(result.getRenderable() instanceof Message);
    verify(messages).getWithDefault(ArgumentMatchers.eq(NinjaConstant.I18N_NINJA_SYSTEM_BAD_REQUEST_TEXT_KEY), ArgumentMatchers.eq(NinjaConstant.I18N_NINJA_SYSTEM_BAD_REQUEST_TEXT_DEFAULT), ArgumentMatchers.eq(contextImpl), any(Optional.class));
    verify(messages).getWithDefault(ArgumentMatchers.eq(exception.getMessage()), ArgumentMatchers.eq(exception.getLocalizedMessage()), ArgumentMatchers.eq(contextImpl), any(Optional.class));
    verify(ninjaProperties).getWithDefault(ArgumentMatchers.eq(NinjaConstant.LOCATION_VIEW_HTML_BAD_REQUEST_KEY), ArgumentMatchers.eq(NinjaConstant.LOCATION_VIEW_FTL_HTML_BAD_REQUEST));
}
Also used : Message(ninja.utils.Message) Optional(java.util.Optional) BadRequestException(ninja.exceptions.BadRequestException) 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