org.springframework.restdocs.mockmvc.RestDocumentationResultHandler Java Examples

The following examples show how to use org.springframework.restdocs.mockmvc.RestDocumentationResultHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserControllerDocumentation.java    From spring-tutorials with Apache License 2.0 6 votes vote down vote up
private void updateUser(User user) throws Exception {
    
    RestDocumentationResultHandler document = 
            documentPrettyPrintReqResp("updateUser");
    
    document.document(
            pathParameters(userPathParams()),
            requestFields(userFields(false)),
            responseFields(userFields(false))
    );
    this.mockMvc.perform(
            put("/api/v1/users/{userId}", user.getUserId())
            .accept(MediaType.APPLICATION_JSON)
            .content(objectMapper.writeValueAsString(user))
            .contentType(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andDo(document);
}
 
Example #2
Source File: UserControllerDocumentation.java    From spring-tutorials with Apache License 2.0 6 votes vote down vote up
private void getUsers() throws Exception {
    
    RestDocumentationResultHandler document = documentPrettyPrintReqResp("getUsers");
    
    document.document(
            pathParameters(
                    parameterWithName("page").description("Page of results"),
                    parameterWithName("size").description("Size of results")
            ),
            responseFields(userFields(true))
    );
    
    this.mockMvc.perform(get("/api/v1/users?page={page}&size={size}", 0, 10)
            .contentType(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$").isArray())
            .andExpect(jsonPath("[*].userId").isNotEmpty())
            .andExpect(jsonPath("[*].firstName").isNotEmpty())
            .andExpect(jsonPath("[*].lastName").isNotEmpty())
            .andExpect(jsonPath("[*].username").isNotEmpty())
            .andDo(document);
}
 
Example #3
Source File: UserControllerDocumentation.java    From spring-tutorials with Apache License 2.0 6 votes vote down vote up
private void getUser(UUID userId) throws Exception {
    
    RestDocumentationResultHandler document = documentPrettyPrintReqResp("getUser");
    
    document.document(
            pathParameters(userPathParams()),
            responseFields(userFields(false))
    );
    
    this.mockMvc.perform(get("/api/v1/users/{userId}", userId)
            .contentType(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(jsonPath("userId").isNotEmpty())
            .andExpect(jsonPath("firstName").isNotEmpty())
            .andExpect(jsonPath("lastName").isNotEmpty())
            .andExpect(jsonPath("username").isNotEmpty())
            .andDo(document);
}
 
Example #4
Source File: RamlDocumentation.java    From restdocs-raml with MIT License 6 votes vote down vote up
public static RestDocumentationResultHandler document(String identifier,
                                                      String description,
                                                      boolean privateResource,
                                                      OperationRequestPreprocessor requestPreprocessor,
                                                      OperationResponsePreprocessor responsePreprocessor,
                                                      Function<List<Snippet>, List<Snippet>> snippetFilter,
                                                      Snippet... snippets) {

    Snippet[] enhancedSnippets = enhanceSnippetsWithRaml(description, privateResource, snippetFilter, snippets);

    if (requestPreprocessor != null && responsePreprocessor != null) {
        return MockMvcRestDocumentation.document(identifier, requestPreprocessor, responsePreprocessor, enhancedSnippets);
    } else if (requestPreprocessor != null) {
        return MockMvcRestDocumentation.document(identifier, requestPreprocessor, enhancedSnippets);
    } else if (responsePreprocessor != null) {
        return MockMvcRestDocumentation.document(identifier, responsePreprocessor, enhancedSnippets);
    }

    return MockMvcRestDocumentation.document(identifier, enhancedSnippets);
}
 
Example #5
Source File: UserControllerDocumentation.java    From spring-tutorials with Apache License 2.0 6 votes vote down vote up
private void insertUser(User user) throws Exception {
    
    RestDocumentationResultHandler document = documentPrettyPrintReqResp("insertUser");
    
    document.document(
            requestFields(userFields(false)),
            responseFields(userFields(false))
    );
    
    this.mockMvc.perform(post("/api/v1/users")
            .accept(MediaType.APPLICATION_JSON)
            .content(objectMapper.writeValueAsString(user))
            .contentType(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andDo(document);
}
 
Example #6
Source File: ControllerTestTemplate.java    From coderadar with MIT License 5 votes vote down vote up
/**
 * Wraps the static document() method of RestDocs and configures it to pretty print request and
 * response JSON structures.
 */
protected RestDocumentationResultHandler document(String identifier, Snippet... snippets) {
  return MockMvcRestDocumentation.document(
      identifier,
      Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
      Preprocessors.preprocessResponse(Preprocessors.prettyPrint()),
      snippets);
}
 
Example #7
Source File: UserControllerDocumentation.java    From spring-tutorials with Apache License 2.0 5 votes vote down vote up
private void deleteUser(UUID userId) throws Exception {
    
    RestDocumentationResultHandler document = 
            documentPrettyPrintReqResp("deleteUser");
            
    document.document(
            pathParameters(userPathParams()),
            responseFields(userFields(false))
    );
    
    this.mockMvc.perform(delete("/api/v1/users/{userId}", userId)
            .contentType(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andDo(document);
}
 
Example #8
Source File: RamlDocumentation.java    From restdocs-raml with MIT License 5 votes vote down vote up
public static RestDocumentationResultHandler document(String identifier,
                                                      String description,
                                                      boolean privateResource,
                                                      OperationRequestPreprocessor requestPreprocessor,
                                                      OperationResponsePreprocessor responsePreprocessor,
                                                      Snippet... snippets) {
    return document(identifier, description, privateResource, requestPreprocessor, responsePreprocessor, Function.identity(), snippets);
}
 
Example #9
Source File: RamlDocumentation.java    From restdocs-raml with MIT License 4 votes vote down vote up
public static RestDocumentationResultHandler document(String identifier,
                                                      OperationRequestPreprocessor requestPreprocessor,
                                                      Snippet... snippets) {
    return document(identifier, "", false, requestPreprocessor, null, Function.identity(), snippets);
}
 
Example #10
Source File: MockMvcBase.java    From spring-auto-restdocs with Apache License 2.0 4 votes vote down vote up
protected RestDocumentationResultHandler commonDocumentation(Snippet... snippets) {
    return document("{class-name}/{method-name}", commonResponsePreprocessor(), snippets);
}
 
Example #11
Source File: DhisWebSpringTest.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public RestDocumentationResultHandler documentPrettyPrint( String useCase, Snippet... snippets )
{
    return document( useCase, preprocessRequest( prettyPrint() ), preprocessResponse( prettyPrint() ), snippets );
}
 
Example #12
Source File: RamlDocumentation.java    From restdocs-raml with MIT License 4 votes vote down vote up
public static RestDocumentationResultHandler document(String identifier,
                                                      String description,
                                                      boolean privateResource,
                                                      Snippet... snippets) {
    return document(identifier, description, privateResource, null, null, Function.identity(), snippets);
}
 
Example #13
Source File: RamlDocumentation.java    From restdocs-raml with MIT License 4 votes vote down vote up
public static RestDocumentationResultHandler document(String identifier,
                                                      boolean privateResource,
                                                      OperationResponsePreprocessor responsePreprocessor,
                                                      Snippet... snippets) {
    return document(identifier, "", privateResource, null, responsePreprocessor, Function.identity(), snippets);
}
 
Example #14
Source File: RamlDocumentation.java    From restdocs-raml with MIT License 4 votes vote down vote up
public static RestDocumentationResultHandler document(String identifier,
                                                      boolean privateResource,
                                                      OperationRequestPreprocessor requestPreprocessor,
                                                      Snippet... snippets) {
    return document(identifier, "", privateResource, requestPreprocessor, null, Function.identity(), snippets);
}
 
Example #15
Source File: RamlDocumentation.java    From restdocs-raml with MIT License 4 votes vote down vote up
public static RestDocumentationResultHandler document(String identifier,
                                                      OperationRequestPreprocessor requestPreprocessor,
                                                      OperationResponsePreprocessor responsePreprocessor,
                                                      Snippet... snippets) {
    return document(identifier, "", false, requestPreprocessor, responsePreprocessor, Function.identity(), snippets);
}
 
Example #16
Source File: RamlDocumentation.java    From restdocs-raml with MIT License 4 votes vote down vote up
public static RestDocumentationResultHandler document(String identifier,
                                                      OperationResponsePreprocessor responsePreprocessor,
                                                      Snippet... snippets) {
    return document(identifier, "", false, null, responsePreprocessor, Function.identity(), snippets);
}
 
Example #17
Source File: RamlDocumentation.java    From restdocs-raml with MIT License 4 votes vote down vote up
public static RestDocumentationResultHandler document(String identifier,
                                                      Snippet... snippets) {
    return document(identifier, "", false, null, null, Function.identity(), snippets);
}
 
Example #18
Source File: AbstractSpringMVCTest.java    From SMSC with Apache License 2.0 2 votes vote down vote up
/**
 * Pretty print request and response
 *
 * @param useCase the name of the snippet
 * @return RestDocumentationResultHandler
 */
protected RestDocumentationResultHandler documentPrettyPrintReqResp(String useCase) {
    return document(useCase,
            preprocessRequest(prettyPrint()),
            preprocessResponse(prettyPrint()));
}
 
Example #19
Source File: UserControllerDocumentation.java    From spring-tutorials with Apache License 2.0 2 votes vote down vote up
/**
 * Pretty print request and response
 * 
 * @param useCase the name of the snippet
 * @return RestDocumentationResultHandler
 */
private RestDocumentationResultHandler documentPrettyPrintReqResp(String useCase) {
    return document(useCase,
                    preprocessRequest(prettyPrint()),
                    preprocessResponse(prettyPrint()));
}