|
| 1 | +package org.springframework.hateoas.mediatype.hal.forms; |
| 2 | + |
| 3 | +import lombok.Data; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.BeforeEach; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.springframework.hateoas.Link; |
| 8 | +import org.springframework.hateoas.RepresentationModel; |
| 9 | +import org.springframework.hateoas.mediatype.Affordances; |
| 10 | +import org.springframework.hateoas.mediatype.MessageResolver; |
| 11 | +import org.springframework.http.HttpMethod; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.*; |
| 14 | + |
| 15 | +public class HalFormsPropertyOrderingUnitTest { |
| 16 | + |
| 17 | + private RepresentationModel<?> model; |
| 18 | + |
| 19 | + @BeforeEach |
| 20 | + void setUp() { |
| 21 | + |
| 22 | + this.model = new RepresentationModel<>(); |
| 23 | + |
| 24 | + this.model.add(Affordances.of(Link.of("/example")) // |
| 25 | + .afford(HttpMethod.POST) // |
| 26 | + .withInput(Thing.class) // |
| 27 | + .toLink()); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + void noCustomOrdering() { |
| 32 | + |
| 33 | + HalFormsConfiguration halFormsConfiguration = new HalFormsConfiguration(); |
| 34 | + |
| 35 | + assertThat(createTemplate(halFormsConfiguration).getProperties()).flatExtracting(HalFormsProperty::getName) |
| 36 | + .containsExactly("a", "b", "z"); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void specifyAllProperties() { |
| 42 | + |
| 43 | + HalFormsConfiguration halFormsConfiguration = new HalFormsConfiguration() // |
| 44 | + .withFieldOrderFor(Thing.class, "z", "b", "a"); |
| 45 | + |
| 46 | + assertThat(createTemplate(halFormsConfiguration).getProperties()).flatExtracting(HalFormsProperty::getName) |
| 47 | + .containsExactly("z", "b", "a"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void specifySomeProperties() { |
| 52 | + |
| 53 | + HalFormsConfiguration halFormsConfiguration = new HalFormsConfiguration() // |
| 54 | + .withFieldOrderFor(Thing.class, "z"); |
| 55 | + |
| 56 | + assertThat(createTemplate(halFormsConfiguration).getProperties()).flatExtracting(HalFormsProperty::getName) |
| 57 | + .containsExactly("z", "a", "b"); |
| 58 | + } |
| 59 | + |
| 60 | + private HalFormsTemplate createTemplate(HalFormsConfiguration halFormsConfiguration) { |
| 61 | + return new HalFormsTemplateBuilder(halFormsConfiguration, MessageResolver.DEFAULTS_ONLY).findTemplates(this.model) |
| 62 | + .get("default"); |
| 63 | + } |
| 64 | + |
| 65 | + @Data |
| 66 | + private static class Thing { |
| 67 | + |
| 68 | + private String a; |
| 69 | + private String b; |
| 70 | + private String z; |
| 71 | + } |
| 72 | +} |
0 commit comments