ResponseTemplateProcessor { String replaceTemplateFields() { if (templateDescriptors.isEmpty()) { return responseBody; } DocumentContext documentContext = JsonPath.parse(responseBody); for (ResponseFieldTemplateDescriptor descriptor: templateDescriptors) { String expression = null; if (uriTemplate != null && !uriTemplate.getVariableNames().isEmpty()) { expression = preProcessUriTemplateVariableNameExpression(descriptor); } else if (descriptor.getUriTemplateVariableName() != null) { throw new IllegalArgumentException("Descriptor for field '" + descriptor.getPath() + "' specifies a 'replacedWithUriTemplateVariableValue' but no URI Template could be found in. " + "Make sure to construct your request with the methods in org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders that use URI templates"); } if (expression == null) { if (descriptor.getWireMockTemplateExpression() == null) { throw new IllegalArgumentException("Descriptor for field '" + descriptor.getPath() + "' contains no replacedWithWireMockTemplateExpression"); } expression = "{{" + descriptor.getWireMockTemplateExpression() + "}}"; } documentContext.set(descriptor.getPath(), expression); } return documentContext.jsonString(); } ResponseTemplateProcessor(List<ResponseFieldTemplateDescriptor> templateDescriptors, UriTemplate uriTemplate, String responseBody);   }
@Test(expected = IllegalArgumentException.class) public void should_throw_when_variable_name_not_found() { ResponseFieldTemplateDescriptor templateDescriptor = templatedResponseField("id").replacedWithUriTemplateVariableValue("someId"); ResponseTemplateProcessor templateProcessor = new ResponseTemplateProcessor( singletonList(templateDescriptor), new UriTemplate("http: jsonBody); templateProcessor.replaceTemplateFields(); }
@Test public void should_replace_with_uri_variable_expression() { ResponseFieldTemplateDescriptor templateDescriptor = templatedResponseField("id").replacedWithUriTemplateVariableValue("someId"); ResponseTemplateProcessor templateProcessor = new ResponseTemplateProcessor( singletonList(templateDescriptor), new UriTemplate("http: jsonBody); String result = templateProcessor.replaceTemplateFields(); assertThat(result, sameJSONAs("{\n" + " \"id\": \"{{request.requestLine.pathSegments.[2]}}\",\n" + " \"name\": \"some\"\n" + "}")); }
@Test public void should_handle_multiple_descriptors() { ResponseTemplateProcessor templateProcessor = new ResponseTemplateProcessor( Arrays.asList( templatedResponseField("id").replacedWithWireMockTemplateExpression("randomValue length=33 type='ALPHANUMERIC'"), templatedResponseField("name").replacedWithWireMockTemplateExpression("randomValue type='UUID'") ), null, jsonBody); String result = templateProcessor.replaceTemplateFields(); assertThat(result, sameJSONAs("{\n" + " \"id\":\"{{randomValue length=33 type='ALPHANUMERIC'}}\",\n" + " \"name\":\"{{randomValue type='UUID'}}\"\n" + "}")); }
@Test public void should_return_response_on_empty_descriptors() { ResponseTemplateProcessor templateProcessor = new ResponseTemplateProcessor( Collections.<ResponseFieldTemplateDescriptor>emptyList(), null, jsonBody); String result = templateProcessor.replaceTemplateFields(); assertThat(result, is(jsonBody)); }