From 8b07e98cee4855147e38ecaf8f114e4ba9c04b8b Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Tue, 10 Dec 2024 20:33:24 +0100 Subject: [PATCH] GH-2261 - Polishing. --- .../mediatype/hal/forms/HalFormsOptions.java | 2 +- .../Jackson2HalFormsIntegrationTest.java | 49 ++++++++++--------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsOptions.java b/src/main/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsOptions.java index 20e51956a..50058fb7d 100644 --- a/src/main/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsOptions.java +++ b/src/main/java/org/springframework/hateoas/mediatype/hal/forms/HalFormsOptions.java @@ -45,7 +45,7 @@ public interface HalFormsOptions { * @param values must not be {@literal null}. * @return will never be {@literal null}. */ - @SuppressWarnings("unchecked") + @SafeVarargs public static Inline inline(T... values) { Assert.notNull(values, "Values must not be null!"); diff --git a/src/test/java/org/springframework/hateoas/mediatype/hal/forms/Jackson2HalFormsIntegrationTest.java b/src/test/java/org/springframework/hateoas/mediatype/hal/forms/Jackson2HalFormsIntegrationTest.java index 6daa9b4c6..a49afe54a 100644 --- a/src/test/java/org/springframework/hateoas/mediatype/hal/forms/Jackson2HalFormsIntegrationTest.java +++ b/src/test/java/org/springframework/hateoas/mediatype/hal/forms/Jackson2HalFormsIntegrationTest.java @@ -573,15 +573,31 @@ void rendersPromptedOptionsValues() throws Exception { .isEqualTo("My Prompt"); } - @Test - // #2257 + @Test // #1483 + void rendersRemoteOptions() { + + Link link = Link.of("/foo{?bar}").withType(MediaType.APPLICATION_JSON_VALUE); + + Remote remote = HalFormsOptions.remote(link); + + DocumentContext result = JsonPath.parse(getCuriedObjectMapper().writeObject(remote)); + + assertThat(result.read("$.link.href", String.class)).isEqualTo("/foo{?bar}"); + assertThat(result.read("$.link.type", String.class)).isEqualTo(MediaType.APPLICATION_JSON_VALUE); + assertThat(result.read("$.link.templated", boolean.class)).isTrue(); + } + + @Test // #2257 void rendersFullInlineOptions() { - Inline inline = HalFormsOptions.inline(Map.of("my-prompt-field", "foo","my-value-field", "bar")).withPromptField("my-prompt-field") + + var options = HalFormsOptions.inline(Map.of("my-prompt-field", "foo", "my-value-field", "bar")) + .withPromptField("my-prompt-field") .withValueField("my-value-field") .withMinItems(2L) .withMaxItems(3L); - DocumentContext result = JsonPath.parse(getCuriedObjectMapper().writeObject(inline)); + var result = JsonPath.parse(getCuriedObjectMapper().writeObject(options)); + assertThat(result.read("$.inline[0].my-prompt-field", String.class)).isEqualTo("foo"); assertThat(result.read("$.inline[0].my-value-field", String.class)).isEqualTo("bar"); assertThat(result.read("$.promptField", String.class)).isEqualTo("my-prompt-field"); @@ -590,18 +606,19 @@ void rendersFullInlineOptions() { assertThat(result.read("$.maxItems", Long.class)).isEqualTo(3L); } - @Test - // #2257 + @Test // #2257 void rendersFullRemoteOptions() { - Link link = Link.of("/foo{?bar}").withType(MediaType.APPLICATION_JSON_VALUE); - Remote remote = HalFormsOptions.remote(link) + var link = Link.of("/foo{?bar}").withType(MediaType.APPLICATION_JSON_VALUE); + + var options = HalFormsOptions.remote(link) .withPromptField("my-prompt-field") .withValueField("my-value-field") .withMinItems(2L) .withMaxItems(3L); - DocumentContext result = JsonPath.parse(getCuriedObjectMapper().writeObject(remote)); + var result = JsonPath.parse(getCuriedObjectMapper().writeObject(options)); + assertThat(result.read("$.link.href", String.class)).isEqualTo("/foo{?bar}"); assertThat(result.read("$.link.type", String.class)).isEqualTo(MediaType.APPLICATION_JSON_VALUE); assertThat(result.read("$.link.templated", boolean.class)).isTrue(); @@ -611,20 +628,6 @@ void rendersFullRemoteOptions() { assertThat(result.read("$.maxItems", Long.class)).isEqualTo(3L); } - @Test // #1483 - void rendersRemoteOptions() { - - Link link = Link.of("/foo{?bar}").withType(MediaType.APPLICATION_JSON_VALUE); - - Remote remote = HalFormsOptions.remote(link); - - DocumentContext result = JsonPath.parse(getCuriedObjectMapper().writeObject(remote)); - - assertThat(result.read("$.link.href", String.class)).isEqualTo("/foo{?bar}"); - assertThat(result.read("$.link.type", String.class)).isEqualTo(MediaType.APPLICATION_JSON_VALUE); - assertThat(result.read("$.link.templated", boolean.class)).isTrue(); - } - private void assertThatPathDoesNotExist(Object toMarshall, String path) throws Exception { String json = getCuriedObjectMapper().writeObject(toMarshall);