@Test public void testSpecialChars() throws IOException { String specialChars = new String(new char[] {(char) '香'}) + " 香蕉"; ResourceWebScriptPost extractor = new ResourceWebScriptPost(); extractor.setJsonHelper(jsonHelper); Map<String, String> templateVars = new HashMap<String, String>(); String mockMe = "{\"name\":\"" + specialChars + "\",\"created\":\"2012-03-23T15:56:18.552+0000\",\"age\":54,\"id\":\"1234A3\",\"farm\":\"LARGE\"}"; Content content = mock(Content.class); when(content.getReader()).thenReturn(new StringReader(mockMe)); WebScriptRequest request = mock(WebScriptRequest.class); when(request.getServiceMatch()).thenReturn(new Match(null, templateVars, null)); when(request.getContent()).thenReturn(content); Params params = extractor.extractParams(mockEntity(), request); assertNotNull(params); Object passed = params.getPassedIn(); assertTrue(List.class.isAssignableFrom(passed.getClass())); @SuppressWarnings("unchecked") List<Object> passedObjs = (List<Object>) passed; assertTrue(passedObjs.size() == 1); assertTrue("A Farmer was passed in.", Farmer.class.equals(passedObjs.get(0).getClass())); Farmer f = (Farmer) passedObjs.get(0); assertTrue(f.getName().equals("香 香蕉")); // Test passing in special characters as a param. ResourceWebScriptGet getExtractor = new ResourceWebScriptGet(); getExtractor.setJsonHelper(jsonHelper); Map<String, String> getTemplateVars = new HashMap<String, String>(); WebScriptRequest getRequest = mock(WebScriptRequest.class); when(getRequest.getServiceMatch()).thenReturn(new Match(null, getTemplateVars, null)); when(getRequest.getParameterNames()).thenReturn(new String[] {"aParam"}); when(getRequest.getParameterValues("aParam")).thenReturn(new String[] {specialChars}); Params pGet = getExtractor.extractParams(mockEntity(), getRequest); assertNotNull(pGet); String pVal = pGet.getParameter("aParam"); assertTrue(pVal.equals("香 香蕉")); }
@Test public void testPutExtractor() throws IOException { // Put together the stubs ResourceWebScriptPut extractor = new ResourceWebScriptPut(); extractor.setJsonHelper(jsonHelper); Map<String, String> templateVars = new HashMap<String, String>(); Content content = mock(Content.class); when(content.getReader()).thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON)); WebScriptRequest request = mock(WebScriptRequest.class); when(request.getServiceMatch()).thenReturn(new Match(null, templateVars, null)); when(request.getContent()).thenReturn(content); when(request.getContentType()).thenReturn("application/pdf; charset=UTF-16BE"); Params params = null; try { params = extractor.extractParams(mockEntity(), request); fail("Should not get here. PUT is executed against the instance URL"); } catch (UnsupportedResourceOperationException uoe) { assertNotNull(uoe); // Must throw this exception } templateVars.put(ResourceLocator.ENTITY_ID, "1234"); try { params = extractor.extractParams(mockRelationship(), request); fail("Should not get here. PUT is executed against the instance URL"); } catch (UnsupportedResourceOperationException uoe) { assertNotNull(uoe); // Must throw this exception } templateVars.put(ResourceLocator.ENTITY_ID, "1234"); // Put single entity wrapped in array params = extractor.extractParams(mockEntity(), request); assertNotNull(params); Object passed = params.getPassedIn(); assertNotNull(passed); assertTrue("A Farmer was passed in.", Farmer.class.equals(passed.getClass())); assertNotNull(params.getFilter()); assertTrue( "Default filter is BeanPropertiesFilter.AllProperties", BeanPropertiesFilter.AllProperties.class.equals(params.getFilter().getClass())); when(content.getReader()) .thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON)); // reset the reader params = extractor.extractParams(mockEntity(), request); assertNotNull(params); assertEquals("1234", params.getEntityId()); passed = params.getPassedIn(); assertNotNull(passed); when(content.getReader()) .thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON)); // reset the reader templateVars.put(ResourceLocator.RELATIONSHIP_ID, "67890"); params = extractor.extractParams(mockRelationship(), request); assertNotNull(params); assertEquals("1234", params.getEntityId()); passed = params.getPassedIn(); assertNotNull(passed); assertTrue("A Farmer was passed in.", Farmer.class.equals(passed.getClass())); Farmer aFarmer = (Farmer) passed; assertEquals( "Relationship id should be automatically set on the object passed in.", aFarmer.getId(), "67890"); when(content.getReader()) .thenReturn(new StringReader(JsonJacksonTests.FARMER_JSON)); // reset the reader params = testExtractAddressedParams(templateVars, request, extractor); assertEquals("UTF-16BE", params.getContentInfo().getEncoding()); assertEquals(MimetypeMap.MIMETYPE_PDF, params.getContentInfo().getMimeType()); }