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.CommandRestControllerIntegrationTests.java

/**
 * Make sure that we can remove an application from a command.
 *
 * @throws Exception on configuration error
 *///  ww  w  . j a va2  s  .  co 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)));
}

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

/**
 * Make sure can get all the clusters which use a given command.
 *
 * @throws Exception on configuration error
 *///w ww .j a va 2 s.c om
@Test
public void canGetClustersForCommand() throws Exception {
    this.createConfigResource(
            new Command.Builder(NAME, USER, VERSION, CommandStatus.ACTIVE, EXECUTABLE, CHECK_DELAY).withId(ID)
                    .build(),
            null);
    final String placeholder = UUID.randomUUID().toString();
    final String cluster1Id = UUID.randomUUID().toString();
    final String cluster2Id = UUID.randomUUID().toString();
    final String cluster3Id = UUID.randomUUID().toString();
    this.createConfigResource(new Cluster.Builder(placeholder, placeholder, placeholder, ClusterStatus.UP)
            .withId(cluster1Id).build(), null);
    this.createConfigResource(
            new Cluster.Builder(placeholder, placeholder, placeholder, ClusterStatus.OUT_OF_SERVICE)
                    .withId(cluster2Id).build(),
            null);
    this.createConfigResource(
            new Cluster.Builder(placeholder, placeholder, placeholder, ClusterStatus.TERMINATED)
                    .withId(cluster3Id).build(),
            null);

    final List<String> commandIds = Lists.newArrayList(ID);
    this.mvc.perform(MockMvcRequestBuilders.post(CLUSTERS_API + "/" + cluster1Id + "/commands")
            .contentType(MediaType.APPLICATION_JSON).content(this.objectMapper.writeValueAsBytes(commandIds)))
            .andExpect(MockMvcResultMatchers.status().isNoContent());
    this.mvc.perform(MockMvcRequestBuilders.post(CLUSTERS_API + "/" + cluster3Id + "/commands")
            .contentType(MediaType.APPLICATION_JSON).content(this.objectMapper.writeValueAsBytes(commandIds)))
            .andExpect(MockMvcResultMatchers.status().isNoContent());

    Arrays.stream(
            this.objectMapper
                    .readValue(
                            this.mvc.perform(MockMvcRequestBuilders.get(COMMANDS_API + "/" + ID + "/clusters"))
                                    .andExpect(MockMvcResultMatchers.status().isOk())
                                    .andExpect(MockMvcResultMatchers.content()
                                            .contentTypeCompatibleWith(MediaTypes.HAL_JSON))
                                    .andExpect(MockMvcResultMatchers.jsonPath("$", Matchers.hasSize(2)))
                                    .andReturn().getResponse().getContentAsByteArray(),
                            ClusterResource[].class))
            .map(ClusterResource::getContent).forEach(cluster -> {
                final String id = cluster.getId().orElseThrow(IllegalArgumentException::new);
                if (!id.equals(cluster1Id) && !id.equals(cluster3Id)) {
                    Assert.fail();
                }
            });

    // Test 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 clusters 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 clusters found").attributes(Snippets.EMPTY_CONSTRAINTS)));
    this.mvc.perform(RestDocumentationRequestBuilders.get(COMMANDS_API + "/{id}/clusters", ID).param("status",
            ClusterStatus.UP.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(cluster1Id)))
            .andDo(getResultHandler);
}