JsonHomeController { @RequestMapping(produces = {"application/json-home"}) @ResponseBody public Map<String, ?> getAsApplicationJsonHome(final HttpServletResponse response) { LOG.info("Returning json-home in application/json-home format."); response.setHeader("Cache-Control", "max-age=" + maxAge); response.setHeader("Vary", "Accept"); return toRepresentation(jsonHomeSource.getJsonHome(), APPLICATION_JSONHOME); }  @Autowired void setJsonHomeSource(final JsonHomeSource jsonHomeSource); @Value("${jsonhome.relationTypeBaseUri}") void setRelationTypeBaseUri(String relationTypeBaseUri); URI relationTypeBaseUri(); void setMaxAgeSeconds(int maxAge); @RequestMapping(produces = {"application/json-home"}) @ResponseBody Map<String, ?> getAsApplicationJsonHome(final HttpServletResponse response); @RequestMapping(produces = {"application/json"}) @ResponseBody Map<String, ?> getAsApplicationJson(final HttpServletResponse response);  }
@Test @SuppressWarnings("unchecked") public void shouldContainAuthReq() { final JsonHomeController controller = jsonHomeController( ControllerWithHints.class, "http: "http: final MockHttpServletResponse response = new MockHttpServletResponse(); final Map<String, ?> resourcesMap = controller.getAsApplicationJsonHome(response); @SuppressWarnings("unchecked") final Map<String, Map<String, ?>> resources = (Map<String, Map<String, ?>>) resourcesMap.get("resources"); final Map<String, ?> relFoo = resources.get("http: assertNotNull(relFoo); final Map<String, Object> basicAuth = new HashMap<String, Object>(); basicAuth.put("scheme", "Basic"); basicAuth.put("realms", asList("foo")); final Map<String, Object> digestAuth = new HashMap<String, Object>(); digestAuth.put("scheme", "Digest"); digestAuth.put("realms", asList("bar")); assertEquals(asMap(relFoo.get("hints")).get("auth-req"), asList( basicAuth, digestAuth)); }
@Test public void testGetHomeDocument() throws Exception { final JsonHomeController controller = jsonHomeController( ControllerWithRequestMappingAndLinkRelationTypeAtClassLevel.class, "http: final MockHttpServletResponse response = new MockHttpServletResponse(); final Map<String, ?> resourcesMap = controller.getAsApplicationJsonHome(response); assertEquals(response.getHeader("Cache-Control"), "max-age=3600"); assertEquals(resourcesMap.size(), 1); assertTrue(resourcesMap.containsKey("resources")); @SuppressWarnings("unchecked") final Map<String, Map<String, ?>> resources = (Map<String, Map<String, ?>>) resourcesMap.get("resources"); final Map<String, Object> expected = new HashMap<String, Object>(); expected.put("representations", asList("text/html")); expected.put("allow", of(GET)); final Object hints = resources.get("http: assertEquals(hints, expected); }
@Test public void applicationJsonHomeShouldNotContainAdditionalInformation() throws Exception { final JsonHomeController controller = jsonHomeController( ControllerWithDocumentation.class, "http: final MockHttpServletResponse response = new MockHttpServletResponse(); final Map<String, ?> resourcesMap = controller.getAsApplicationJsonHome(response); @SuppressWarnings("unchecked") final Map<String, Map<String, ?>> resources = (Map<String, Map<String, ?>>) resourcesMap.get("resources"); final Map<String, ?> hints = asMap(resources.get("http: assertNull(hints.get("description")); }
@Test public void shouldUseRootLinkRelationTypeUri() throws Exception { final JsonHomeController controller = jsonHomeController( ControllerWithRequestMappingAndLinkRelationTypeAtClassLevel.class, "http: "http: final MockHttpServletResponse response = new MockHttpServletResponse(); final Map<String, ?> resourcesMap = controller.getAsApplicationJsonHome(response); @SuppressWarnings("unchecked") final Map<String, Map<String, ?>> resources = (Map<String, Map<String, ?>>) resourcesMap.get("resources"); assertNotNull(resources.get("http: }
@Test public void shouldContainDocsHint() throws Exception { final JsonHomeController controller = jsonHomeController( ControllerWithDocumentation.class, "http: "http: final MockHttpServletResponse response = new MockHttpServletResponse(); final Map<String, ?> resourcesMap = controller.getAsApplicationJsonHome(response); @SuppressWarnings("unchecked") final Map<String, Map<String, ?>> resources = (Map<String, Map<String, ?>>) resourcesMap.get("resources"); final Map<String, ?> relFoo = resources.get("http: assertNotNull(relFoo); assertEquals(asMap(relFoo.get("hints")).get("docs"), "http: }
@Test public void shouldContainPreferHint() { final JsonHomeController controller = jsonHomeController( ControllerWithHints.class, "http: "http: final MockHttpServletResponse response = new MockHttpServletResponse(); final Map<String, ?> resourcesMap = controller.getAsApplicationJsonHome(response); @SuppressWarnings("unchecked") final Map<String, Map<String, ?>> resources = (Map<String, Map<String, ?>>) resourcesMap.get("resources"); final Map<String, ?> relFoo = resources.get("http: assertNotNull(relFoo); assertEquals(asMap(relFoo.get("hints")).get("prefer"), asList("return-representation=application/json", "return-asynch")); }
@Test public void shouldContainAcceptRangesHint() { final JsonHomeController controller = jsonHomeController( ControllerWithHints.class, "http: "http: final MockHttpServletResponse response = new MockHttpServletResponse(); final Map<String, ?> resourcesMap = controller.getAsApplicationJsonHome(response); @SuppressWarnings("unchecked") final Map<String, Map<String, ?>> resources = (Map<String, Map<String, ?>>) resourcesMap.get("resources"); final Map<String, ?> relFoo = resources.get("http: assertNotNull(relFoo); assertEquals(asMap(relFoo.get("hints")).get("accept-ranges"), asList("bytes")); }
@Test public void shouldContainPreconditionReqEtag() { final JsonHomeController controller = jsonHomeController( ControllerWithHints.class, "http: "http: final MockHttpServletResponse response = new MockHttpServletResponse(); final Map<String, ?> resourcesMap = controller.getAsApplicationJsonHome(response); @SuppressWarnings("unchecked") final Map<String, Map<String, ?>> resources = (Map<String, Map<String, ?>>) resourcesMap.get("resources"); final Map<String, ?> relFoo = resources.get("http: assertNotNull(relFoo); assertEquals(asMap(relFoo.get("hints")).get("precondition-req"), asList("etag", "last-modified")); }