org.springframework.restdocs.payload.ResponseFieldsSnippet Java Examples

The following examples show how to use org.springframework.restdocs.payload.ResponseFieldsSnippet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DescriptorExtractorTest.java    From restdocs-raml with MIT License 6 votes vote down vote up
@Test
public void should_extract_response_field_descriptors() {
    // given
    ResponseFieldsSnippet snippet = responseFields(
        fieldWithPath("object.field").description("Is documented!"),
        fieldWithPath("object.anotherField").description("Is documented, too!")
    );

    // when
    List<FieldDescriptor> descriptors = extract(snippet);

    then(descriptors).hasSize(2);
    then(descriptors.stream().map(FieldDescriptor::getPath).collect(toList()))
            .containsExactly("object.field", "object.anotherField");
    then(descriptors.stream().map(AbstractDescriptor::getDescription).collect(toList()))
            .containsExactly("Is documented!", "Is documented, too!");
}
 
Example #2
Source File: Snippets.java    From genie with Apache License 2.0 6 votes vote down vote up
static ResponseFieldsSnippet resolveClustersForCommandClusterCriteriaResponsePayload() {
    return PayloadDocumentation
        .responseFields(
            PayloadDocumentation
                .fieldWithPath("[]")
                .attributes(EMPTY_CONSTRAINTS)
                .description("The list of criterion and associated resolved clusters")
                .type(JsonFieldType.ARRAY)
                .optional()
        )
        .andWithPrefix(
            "[].",
            PayloadDocumentation
                .fieldWithPath("criterion")
                .attributes(EMPTY_CONSTRAINTS)
                .description("The criterion that was evaluated to yield the resources")
                .type(JsonFieldType.OBJECT),
            PayloadDocumentation
                .fieldWithPath("resources")
                .attributes(EMPTY_CONSTRAINTS)
                .description("The resources that were resolved by evaluating the criterion")
                .type(JsonFieldType.ARRAY)
        )
        .andWithPrefix("[].criterion.", getCriterionFieldDescriptors())
        .andWithPrefix("[].resources[].", getClusterFieldDescriptors());
}
 
Example #3
Source File: Snippets.java    From genie with Apache License 2.0 5 votes vote down vote up
static ResponseFieldsSnippet getApplicationResponsePayload() {
    return PayloadDocumentation.responseFields(getApplicationFieldDescriptors())
        .and(
            PayloadDocumentation
                .subsectionWithPath("_links")
                .attributes(
                    Attributes
                        .key(CONSTRAINTS)
                        .value("")
                )
                .description("<<_hateoas,Links>> to other resources.")
                .ignored()
        );
}
 
Example #4
Source File: Snippets.java    From genie with Apache License 2.0 5 votes vote down vote up
static ResponseFieldsSnippet getClusterResponsePayload() {
    return PayloadDocumentation.responseFields(getClusterFieldDescriptors())
        .and(
            PayloadDocumentation
                .subsectionWithPath("_links")
                .attributes(
                    Attributes
                        .key(CONSTRAINTS)
                        .value("")
                )
                .description("<<_hateoas,Links>> to other resources.")
                .ignored()
        );
}
 
Example #5
Source File: Snippets.java    From genie with Apache License 2.0 5 votes vote down vote up
static ResponseFieldsSnippet getCommandResponsePayload() {
    return PayloadDocumentation.responseFields(getCommandFieldDescriptors())
        .and(
            PayloadDocumentation
                .subsectionWithPath("_links")
                .attributes(
                    Attributes
                        .key(CONSTRAINTS)
                        .value("")
                )
                .description("<<_hateoas,Links>> to other resources.")
                .ignored()
        );
}
 
Example #6
Source File: Snippets.java    From genie with Apache License 2.0 5 votes vote down vote up
static ResponseFieldsSnippet getJobRequestResponsePayload() {
    return PayloadDocumentation.responseFields(getJobRequestFieldDescriptors())
        .and(
            PayloadDocumentation
                .subsectionWithPath("_links")
                .attributes(
                    Attributes
                        .key(CONSTRAINTS)
                        .value("")
                )
                .description("<<_hateoas,Links>> to other resources.")
                .ignored()
        );
}
 
Example #7
Source File: Snippets.java    From genie with Apache License 2.0 5 votes vote down vote up
static ResponseFieldsSnippet getJobResponsePayload() {
    return PayloadDocumentation.responseFields(getJobFieldDescriptors())
        .and(
            PayloadDocumentation
                .subsectionWithPath("_links")
                .attributes(
                    Attributes
                        .key(CONSTRAINTS)
                        .value("")
                )
                .description("<<_hateoas,Links>> to other resources.")
                .ignored()
        );
}
 
Example #8
Source File: Snippets.java    From genie with Apache License 2.0 5 votes vote down vote up
static ResponseFieldsSnippet getJobExecutionResponsePayload() {
    return PayloadDocumentation.responseFields(getJobExecutionFieldDescriptors())
        .and(
            PayloadDocumentation
                .subsectionWithPath("_links")
                .attributes(
                    Attributes
                        .key(CONSTRAINTS)
                        .value("")
                )
                .description("<<_hateoas,Links>> to other resources.")
                .ignored()
        );
}
 
Example #9
Source File: Snippets.java    From genie with Apache License 2.0 5 votes vote down vote up
static ResponseFieldsSnippet getJobMetadataResponsePayload() {
    return PayloadDocumentation.responseFields(getJobMetadataFieldDescriptors())
        .and(
            PayloadDocumentation
                .subsectionWithPath("_links")
                .attributes(
                    Attributes
                        .key(CONSTRAINTS)
                        .value("")
                )
                .description("<<_hateoas,Links>> to other resources.")
                .ignored()
        );
}
 
Example #10
Source File: Snippets.java    From genie with Apache License 2.0 5 votes vote down vote up
static ResponseFieldsSnippet getClusterCriteriaForCommandResponsePayload() {
    return PayloadDocumentation
        .responseFields(
            PayloadDocumentation
                .fieldWithPath("[]")
                .attributes(EMPTY_CONSTRAINTS)
                .description("A priority ordered list of criteria")
                .type(JsonFieldType.ARRAY)
                .optional()
        )
        .andWithPrefix("[].", getCriterionFieldDescriptors());
}
 
Example #11
Source File: RamlDocumentation.java    From restdocs-raml with MIT License 4 votes vote down vote up
protected static Snippet[] enhanceSnippetsWithRaml(String description,
                                                   boolean privateResource,
                                                   Function<List<Snippet>, List<Snippet>> snippetFilter,
                                                   Snippet... snippets) {

    List<RequestFieldsSnippet> requestFieldsSnippets = new ArrayList<>();
    List<ResponseFieldsSnippet> responseFieldsSnippets = new ArrayList<>();
    List<LinksSnippet> linkSnippets = new ArrayList<>();
    List<RequestParametersSnippet> requestParametersSnippets = new ArrayList<>();
    List<PathParametersSnippet> pathParametersSnippets = new ArrayList<>();
    List<RequestHeadersSnippet> requestHeadersSnippets = new ArrayList<>();
    List<ResponseHeadersSnippet> responseHeadersSnippets = new ArrayList<>();

    List<Snippet> ramlSnippets = new ArrayList<>();

    List<Snippet> otherSnippets = new ArrayList<>();

    for (Snippet snippet : snippets) {
        if (snippet instanceof RequestFieldsSnippet) {
            requestFieldsSnippets.add((RequestFieldsSnippet) snippet);
        } else if (snippet instanceof ResponseFieldsSnippet) {
            responseFieldsSnippets.add((ResponseFieldsSnippet) snippet);
        } else if (snippet instanceof LinksSnippet) {
            linkSnippets.add((LinksSnippet) snippet);
        } else if (snippet instanceof RequestParametersSnippet) {
            requestParametersSnippets.add((RequestParametersSnippet) snippet);
        } else if (snippet instanceof PathParametersSnippet) {
            pathParametersSnippets.add((PathParametersSnippet) snippet);
        } else if (snippet instanceof RequestHeadersSnippet) {
            requestHeadersSnippets.add((RequestHeadersSnippet) snippet);
        } else if (snippet instanceof ResponseHeadersSnippet) {
            responseHeadersSnippets.add((ResponseHeadersSnippet) snippet);
        } else if (snippet instanceof RamlResourceSnippet) {
            ramlSnippets.add(snippet);
        } else {
            otherSnippets.add(snippet);
        }
    }

    List<Snippet> enhancedSnippets = new ArrayList<>();
    enhancedSnippets.addAll(requestFieldsSnippets);
    enhancedSnippets.addAll(responseFieldsSnippets);
    enhancedSnippets.addAll(linkSnippets);
    enhancedSnippets.addAll(requestParametersSnippets);
    enhancedSnippets.addAll(pathParametersSnippets);
    enhancedSnippets.addAll(requestHeadersSnippets);
    enhancedSnippets.addAll(responseHeadersSnippets);
    enhancedSnippets.addAll(otherSnippets);

    if (ramlSnippets.isEmpty()) { // No RamlResourceSnippet, so we configure our own based on the info of the other snippets
        RamlResourceSnippetParameters ramlParameters = RamlResourceSnippetParameters.builder()
                .description(description)
                .privateResource(privateResource)
                .requestFields(requestFieldsSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(FieldDescriptor[]::new))
                .responseFields(responseFieldsSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(FieldDescriptor[]::new))
                .links(linkSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(LinkDescriptor[]::new))
                .requestParameters(requestParametersSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(ParameterDescriptor[]::new))
                .pathParameters(pathParametersSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(ParameterDescriptor[]::new))
                .requestHeaders(requestHeadersSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(HeaderDescriptor[]::new))
                .responseHeaders(responseHeadersSnippets.stream().map(DescriptorExtractor::extract).flatMap(List::stream).toArray(HeaderDescriptor[]::new))
                .build();
        enhancedSnippets.add(ramlResource(ramlParameters));
    } else {
        enhancedSnippets.addAll(ramlSnippets);
    }

    enhancedSnippets = snippetFilter.apply(enhancedSnippets);

    return enhancedSnippets.toArray(new Snippet[0]);
}