Improve testing of optional links and request and response fields

This commit is contained in:
Andy Wilkinson
2016-05-05 10:05:20 +01:00
parent a3e6883524
commit 96330b2e30
3 changed files with 53 additions and 3 deletions

View File

@@ -92,12 +92,12 @@ public class LinksSnippetTests {
}
@Test
public void documentedOptionalLink() throws IOException {
this.snippet.expectLinks("documented-optional-link").withContents(
public void presentOptionalLink() throws IOException {
this.snippet.expectLinks("present-optional-link").withContents(
tableWithHeader("Relation", "Description").row("foo", "bar"));
new LinksSnippet(new StubLinkExtractor().withLinks(new Link("foo", "blah")),
Arrays.asList(new LinkDescriptor("foo").description("bar").optional()))
.document(new OperationBuilder("documented-optional-link",
.document(new OperationBuilder("present-optional-link",
this.snippet.getOutputDirectory()).build());
}

View File

@@ -129,6 +129,32 @@ public class RequestFieldsSnippetTests {
.content("{}").build());
}
@Test
public void missingOptionalRequestField() throws IOException {
this.snippet.expectRequestFields("missing-optional-request-field")
.withContents(tableWithHeader("Path", "Type", "Description").row("a.b",
"String", "one"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")
.type(JsonFieldType.STRING).optional()))
.document(new OperationBuilder("missing-optional-request-field",
this.snippet.getOutputDirectory())
.request("http://localhost").content("{}")
.build());
}
@Test
public void presentOptionalRequestField() throws IOException {
this.snippet.expectRequestFields("present-optional-request-field")
.withContents(tableWithHeader("Path", "Type", "Description").row("a.b",
"String", "one"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")
.type(JsonFieldType.STRING).optional()))
.document(new OperationBuilder("present-optional-request-field",
this.snippet.getOutputDirectory())
.request("http://localhost")
.content("{\"a\": { \"b\": \"bravo\"}}").build());
}
@Test
public void missingOptionalRequestFieldWithNoTypeProvided() throws IOException {
this.thrown.expect(FieldTypeRequiredException.class);

View File

@@ -121,6 +121,30 @@ public class ResponseFieldsSnippetTests {
.content("{\"a\": 5, \"b\": 4}").build());
}
@Test
public void missingOptionalResponseField() throws IOException {
this.snippet.expectResponseFields("missing-optional-response-field")
.withContents(tableWithHeader("Path", "Type", "Description").row("a.b",
"String", "one"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")
.type(JsonFieldType.STRING).optional()))
.document(new OperationBuilder("missing-optional-response-field",
this.snippet.getOutputDirectory()).response()
.content("{}").build());
}
@Test
public void presentOptionalResponseField() throws IOException {
this.snippet.expectResponseFields("present-optional-response-field")
.withContents(tableWithHeader("Path", "Type", "Description").row("a.b",
"String", "one"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a.b").description("one")
.type(JsonFieldType.STRING).optional()))
.document(new OperationBuilder("present-optional-response-field",
this.snippet.getOutputDirectory()).response()
.content("{\"a\": { \"b\": \"bravo\"}}").build());
}
@Test
public void responseFieldsWithCustomDescriptorAttributes() throws IOException {
TemplateResourceResolver resolver = mock(TemplateResourceResolver.class);