HttpRequestUtils { public static Object getJsonBody(final HttpServletRequest request) throws ServletException { try { return JSONUtils.parseJSONFromString(getBody(request)); } catch (final IOException e) { throw new ServletException("HTTP Request JSON Body cannot be parsed.", e); } }  static ExecutionOptions parseFlowOptions(final HttpServletRequest req,
      final String flowName); static void filterAdminOnlyFlowParams(final UserManager userManager,
      final ExecutionOptions options, final User user); static boolean validateIntegerParam(final Map<String, String> params,
      final String paramName); static boolean hasPermission(final UserManager userManager, final User user,
      final Permission.Type type); static boolean hasParam(final HttpServletRequest request, final String param); static String getParam(final HttpServletRequest request, final String name); static String getParam(final HttpServletRequest request, final String name,
      final String defaultVal); static int getIntParam(final HttpServletRequest request, final String name); static int getIntParam(final HttpServletRequest request, final String name,
      final int defaultVal); static boolean getBooleanParam(final HttpServletRequest request, final String name); static boolean getBooleanParam(final HttpServletRequest request,
      final String name, final boolean defaultVal); static long getLongParam(final HttpServletRequest request, final String name); static long getLongParam(final HttpServletRequest request, final String name,
      final long defaultVal); static Map<String, String> getParamGroup(final HttpServletRequest request,
      final String groupName); static Object getJsonBody(final HttpServletRequest request); static String getBody(final HttpServletRequest request); static final String PARAM_SLA_EMAILS; static final String PARAM_SLA_SETTINGS; }
@Test public void testGetJsonBodyForListOfMapObject() throws IOException, ServletException { final HttpServletRequest httpRequest = Mockito.mock(HttpServletRequest.class); final String originalString = "[\n" + " {\n" + " \"action\": \"update\",\n" + " \"table\": \"ramp\",\n" + " \"conditions\" : {\n" + " \"rampId\" : \"dali\"\n" + " },\n" + " \"values\": {\n" + " \"rampStage\": 2,\n" + " \"lastUpdatedTime\": 1566259437000\n" + " }\n" + " }\n" + "]"; final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes(UTF_8)); final InputStreamReader inputStreamReader = new InputStreamReader(inputStream, UTF_8); final BufferedReader bufferedReader = new BufferedReader(inputStreamReader); Mockito.when(httpRequest.getReader()).thenReturn(bufferedReader); final Object object = HttpRequestUtils.getJsonBody(httpRequest); Assert.assertTrue(object instanceof List); final List<Map<String, Object>> list = (List<Map<String, Object>>) object; Assert.assertEquals(list.size(), 1); Assert.assertEquals(list.get(0).get("action").toString(), "update"); Assert.assertEquals(list.get(0).get("table").toString(), "ramp"); Assert.assertEquals( ((Map<String, Object>) list.get(0).get("conditions")).get("rampId").toString(), "dali"); Assert.assertEquals(((Map<String, Object>) list.get(0).get("values")).get("rampStage"), 2); Assert.assertEquals(((Map<String, Object>) list.get(0).get("values")).get("lastUpdatedTime"), 1566259437000L); }
@Test public void testGetJsonBodyForSingleMapObject() throws IOException, ServletException { final HttpServletRequest httpRequest = Mockito.mock(HttpServletRequest.class); final String originalString = " {\n" + " \"action\": \"update\",\n" + " \"table\": \"ramp\",\n" + " \"conditions\" : {\n" + " \"rampId\" : \"dali\"\n" + " },\n" + " \"values\": {\n" + " \"rampStage\": 2,\n" + " \"lastUpdatedTime\": 1566259437000\n" + " }\n" + " }\n"; final InputStream inputStream = new ByteArrayInputStream(originalString.getBytes(UTF_8)); final InputStreamReader inputStreamReader = new InputStreamReader(inputStream, UTF_8); final BufferedReader bufferedReader = new BufferedReader(inputStreamReader); Mockito.when(httpRequest.getReader()).thenReturn(bufferedReader); final Object object = HttpRequestUtils.getJsonBody(httpRequest); Assert.assertTrue(object instanceof Map); final Map<String, Object> singleObj = (Map<String, Object>) object; Assert.assertEquals(singleObj.get("action").toString(), "update"); Assert.assertEquals(singleObj.get("table").toString(), "ramp"); Assert .assertEquals(((Map<String, Object>) singleObj.get("conditions")).get("rampId").toString(), "dali"); Assert.assertEquals(((Map<String, Object>) singleObj.get("values")).get("rampStage"), 2); Assert.assertEquals(((Map<String, Object>) singleObj.get("values")).get("lastUpdatedTime"), 1566259437000L); }