Search in sources :

Example 1 with BadRequestException

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);
}
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)

Example 2 with BadRequestException

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);
}
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)

Example 3 with BadRequestException

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);
}
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)

Example 4 with BadRequestException

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);
}
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 5 with BadRequestException

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);
}
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)

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