WireMockJsonSnippet implements Snippet { @Override public void document(Operation operation) throws IOException { RestDocumentationContext context = (RestDocumentationContext) operation.getAttributes() .get(RestDocumentationContext.class.getName()); WriterResolver writerResolver = new StandardWriterResolver( new RestDocumentationContextPlaceholderResolverFactory(), "UTF-8", TEMPLATE_FORMAT); try (Writer writer = writerResolver.resolve(operation.getName(), SNIPPET_NAME, context)) { writer.append(toJsonString(operation)); } } WireMockJsonSnippet(ResponseFieldTemplateDescriptor[] responseFieldTemplateDescriptors); @Override void document(Operation operation);  }
@SuppressWarnings("unchecked") @Test public void simpleRequest() throws IOException { this.expectedSnippet.expectWireMockJson("simple-request").withContents( (Matcher<String>) sameJSONAs(new ObjectMapper().writeValueAsString(expectedJsonForSimpleRequest()))); wiremockJson().document(operationBuilder("simple-request").request("http: }
@SuppressWarnings("unchecked") @Test public void simpleRequestWithUriTemplate() throws IOException { this.expectedSnippet.expectWireMockJson("simple-request").withContents( (Matcher<String>) sameJSONAs(new ObjectMapper().writeValueAsString(expectedJsonForSimpleRequestWithUrlPattern()))); wiremockJson().document(operationBuilder("simple-request") .attribute(ATTRIBUTE_NAME_URL_TEMPLATE, "http: .request("http: .method("GET").build()); }
@SuppressWarnings("unchecked") @Test public void simpleRequestWithUriTemplateAndResponseTemplate() throws IOException { this.expectedSnippet.expectWireMockJson("simple-request") .withContents((Matcher<String>) sameJSONAs( new ObjectMapper().writeValueAsString(expectedJsonForSimpleRequestWithUrlPatternAndResponseTemplate()))); OperationBuilder operationBuilder = operationBuilder("simple-request") .attribute(ATTRIBUTE_NAME_URL_TEMPLATE, "http: operationBuilder.request("http: .method("GET").build(); operationBuilder.response().status(200).content("{\"id\": \"some\"}"); wiremockJson(templatedResponseField("id").replacedWithUriTemplateVariableValue("id")) .document(operationBuilder.build()); }
@SuppressWarnings("unchecked") @Test public void simpleRequestWithUriTemplateAndResponseTemplate1() throws IOException { this.expectedSnippet.expectWireMockJson("simple-request") .withContents((Matcher<String>) sameJSONAs( new ObjectMapper().writeValueAsString(expectedJsonForSimpleRequestWithUrlPatternAndResponseTemplate()))); OperationBuilder operationBuilder = operationBuilder("simple-request") .attribute(ATTRIBUTE_NAME_URL_TEMPLATE, "http: operationBuilder.request("http: .method("GET").build(); operationBuilder.response().status(200).content("{\"id\": \"some\"}"); wiremockJson(idFieldReplacedWithPathParameterValue()) .document(operationBuilder.build()); }
@SuppressWarnings("unchecked") @Test public void getRequestWithParams() throws IOException { this.expectedSnippet.expectWireMockJson("get-request").withContents( (Matcher<String>) sameJSONAs(new ObjectMapper().writeValueAsString( of( "request", of("method", "GET", "urlPath", "/foo", "queryParameters", of("a", of("equalTo", "b")), "headers", of("Accept", of("contains", "json"))), "response", of("headers", emptyMap(), "body", "", "status", 200)) ))); wiremockJson().document(operationBuilder("get-request").request("http: .header("Accept", "application/json").build()); }
@SuppressWarnings("unchecked") @Test public void postRequest() throws IOException { this.expectedSnippet.expectWireMockJson("post-request").withContents((Matcher<String>) sameJSONAs(new ObjectMapper().writeValueAsString( of( "request", of("method", "POST", "urlPath", "/", "headers", of("Content-Type", of("contains", "uri-list"))), "response", of("headers", of("Content-Length", "16", "Content-Type", "text/plain"), "body", "response-content", "status", 200))))); OperationBuilder operationBuilder = operationBuilder("post-request"); operationBuilder.response().content("response-content").header("Content-Type", "text/plain").build(); wiremockJson().document(operationBuilder.request("http: .header("Content-Type", "text/uri-list").content("http: }
@Test @SuppressWarnings("unchecked") public void customMediaType() throws IOException { this.expectedSnippet.expectWireMockJson("custom-mediatype").withContents((Matcher<String>) sameJSONAs(new ObjectMapper().writeValueAsString( of( "request", of("method", "GET", "urlPath", "/foo", "headers", of("Accept", of("contains", "json"))), "response", of("headers", emptyMap(), "body", "", "status", 200)) ))); wiremockJson().document(operationBuilder("custom-mediatype").request("http: .header("Accept", "application/com.carlosjgp.myservice+json; version=1.0").build()); }