Example usage for org.springframework.restdocs.restassured3 RestAssuredRestDocumentation document

List of usage examples for org.springframework.restdocs.restassured3 RestAssuredRestDocumentation document

Introduction

In this page you can find the example usage for org.springframework.restdocs.restassured3 RestAssuredRestDocumentation document.

Prototype

public static RestDocumentationFilter document(String identifier,
        OperationRequestPreprocessor requestPreprocessor, OperationResponsePreprocessor responsePreprocessor,
        Snippet... snippets) 

Source Link

Document

Documents the API call with the given identifier using the given snippets in addition to any default snippets.

Usage

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

private String submitJob(final int documentationId, final JobRequest jobRequest,
        @Nullable final List<MockMultipartFile> attachments) throws Exception {
    if (attachments != null) {
        final RestDocumentationFilter createResultFilter = RestAssuredRestDocumentation.document(
                "{class-name}/" + documentationId + "/submitJobWithAttachments/",
                HeaderDocumentation.requestHeaders(HeaderDocumentation.headerWithName(HttpHeaders.CONTENT_TYPE)
                        .description(MediaType.MULTIPART_FORM_DATA_VALUE)), // Request headers
                RequestDocumentation.requestParts(
                        RequestDocumentation.partWithName("request").description(
                                "The job request JSON. Content type must be application/json for part"),
                        RequestDocumentation.partWithName("attachment").description(
                                "An attachment file. There can be multiple. Type should be octet-stream")), // Request parts
                Snippets.LOCATION_HEADER // Response Headers
        );//from  w  w w  . j  av  a  2 s .c  o m

        final RequestSpecification jobRequestSpecification = RestAssured.given(this.getRequestSpecification())
                .filter(createResultFilter).contentType(MediaType.MULTIPART_FORM_DATA_VALUE)
                .multiPart("request", GenieObjectMapper.getMapper().writeValueAsString(jobRequest),
                        MediaType.APPLICATION_JSON_VALUE);

        for (final MockMultipartFile attachment : attachments) {
            jobRequestSpecification.multiPart("attachment", attachment.getOriginalFilename(),
                    attachment.getBytes(), MediaType.APPLICATION_OCTET_STREAM_VALUE);
        }

        return this.getIdFromLocation(jobRequestSpecification.when().port(this.port).post(JOBS_API).then()
                .statusCode(Matchers.is(HttpStatus.ACCEPTED.value()))
                .header(HttpHeaders.LOCATION, Matchers.notNullValue()).extract().header(HttpHeaders.LOCATION));
    } else {
        // Use regular POST
        final RestDocumentationFilter createResultFilter = RestAssuredRestDocumentation.document(
                "{class-name}/" + documentationId + "/submitJobWithoutAttachments/",
                Snippets.CONTENT_TYPE_HEADER, // Request headers
                Snippets.getJobRequestRequestPayload(), // Request Fields
                Snippets.LOCATION_HEADER // Response Headers
        );

        return this.getIdFromLocation(RestAssured.given(this.getRequestSpecification())
                .filter(createResultFilter).contentType(MediaType.APPLICATION_JSON_VALUE)
                .body(GenieObjectMapper.getMapper().writeValueAsBytes(jobRequest)).when().port(this.port)
                .post(JOBS_API).then().statusCode(Matchers.is(HttpStatus.ACCEPTED.value()))
                .header(HttpHeaders.LOCATION, Matchers.notNullValue()).extract().header(HttpHeaders.LOCATION));
    }
}

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

private void checkJobStatus(final int documentationId, final String id) {
    final RestDocumentationFilter getResultFilter = RestAssuredRestDocumentation.document(
            "{class-name}/" + documentationId + "/getJobStatus/", Snippets.ID_PATH_PARAM, // Path parameters
            Snippets.JSON_CONTENT_TYPE_HEADER, // Response Headers
            PayloadDocumentation.responseFields(PayloadDocumentation.fieldWithPath("status")
                    .description("The job status. One of: " + Arrays.toString(JobStatus.values()))
                    .attributes(Snippets.EMPTY_CONSTRAINTS)) // Response fields
    );/*from  w  w  w.ja  v  a2  s . co m*/

    RestAssured.given(this.getRequestSpecification()).filter(getResultFilter).when().port(this.port)
            .get(JOBS_API + "/{id}/status", id).then()
            .contentType(Matchers.containsString(MediaType.APPLICATION_JSON_VALUE))
            .body(STATUS_PATH, Matchers.is(JobStatus.SUCCEEDED.toString()));
}

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

private void checkJobApplications(final int documentationId, final String id) {
    final RestDocumentationFilter getResultFilter = RestAssuredRestDocumentation.document(
            "{class-name}/" + documentationId + "/getJobApplications/", Snippets.ID_PATH_PARAM, // Path parameters
            Snippets.HAL_CONTENT_TYPE_HEADER, // Response Headers
            PayloadDocumentation.responseFields(PayloadDocumentation.subsectionWithPath("[]")
                    .description("The applications for the job").attributes(Snippets.EMPTY_CONSTRAINTS)) // Response fields
    );/*from w  w  w  . j  av a 2 s.c  o m*/

    RestAssured.given(this.getRequestSpecification()).filter(getResultFilter).when().port(this.port)
            .get(JOBS_API + "/{id}/applications", id).then().statusCode(Matchers.is(HttpStatus.OK.value()))
            .contentType(Matchers.is(MediaTypes.HAL_JSON_UTF8_VALUE)).body("$", Matchers.hasSize(2))
            .body("[0].id", Matchers.is(APP1_ID)).body("[1].id", Matchers.is(APP2_ID));
}