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