Example usage for org.springframework.restdocs.constraints Constraint Constraint

List of usage examples for org.springframework.restdocs.constraints Constraint Constraint

Introduction

In this page you can find the example usage for org.springframework.restdocs.constraints Constraint Constraint.

Prototype

public Constraint(String name, Map<String, Object> configuration) 

Source Link

Document

Creates a new Constraint with the given name and configuration .

Usage

From source file:capital.scalable.restdocs.constraints.HumanReadableConstraintResolver.java

@Override
public List<Constraint> resolveForProperty(String property, Class<?> clazz) {
    List<Constraint> result = new ArrayList<>();
    for (Constraint constraint : delegate.resolveForProperty(property, clazz)) {
        result.add(new Constraint(constraint.getName(), extendConfiguration(constraint.getConfiguration())));
    }//  w  ww. jav  a2s. c  om
    return result;
}

From source file:capital.scalable.restdocs.constraints.MethodParameterValidatorConstraintResolver.java

private Constraint createConstraint(Annotation annot, Class<? extends Annotation> type) {
    Map<String, Object> configuration = new HashMap();
    for (Method method : type.getDeclaredMethods()) {
        String methodName = method.getName();
        Object value = getAnnotValue(annot, method);
        configuration.put(methodName, value);
    }// w ww  . j  av a 2  s  . c  om
    return new Constraint(type.getName(), configuration);
}

From source file:capital.scalable.restdocs.constraints.HumanReadableConstraintResolverTest.java

@Test
public void testHumanReadable() {
    // setup//from   w  w w. j ava 2 s  .co m
    Map<String, Object> configuration = new HashedMap();
    configuration.put("primitive", 1);
    configuration.put("wrapper", 1);
    configuration.put("object", new CustomObj("Peter"));
    configuration.put("array", new Object[] { "value1", "value2" });
    configuration.put("class", CustomConstraint.class);
    configuration.put("groups", new Class<?>[] { Update.class });
    configuration.put("payload", new Class<?>[0]);
    Constraint constraint = new Constraint("Custom", configuration);

    when(delegate.resolveForProperty("prop", this.getClass())).thenReturn(singletonList(constraint));
    when(delegate.resolveForParameter(any(MethodParameter.class))).thenReturn(singletonList(constraint));

    List<Constraint> constraints = resolver.resolveForProperty("prop", this.getClass());
    assertConstraints(constraints);

    constraints = resolver.resolveForParameter(Mockito.mock(MethodParameter.class));
    assertConstraints(constraints);
}

From source file:capital.scalable.restdocs.constraints.ConstraintAndGroupDescriptionResolverTest.java

@Test
public void noGroupDescriptionIsResolved() {
    // given/*w  w  w  . ja v a 2  s .  co  m*/
    Map<String, Object> configuration = new HashMap<>();
    configuration.put(GROUPS, new Class<?>[] {});
    Constraint constraint = new Constraint("Constraint", configuration);
    when(delegate.resolveDescription(eq(constraint))).thenReturn("Must be it");
    // when
    String description = resolver.resolveDescription(constraint);
    // then
    assertThat(description, is("Must be it"));
}

From source file:capital.scalable.restdocs.constraints.HumanReadableConstraintResolver.java

@Override
public List<Constraint> resolveForParameter(MethodParameter param) {
    List<Constraint> result = new ArrayList<>();
    for (Constraint constraint : delegate.resolveForParameter(param)) {
        result.add(new Constraint(constraint.getName(), extendConfiguration(constraint.getConfiguration())));
    }/*from w w  w.  jav  a2 s  .c  o m*/
    return result;
}

From source file:capital.scalable.restdocs.constraints.ConstraintAndGroupDescriptionResolverTest.java

@Test
public void noDescriptionIsNotResolved() {
    // given//  w  ww .  j  av a2 s . c o m
    Map<String, Object> configuration = new HashMap<>();
    configuration.put(GROUPS, new Class<?>[] {});
    Constraint constraint = new Constraint("Constraint", configuration);
    when(delegate.resolveDescription(eq(constraint))).thenThrow(MissingResourceException.class);
    // when
    String description = resolver.resolveDescription(constraint);
    // then
    assertThat(description, is("Constraint"));
}

From source file:capital.scalable.restdocs.constraints.ConstraintAndGroupDescriptionResolver.java

@Override
public String resolveGroupDescription(Class group, String constraintDescription) {
    // Pretending that the group class is a constraint to use the same logic for getting
    // a description.
    Constraint groupConstraint = new Constraint(group.getCanonicalName(),
            singletonMap(VALUE, (Object) constraintDescription));
    String result = resolvePlainDescription(groupConstraint);
    return isBlank(result) ? fallbackGroupDescription(group, constraintDescription) : result;
}

From source file:capital.scalable.restdocs.constraints.ConstraintAndGroupDescriptionResolverTest.java

@Test
public void noDescriptionWithGroupsIsResolved() {
    // given/*  w w w .  j ava 2s.c om*/
    Map<String, Object> configuration = new HashMap<>();
    configuration.put(GROUPS, new Class<?>[] { Update.class });
    Constraint constraint = new Constraint("Constraint", configuration);
    when(delegate.resolveDescription(eq(constraint))).thenThrow(MissingResourceException.class);
    // when
    String description = resolver.resolveDescription(constraint);
    // then
    assertThat(description, is("Constraint (groups: [Update])"));
}

From source file:capital.scalable.restdocs.constraints.ConstraintAndGroupDescriptionResolverTest.java

@Test
public void singleGroupDescriptionIsResolved() {
    // given// w  w  w.j av a2s .c o  m
    Map<String, Object> configuration = new HashMap<>();
    configuration.put(GROUPS, new Class<?>[] { Update.class });
    Constraint constraint = new Constraint("Constraint", configuration);
    when(delegate.resolveDescription(eq(constraint))).thenReturn("Must be it");
    when(delegate.resolveDescription(not(eq(constraint)))).thenReturn("Must be it (update)");
    // when
    String description = resolver.resolveDescription(constraint);
    // then
    assertThat(description, is("Must be it (update)"));
}

From source file:capital.scalable.restdocs.constraints.ConstraintReaderImpl.java

private List<String> getEnumConstraintMessage(Class<?> javaBaseClass, String javaFieldName) {
    // could be getter actually
    Field field = findField(javaBaseClass, javaFieldName);
    if (field == null) {
        return emptyList();
    }//  w ww .java2 s.c om

    Class<?> rawClass = field.getType();
    if (!rawClass.isEnum()) {
        return emptyList();
    }

    Class<Enum> enumClass = (Class<Enum>) rawClass;

    String value = arrayToString(enumClass.getEnumConstants());
    String enumName = enumClass.getCanonicalName();
    String message = constraintDescriptionResolver
            .resolveDescription(new Constraint(enumName, singletonMap(VALUE, (Object) value)));

    // fallback
    if (isBlank(message) || message.equals(enumName)) {
        message = "Must be one of " + value;
    }
    return singletonList(message);
}