MessageMarshallerUtils { public static String getMessage(RopRequest request, MessageFormat format) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); if (format == MessageFormat.json) { if (request.getRopRequestContext() != null) { jsonObjectMapper.writeValue(bos, request.getRopRequestContext().getAllParams()); } else { return ""; } } else { if (request.getRopRequestContext() != null) { xmlObjectMapper.writeValue(bos, request.getRopRequestContext().getAllParams()); } else { return ""; } } return bos.toString(UTF_8); } catch (Exception e) { throw new RopException(e); } }  static String getMessage(RopRequest request, MessageFormat format); static String asUrlString(Map<String,String> allParams); static String getMessage(Object object, MessageFormat format);  }
@Test public void testMarshallerRopRequest() throws Exception { RopRequest ropRequest = mock(RopRequest.class); RopRequestContext msc = mock(RopRequestContext.class); when(ropRequest.getRopRequestContext()).thenReturn(msc); HashMap<String, String> map = new HashMap<String, String>(); map.put("key1", "key1Value"); map.put("key2", "key2Value"); map.put("key3", "key3Value"); when(msc.getAllParams()).thenReturn(map); String message = MessageMarshallerUtils.getMessage(ropRequest, MessageFormat.json); assertNotNull(message); logger.info("json:{}",message); assertTrue(message.indexOf("}") > -1); assertTrue(message.indexOf("{") > -1); assertTrue(message.indexOf("key1") > -1); assertTrue(message.indexOf("key1Value") > -1); message = MessageMarshallerUtils.getMessage(ropRequest, MessageFormat.xml); assertNotNull(message); assertTrue(message.indexOf("<") > -1); assertTrue(message.indexOf(">") > -1); assertTrue(message.indexOf("key1") > -1); assertTrue(message.indexOf("key1Value") > -1); logger.info("xml:{}", message); }
@Test public void testMarshallerRopResponse() throws Exception { SampleResponse response = new SampleResponse(); response.setUserId("tom"); response.setCreateTime("20120101"); List<HashMap<String,Object>> table = new ArrayList<HashMap<String,Object>>(); HashMap<String,Object> row1 = new HashMap<String, Object>(); row1.put("col1", "id1"); row1.put("col2", "user1"); row1.put("col3", 20); row1.put("col4", 1000.34); table.add(row1); HashMap<String,Object> row2 = new HashMap<String, Object>(); row2.put("col1", "id2"); row2.put("col2", null); row2.put("col3", 22); row2.put("col4", 2000.34); table.add(row2); response.setTable(table); HashMap<String,String> attaches = new HashMap<String, String>(); attaches.put("a","aa"); attaches.put("b","bb"); attaches.put("c","cc"); response.setAttaches(attaches); String message = MessageMarshallerUtils.getMessage(response, MessageFormat.json); assertTrue(message.indexOf("}") > -1); assertTrue(message.indexOf("{") > -1); assertTrue(message.indexOf("tom") > -1); assertTrue(message.indexOf("20120101") > -1); logger.info("json:{}",message); }