org.springframework.restdocs.snippet.Attributes Java Examples

The following examples show how to use org.springframework.restdocs.snippet.Attributes. 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: JsonSchemaFromFieldDescriptorsGeneratorTest.java    From restdocs-raml with MIT License 6 votes vote down vote up
private void givenFieldDescriptorsWithConstraints() {
    Attribute constraintAttributeWithNotNull = Attributes.key("notImportant").value(singletonList(new Constraint(NotNull.class.getName(), emptyMap())));

    HashMap<String, Object> lengthAttributes = new HashMap<>();
    lengthAttributes.put("min", 2);
    lengthAttributes.put("max", 255);
    Attribute constraintAttributeWithLength = Attributes.key("notImportant").value(singletonList(new Constraint(Length.class.getName(), lengthAttributes)));

    fieldDescriptors =  Arrays.asList(
            fieldWithPath("id").description("some").type(STRING).attributes(constraintAttributeWithNotNull),
            fieldWithPath("lineItems[*].name").description("some").type(STRING).type(STRING).attributes(constraintAttributeWithLength),
            fieldWithPath("lineItems[*]._id").description("some").type(STRING).attributes(constraintAttributeWithNotNull),
            fieldWithPath("lineItems[*].quantity.value").description("some").type(NUMBER).attributes(constraintAttributeWithNotNull),
            fieldWithPath("lineItems[*].quantity.unit").description("some").type(STRING),
            fieldWithPath("shippingAddress").description("some").type(OBJECT),
            fieldWithPath("billingAddress").description("some").type(OBJECT).attributes(constraintAttributeWithNotNull),
            fieldWithPath("billingAddress.firstName").description("some").type(STRING).attributes(Attributes
                    .key("notImportant")
                    .value(singletonList(new Constraint(NotEmpty.class.getName(), emptyMap())))),
            fieldWithPath("billingAddress.valid").description("some").type(BOOLEAN),
            fieldWithPath("paymentLineItem.lineItemTaxes").description("some").type(ARRAY)
    );
}
 
Example #2
Source File: ControllerTestTemplate.java    From coderadar with MIT License 5 votes vote down vote up
public FieldDescriptor withPath(String path) {
  return PayloadDocumentation.fieldWithPath(path)
      .attributes(
          Attributes.key("constraints")
              .value(
                  StringUtils.collectionToDelimitedString(
                      this.constraintDescriptions.descriptionsForProperty(path), ". ")));
}
 
Example #3
Source File: ControllerTestTemplate.java    From coderadar with MIT License 5 votes vote down vote up
/** Returns field descriptor for custom validators. */
public FieldDescriptor withCustomPath(String path) {
  return PayloadDocumentation.fieldWithPath(path)
      .attributes(
          Attributes.key("constraints")
              .value(
                  StringUtils.collectionToDelimitedString(
                      Collections.singletonList(customValidationDescription.getProperty(path)),
                      ". ")));
}
 
Example #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
Source File: Snippets.java    From genie with Apache License 2.0 5 votes vote down vote up
private static Attributes.Attribute getConstraintsForField(
    final ConstraintDescriptions constraints,
    final String fieldName
) {
    return Attributes
        .key(CONSTRAINTS)
        .value(StringUtils.collectionToDelimitedString(constraints.descriptionsForProperty(fieldName), ". "));
}
 
Example #12
Source File: CommandRestControllerIntegrationTest.java    From genie with Apache License 2.0 4 votes vote down vote up
@Test
void canGetClustersForCommand() throws Exception {
    final String placeholder = UUID.randomUUID().toString();
    final String cluster1Id = this.createConfigResource(
        new Cluster.Builder(placeholder, placeholder, placeholder, ClusterStatus.UP).build(),
        null
    );
    final String cluster2Id = this.createConfigResource(
        new Cluster.Builder(placeholder, placeholder, placeholder, ClusterStatus.OUT_OF_SERVICE).build(),
        null
    );
    final String cluster3Id = this.createConfigResource(
        new Cluster
            .Builder(placeholder, placeholder, placeholder, ClusterStatus.TERMINATED)
            .build(),
        null
    );

    final String commandId = this.createConfigResource(
        new Command
            .Builder(NAME, USER, VERSION, CommandStatus.ACTIVE, EXECUTABLE_AND_ARGS, CHECK_DELAY)
            .withClusterCriteria(
                Lists.newArrayList(
                    new Criterion.Builder().withId(cluster1Id).build(),
                    new Criterion.Builder().withId(cluster2Id).build(),
                    new Criterion.Builder().withId(cluster3Id).build()
                )
            )
            .build(),
        null
    );

    Assertions
        .assertThat(
            Arrays.stream(
                GenieObjectMapper.getMapper().readValue(
                    RestAssured
                        .given(this.getRequestSpecification())
                        .when()
                        .port(this.port)
                        .get(COMMANDS_API + "/{id}/clusters", commandId)
                        .then()
                        .statusCode(Matchers.is(HttpStatus.OK.value()))
                        .contentType(Matchers.containsString(MediaTypes.HAL_JSON_VALUE))
                        .extract()
                        .asByteArray(),
                    new TypeReference<EntityModel<Cluster>[]>() {
                    }
                )
            )
                .map(EntityModel::getContent)
                .filter(Objects::nonNull)
                .map(Cluster::getId)
                .filter(Optional::isPresent)
                .map(Optional::get)
                .collect(Collectors.toList())
        )
        .containsExactlyInAnyOrder(cluster1Id, cluster2Id, cluster3Id);

    // Test filtering
    final RestDocumentationFilter getFilter = RestAssuredRestDocumentation.document(
        "{class-name}/{method-name}/{step}/",
        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
                .subsectionWithPath("[]")
                .description("The list of clusters found")
                .attributes(Snippets.EMPTY_CONSTRAINTS)
        )
    );

    RestAssured
        .given(this.getRequestSpecification())
        .filter(getFilter)
        .param("status", ClusterStatus.UP.toString())
        .when()
        .port(this.port)
        .get(COMMANDS_API + "/{id}/clusters", commandId)
        .then()
        .statusCode(Matchers.is(HttpStatus.OK.value()))
        .contentType(Matchers.containsString(MediaTypes.HAL_JSON_VALUE))
        .body("$", Matchers.hasSize(1))
        .body("[0].id", Matchers.is(cluster1Id));
}
 
Example #13
Source File: ClusterRestControllerIntegrationTest.java    From genie with Apache License 2.0 4 votes vote down vote up
@Test
void canAddCommandsForACluster() throws Exception {
    this.createConfigResource(new Cluster.Builder(NAME, USER, VERSION, ClusterStatus.UP).withId(ID).build(), null);
    final String clusterCommandsAPI = CLUSTERS_API + "/{id}/commands";

    final RestDocumentationFilter addFilter = RestAssuredRestDocumentation.document(
        "{class-name}/{method-name}/{step}/",
        Snippets.CONTENT_TYPE_HEADER, // Request Headers
        Snippets.ID_PATH_PARAM, // Path parameters
        PayloadDocumentation.requestFields(
            PayloadDocumentation
                .fieldWithPath("[]")
                .description("Array of command ids (in preferred order) to append to the existing list of commands")
                .attributes(Snippets.EMPTY_CONSTRAINTS)
        ) // Request payload
    );

    RestAssured
        .given(this.getRequestSpecification())
        .filter(addFilter)
        .contentType(MediaType.APPLICATION_JSON_VALUE)
        .body(
            GenieObjectMapper
                .getMapper()
                .writeValueAsBytes(Lists.newArrayList(UUID.randomUUID().toString(), UUID.randomUUID().toString()))
        )
        .when()
        .port(this.port)
        .post(clusterCommandsAPI, ID)
        .then()
        .statusCode(Matchers.is(HttpStatus.NO_CONTENT.value()));

    // Test the filtering
    final RestDocumentationFilter getFilter = RestAssuredRestDocumentation.document(
        "{class-name}/{method-name}/{step}/",
        Snippets.ID_PATH_PARAM, // Path parameters
        RequestDocumentation.requestParameters(
            RequestDocumentation
                .parameterWithName("status")
                .description("The status of commands to search for")
                .attributes(Attributes.key(Snippets.CONSTRAINTS).value(CommandStatus.values()))
                .optional()
        ), // Query Parameters
        Snippets.HAL_CONTENT_TYPE_HEADER, // Response Headers
        PayloadDocumentation.responseFields(
            PayloadDocumentation
                .subsectionWithPath("[]")
                .description("The list of commands found")
                .attributes(Snippets.EMPTY_CONSTRAINTS)
        )
    );

    RestAssured
        .given(this.getRequestSpecification())
        .filter(getFilter)
        .param("status", CommandStatus.ACTIVE.toString())
        .when()
        .port(this.port)
        .get(clusterCommandsAPI, ID)
        .then()
        .statusCode(Matchers.is(HttpStatus.OK.value()))
        .contentType(Matchers.containsString(MediaTypes.HAL_JSON_VALUE))
        .body("$", Matchers.empty());
}