|
18 | 18 | import static org.assertj.core.api.Assertions.*;
|
19 | 19 |
|
20 | 20 | import lombok.Getter;
|
| 21 | +import lombok.Value; |
21 | 22 |
|
22 | 23 | import java.util.Arrays;
|
23 | 24 | import java.util.List;
|
24 | 25 | import java.util.Map;
|
25 | 26 | import java.util.Optional;
|
| 27 | +import java.util.function.Function; |
| 28 | +import java.util.stream.Stream; |
26 | 29 |
|
27 | 30 | import javax.validation.constraints.Max;
|
28 | 31 | import javax.validation.constraints.Min;
|
|
31 | 34 |
|
32 | 35 | import org.hibernate.validator.constraints.Length;
|
33 | 36 | import org.hibernate.validator.constraints.Range;
|
| 37 | +import org.junit.jupiter.api.DynamicTest; |
34 | 38 | import org.junit.jupiter.api.Test;
|
| 39 | +import org.junit.jupiter.api.TestFactory; |
35 | 40 | import org.junit.jupiter.params.ParameterizedTest;
|
36 | 41 | import org.junit.jupiter.params.provider.CsvSource;
|
37 | 42 | import org.springframework.hateoas.Link;
|
38 | 43 | import org.springframework.hateoas.LinkRelation;
|
39 | 44 | import org.springframework.hateoas.RepresentationModel;
|
40 | 45 | import org.springframework.hateoas.mediatype.Affordances;
|
41 | 46 | import org.springframework.hateoas.mediatype.MessageResolver;
|
| 47 | +import org.springframework.hateoas.mediatype.hal.forms.HalFormsOptions.AbstractHalFormsOptions; |
| 48 | +import org.springframework.hateoas.mediatype.hal.forms.HalFormsOptions.Inline; |
42 | 49 | import org.springframework.http.HttpMethod;
|
43 | 50 | import org.springframework.http.MediaType;
|
| 51 | +import org.springframework.lang.Nullable; |
44 | 52 |
|
45 | 53 | /**
|
46 | 54 | * @author Oliver Drotbohm
|
@@ -182,6 +190,20 @@ void rendersRegisteredSuggest() {
|
182 | 190 | });
|
183 | 191 | }
|
184 | 192 |
|
| 193 | + @TestFactory |
| 194 | + Stream<DynamicTest> expressesCardinalityOnOptions() { |
| 195 | + |
| 196 | + Stream<Cardinality> stream = Stream.of( // |
| 197 | + Cardinality.of("optional", AbstractHalFormsOptions::optional, 0L, 1L), // |
| 198 | + Cardinality.of("optionalCollection", AbstractHalFormsOptions::optionalCollection, 0L, null), // |
| 199 | + Cardinality.of("optionalCollection(n)", it -> it.optionalCollection(5L), 0L, 5L), // |
| 200 | + Cardinality.of("required", AbstractHalFormsOptions::required, 1L, 1L), // |
| 201 | + Cardinality.of("requiredCollection", AbstractHalFormsOptions::requiredCollection, 1L, null), // |
| 202 | + Cardinality.of("requiredCollection(n)", it -> it.requiredCollection(5L), 1L, 5L)); |
| 203 | + |
| 204 | + return DynamicTest.stream(stream, Cardinality::toString, Cardinality::verify); |
| 205 | + } |
| 206 | + |
185 | 207 | @Getter
|
186 | 208 | static class PatternExample extends RepresentationModel<PatternExample> {
|
187 | 209 |
|
@@ -215,4 +237,28 @@ static class Payload {
|
215 | 237 | @Range(min = 8, max = 10) //
|
216 | 238 | Integer range;
|
217 | 239 | }
|
| 240 | + |
| 241 | + @Value(staticConstructor = "of") |
| 242 | + static class Cardinality { |
| 243 | + |
| 244 | + String title; |
| 245 | + Function<AbstractHalFormsOptions<?>, AbstractHalFormsOptions<?>> mapper; |
| 246 | + Long expectedMin; |
| 247 | + @Nullable Long expectedMax; |
| 248 | + |
| 249 | + void verify() { |
| 250 | + |
| 251 | + Inline inline = HalFormsOptions.inline(); |
| 252 | + HalFormsOptions result = mapper.apply(inline); |
| 253 | + |
| 254 | + assertThat(result.getMinItems()).isEqualTo(expectedMin); |
| 255 | + assertThat(result.getMaxItems()).isEqualTo(expectedMax); |
| 256 | + } |
| 257 | + |
| 258 | + @Override |
| 259 | + public String toString() { |
| 260 | + return String.format("Calling HalFormsOptions.%s() configures minItems to %s and maxItems to %s.", // |
| 261 | + title, expectedMin, expectedMax); |
| 262 | + } |
| 263 | + } |
218 | 264 | }
|
0 commit comments