@Test public void renderSimpleBeanWithJsonView() throws Exception { Object bean = new TestBeanSimple(); Map<String, Object> model = new HashMap<>(); model.put("bindingResult", mock(BindingResult.class, "binding_result")); model.put("foo", bean); model.put(JsonView.class.getName(), MyJacksonView1.class); view.setUpdateContentLength(true); view.render(model, request, response); String content = response.getContentAsString(); assertTrue(content.length() > 0); assertEquals(content.length(), response.getContentLength()); assertTrue(content.contains("foo")); assertFalse(content.contains("boo")); assertFalse(content.contains(JsonView.class.getName())); }
@Test public void renderSimpleBean() throws Exception { Object bean = new TestBeanSimple(); Map<String, Object> model = new HashMap<>(); model.put("bindingResult", mock(BindingResult.class, "binding_result")); model.put("foo", bean); view.setUpdateContentLength(true); view.render(model, request, response); assertTrue(response.getContentAsString().length() > 0); assertEquals(response.getContentAsString().length(), response.getContentLength()); validateResult(); }
@Test public void renderSimpleBeanWithFilters() throws Exception { TestSimpleBeanFiltered bean = new TestSimpleBeanFiltered(); bean.setProperty1("value"); bean.setProperty2("value"); Map<String, Object> model = new HashMap<>(); model.put("bindingResult", mock(BindingResult.class, "binding_result")); model.put("foo", bean); FilterProvider filters = new SimpleFilterProvider().addFilter("myJacksonFilter", SimpleBeanPropertyFilter.serializeAllExcept("property2")); model.put(FilterProvider.class.getName(), filters); view.setUpdateContentLength(true); view.render(model, request, response); String content = response.getContentAsString(); assertTrue(content.length() > 0); assertEquals(content.length(), response.getContentLength()); assertThat(content, containsString("\"property1\":\"value\"")); assertThat(content, not(containsString("\"property2\":\"value\""))); assertFalse(content.contains(FilterProvider.class.getName())); }
@Test public void renderSimpleMap() throws Exception { Map<String, Object> model = new HashMap<>(); model.put("bindingResult", mock(BindingResult.class, "binding_result")); model.put("foo", "bar"); view.setUpdateContentLength(true); view.render(model, request, response); assertEquals("no-store", response.getHeader("Cache-Control")); assertEquals(MappingJackson2JsonView.DEFAULT_CONTENT_TYPE, response.getContentType()); String jsonResult = response.getContentAsString(); assertTrue(jsonResult.length() > 0); assertEquals(jsonResult.length(), response.getContentLength()); validateResult(); }