@Test public void multipleMatches() throws Exception { this.view.setModelKeys(new HashSet<>(Arrays.asList("foo1", "foo2"))); this.model.addAttribute("foo1", Collections.singleton("bar1")); this.model.addAttribute("foo2", Collections.singleton("bar2")); this.model.addAttribute("foo3", Collections.singleton("bar3")); assertEquals("{\"foo1\":[\"bar1\"],\"foo2\":[\"bar2\"]}", doRender()); }
@Test public void singleMatch() throws Exception { this.view.setModelKeys(Collections.singleton("foo2")); this.model.addAttribute("foo1", Collections.singleton("bar1")); this.model.addAttribute("foo2", Collections.singleton("bar2")); this.model.addAttribute("foo3", Collections.singleton("bar3")); assertEquals("[\"bar2\"]", doRender()); }
@Test public void noMatch() throws Exception { this.view.setModelKeys(Collections.singleton("foo2")); this.model.addAttribute("foo1", "bar1"); assertEquals("", doRender()); }
@Test public void multipleMatchesNotSupported() throws Exception { this.view = new HttpMessageWriterView(CharSequenceEncoder.allMimeTypes()); this.view.setModelKeys(new HashSet<>(Arrays.asList("foo1", "foo2"))); this.model.addAttribute("foo1", "bar1"); this.model.addAttribute("foo2", "bar2"); try { doRender(); fail(); } catch (IllegalStateException ex) { String message = ex.getMessage(); assertTrue(message, message.contains("Map rendering is not supported")); } }
@Test public void noMatchBecauseNotSupported() throws Exception { this.view = new HttpMessageWriterView(new Jaxb2XmlEncoder()); this.view.setModelKeys(new HashSet<>(Collections.singletonList("foo1"))); this.model.addAttribute("foo1", "bar1"); assertEquals("", doRender()); }
@Test public void render() throws Exception { Map<String, String> pojoData = new LinkedHashMap<>(); pojoData.put("foo", "f"); pojoData.put("bar", "b"); this.model.addAttribute("pojoData", pojoData); this.view.setModelKeys(Collections.singleton("pojoData")); this.view.render(this.model, MediaType.APPLICATION_JSON, exchange).block(Duration.ZERO); StepVerifier.create(this.exchange.getResponse().getBody()) .consumeNextWith(buf -> assertEquals("{\"foo\":\"f\",\"bar\":\"b\"}", dumpString(buf))) .expectComplete() .verify(); }