private LinkDescriptor createDescriptor(String description, LinkDescriptor source) { LinkDescriptor newDescriptor = new LinkDescriptor(source.getRel()) .description(description); if (source.isOptional()) { newDescriptor.optional(); } if (source.isIgnored()) { newDescriptor.ignored(); } return newDescriptor; }
/** * Returns a model for the given {@code descriptor}. * @param descriptor the descriptor * @return the model */ protected Map<String, Object> createModelForDescriptor(LinkDescriptor descriptor) { Map<String, Object> model = new HashMap<>(); model.put("rel", descriptor.getRel()); model.put("description", descriptor.getDescription()); model.put("optional", descriptor.isOptional()); model.putAll(descriptor.getAttributes()); return model; }
public void documentation() throws Exception { // tag::use[] RestAssured.given(this.spec) .accept("application/json") .filter(document("example", this.pagingLinks.and( // <1> linkWithRel("alpha").description("Link to the alpha resource"), linkWithRel("bravo").description("Link to the bravo resource")))) .get("/").then().assertThat().statusCode(is(200)); // end::use[] }
@Test public void missingLink() throws IOException { this.thrown.expect(SnippetException.class); this.thrown.expectMessage(equalTo("Links with the following relations were not" + " found in the response: [foo]")); new LinksSnippet(new StubLinkExtractor(), Arrays.asList(new LinkDescriptor("foo").description("bar"))) .document(this.operationBuilder.build()); }
@Test public void missingOptionalLink() throws IOException { new LinksSnippet(new StubLinkExtractor(), Arrays.asList(new LinkDescriptor("foo").description("bar").optional())) .document(this.operationBuilder.build()); assertThat(this.generatedSnippets.links()) .is(tableWithHeader("Relation", "Description").row("`foo`", "bar")); }
@Test public void ignoredLink() throws IOException { new LinksSnippet( new StubLinkExtractor().withLinks(new Link("a", "alpha"), new Link("b", "bravo")), Arrays.asList(new LinkDescriptor("a").ignored(), new LinkDescriptor("b").description("Link b"))) .document(this.operationBuilder.build()); assertThat(this.generatedSnippets.links()) .is(tableWithHeader("Relation", "Description").row("`b`", "Link b")); }
@Test public void linksWithCustomDescriptorAttributes() throws IOException { TemplateResourceResolver resolver = mock(TemplateResourceResolver.class); given(resolver.resolveTemplateResource("links")) .willReturn(snippetResource("links-with-extra-column")); new LinksSnippet( new StubLinkExtractor().withLinks(new Link("a", "alpha"), new Link("b", "bravo")), Arrays.asList( new LinkDescriptor("a").description("one") .attributes(key("foo").value("alpha")), new LinkDescriptor("b").description("two") .attributes(key("foo").value("bravo")))) .document(this.operationBuilder.attribute( TemplateEngine.class.getName(), new MustacheTemplateEngine(resolver)) .build()); assertThat(this.generatedSnippets.links()) .is(tableWithHeader("Relation", "Description", "Foo") .row("a", "one", "alpha").row("b", "two", "bravo")); }
/** * Creates a {@code LinkDescriptor} that describes a link with the given {@code rel}. * @param rel the rel of the link * @return a {@code LinkDescriptor} ready for further configuration */ public static LinkDescriptor linkWithRel(String rel) { return new LinkDescriptor(rel); }
public void defaultExtractor() throws Exception { // tag::links[] RestAssured.given(this.spec) .accept("application/json") .filter(document("index", links( // <1> linkWithRel("alpha").description("Link to the alpha resource"), // <2> linkWithRel("bravo").description("Link to the bravo resource")))) // <3> .get("/").then().assertThat().statusCode(is(200)); // end::links[] }
@Test public void documentedLinks() throws IOException { new LinksSnippet( new StubLinkExtractor().withLinks(new Link("a", "alpha"), new Link("b", "bravo")), Arrays.asList(new LinkDescriptor("a").description("one"), new LinkDescriptor("b").description("two"))) .document(this.operationBuilder.build()); assertThat(this.generatedSnippets.links()) .is(tableWithHeader("Relation", "Description").row("`a`", "one") .row("`b`", "two")); }
@Test public void presentOptionalLink() throws IOException { new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "blah")), Arrays.asList(new LinkDescriptor("foo").description("bar").optional())) .document(this.operationBuilder.build()); assertThat(this.generatedSnippets.links()) .is(tableWithHeader("Relation", "Description").row("`foo`", "bar")); }
@Test public void linkWithNoDescription() throws IOException { this.thrown.expect(SnippetException.class); this.thrown.expectMessage( equalTo("No description was provided for the link with rel 'foo' and no" + " title was available from the link in the payload")); new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "bar")), Arrays.asList(new LinkDescriptor("foo"))) .document(this.operationBuilder.build()); }
public void explicitExtractor() throws Exception { RestAssured.given(this.spec) .accept("application/json") // tag::explicit-extractor[] .filter(document("index", links(halLinks(), // <1> linkWithRel("alpha").description("Link to the alpha resource"), linkWithRel("bravo").description("Link to the bravo resource")))) // end::explicit-extractor[] .get("/").then().assertThat().statusCode(is(200)); }
@Test public void linkDescriptionFromTitleInPayload() throws IOException { new LinksSnippet( new StubLinkExtractor().withLinks(new Link("a", "alpha", "Link a"), new Link("b", "bravo", "Link b")), Arrays.asList(new LinkDescriptor("a").description("one"), new LinkDescriptor("b"))).document(this.operationBuilder.build()); assertThat(this.generatedSnippets.links()) .is(tableWithHeader("Relation", "Description").row("`a`", "one") .row("`b`", "Link b")); }
public void documentation() throws Exception { // tag::use[] this.mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andDo(document("example", this.pagingLinks.and( // <1> linkWithRel("alpha").description("Link to the alpha resource"), linkWithRel("bravo").description("Link to the bravo resource")))); // end::use[] }
@Test public void undocumentedLinkAndMissingLink() throws IOException { this.thrown.expect(SnippetException.class); this.thrown.expectMessage(equalTo("Links with the following relations were not" + " documented: [a]. Links with the following relations were not" + " found in the response: [foo]")); new LinksSnippet(new StubLinkExtractor().withLinks(new Link("a", "alpha")), Arrays.asList(new LinkDescriptor("foo").description("bar"))) .document(this.operationBuilder.build()); }
public void defaultExtractor() throws Exception { // tag::links[] this.mockMvc.perform(get("/").accept(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andDo(document("index", links( // <1> linkWithRel("alpha").description("Link to the alpha resource"), // <2> linkWithRel("bravo").description("Link to the bravo resource")))); // <3> // end::links[] }
@Test public void additionalDescriptors() throws IOException { HypermediaDocumentation .links(new StubLinkExtractor().withLinks(new Link("a", "alpha"), new Link("b", "bravo")), new LinkDescriptor("a").description("one")) .and(new LinkDescriptor("b").description("two")) .document(this.operationBuilder.build()); assertThat(this.generatedSnippets.links()) .is(tableWithHeader("Relation", "Description").row("`a`", "one") .row("`b`", "two")); }
public void defaultExtractor() throws Exception { // tag::links[] this.webTestClient.get().uri("/").accept(MediaType.APPLICATION_JSON).exchange() .expectStatus().isOk().expectBody() .consumeWith(document("index",links( // <1> linkWithRel("alpha").description("Link to the alpha resource"), // <2> linkWithRel("bravo").description("Link to the bravo resource")))); // <3> // end::links[] }