-
Notifications
You must be signed in to change notification settings - Fork 737
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Bean Validation's constraint groups
- Loading branch information
Showing
5 changed files
with
241 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...e/src/main/java/org/springframework/restdocs/constraints/GroupConstraintDescriptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright 2014-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.restdocs.constraints; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* Provides access to descriptions of a class's constraints. | ||
* | ||
* @author Dmytro Nosan | ||
*/ | ||
public class GroupConstraintDescriptions { | ||
|
||
private final Class<?> clazz; | ||
|
||
private final ConstraintResolver constraintResolver; | ||
|
||
private final ConstraintDescriptionResolver descriptionResolver; | ||
|
||
/** | ||
* Create a new {@code GroupConstraintDescriptions} for the given {@code clazz}. | ||
* Constraints will be resolved using a {@link ValidatorConstraintResolver} and | ||
* descriptions will be resolved using a | ||
* {@link ResourceBundleConstraintDescriptionResolver}. | ||
* @param clazz the class | ||
*/ | ||
public GroupConstraintDescriptions(Class<?> clazz) { | ||
this(clazz, new ValidatorConstraintResolver(), new ResourceBundleConstraintDescriptionResolver()); | ||
} | ||
|
||
/** | ||
* Create a new {@code GroupConstraintDescriptions} for the given {@code clazz}. | ||
* Constraints will be resolved using the given {@code constraintResolver} and | ||
* descriptions will be resolved using a | ||
* {@link ResourceBundleConstraintDescriptionResolver}. | ||
* @param clazz the class | ||
* @param constraintResolver the constraint resolver | ||
*/ | ||
public GroupConstraintDescriptions(Class<?> clazz, ConstraintResolver constraintResolver) { | ||
this(clazz, constraintResolver, new ResourceBundleConstraintDescriptionResolver()); | ||
} | ||
|
||
/** | ||
* Create a new {@code GroupConstraintDescriptions} for the given {@code clazz}. | ||
* Constraints will be resolved using a {@link ValidatorConstraintResolver} and | ||
* descriptions will be resolved using the given {@code descriptionResolver}. | ||
* @param clazz the class | ||
* @param descriptionResolver the description resolver | ||
*/ | ||
public GroupConstraintDescriptions(Class<?> clazz, ConstraintDescriptionResolver descriptionResolver) { | ||
this(clazz, new ValidatorConstraintResolver(), descriptionResolver); | ||
} | ||
|
||
/** | ||
* Create a new {@code GroupConstraintDescriptions} for the given {@code clazz}. | ||
* Constraints will be resolved using the given {@code constraintResolver} and | ||
* descriptions will be resolved using the given {@code descriptionResolver}. | ||
* @param clazz the class | ||
* @param constraintResolver the constraint resolver | ||
* @param descriptionResolver the description resolver | ||
*/ | ||
public GroupConstraintDescriptions(Class<?> clazz, ConstraintResolver constraintResolver, | ||
ConstraintDescriptionResolver descriptionResolver) { | ||
this.clazz = clazz; | ||
this.constraintResolver = constraintResolver; | ||
this.descriptionResolver = descriptionResolver; | ||
} | ||
|
||
/** | ||
* Returns a list of the descriptions for the constraints on the given property. | ||
* @param property the property | ||
* @param groups list of groups targeted for constraints | ||
* @return the list of constraint descriptions | ||
*/ | ||
public List<String> descriptionsForProperty(String property, Class<?>... groups) { | ||
List<Constraint> constraints = this.constraintResolver.resolveForProperty(property, this.clazz); | ||
List<String> descriptions = new ArrayList<>(); | ||
for (Constraint constraint : constraints) { | ||
if (includes(constraint, groups)) { | ||
descriptions.add(this.descriptionResolver.resolveDescription(constraint)); | ||
} | ||
} | ||
Collections.sort(descriptions); | ||
return descriptions; | ||
} | ||
|
||
private boolean includes(Constraint constraint, Class<?>[] groups) { | ||
if (groups.length == 0 && constraint.getGroups().isEmpty()) { | ||
return true; | ||
} | ||
return Stream.of(groups).anyMatch((clazz) -> constraint.getGroups().contains(clazz)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
.../test/java/org/springframework/restdocs/constraints/GroupConstraintDescriptionsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2014-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.restdocs.constraints; | ||
|
||
import java.io.Serializable; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.BDDMockito.given; | ||
import static org.mockito.Mockito.mock; | ||
|
||
/** | ||
* Tests for {@link GroupConstraintDescriptions}. | ||
* | ||
* @author Dmytro Nosan | ||
* | ||
*/ | ||
public class GroupConstraintDescriptionsTests { | ||
|
||
private final ConstraintResolver constraintResolver = mock(ConstraintResolver.class); | ||
|
||
private final ConstraintDescriptionResolver constraintDescriptionResolver = mock( | ||
ConstraintDescriptionResolver.class); | ||
|
||
private final GroupConstraintDescriptions constraintDescriptions = new GroupConstraintDescriptions( | ||
Constrained.class, this.constraintResolver, this.constraintDescriptionResolver); | ||
|
||
@Test | ||
public void descriptionsForConstraints() { | ||
Constraint constraint1 = new Constraint("constraint1", Collections.emptyMap(), Set.of(Cloneable.class)); | ||
Constraint constraint2 = new Constraint("constraint2", Collections.emptyMap()); | ||
Constraint constraint3 = new Constraint("constraint3", Collections.emptyMap(), | ||
Set.of(Cloneable.class, Serializable.class)); | ||
|
||
given(this.constraintResolver.resolveForProperty("foo", Constrained.class)) | ||
.willReturn(Arrays.asList(constraint1, constraint2, constraint3)); | ||
given(this.constraintDescriptionResolver.resolveDescription(constraint1)).willReturn("Bravo"); | ||
given(this.constraintDescriptionResolver.resolveDescription(constraint2)).willReturn("Alpha"); | ||
given(this.constraintDescriptionResolver.resolveDescription(constraint3)).willReturn("Delta"); | ||
|
||
assertThat(this.constraintDescriptions.descriptionsForProperty("foo", Cloneable.class)).containsExactly("Bravo", | ||
"Delta"); | ||
assertThat(this.constraintDescriptions.descriptionsForProperty("foo", Serializable.class)) | ||
.containsExactly("Delta"); | ||
assertThat(this.constraintDescriptions.descriptionsForProperty("foo")).containsExactly("Alpha"); | ||
} | ||
|
||
@Test | ||
public void emptyListOfDescriptionsWhenThereAreNoConstraints() { | ||
given(this.constraintResolver.resolveForProperty("foo", Constrained.class)).willReturn(Collections.emptyList()); | ||
assertThat(this.constraintDescriptions.descriptionsForProperty("foo").size()).isEqualTo(0); | ||
} | ||
|
||
private static final class Constrained { | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters