Example usage for org.springframework.restdocs.mockmvc MockMvcRestDocumentation document

List of usage examples for org.springframework.restdocs.mockmvc MockMvcRestDocumentation document

Introduction

In this page you can find the example usage for org.springframework.restdocs.mockmvc MockMvcRestDocumentation document.

Prototype

public static RestDocumentationResultHandler document(String identifier,
        OperationRequestPreprocessor requestPreprocessor, OperationResponsePreprocessor responsePreprocessor,
        Snippet... snippets) 

Source Link

Document

Documents the API call with the given identifier using the given snippets in addition to any default snippets.

Usage

From source file:com.netflix.genie.web.controllers.CommandRestControllerIntegrationTests.java

/**
 * Make sure that we can remove an application from a command.
 *
 * @throws Exception on configuration error
 *//*from  w w  w  .  j  a v a2 s.  c o m*/
@Test
public void canRemoveApplicationFromACommand() throws Exception {
    this.createConfigResource(
            new Command.Builder(NAME, USER, VERSION, CommandStatus.ACTIVE, EXECUTABLE, CHECK_DELAY).withId(ID)
                    .build(),
            null);
    final String commandApplicationsAPI = COMMANDS_API + "/{id}/applications";

    final String placeholder = UUID.randomUUID().toString();
    final String applicationId1 = UUID.randomUUID().toString();
    final String applicationId2 = UUID.randomUUID().toString();
    final String applicationId3 = UUID.randomUUID().toString();
    this.createConfigResource(
            new Application.Builder(placeholder, placeholder, placeholder, ApplicationStatus.ACTIVE)
                    .withId(applicationId1).build(),
            null);
    this.createConfigResource(
            new Application.Builder(placeholder, placeholder, placeholder, ApplicationStatus.ACTIVE)
                    .withId(applicationId2).build(),
            null);
    this.createConfigResource(
            new Application.Builder(placeholder, placeholder, placeholder, ApplicationStatus.ACTIVE)
                    .withId(applicationId3).build(),
            null);

    this.mvc.perform(
            MockMvcRequestBuilders.post(commandApplicationsAPI, ID).contentType(MediaType.APPLICATION_JSON)
                    .content(this.objectMapper.writeValueAsBytes(
                            Lists.newArrayList(applicationId1, applicationId2, applicationId3))))
            .andExpect(MockMvcResultMatchers.status().isNoContent());

    final RestDocumentationResultHandler deleteResultHandler = MockMvcRestDocumentation.document(
            "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
            Preprocessors.preprocessResponse(Preprocessors.prettyPrint()),
            Snippets.ID_PATH_PARAM.and(RequestDocumentation.parameterWithName("applicationId")
                    .description("The id of the application to remove")) // Path parameters
    );

    this.mvc.perform(RestDocumentationRequestBuilders.delete(commandApplicationsAPI + "/{applicationId}", ID,
            applicationId2)).andExpect(MockMvcResultMatchers.status().isNoContent()).andDo(deleteResultHandler);

    this.mvc.perform(MockMvcRequestBuilders.get(commandApplicationsAPI, ID))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(2)))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].id", Matchers.is(applicationId1)))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].id", Matchers.is(applicationId3)));

    // Check reverse side of relationship
    this.mvc.perform(MockMvcRequestBuilders.get(APPLICATIONS_API + "/{id}/commands", applicationId1))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(1)))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].id", Matchers.is(ID)));

    this.mvc.perform(MockMvcRequestBuilders.get(APPLICATIONS_API + "/{id}/commands", applicationId2))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.empty()));

    this.mvc.perform(MockMvcRequestBuilders.get(APPLICATIONS_API + "/{id}/commands", applicationId3))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(1)))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].id", Matchers.is(ID)));
}