JsonHomeResource { @GET @Produces("application/json-home") public Response getAsApplicationJsonHome() { final JsonHome jsonHome = jsonHomeSource.getJsonHome(); try { return addCacheControlHeaders(Response.ok(toJsonString(jsonHome, APPLICATION_JSONHOME)), maxAge); } catch (IOException e) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } } JsonHomeResource(JsonHomeSource jsonHomeSource);  JsonHomeResource(); void setJsonHomeSource(JsonHomeSource jsonHomeSource); void setMaxAge(int maxAge); @GET @Produces(MediaType.TEXT_HTML) Response getAsTextHtmlHome(); @GET @Produces("application/json-home") Response getAsApplicationJsonHome(); @GET @Produces(MediaType.APPLICATION_JSON) Response getAsApplicationJson();  }
@Test public void testGetHomeDocument() throws Exception { final JsonHomeResource controller = new JsonHomeResource( jsonHomeSource(ResourceFixtures.ResourceWithRequestMappingAndLinkRelationTypeAtClassLevel.class)); final Response response = controller.getAsApplicationJsonHome(); @SuppressWarnings("unchecked") final Map<String, ?> resourcesMap = new ObjectMapper().readValue((String) response.getEntity(), Map.class); assertEquals(response.getMetadata().getFirst("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", asList("GET")); final Object hints = resources.get("http: assertEquals(hints, expected); }
@Test public void testGetHomeDocumentWithDefaultHomeResource() throws Exception { final JsonHomeResource controller = new JsonHomeResource(); final Response response = controller.getAsApplicationJsonHome(); @SuppressWarnings("unchecked") final Map<String, ?> resourcesMap = new ObjectMapper().readValue((String) response.getEntity(), Map.class); assertEquals(response.getMetadata().getFirst("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"); Map<String, Object> expected = new HashMap<String, Object>(); expected.put("representations", asList(MediaType.TEXT_HTML)); expected.put("allow", asList("GET")); final Object hintsBar = resources.get("http: assertEquals(hintsBar, expected); expected = new HashMap<String, Object>(); expected.put("representations", Collections.emptyList()); expected.put("accept-post", asList(MediaType.APPLICATION_FORM_URLENCODED)); expected.put("allow", asList("POST")); final Object hintsFoo = resources.get("http: assertEquals(hintsFoo, expected); }
@Test public void applicationJsonHomeShouldNotContainAdditionalInformation() throws Exception { final JsonHomeResource controller = new JsonHomeResource( jsonHomeSource(ResourceFixtures.ResourceWithDocumentation.class)); final Response response = controller.getAsApplicationJsonHome(); @SuppressWarnings("unchecked") final Map<String, ?> resourcesMap = new ObjectMapper().readValue((String) response.getEntity(), Map.class); @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 JsonHomeResource controller = new JsonHomeResource( jsonHomeSource(ResourceFixtures.ResourceWithRequestMappingAndLinkRelationTypeAtClassLevel.class)); final Response response = controller.getAsApplicationJsonHome(); @SuppressWarnings("unchecked") final Map<String, ?> resourcesMap = new ObjectMapper().readValue((String) response.getEntity(), Map.class); @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 JsonHomeResource controller = new JsonHomeResource( jsonHomeSource(ResourceFixtures.ResourceWithDocumentation.class)); final Response response = controller.getAsApplicationJsonHome(); @SuppressWarnings("unchecked") final Map<String, ?> resourcesMap = new ObjectMapper().readValue((String) response.getEntity(), Map.class); @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: }