Example usage for org.springframework.restdocs.request RequestDocumentation parameterWithName

List of usage examples for org.springframework.restdocs.request RequestDocumentation parameterWithName

Introduction

In this page you can find the example usage for org.springframework.restdocs.request RequestDocumentation parameterWithName.

Prototype

public static ParameterDescriptor parameterWithName(String name) 

Source Link

Document

Creates a ParameterDescriptor that describes a request or path parameter with the given name .

Usage

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

private void checkJobOutput(final int documentationId, final String id) throws Exception {
    // Check getting a directory as json
    final RestDocumentationFilter jsonResultFilter = RestAssuredRestDocumentation.document(
            "{class-name}/" + documentationId + "/getJobOutput/json/",
            Snippets.ID_PATH_PARAM.and(RequestDocumentation.parameterWithName("filePath")
                    .description("The path to the directory to get").optional()), // Path parameters
            HeaderDocumentation.requestHeaders(HeaderDocumentation.headerWithName(HttpHeaders.ACCEPT)
                    .description(MediaType.APPLICATION_JSON_VALUE).optional()), // Request header
            HeaderDocumentation.responseHeaders(HeaderDocumentation.headerWithName(HttpHeaders.CONTENT_TYPE)
                    .description(MediaType.APPLICATION_JSON_VALUE)), // Response Headers
            Snippets.OUTPUT_DIRECTORY_FIELDS);

    RestAssured.given(this.getRequestSpecification()).filter(jsonResultFilter)
            .accept(MediaType.APPLICATION_JSON_VALUE).when().port(this.port)
            .get(JOBS_API + "/{id}/output/{filePath}", id, "").then()
            .statusCode(Matchers.is(HttpStatus.OK.value()))
            .contentType(Matchers.equalToIgnoringCase(MediaType.APPLICATION_JSON_UTF8_VALUE))
            .body("parent", Matchers.isEmptyOrNullString()).body("directories[0].name", Matchers.is("genie/"))
            .body("files[0].name", Matchers.is("config1")).body("files[1].name", Matchers.is("dep1"))
            .body("files[2].name", Matchers.is("jobsetupfile")).body("files[3].name", Matchers.is("run"))
            .body("files[4].name", Matchers.is("stderr")).body("files[5].name", Matchers.is("stdout"));

    // Check getting a directory as HTML
    final RestDocumentationFilter htmlResultFilter = RestAssuredRestDocumentation.document(
            "{class-name}/" + documentationId + "/getJobOutput/html/",
            Snippets.ID_PATH_PARAM.and(RequestDocumentation.parameterWithName("filePath")
                    .description("The path to the directory to get").optional()), // Path parameters
            HeaderDocumentation.requestHeaders(
                    HeaderDocumentation.headerWithName(HttpHeaders.ACCEPT).description(MediaType.TEXT_HTML)), // Request header
            HeaderDocumentation.responseHeaders(HeaderDocumentation.headerWithName(HttpHeaders.CONTENT_TYPE)
                    .description(MediaType.TEXT_HTML)) // Response Headers
    );/*from  w w  w  .  ja  v a2  s  . c  om*/

    RestAssured.given(this.getRequestSpecification()).filter(htmlResultFilter).accept(MediaType.TEXT_HTML_VALUE)
            .when().port(this.port).get(JOBS_API + "/{id}/output/{filePath}", id, "").then()
            .statusCode(Matchers.is(HttpStatus.OK.value()))
            .contentType(Matchers.containsString(MediaType.TEXT_HTML_VALUE));

    // Check getting a file
    final RestDocumentationFilter fileResultFilter = RestAssuredRestDocumentation.document(
            "{class-name}/" + documentationId + "/getJobOutput/file/",
            Snippets.ID_PATH_PARAM.and(RequestDocumentation.parameterWithName("filePath")
                    .description("The path to the file to get").optional()), // Path parameters
            HeaderDocumentation.requestHeaders(HeaderDocumentation.headerWithName(HttpHeaders.ACCEPT)
                    .description(MediaType.ALL_VALUE).optional()), // Request header
            HeaderDocumentation.responseHeaders(HeaderDocumentation.headerWithName(HttpHeaders.CONTENT_TYPE)
                    .description("The content type of the file being returned").optional()) // Response Headers
    );

    // check that the generated run file is correct
    final String runShFileName = SystemUtils.IS_OS_LINUX ? "linux-runsh.txt" : "non-linux-runsh.txt";

    final String runShFile = this.resourceLoader.getResource(BASE_DIR + runShFileName).getFile()
            .getAbsolutePath();
    final String runFileContents = new String(Files.readAllBytes(Paths.get(runShFile)), StandardCharsets.UTF_8);

    final String jobWorkingDir = this.jobDirResource.getFile().getCanonicalPath() + FILE_DELIMITER + id;
    final String expectedRunScriptContent = this.getExpectedRunContents(runFileContents, jobWorkingDir, id);

    RestAssured.given(this.getRequestSpecification()).filter(fileResultFilter).when().port(this.port)
            .get(JOBS_API + "/{id}/output/{filePath}", id, "run").then()
            .statusCode(Matchers.is(HttpStatus.OK.value())).body(Matchers.is(expectedRunScriptContent));
}

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

private void checkJobOutput(final int documentationId, final String id) throws Exception {
    // Check getting a directory as json
    final RestDocumentationResultHandler jsonResultHandler = MockMvcRestDocumentation.document(
            "{class-name}/" + documentationId + "/getJobOutput/json/",
            Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
            Preprocessors.preprocessResponse(Preprocessors.prettyPrint()),
            Snippets.ID_PATH_PARAM.and(RequestDocumentation.parameterWithName("filePath")
                    .description("The path to the directory to get").optional()), // Path parameters
            HeaderDocumentation.requestHeaders(HeaderDocumentation.headerWithName(HttpHeaders.ACCEPT)
                    .description(MediaType.APPLICATION_JSON_VALUE).optional()), // Request header
            HeaderDocumentation.responseHeaders(HeaderDocumentation.headerWithName(HttpHeaders.CONTENT_TYPE)
                    .description(MediaType.APPLICATION_JSON_VALUE)), // Response Headers
            Snippets.OUTPUT_DIRECTORY_FIELDS);
    this.mvc.perform(RestDocumentationRequestBuilders.get(JOBS_API + "/{id}/output/{filePath}", id, ""))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath("parent", Matchers.isEmptyOrNullString()))
            .andExpect(MockMvcResultMatchers.jsonPath("$.directories[0].name", Matchers.is("genie/")))
            .andExpect(MockMvcResultMatchers.jsonPath("$.files[0].name", Matchers.is("dep1")))
            .andExpect(MockMvcResultMatchers.jsonPath("$.files[1].name", Matchers.is("jobsetupfile")))
            .andExpect(MockMvcResultMatchers.jsonPath("$.files[2].name", Matchers.is("run")))
            .andExpect(MockMvcResultMatchers.jsonPath("$.files[3].name", Matchers.is("stderr")))
            .andExpect(MockMvcResultMatchers.jsonPath("$.files[4].name", Matchers.is("stdout")))
            .andDo(jsonResultHandler);/*from   w w  w .jav  a 2  s  .  c o  m*/

    // Check getting a directory as HTML
    final RestDocumentationResultHandler htmlResultHandler = MockMvcRestDocumentation.document(
            "{class-name}/" + documentationId + "/getJobOutput/html/",
            Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
            Preprocessors.preprocessResponse(Preprocessors.prettyPrint()),
            Snippets.ID_PATH_PARAM.and(RequestDocumentation.parameterWithName("filePath")
                    .description("The path to the directory to get").optional()), // Path parameters
            HeaderDocumentation.requestHeaders(
                    HeaderDocumentation.headerWithName(HttpHeaders.ACCEPT).description(MediaType.TEXT_HTML)), // Request header
            HeaderDocumentation.responseHeaders(HeaderDocumentation.headerWithName(HttpHeaders.CONTENT_TYPE)
                    .description(MediaType.TEXT_HTML)) // Response Headers
    );
    this.mvc.perform(RestDocumentationRequestBuilders.get(JOBS_API + "/{id}/output/{filePath}", id, "")
            .accept(MediaType.TEXT_HTML)).andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaType.TEXT_HTML))
            .andDo(htmlResultHandler);

    // Check getting a file

    // Check getting a directory as HTML
    final RestDocumentationResultHandler fileResultHandler = MockMvcRestDocumentation.document(
            "{class-name}/" + documentationId + "/getJobOutput/file/",
            Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
            Preprocessors.preprocessResponse(Preprocessors.prettyPrint()),
            Snippets.ID_PATH_PARAM.and(RequestDocumentation.parameterWithName("filePath")
                    .description("The path to the file to get").optional()), // Path parameters
            HeaderDocumentation.requestHeaders(HeaderDocumentation.headerWithName(HttpHeaders.ACCEPT)
                    .description(MediaType.ALL_VALUE).optional()), // Request header
            HeaderDocumentation.responseHeaders(HeaderDocumentation.headerWithName(HttpHeaders.CONTENT_TYPE)
                    .description("The content type of the file being returned").optional()) // Response Headers
    );

    // check that the generated run file is correct
    final String runShFileName = SystemUtils.IS_OS_LINUX ? "linux-runsh.txt" : "non-linux-runsh.txt";

    final String runShFile = this.resourceLoader.getResource(BASE_DIR + runShFileName).getFile()
            .getAbsolutePath();
    final String runFileContents = new String(Files.readAllBytes(Paths.get(runShFile)), "UTF-8");

    final String jobWorkingDir = this.jobDirResource.getFile().getCanonicalPath() + FILE_DELIMITER + id;
    final String expectedRunScriptContent = this.getExpectedRunContents(runFileContents, jobWorkingDir, id);

    this.mvc.perform(RestDocumentationRequestBuilders.get(JOBS_API + "/{id}/output/{filePath}", id, "run"))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().string(expectedRunScriptContent))
            .andDo(fileResultHandler);
}

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

/**
 * Test to make sure we can delete a tag for a cluster after it is created.
 *
 * @throws Exception on configuration problems
 */// w ww.  j  av a2  s  .co m
@Test
public void canDeleteTagForCluster() 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 api = CLUSTERS_API + "/{id}/tags";
    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("tag").description("The tag to remove")));
    this.canDeleteTagForResource(api, ID, NAME, deleteResultHandler);
}

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

private static ParameterDescriptor[] getCommonSearchParameters() {
    return new ParameterDescriptor[] {
            RequestDocumentation.parameterWithName("page").description("The page number to get. Default to 0.")
                    .optional(),/*from ww w  .j  a v  a 2s.com*/
            RequestDocumentation.parameterWithName("size")
                    .description("The size of the page to get. Default to 64.").optional(),
            RequestDocumentation.parameterWithName("sort")
                    .description("The fields to sort the results by. Defaults to 'updated,desc'.")
                    .optional(), };
}

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

/**
 * Test to make sure we can delete a tag for a command after it is created.
 *
 * @throws Exception on configuration problems
 */// www  .j a v  a  2  s.  c  o m
@Test
public void canDeleteTagForCommand() 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 api = COMMANDS_API + "/{id}/tags";

    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("tag").description("The tag to remove")));
    this.canDeleteTagForResource(api, ID, NAME, deleteResultHandler);
}

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

/**
 * Make sure can add the commands for a cluster.
 *
 * @throws Exception on configuration error
 *//*from   www . j a  v a2 s.  c  o m*/
@Test
public void canAddCommandsForACluster() 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, 1000L)
                    .withId(commandId1).build(),
            null);
    this.createConfigResource(
            new Command.Builder(placeholder, placeholder, placeholder, CommandStatus.ACTIVE, placeholder, 2000L)
                    .withId(commandId2).build(),
            null);

    final RestDocumentationResultHandler addResultHandler = 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 append to the existing list of commands")
                    .attributes(Snippets.EMPTY_CONSTRAINTS)) // Request payload
    );

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

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

    //Shouldn't add anything
    this.mvc.perform(MockMvcRequestBuilders.post(clusterCommandsAPI, ID).contentType(MediaType.APPLICATION_JSON)
            .content(this.objectMapper.writeValueAsBytes(Lists.newArrayList())))
            .andExpect(MockMvcResultMatchers.status().isPreconditionFailed());

    final String commandId3 = UUID.randomUUID().toString();
    this.createConfigResource(new Command.Builder(placeholder, placeholder, placeholder, CommandStatus.INACTIVE,
            placeholder, 1000L).withId(commandId3).build(), null);
    this.mvc.perform(MockMvcRequestBuilders.post(clusterCommandsAPI, ID).contentType(MediaType.APPLICATION_JSON)
            .content(this.objectMapper.writeValueAsBytes(Lists.newArrayList(commandId3))))
            .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.hasSize(3)))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].id", Matchers.is(commandId1)))
            .andExpect(MockMvcResultMatchers.jsonPath("$[1].id", Matchers.is(commandId2)))
            .andExpect(MockMvcResultMatchers.jsonPath("$[2].id", Matchers.is(commandId3)));

    // Test the filtering
    final RestDocumentationResultHandler getResultHandler = MockMvcRestDocumentation.document(
            "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
            Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.ID_PATH_PARAM, // Path parameters
            RequestDocumentation.requestParameters(RequestDocumentation.parameterWithName("status")
                    .description("The status of commands to search for")
                    .attributes(Attributes.key(Snippets.CONSTRAINTS).value(CommandStatus.values())).optional()), // Query Parameters
            Snippets.HAL_CONTENT_TYPE_HEADER, // Response Headers
            PayloadDocumentation.responseFields(PayloadDocumentation.fieldWithPath("[]")
                    .description("The list of commands found").attributes(Snippets.EMPTY_CONSTRAINTS)));
    this.mvc.perform(RestDocumentationRequestBuilders.get(clusterCommandsAPI, ID).param("status",
            CommandStatus.INACTIVE.toString())).andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(1)))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].id", Matchers.is(commandId3)))
            .andDo(getResultHandler);
}

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

/**
 * Test to make sure we can delete a tag for an application after it is created.
 *
 * @throws Exception on configuration problems
 *//*from w  w  w .  j  a v  a  2s .co  m*/
@Test
public void canDeleteTagForApplication() throws Exception {
    Assert.assertThat(this.jpaApplicationRepository.count(), Matchers.is(0L));
    this.createConfigResource(
            new Application.Builder(NAME, USER, VERSION, ApplicationStatus.ACTIVE).withId(ID).build(), null);
    final String api = APPLICATIONS_API + "/{id}/tags";

    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("tag").description("The tag to remove")));
    this.canDeleteTagForResource(api, ID, NAME, deleteResultHandler);
}

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

/**
 * Make sure can get all the commands which use a given application.
 *
 * @throws Exception on configuration error
 *//*from www  .  j a v  a 2  s.  c  o m*/
@Test
public void canGetCommandsForApplication() throws Exception {
    this.createConfigResource(
            new Application.Builder(NAME, USER, VERSION, ApplicationStatus.ACTIVE).withId(ID).build(), null);
    final String placeholder = UUID.randomUUID().toString();
    final String command1Id = UUID.randomUUID().toString();
    final String command2Id = UUID.randomUUID().toString();
    final String command3Id = UUID.randomUUID().toString();
    this.createConfigResource(
            new Command.Builder(placeholder, placeholder, placeholder, CommandStatus.ACTIVE, placeholder, 1000L)
                    .withId(command1Id).build(),
            null);
    this.createConfigResource(new Command.Builder(placeholder, placeholder, placeholder, CommandStatus.INACTIVE,
            placeholder, 1100L).withId(command2Id).build(), null);
    this.createConfigResource(new Command.Builder(placeholder, placeholder, placeholder,
            CommandStatus.DEPRECATED, placeholder, 1200L).withId(command3Id).build(), null);

    final Set<String> appIds = Sets.newHashSet(ID);
    this.mvc.perform(MockMvcRequestBuilders.post(COMMANDS_API + "/" + command1Id + "/applications")
            .contentType(MediaType.APPLICATION_JSON).content(this.objectMapper.writeValueAsBytes(appIds)))
            .andExpect(MockMvcResultMatchers.status().isNoContent());
    this.mvc.perform(MockMvcRequestBuilders.post(COMMANDS_API + "/" + command3Id + "/applications")
            .contentType(MediaType.APPLICATION_JSON).content(this.objectMapper.writeValueAsBytes(appIds)))
            .andExpect(MockMvcResultMatchers.status().isNoContent());

    final String applicationCommandsAPI = APPLICATIONS_API + "/{id}/commands";

    Arrays.asList(
            this.objectMapper.readValue(this.mvc.perform(MockMvcRequestBuilders.get(applicationCommandsAPI, ID))
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
                    .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(2))).andReturn()
                    .getResponse().getContentAsByteArray(), Command[].class))
            .forEach(command -> {
                if (!command.getId().orElseThrow(IllegalArgumentException::new).equals(command1Id)
                        && !command.getId().orElseThrow(IllegalArgumentException::new).equals(command3Id)) {
                    Assert.fail();
                }
            });

    // Filter by status
    final RestDocumentationResultHandler getResultHandler = MockMvcRestDocumentation.document(
            "{class-name}/{method-name}/{step}/", Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
            Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.ID_PATH_PARAM, // Path parameters
            RequestDocumentation.requestParameters(RequestDocumentation.parameterWithName("status")
                    .description("The status of commands to search for")
                    .attributes(Attributes.key(Snippets.CONSTRAINTS).value(CommandStatus.values())).optional()), // Query Parameters
            Snippets.HAL_CONTENT_TYPE_HEADER, // Response Headers
            PayloadDocumentation.responseFields(PayloadDocumentation.fieldWithPath("[]")
                    .description("The list of commands found").attributes(Snippets.EMPTY_CONSTRAINTS)));
    this.mvc.perform(RestDocumentationRequestBuilders.get(applicationCommandsAPI, ID).param("status",
            CommandStatus.ACTIVE.toString(), CommandStatus.INACTIVE.toString()))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(1)))
            .andExpect(MockMvcResultMatchers.jsonPath("$[0].id", Matchers.is(command1Id)))
            .andDo(getResultHandler);
}

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

/**
 * Make sure that we can remove a command from a cluster.
 *
 * @throws Exception on configuration error
 *//*from   www.jav a2  s.  c  om*/
@Test
public void canRemoveCommandFromACluster() throws Exception {
    this.createConfigResource(new Cluster.Builder(NAME, USER, VERSION, ClusterStatus.UP).withId(ID).build(),
            null);
    final String clusterCommandsAPI = CLUSTERS_API + "/{id}/commands";

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

    this.mvc.perform(MockMvcRequestBuilders.post(clusterCommandsAPI, ID).contentType(MediaType.APPLICATION_JSON)
            .content(this.objectMapper
                    .writeValueAsBytes(Lists.newArrayList(commandId1, commandId2, commandId3))))
            .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("commandId")
                    .description("The id of the command to remove")) // Path parameters
    );

    this.mvc.perform(
            RestDocumentationRequestBuilders.delete(clusterCommandsAPI + "/{commandId}", ID, commandId2))
            .andExpect(MockMvcResultMatchers.status().isNoContent()).andDo(deleteResultHandler);

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

    // Check reverse side of relationship
    this.mvc.perform(MockMvcRequestBuilders.get(COMMANDS_API + "/{id}/clusters", commandId1))
            .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(COMMANDS_API + "/{id}/clusters", commandId2))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
            .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.empty()));

    this.mvc.perform(MockMvcRequestBuilders.get(COMMANDS_API + "/{id}/clusters", commandId3))
            .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)));
}