Example usage for org.springframework.restdocs.headers HeaderDocumentation headerWithName

List of usage examples for org.springframework.restdocs.headers HeaderDocumentation headerWithName

Introduction

In this page you can find the example usage for org.springframework.restdocs.headers HeaderDocumentation headerWithName.

Prototype

public static HeaderDescriptor headerWithName(String name) 

Source Link

Document

Creates a HeaderDescriptor that describes a header with the given name .

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   ww  w  . j a  va 2 s. com

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

private String submitJob(final int documentationId, final JobRequest jobRequest,
        final List<MockMultipartFile> attachments) throws Exception {
    final MvcResult result;

    if (attachments != null) {
        final RestDocumentationResultHandler createResultHandler = MockMvcRestDocumentation.document(
                "{class-name}/" + documentationId + "/submitJobWithAttachments/",
                Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
                Preprocessors.preprocessResponse(Preprocessors.prettyPrint()),
                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 ww  . j  a  va  2s .c  o m*/

        final MockMultipartFile json = new MockMultipartFile("request", "", MediaType.APPLICATION_JSON_VALUE,
                this.objectMapper.writeValueAsBytes(jobRequest));

        final MockMultipartHttpServletRequestBuilder builder = RestDocumentationRequestBuilders
                .fileUpload(JOBS_API).file(json);

        for (final MockMultipartFile attachment : attachments) {
            builder.file(attachment);
        }

        builder.contentType(MediaType.MULTIPART_FORM_DATA);
        result = this.mvc.perform(builder).andExpect(MockMvcResultMatchers.status().isAccepted())
                .andExpect(MockMvcResultMatchers.header().string(HttpHeaders.LOCATION, Matchers.notNullValue()))
                .andDo(createResultHandler).andReturn();
    } else {
        // Use regular POST
        final RestDocumentationResultHandler createResultHandler = MockMvcRestDocumentation.document(
                "{class-name}/" + documentationId + "/submitJobWithoutAttachments/",
                Preprocessors.preprocessRequest(Preprocessors.prettyPrint()),
                Preprocessors.preprocessResponse(Preprocessors.prettyPrint()), Snippets.CONTENT_TYPE_HEADER, // Request headers
                Snippets.getJobRequestRequestPayload(), // Request Fields
                Snippets.LOCATION_HEADER // Response Headers
        );

        result = this.mvc
                .perform(MockMvcRequestBuilders.post(JOBS_API).contentType(MediaType.APPLICATION_JSON)
                        .content(this.objectMapper.writeValueAsBytes(jobRequest)))
                .andExpect(MockMvcResultMatchers.status().isAccepted())
                .andExpect(MockMvcResultMatchers.header().string(HttpHeaders.LOCATION, Matchers.notNullValue()))
                .andDo(createResultHandler).andReturn();
    }

    return this.getIdFromLocation(result.getResponse().getHeader(HttpHeaders.LOCATION));
}

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
    );//  w  w  w . j a va 2 s. c  o  m

    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);/* w ww  . j a  v  a  2 s . c  om*/

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