AbstractContext implements Context.Impl { @Override public <T> T parseBody(Class<T> classOfT) { String rawContentType = getRequestContentType(); if (rawContentType == null) { logger.debug("Not able to parse body because request did not send content type header at: {}", getRequestPath()); return null; } String contentTypeOnly = HttpHeaderUtils.getContentTypeFromContentTypeAndCharacterSetting( rawContentType); BodyParserEngine bodyParserEngine = bodyParserEngineManager .getBodyParserEngineForContentType(contentTypeOnly); if (bodyParserEngine == null) { logger.debug("No BodyParserEngine found for Content-Type {} at route {}", contentTypeOnly, getRequestPath()); return null; } return bodyParserEngine.invoke(this, classOfT); } @Inject  AbstractContext(
            BodyParserEngineManager bodyParserEngineManager,
            FlashScope flashScope,
            NinjaProperties ninjaProperties,
            Session session,
            Validation validation,
            Injector injector,
            ParamParsers paramParsers); @Override void setRoute(Route route); @Override Route getRoute(); @Override Validation getValidation(); @Deprecated @Override FlashScope getFlashCookie(); @Override FlashScope getFlashScope(); @Deprecated @Override Session getSessionCookie(); @Override Session getSession(); @Override String getContextPath(); @Override String getRequestPath(); @Override String getRemoteAddr(); @Override T getAttribute(String name, Class<T> clazz); @Override String getPathParameter(String key); @Override String getPathParameterEncoded(String key); @Override Integer getPathParameterAsInteger(String key); @Override String getParameter(String key, String defaultValue); @Override Integer getParameterAsInteger(String key); @Override Integer getParameterAsInteger(String key, Integer defaultValue); @Override T getParameterAs(String key, Class<T> clazz); @Override T getParameterAs(String key, Class<T> clazz, T defaultValue); @Override T parseBody(Class<T> classOfT); @Override String getCookieValue(String name); @Override void unsetCookie(Cookie cookie); @Override void asyncRequestComplete(); @Override ResponseStreams finalizeHeadersWithoutFlashAndSessionCookie(Result result); @Override ResponseStreams finalizeHeaders(Result result); @Override String getAcceptContentType(); @Override String getAcceptEncoding(); @Override String getAcceptLanguage(); @Override String getAcceptCharset(); @Override boolean isRequestJson(); @Override boolean isRequestXml();  }
@Test public void testParseBodyJsonWorks() { AbstractContextImpl context = spy(abstractContext); doReturn("application/json; charset=utf-8").when(context).getRequestContentType(); when(bodyParserEngineManager.getBodyParserEngineForContentType("application/json")).thenReturn(bodyParserEngine); when(bodyParserEngine.invoke(context, Dummy.class)).thenReturn(new Dummy()); Object o = context.parseBody(Dummy.class); verify(bodyParserEngineManager).getBodyParserEngineForContentType("application/json"); assertTrue(o instanceof Dummy); }
@Test public void testParseBodyPostWorks() { AbstractContextImpl context = spy(abstractContext); doReturn(ContentTypes.APPLICATION_POST_FORM).when(context).getRequestContentType(); when(bodyParserEngineManager.getBodyParserEngineForContentType(ContentTypes.APPLICATION_POST_FORM)).thenReturn(bodyParserEngine); Dummy dummy = new Dummy(); dummy.name = "post"; dummy.count = 245L; when(bodyParserEngine.invoke(context, Dummy.class)).thenReturn(dummy); Dummy o = context.parseBody(Dummy.class); verify(bodyParserEngineManager).getBodyParserEngineForContentType(ContentTypes.APPLICATION_POST_FORM); assertTrue(o instanceof Dummy); assertTrue(o.name.equals(dummy.name)); assertTrue(o.count.equals(dummy.count)); }
@Test public void testParseBodyXmlWorks() { AbstractContextImpl context = spy(abstractContext); doReturn(ContentTypes.APPLICATION_XML).when(context).getRequestContentType(); when(bodyParserEngineManager.getBodyParserEngineForContentType("application/xml")).thenReturn(bodyParserEngine); when(bodyParserEngine.invoke(context, Dummy.class)).thenReturn(new Dummy()); Object o = context.parseBody(Dummy.class); verify(bodyParserEngineManager).getBodyParserEngineForContentType("application/xml"); assertTrue(o instanceof Dummy); }
@Test public void testParseBodyWithUnkownContentTypeWorks() { AbstractContextImpl context = spy(abstractContext); doReturn(null).when(context).getRequestContentType(); Object o = context.parseBody(Dummy.class); assertNull(o); }
@Test public void testParseBodyWithUnknownRequestContentTypeWorks() { AbstractContextImpl context = spy(abstractContext); doReturn("application/UNKNOWN").when(context).getRequestContentType(); Object o = context.parseBody(Dummy.class); assertNull(o); }