@Override public Map<String, List<Link>> extractLinks(OperationResponse response) throws IOException { MediaType contentType = response.getHeaders().getContentType(); LinkExtractor extractorForContentType = getExtractorForContentType(contentType); if (extractorForContentType != null) { return extractorForContentType.extractLinks(response); } throw new IllegalStateException( "No LinkExtractor has been provided and one is not available for the " + "content type " + contentType); }
@Override protected Map<String, Object> createModel(Operation operation) { OperationResponse response = operation.getResponse(); Map<String, List<Link>> links; try { links = this.linkExtractor.extractLinks(response); validate(links); } catch (IOException ex) { throw new ModelCreationException(ex); } Map<String, Object> model = new HashMap<>(); model.put("links", createLinksModel(links)); return model; }
@Test public void extractorCalledWithMatchingContextType() throws IOException { Map<MediaType, LinkExtractor> extractors = new HashMap<>(); LinkExtractor extractor = mock(LinkExtractor.class); extractors.put(MediaType.APPLICATION_JSON, extractor); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.APPLICATION_JSON); OperationResponse response = this.responseFactory.create(HttpStatus.OK, httpHeaders, null); new ContentTypeLinkExtractor(extractors).extractLinks(response); verify(extractor).extractLinks(response); }
@Test public void extractorCalledWithCompatibleContextType() throws IOException { Map<MediaType, LinkExtractor> extractors = new HashMap<>(); LinkExtractor extractor = mock(LinkExtractor.class); extractors.put(MediaType.APPLICATION_JSON, extractor); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.parseMediaType("application/json;foo=bar")); OperationResponse response = this.responseFactory.create(HttpStatus.OK, httpHeaders, null); new ContentTypeLinkExtractor(extractors).extractLinks(response); verify(extractor).extractLinks(response); }
@Test public void noLinks() throws IOException { Map<String, List<Link>> links = this.linkExtractor .extractLinks(createResponse("no-links")); assertLinks(Collections.<Link>emptyList(), links); }
@Test public void linksInTheWrongFormat() throws IOException { Map<String, List<Link>> links = this.linkExtractor .extractLinks(createResponse("wrong-format")); assertLinks(Collections.<Link>emptyList(), links); }
@Test public void singleLink() throws IOException { Map<String, List<Link>> links = this.linkExtractor .extractLinks(createResponse("single-link")); assertLinks(Arrays.asList(new Link("alpha", "http://alpha.example.com", "Alpha")), links); }
@Test public void multipleLinksWithDifferentRels() throws IOException { Map<String, List<Link>> links = this.linkExtractor .extractLinks(createResponse("multiple-links-different-rels")); assertLinks(Arrays.asList(new Link("alpha", "http://alpha.example.com", "Alpha"), new Link("bravo", "http://bravo.example.com")), links); }
@Test public void multipleLinksWithSameRels() throws IOException { Map<String, List<Link>> links = this.linkExtractor .extractLinks(createResponse("multiple-links-same-rels")); assertLinks(Arrays.asList( new Link("alpha", "http://alpha.example.com/one", "Alpha one"), new Link("alpha", "http://alpha.example.com/two")), links); }