ConstraintAndGroupDescriptionResolver implements
        ConstraintDescriptionResolver, GroupDescriptionResolver { @Override public String resolveDescription(Constraint constraint) { String constraintDescription = trimToEmpty(resolvePlainDescription(constraint)); if (isBlank(constraintDescription)) { constraintDescription = constraint.getName(); } List<Class<?>> groups = getGroups(constraint); if (groups.isEmpty()) { return constraintDescription; } StringBuilder result = new StringBuilder(); for (Class group : groups) { result.append(", "); result.append(trimToEmpty(resolveGroupDescription(group, constraintDescription))); } result.replace(0, 2, ""); return result.toString(); } ConstraintAndGroupDescriptionResolver(ConstraintDescriptionResolver delegate, SnippetTranslationResolver translationResolver); @Override String resolveDescription(Constraint constraint); @Override List<Class<?>> getGroups(Constraint constraint); @Override String resolveGroupDescription(Class group, String constraintDescription);  }
@Test public void noGroupDescriptionIsResolved() { 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"); String description = resolver.resolveDescription(constraint); assertThat(description).isEqualTo("Must be it"); }
@Test public void noDescriptionIsNotResolved() { Map<String, Object> configuration = new HashMap<>(); configuration.put(GROUPS, new Class<?>[]{}); Constraint constraint = new Constraint("Constraint", configuration); when(delegate.resolveDescription(eq(constraint))).thenReturn(""); String description = resolver.resolveDescription(constraint); assertThat(description).isEqualTo("Constraint"); }
@Test public void noDescriptionWithGroupsIsResolved() { 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(""); String description = resolver.resolveDescription(constraint); assertThat(description).isEqualTo("Constraint (groups: [Update])"); }
@Test public void singleGroupDescriptionIsResolved() { 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)"); String description = resolver.resolveDescription(constraint); assertThat(description).isEqualTo("Must be it (update)"); }
@Test public void multipleGroupDescriptionsAreResolved() { Map<String, Object> configuration = new HashMap<>(); configuration.put(GROUPS, new Class<?>[]{Update.class, Create.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 (create)", "Must be it (update)"); String description = resolver.resolveDescription(constraint); assertThat(description).isEqualTo("Must be it (create), Must be it (update)"); }
@Test public void groupDescriptionIsNotResolved() { 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(""); String description = resolver.resolveDescription(constraint); assertThat(description).isEqualTo("Must be it (groups: [Update])"); }
@Test public void strangeGroupConfigurationIsResolved() { Map<String, Object> configuration = new HashMap<>(); configuration.put(GROUPS, "no group"); Constraint constraint = new Constraint("Constraint", configuration); when(delegate.resolveDescription(eq(constraint))).thenReturn("Must be it"); when(delegate.resolveDescription(not(eq(constraint)))).thenReturn("here"); String description = resolver.resolveDescription(constraint); assertThat(description).isEqualTo("Must be it"); }
@Test public void noConstraintDescriptionIsResolved() { 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(""); String description = resolver.resolveDescription(constraint); assertThat(description).isEqualTo("Constraint (groups: [Update])"); }