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

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

Introduction

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

Prototype

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

Source Link

Document

Create a MockHttpServletRequestBuilder for a PUT request.

Usage

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

/**
 * Test to make sure that a cluster can be updated.
 *
 * @throws Exception on configuration errors
 *//*from   w ww.j  a  v  a2  s . c o m*/
@Test
public void canUpdateCluster() throws Exception {
    Assert.assertThat(this.jpaClusterRepository.count(), Matchers.is(0L));
    this.createConfigResource(new Cluster.Builder(NAME, USER, VERSION, ClusterStatus.UP).withId(ID).build(),
            null);
    final String clusterResource = CLUSTERS_API + "/{id}";
    final Cluster createdCluster = this.objectMapper
            .readValue(this.mvc.perform(MockMvcRequestBuilders.get(clusterResource, ID)).andReturn()
                    .getResponse().getContentAsByteArray(), ClusterResource.class)
            .getContent();
    Assert.assertThat(createdCluster.getStatus(), Matchers.is(ClusterStatus.UP));

    final Cluster.Builder updateCluster = new Cluster.Builder(createdCluster.getName(),
            createdCluster.getUser(), createdCluster.getVersion(), ClusterStatus.OUT_OF_SERVICE)
                    .withId(createdCluster.getId().orElseThrow(IllegalArgumentException::new))
                    .withCreated(createdCluster.getCreated().orElseThrow(IllegalArgumentException::new))
                    .withUpdated(createdCluster.getUpdated().orElseThrow(IllegalArgumentException::new))
                    .withTags(createdCluster.getTags()).withConfigs(createdCluster.getConfigs())
                    .withDependencies(createdCluster.getDependencies());

    createdCluster.getDescription().ifPresent(updateCluster::withDescription);
    createdCluster.getSetupFile().ifPresent(updateCluster::withSetupFile);

    final RestDocumentationResultHandler updateResultHandler = MockMvcRestDocumentation.document(
            "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
            Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.CONTENT_TYPE_HEADER, // request header
            Snippets.ID_PATH_PARAM, // path parameters
            Snippets.getClusterRequestPayload() // payload fields
    );

    this.mvc.perform(
            RestDocumentationRequestBuilders.put(clusterResource, ID).contentType(MediaType.APPLICATION_JSON)
                    .content(this.objectMapper.writeValueAsBytes(updateCluster.build())))
            .andExpect(MockMvcResultMatchers.status().isNoContent()).andDo(updateResultHandler);

    this.mvc.perform(MockMvcRequestBuilders.get(clusterResource, ID))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath(STATUS_PATH,
                    Matchers.is(ClusterStatus.OUT_OF_SERVICE.toString())));
    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 updated.
 *
 * @throws Exception on configuration errors
 *//*from   w  ww.  j  a v  a2  s .  c  o  m*/
@Test
public void canUpdateCommand() throws Exception {
    Assert.assertThat(this.jpaCommandRepository.count(), Matchers.is(0L));
    this.createConfigResource(
            new Command.Builder(NAME, USER, VERSION, CommandStatus.ACTIVE, EXECUTABLE, CHECK_DELAY).withId(ID)
                    .build(),
            null);
    final String commandResource = COMMANDS_API + "/{id}";
    final Command createdCommand = objectMapper
            .readValue(this.mvc.perform(MockMvcRequestBuilders.get(commandResource, ID)).andReturn()
                    .getResponse().getContentAsByteArray(), CommandResource.class)
            .getContent();
    Assert.assertThat(createdCommand.getStatus(), Matchers.is(CommandStatus.ACTIVE));

    final Command.Builder updateCommand = new Command.Builder(createdCommand.getName(),
            createdCommand.getUser(), createdCommand.getVersion(), CommandStatus.INACTIVE,
            createdCommand.getExecutable(), createdCommand.getCheckDelay())
                    .withId(createdCommand.getId().orElseThrow(IllegalArgumentException::new))
                    .withCreated(createdCommand.getCreated().orElseThrow(IllegalArgumentException::new))
                    .withUpdated(createdCommand.getUpdated().orElseThrow(IllegalArgumentException::new))
                    .withTags(createdCommand.getTags()).withConfigs(createdCommand.getConfigs())
                    .withDependencies(createdCommand.getDependencies());

    createdCommand.getDescription().ifPresent(updateCommand::withDescription);
    createdCommand.getSetupFile().ifPresent(updateCommand::withSetupFile);

    final RestDocumentationResultHandler updateResultHandler = MockMvcRestDocumentation.document(
            "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
            Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.CONTENT_TYPE_HEADER, // request header
            Snippets.ID_PATH_PARAM, // path parameters
            Snippets.getCommandRequestPayload() // payload fields
    );

    this.mvc.perform(
            RestDocumentationRequestBuilders.put(commandResource, ID).contentType(MediaType.APPLICATION_JSON)
                    .content(this.objectMapper.writeValueAsBytes(updateCommand.build())))
            .andExpect(MockMvcResultMatchers.status().isNoContent()).andDo(updateResultHandler);

    this.mvc.perform(MockMvcRequestBuilders.get(commandResource, ID))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath(STATUS_PATH,
                    Matchers.is(CommandStatus.INACTIVE.toString())));
    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 updated.
 *
 * @throws Exception on configuration errors
 *///w  w  w  . j  a v a2 s  . c om
@Test
public void canUpdateApplication() 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}";
    final Application createdApp = this.objectMapper
            .readValue(this.mvc.perform(MockMvcRequestBuilders.get(applicationResource, id)).andReturn()
                    .getResponse().getContentAsByteArray(), ApplicationResource.class)
            .getContent();
    Assert.assertThat(createdApp.getStatus(), Matchers.is(ApplicationStatus.ACTIVE));

    final Application.Builder newStatusApp = new Application.Builder(createdApp.getName(), createdApp.getUser(),
            createdApp.getVersion(), ApplicationStatus.INACTIVE)
                    .withId(createdApp.getId().orElseThrow(IllegalArgumentException::new))
                    .withCreated(createdApp.getCreated().orElseThrow(IllegalArgumentException::new))
                    .withUpdated(createdApp.getUpdated().orElseThrow(IllegalArgumentException::new))
                    .withTags(createdApp.getTags()).withConfigs(createdApp.getConfigs())
                    .withDependencies(createdApp.getDependencies());

    createdApp.getDescription().ifPresent(newStatusApp::withDescription);
    createdApp.getSetupFile().ifPresent(newStatusApp::withSetupFile);

    final RestDocumentationResultHandler updateResultHandler = MockMvcRestDocumentation.document(
            "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
            Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.CONTENT_TYPE_HEADER, // request header
            Snippets.ID_PATH_PARAM, // path parameters
            Snippets.getApplicationRequestPayload() // payload fields
    );

    this.mvc.perform(RestDocumentationRequestBuilders.put(applicationResource, id)
            .contentType(MediaType.APPLICATION_JSON)
            .content(this.objectMapper.writeValueAsBytes(newStatusApp.build())))
            .andExpect(MockMvcResultMatchers.status().isNoContent()).andDo(updateResultHandler);

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

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

/**
 * Make sure can set the commands for a cluster.
 *
 * @throws Exception on configuration error
 *//* w w w  . ja  v  a  2  s. co m*/
@Test
public void canSetCommandsForACluster() throws Exception {
    this.createConfigResource(new Cluster.Builder(NAME, USER, VERSION, ClusterStatus.UP).withId(ID).build(),
            null);
    final String clusterCommandsAPI = CLUSTERS_API + "/{id}/commands";
    this.mvc.perform(MockMvcRequestBuilders.get(clusterCommandsAPI, ID))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.empty()));

    final String placeholder = UUID.randomUUID().toString();
    final String commandId1 = UUID.randomUUID().toString();
    final String commandId2 = UUID.randomUUID().toString();
    this.createConfigResource(
            new Command.Builder(placeholder, placeholder, placeholder, CommandStatus.ACTIVE, placeholder, 4000L)
                    .withId(commandId1).build(),
            null);
    this.createConfigResource(
            new Command.Builder(placeholder, placeholder, placeholder, CommandStatus.ACTIVE, placeholder, 5000L)
                    .withId(commandId2).build(),
            null);

    final RestDocumentationResultHandler setResultHandler = 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 parameters
            PayloadDocumentation.requestFields(PayloadDocumentation.fieldWithPath("[]").description(
                    "Array of command ids (in preferred order) to replace the existing list of commands")
                    .attributes(Snippets.EMPTY_CONSTRAINTS)) // Request payload
    );

    this.mvc.perform(
            RestDocumentationRequestBuilders.put(clusterCommandsAPI, ID).contentType(MediaType.APPLICATION_JSON)
                    .content(this.objectMapper.writeValueAsBytes(Lists.newArrayList(commandId1, commandId2))))
            .andExpect(MockMvcResultMatchers.status().isNoContent()).andDo(setResultHandler);

    this.mvc.perform(MockMvcRequestBuilders.get(clusterCommandsAPI, 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(commandId1)))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].id", Matchers.is(commandId2)));

    //Should clear commands
    this.mvc.perform(MockMvcRequestBuilders.put(clusterCommandsAPI, ID).contentType(MediaType.APPLICATION_JSON)
            .content(this.objectMapper.writeValueAsBytes(Lists.newArrayList())))
            .andExpect(MockMvcResultMatchers.status().isNoContent());

    this.mvc.perform(MockMvcRequestBuilders.get(clusterCommandsAPI, ID))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.empty()));
}

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

/**
 * Make sure can set the applications for a command.
 *
 * @throws Exception on configuration error
 *///from   w w w .ja  v a2 s. c  om
@Test
public void canSetApplicationsForACommand() 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";
    this.mvc.perform(MockMvcRequestBuilders.get(commandApplicationsAPI, ID))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.empty()));

    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);

    final RestDocumentationResultHandler setResultHandler = 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 parameters
            PayloadDocumentation.requestFields(PayloadDocumentation.fieldWithPath("[]")
                    .description("Array of application ids to replace the existing set of applications with")
                    .attributes(Snippets.EMPTY_CONSTRAINTS)) // Request payload
    );

    this.mvc.perform(RestDocumentationRequestBuilders.put(commandApplicationsAPI, ID)
            .contentType(MediaType.APPLICATION_JSON)
            .content(this.objectMapper.writeValueAsBytes(Lists.newArrayList(applicationId1, applicationId2))))
            .andExpect(MockMvcResultMatchers.status().isNoContent()).andDo(setResultHandler);

    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(applicationId2)));

    // Should flip the order
    this.mvc.perform(
            MockMvcRequestBuilders.put(commandApplicationsAPI, ID).contentType(MediaType.APPLICATION_JSON)
                    .content(this.objectMapper
                            .writeValueAsBytes(Lists.newArrayList(applicationId2, applicationId1))))
            .andExpect(MockMvcResultMatchers.status().isNoContent());

    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(applicationId2)))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].id", Matchers.is(applicationId1)));

    // Should reorder and add a new one
    this.mvc.perform(
            MockMvcRequestBuilders.put(commandApplicationsAPI, ID).contentType(MediaType.APPLICATION_JSON)
                    .content(objectMapper.writeValueAsBytes(
                            Lists.newArrayList(applicationId1, applicationId2, applicationId3))))
            .andExpect(MockMvcResultMatchers.status().isNoContent());

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

    //Should clear applications
    this.mvc.perform(
            MockMvcRequestBuilders.put(commandApplicationsAPI, ID).contentType(MediaType.APPLICATION_JSON)
                    .content(objectMapper.writeValueAsBytes(Lists.newArrayList())))
            .andExpect(MockMvcResultMatchers.status().isNoContent());

    this.mvc.perform(MockMvcRequestBuilders.get(commandApplicationsAPI, ID))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.empty()));
}