Cause a failure when field's documented and actual types do not match

Closes gh-276
This commit is contained in:
Andy Wilkinson
2016-07-22 13:20:44 +01:00
parent 86c2310b90
commit 78318775e0
9 changed files with 186 additions and 22 deletions

View File

@@ -110,6 +110,33 @@ public class RequestFieldsSnippetFailureTests {
.build());
}
@Test
public void fieldWithExplicitTypeThatDoesNotMatchThePayload() throws IOException {
this.thrown.expect(FieldTypesDoNotMatchException.class);
this.thrown.expectMessage(equalTo("The documented type of the field 'a' is"
+ " Object but the actual type is Number"));
new RequestFieldsSnippet(Arrays
.asList(fieldWithPath("a").description("one").type(JsonFieldType.OBJECT)))
.document(new OperationBuilder("mismatched-field-types",
this.snippet.getOutputDirectory())
.request("http://localhost")
.content("{ \"a\": 5 }").build());
}
@Test
public void fieldWithExplicitSpecificTypeThatActuallyVaries() throws IOException {
this.thrown.expect(FieldTypesDoNotMatchException.class);
this.thrown.expectMessage(equalTo("The documented type of the field '[].a' is"
+ " Object but the actual type is Varies"));
new RequestFieldsSnippet(Arrays.asList(
fieldWithPath("[].a").description("one").type(JsonFieldType.OBJECT)))
.document(new OperationBuilder("mismatched-field-types",
this.snippet.getOutputDirectory())
.request("http://localhost")
.content("[{ \"a\": 5 },{ \"a\": \"b\" }]")
.build());
}
@Test
public void undocumentedXmlRequestField() throws IOException {
this.thrown.expect(SnippetException.class);

View File

@@ -177,6 +177,34 @@ public class RequestFieldsSnippetTests extends AbstractSnippetTests {
.build());
}
@Test
public void fieldWithExplictExactlyMatchingType() throws IOException {
this.snippet
.expectRequestFields("request-field-with-explicit-exactly-matching-type")
.withContents(tableWithHeader("Path", "Type", "Description").row("`a`",
"`Number`", "one"));
new RequestFieldsSnippet(Arrays
.asList(fieldWithPath("a").description("one").type(JsonFieldType.NUMBER)))
.document(operationBuilder(
"request-field-with-explicit-exactly-matching-type")
.request("http://localhost")
.content("{\"a\": 5 }").build());
}
@Test
public void fieldWithExplictVariesType() throws IOException {
this.snippet.expectRequestFields("request-field-with-explicit-varies-type")
.withContents(tableWithHeader("Path", "Type", "Description").row("`a`",
"`Varies`", "one"));
new RequestFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")
.type(JsonFieldType.VARIES))).document(
operationBuilder("request-field-with-explicit-varies-type")
.request("http://localhost").content("{\"a\": 5 }")
.build());
}
@Test
public void xmlRequestFields() throws IOException {
this.snippet.expectRequestFields("xml-request")

View File

@@ -57,8 +57,32 @@ public class ResponseFieldsSnippetFailureTests {
equalTo("Cannot document response fields as the response body is empty"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")))
.document(new OperationBuilder("no-response-body",
this.snippet.getOutputDirectory()).request("http://localhost")
.build());
this.snippet.getOutputDirectory()).build());
}
@Test
public void fieldWithExplicitTypeThatDoesNotMatchThePayload() throws IOException {
this.thrown.expect(FieldTypesDoNotMatchException.class);
this.thrown.expectMessage(equalTo("The documented type of the field 'a' is"
+ " Object but the actual type is Number"));
new ResponseFieldsSnippet(Arrays
.asList(fieldWithPath("a").description("one").type(JsonFieldType.OBJECT)))
.document(new OperationBuilder("mismatched-field-types",
this.snippet.getOutputDirectory()).response()
.content("{ \"a\": 5 }}").build());
}
@Test
public void fieldWithExplicitSpecificTypeThatActuallyVaries() throws IOException {
this.thrown.expect(FieldTypesDoNotMatchException.class);
this.thrown.expectMessage(equalTo("The documented type of the field '[].a' is"
+ " Object but the actual type is Varies"));
new ResponseFieldsSnippet(Arrays.asList(
fieldWithPath("[].a").description("one").type(JsonFieldType.OBJECT)))
.document(new OperationBuilder("mismatched-field-types",
this.snippet.getOutputDirectory()).response()
.content("[{ \"a\": 5 },{ \"a\": \"b\" }]")
.build());
}
@Test

View File

@@ -185,6 +185,33 @@ public class ResponseFieldsSnippetTests extends AbstractSnippetTests {
.build());
}
@Test
public void fieldWithExplictExactlyMatchingType() throws IOException {
this.snippet
.expectResponseFields(
"response-field-with-explicit-exactly-matching-type")
.withContents(tableWithHeader("Path", "Type", "Description").row("`a`",
"`Number`", "one"));
new ResponseFieldsSnippet(Arrays
.asList(fieldWithPath("a").description("one").type(JsonFieldType.NUMBER)))
.document(operationBuilder(
"response-field-with-explicit-exactly-matching-type")
.response().content("{\"a\": 5 }").build());
}
@Test
public void fieldWithExplictVariesType() throws IOException {
this.snippet.expectResponseFields("response-field-with-explicit-varies-type")
.withContents(tableWithHeader("Path", "Type", "Description").row("`a`",
"`Varies`", "one"));
new ResponseFieldsSnippet(Arrays.asList(fieldWithPath("a").description("one")
.type(JsonFieldType.VARIES))).document(
operationBuilder("response-field-with-explicit-varies-type")
.response().content("{\"a\": 5 }").build());
}
@Test
public void xmlResponseFields() throws IOException {
this.snippet.expectResponseFields("xml-response")