@Override public void document(Operation operation) throws IOException { RestDocumentationContext context = (RestDocumentationContext) operation .getAttributes().get(RestDocumentationContext.class.getName()); WriterResolver writerResolver = (WriterResolver) operation.getAttributes() .get(WriterResolver.class.getName()); try (Writer writer = writerResolver.resolve( operation.getName() + "/" + getSnippetName(), this.file, context)) { Map<String, Object> model = createModel(operation); model.putAll(getAttributes()); TemplateEngine templateEngine = (TemplateEngine) operation.getAttributes() .get(TemplateEngine.class.getName()); writer.append(templateEngine.compileTemplate(getSnippetName()).render(model)); } }
@Override protected Map<String, Object> createModel(Operation operation) { try { Object object = this.objectMapper.readValue( operation.getResponse().getContentAsString(), Object.class); Object field = this.fieldProcessor.extract(JsonFieldPath.compile(this.path), object); if (field instanceof List && this.index != null) { field = ((List<?>) field).get(this.index); } return Collections.singletonMap("value", this.objectMapper.writeValueAsString(field)); } catch (Exception ex) { throw new IllegalStateException(ex); } }
@Override protected Map<String, Object> createModel(Operation operation) { Map<String, Object> model = new HashMap<>(); model.put("method", operation.getRequest().getMethod()); model.put("path", getPath(operation.getRequest())); model.put("headers", getHeaders(operation.getRequest())); model.put("requestBody", getRequestBody(operation.getRequest())); return model; }
private void verifySnippetInvocation(Snippet snippet, Map<String, Object> attributes) throws IOException { ArgumentCaptor<Operation> operation = ArgumentCaptor.forClass(Operation.class); verify(snippet).document(operation.capture()); assertThat(this.operationRequest).isEqualTo(operation.getValue().getRequest()); assertThat(this.operationResponse).isEqualTo(operation.getValue().getResponse()); assertThat(attributes).isEqualTo(operation.getValue().getAttributes()); }
private void verifySnippetInvocation(Snippet snippet, OperationRequest operationRequest, OperationResponse operationResponse, Map<String, Object> attributes, int times) throws IOException { ArgumentCaptor<Operation> operation = ArgumentCaptor.forClass(Operation.class); verify(snippet, Mockito.times(times)).document(operation.capture()); assertThat(operationRequest).isEqualTo(operation.getValue().getRequest()); assertThat(operationResponse).isEqualTo(operation.getValue().getResponse()); }
private String extractUrlTemplate(Operation operation) { String urlTemplate = (String) operation.getAttributes() .get(RestDocumentationGenerator.ATTRIBUTE_NAME_URL_TEMPLATE); Assert.notNull(urlTemplate, "urlTemplate not found. If you are using MockMvc did " + "you use RestDocumentationRequestBuilders to build the request?"); return urlTemplate; }
private void verifySnippetInvocation(InOrder inOrder, Snippet snippet, Map<String, Object> attributes) throws IOException { ArgumentCaptor<Operation> operation = ArgumentCaptor.forClass(Operation.class); inOrder.verify(snippet).document(operation.capture()); assertThat(this.operationRequest).isEqualTo(operation.getValue().getRequest()); assertThat(this.operationResponse).isEqualTo(operation.getValue().getResponse()); assertThat(attributes).isEqualTo(operation.getValue().getAttributes()); }
@Override protected byte[] getContent(Operation operation) throws IOException { return operation.getResponse().getContent(); }
@Override protected byte[] getContent(Operation operation) throws IOException { return operation.getRequest().getContent(); }
@Override public void document(Operation operation) throws IOException { RestDocumentationContext context = (RestDocumentationContext) operation .getAttributes().get(RestDocumentationContext.class.getName()); WriterResolver writerResolver = (WriterResolver) operation.getAttributes() .get(WriterResolver.class.getName()); try (Writer writer = writerResolver.resolve(operation.getName(), this.snippetName, context)) { Map<String, Object> model = createModel(operation); model.putAll(this.attributes); TemplateEngine templateEngine = (TemplateEngine) operation.getAttributes() .get(TemplateEngine.class.getName()); writer.append( templateEngine.compileTemplate(this.templateName).render(model)); } }
@Override protected byte[] getContent(Operation operation) throws IOException { return operation.getResponse().getContent(); }
@Override protected byte[] getContent(Operation operation) throws IOException { return operation.getRequest().getContent(); }
@Override protected Map<String, Object> createModel(Operation operation) { OperationResponse response = operation.getResponse(); HttpStatus status = response.getStatus(); Map<String, Object> model = new HashMap<>(); model.put("responseBody", responseBody(response)); model.put("statusCode", status.value()); model.put("statusReason", status.getReasonPhrase()); model.put("headers", headers(response)); return model; }
private Set<String> extractActualRequestParts(Operation operation) { Set<String> actualRequestParts = new HashSet<>(); for (OperationRequestPart requestPart : operation.getRequest().getParts()) { actualRequestParts.add(requestPart.getName()); } return actualRequestParts; }
@Override protected Set<String> extractActualHeaders(Operation operation) { return operation.getResponse().getHeaders().keySet(); }
@Override protected MediaType getContentType(Operation operation) { return operation.getRequest().getHeaders().getContentType(); }
@Override protected MediaType getContentType(Operation operation) { return operation.getResponse().getHeaders().getContentType(); }
@Override protected MediaType getContentType(Operation operation) { return operation.getRequest().getHeaders().getContentType(); }
@Override protected MediaType getContentType(Operation operation) { return operation.getResponse().getHeaders().getContentType(); }
@Override protected Map<String, Object> createModel(Operation operation) { Map<String, Object> model = new HashMap<>(); CliOperationRequest request = new CliOperationRequest(operation.getRequest()); model.put("echoContent", getContentStandardIn(request)); model.put("options", getOptions(request)); model.put("url", getUrl(request)); model.put("requestItems", getRequestItems(request)); return model; }