/** * Returns a new {@code Snippet} that will document the links in the API operation's * response. Links will be extracted from the response automatically based on its * content type and will be documented using the given {@code descriptors}. * <p> * If a link is documented, is not marked as optional, and is not present in the * response, a failure will occur. Any undocumented links will be ignored. * <p> * If a descriptor does not have a {@link LinkDescriptor#description(Object) * description}, the {@link Link#getTitle() title} of the link will be used. If the * link does not have a title a failure will occur. * @param descriptors the descriptions of the response's links * @return the snippet that will document the links */ public static LinksSnippet relaxedLinks(List<LinkDescriptor> descriptors) { return new LinksSnippet(new ContentTypeLinkExtractor(), descriptors, true); }
/** * Returns a new {@code Snippet} that will document the links in the API call's * response. The given {@code attributes} will be available during snippet generation. * Links will be extracted from the response automatically based on its content type * and will be documented using the given {@code descriptors}. * <p> * If a link is documented, is not marked as optional, and is not present in the * response, a failure will occur. Any undocumented links will be ignored. * <p> * If a descriptor does not have a {@link LinkDescriptor#description(Object) * description}, the {@link Link#getTitle() title} of the link will be used. If the * link does not have a title a failure will occur. * @param attributes the attributes * @param descriptors the descriptions of the response's links * @return the snippet that will document the links */ public static LinksSnippet relaxedLinks(Map<String, Object> attributes, List<LinkDescriptor> descriptors) { return new LinksSnippet(new ContentTypeLinkExtractor(), descriptors, attributes, true); }
/** * Returns a new {@code Snippet} that will document the links in the API call's * response. The given {@code attributes} will be available during snippet generation. * Links will be extracted from the response automatically based on its content type * and will be documented using the given {@code descriptors}. * <p> * If a link is present in the response, but is not documented by one of the * descriptors, a failure will occur when the snippet is invoked. Similarly, if a link * is documented, is not marked as optional, and is not present in the response, a * failure will also occur. * <p> * If you do not want to document a link, a link descriptor can be marked as * {@link LinkDescriptor#ignored}. This will prevent it from appearing in the * generated snippet while avoiding the failure described above. * <p> * If a descriptor does not have a {@link LinkDescriptor#description(Object) * description}, the {@link Link#getTitle() title} of the link will be used. If the * link does not have a title a failure will occur. * @param attributes the attributes * @param descriptors the descriptions of the response's links * @return the snippet that will document the links */ public static LinksSnippet links(Map<String, Object> attributes, List<LinkDescriptor> descriptors) { return new LinksSnippet(new ContentTypeLinkExtractor(), descriptors, attributes); }
/** * Returns a new {@code Snippet} that will document the links in the API operation's * response. Links will be extracted from the response automatically based on its * content type and will be documented using the given {@code descriptors}. * <p> * If a link is present in the response, but is not documented by one of the * descriptors, a failure will occur when the snippet is invoked. Similarly, if a link * is documented, is not marked as optional, and is not present in the response, a * failure will also occur. * <p> * If you do not want to document a link, a link descriptor can be marked as * {@link LinkDescriptor#ignored}. This will prevent it from appearing in the * generated snippet while avoiding the failure described above. * <p> * If a descriptor does not have a {@link LinkDescriptor#description(Object) * description}, the {@link Link#getTitle() title} of the link will be used. If the * link does not have a title a failure will occur. * @param descriptors the descriptions of the response's links * @return the snippet that will document the links */ public static LinksSnippet links(List<LinkDescriptor> descriptors) { return new LinksSnippet(new ContentTypeLinkExtractor(), descriptors); }
@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 extractionFailsWithNullContentType() throws IOException { this.thrown.expect(IllegalStateException.class); new ContentTypeLinkExtractor().extractLinks( this.responseFactory.create(HttpStatus.OK, new HttpHeaders(), null)); }