Example usage for org.springframework.restdocs.operation.preprocess Preprocessors prettyPrint

List of usage examples for org.springframework.restdocs.operation.preprocess Preprocessors prettyPrint

Introduction

In this page you can find the example usage for org.springframework.restdocs.operation.preprocess Preprocessors prettyPrint.

Prototype

public static OperationPreprocessor prettyPrint() 

Source Link

Document

Returns an OperationPreprocessor that will pretty print the content of the request or response.

Usage

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
 *///from  ww  w .  jav  a  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);
}