Example usage for org.springframework.restdocs.mockmvc RestDocumentationRequestBuilders patch

List of usage examples for org.springframework.restdocs.mockmvc RestDocumentationRequestBuilders patch

Introduction

In this page you can find the example usage for org.springframework.restdocs.mockmvc RestDocumentationRequestBuilders patch.

Prototype

public static MockHttpServletRequestBuilder patch(String urlTemplate, Object... urlVariables) 

Source Link

Document

Create a MockHttpServletRequestBuilder for a PATCH request.

Usage

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

/**
 * Test to make sure that a cluster can be patched.
 *
 * @throws Exception on configuration errors
 *///from  w  w  w  .jav a  2  s. c  om
@Test
public void canPatchCluster() throws Exception {
    Assert.assertThat(this.jpaClusterRepository.count(), Matchers.is(0L));
    final String id = this.createConfigResource(
            new Cluster.Builder(NAME, USER, VERSION, ClusterStatus.UP).withId(ID).build(), null);
    final String clusterResource = CLUSTERS_API + "/{id}";
    this.mvc.perform(MockMvcRequestBuilders.get(clusterResource, id))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.jsonPath(NAME_PATH, Matchers.is(NAME)));

    final String newName = UUID.randomUUID().toString();
    final String patchString = "[{ \"op\": \"replace\", \"path\": \"/name\", \"value\": \"" + newName + "\" }]";
    final JsonPatch patch = JsonPatch.fromJson(this.objectMapper.readTree(patchString));

    final RestDocumentationResultHandler patchResultHandler = MockMvcRestDocumentation.document(
            "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
            Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.CONTENT_TYPE_HEADER, // request headers
            Snippets.ID_PATH_PARAM, // path params
            Snippets.PATCH_FIELDS // request payload
    );

    this.mvc.perform(RestDocumentationRequestBuilders.patch(clusterResource, id)
            .contentType(MediaType.APPLICATION_JSON).content(this.objectMapper.writeValueAsBytes(patch)))
            .andExpect(MockMvcResultMatchers.status().isNoContent()).andDo(patchResultHandler);

    this.mvc.perform(MockMvcRequestBuilders.get(clusterResource, id))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath(NAME_PATH, Matchers.is(newName)));
    Assert.assertThat(this.jpaClusterRepository.count(), Matchers.is(1L));
}

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

/**
 * Test to make sure that a command can be patched.
 *
 * @throws Exception on configuration errors
 *//*w  ww.  j  a  v a  2  s .  co  m*/
@Test
public void canPatchCommand() throws Exception {
    Assert.assertThat(this.jpaCommandRepository.count(), Matchers.is(0L));
    final String id = this.createConfigResource(
            new Command.Builder(NAME, USER, VERSION, CommandStatus.ACTIVE, EXECUTABLE, CHECK_DELAY).withId(ID)
                    .build(),
            null);
    final String commandResource = COMMANDS_API + "/{id}";
    this.mvc.perform(MockMvcRequestBuilders.get(commandResource, id))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.jsonPath(NAME_PATH, Matchers.is(NAME)));

    final String newName = UUID.randomUUID().toString();
    final String patchString = "[{ \"op\": \"replace\", \"path\": \"/name\", \"value\": \"" + newName + "\" }]";
    final JsonPatch patch = JsonPatch.fromJson(this.objectMapper.readTree(patchString));

    final RestDocumentationResultHandler patchResultHandler = MockMvcRestDocumentation.document(
            "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
            Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.CONTENT_TYPE_HEADER, // request headers
            Snippets.ID_PATH_PARAM, // path params
            Snippets.PATCH_FIELDS // request payload
    );

    this.mvc.perform(RestDocumentationRequestBuilders.patch(commandResource, id)
            .contentType(MediaType.APPLICATION_JSON).content(this.objectMapper.writeValueAsBytes(patch)))
            .andExpect(MockMvcResultMatchers.status().isNoContent()).andDo(patchResultHandler);

    this.mvc.perform(MockMvcRequestBuilders.get(commandResource, id))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath(NAME_PATH, Matchers.is(newName)));
    Assert.assertThat(this.jpaCommandRepository.count(), Matchers.is(1L));
}

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

/**
 * Test to make sure that an application can be patched.
 *
 * @throws Exception on configuration errors
 *//* w  ww .ja  v a 2s.  c om*/
@Test
public void canPatchApplication() throws Exception {
    Assert.assertThat(this.jpaApplicationRepository.count(), Matchers.is(0L));
    final String id = this.createConfigResource(
            new Application.Builder(NAME, USER, VERSION, ApplicationStatus.ACTIVE).withId(ID).build(), null);
    final String applicationResource = APPLICATIONS_API + "/{id}";
    this.mvc.perform(MockMvcRequestBuilders.get(applicationResource, id))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.jsonPath(USER_PATH, Matchers.is(USER)));

    final String newUser = UUID.randomUUID().toString();
    final String patchString = "[{ \"op\": \"replace\", \"path\": \"/user\", \"value\": \"" + newUser + "\" }]";
    final JsonPatch patch = JsonPatch.fromJson(this.objectMapper.readTree(patchString));

    final RestDocumentationResultHandler patchResultHandler = MockMvcRestDocumentation.document(
            "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
            Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.CONTENT_TYPE_HEADER, // request headers
            Snippets.ID_PATH_PARAM, // path params
            Snippets.PATCH_FIELDS // request payload
    );

    this.mvc.perform(RestDocumentationRequestBuilders.patch(applicationResource, id)
            .contentType(MediaType.APPLICATION_JSON).content(this.objectMapper.writeValueAsBytes(patch)))
            .andExpect(MockMvcResultMatchers.status().isNoContent()).andDo(patchResultHandler);

    this.mvc.perform(MockMvcRequestBuilders.get(applicationResource, id))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath(USER_PATH, Matchers.is(newUser)));
    Assert.assertThat(this.jpaApplicationRepository.count(), Matchers.is(1L));
}