From b1d0f65e1837006c1ca517305cf6aa982cb2866c Mon Sep 17 00:00:00 2001 From: Matthew Horridge Date: Fri, 12 Apr 2024 18:17:09 -0700 Subject: [PATCH] Serialization fixes --- .../webprotege/forms/FormDataByFormId.java | 22 ++++----- .../webprotege/forms/FormPageRequest.java | 26 +++-------- .../forms/FormPageRequestIndex.java | 4 +- .../forms/data/FormControlData.java | 2 +- .../forms/data/FormEntitySubject.java | 2 +- .../forms/data/TextControlData.java | 2 + .../forms/FormDataByFormIdTest.java | 45 +++++++++++++++++++ .../webprotege/forms/FormId_TestCase.java | 26 +++++++++++ 8 files changed, 95 insertions(+), 34 deletions(-) create mode 100644 src/test/java/edu/stanford/protege/webprotege/forms/FormDataByFormIdTest.java diff --git a/src/main/java/edu/stanford/protege/webprotege/forms/FormDataByFormId.java b/src/main/java/edu/stanford/protege/webprotege/forms/FormDataByFormId.java index cd9b63a..1c0f87b 100644 --- a/src/main/java/edu/stanford/protege/webprotege/forms/FormDataByFormId.java +++ b/src/main/java/edu/stanford/protege/webprotege/forms/FormDataByFormId.java @@ -1,5 +1,7 @@ package edu.stanford.protege.webprotege.forms; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; import com.google.common.collect.ImmutableSet; import edu.stanford.protege.webprotege.forms.data.FormData; @@ -13,10 +15,7 @@ * Stanford Center for Biomedical Informatics Research * 2021-05-24 */ -public class FormDataByFormId { - - - private Map map; +public record FormDataByFormId(Map map) { /** * Map FormIds to FormData. This map should not contain null keys, but it @@ -26,7 +25,15 @@ public FormDataByFormId(Map map) { this.map = new LinkedHashMap<>(checkNotNull(map)); } - private FormDataByFormId() { + @JsonCreator + public static FormDataByFormId valueOf(Map map) { + return new FormDataByFormId(map); + } + + @Override + @JsonValue + public Map map() { + return new LinkedHashMap<>(map); } @Nonnull @@ -55,11 +62,6 @@ public boolean equals(Object o) { return map.equals(that.map); } - @Override - public int hashCode() { - return Objects.hash(map); - } - @Override public String toString() { return "FormDataByFormId{" + map + '}'; diff --git a/src/main/java/edu/stanford/protege/webprotege/forms/FormPageRequest.java b/src/main/java/edu/stanford/protege/webprotege/forms/FormPageRequest.java index 23b24e6..fb2362e 100644 --- a/src/main/java/edu/stanford/protege/webprotege/forms/FormPageRequest.java +++ b/src/main/java/edu/stanford/protege/webprotege/forms/FormPageRequest.java @@ -15,10 +15,12 @@ * Stanford Center for Biomedical Informatics Research * 2020-04-22 */ -@AutoValue - @JsonTypeName("FormPageRequest") -public abstract class FormPageRequest { +public record FormPageRequest(FormId formId, + FormSubject subject, + FormRegionId regionId, + SourceType sourceType, + PageRequest pageRequest) { public static final int DEFAULT_PAGE_SIZE = 10; @@ -29,25 +31,9 @@ public static FormPageRequest get(@JsonProperty("formId") @Nonnull FormId formId @JsonProperty("regionId") @Nonnull FormRegionId formFieldId, @JsonProperty("sourceType") @Nonnull SourceType sourceType, @JsonProperty("pageRequest") @Nonnull PageRequest pageRequest) { - return new AutoValue_FormPageRequest(formId, subject, formFieldId, sourceType, pageRequest); + return new FormPageRequest(formId, subject, formFieldId, sourceType, pageRequest); } - @Nonnull - public abstract FormId getFormId(); - - @Nonnull - public abstract FormSubject getSubject(); - - @JsonProperty("regionId") - @Nonnull - public abstract FormRegionId getFieldId(); - - @Nonnull - public abstract SourceType getSourceType(); - - @Nonnull - public abstract PageRequest getPageRequest(); - public enum SourceType { CONTROL_STACK, GRID_CONTROL } diff --git a/src/main/java/edu/stanford/protege/webprotege/forms/FormPageRequestIndex.java b/src/main/java/edu/stanford/protege/webprotege/forms/FormPageRequestIndex.java index 15493cd..d3c02ca 100644 --- a/src/main/java/edu/stanford/protege/webprotege/forms/FormPageRequestIndex.java +++ b/src/main/java/edu/stanford/protege/webprotege/forms/FormPageRequestIndex.java @@ -32,7 +32,7 @@ public static FormPageRequestIndex create(@Nonnull ImmutableList map = new HashMap<>(); for (FormPageRequest pageRequest : pageRequests) { - map.put(Key.get(pageRequest.getSubject(), pageRequest.getFieldId(), pageRequest.getSourceType()), + map.put(Key.get(pageRequest.subject(), pageRequest.regionId(), pageRequest.sourceType()), pageRequest); } return new FormPageRequestIndex(ImmutableMap.copyOf(map)); @@ -42,7 +42,7 @@ public static FormPageRequestIndex create(@Nonnull ImmutableList R accept(@Nonnull FormControlDataVisitorEx visitor); diff --git a/src/main/java/edu/stanford/protege/webprotege/forms/data/FormEntitySubject.java b/src/main/java/edu/stanford/protege/webprotege/forms/data/FormEntitySubject.java index e23aa5f..573528b 100644 --- a/src/main/java/edu/stanford/protege/webprotege/forms/data/FormEntitySubject.java +++ b/src/main/java/edu/stanford/protege/webprotege/forms/data/FormEntitySubject.java @@ -20,7 +20,7 @@ public abstract class FormEntitySubject implements FormSubject { @JsonCreator - public static FormEntitySubject get(@JsonProperty("term") @Nonnull OWLEntity entity) { + public static FormEntitySubject get(@JsonProperty("entity") @Nonnull OWLEntity entity) { return new AutoValue_FormEntitySubject(entity); } diff --git a/src/main/java/edu/stanford/protege/webprotege/forms/data/TextControlData.java b/src/main/java/edu/stanford/protege/webprotege/forms/data/TextControlData.java index 256d977..a444876 100644 --- a/src/main/java/edu/stanford/protege/webprotege/forms/data/TextControlData.java +++ b/src/main/java/edu/stanford/protege/webprotege/forms/data/TextControlData.java @@ -1,5 +1,6 @@ package edu.stanford.protege.webprotege.forms.data; +import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonTypeName; @@ -21,6 +22,7 @@ @JsonTypeName("TextControlData") public abstract class TextControlData implements FormControlData { + @JsonCreator @Nonnull public static TextControlData get(@JsonProperty("descriptor") @Nonnull TextControlDescriptor descriptor, @JsonProperty("value") @Nullable OWLLiteral value) { diff --git a/src/test/java/edu/stanford/protege/webprotege/forms/FormDataByFormIdTest.java b/src/test/java/edu/stanford/protege/webprotege/forms/FormDataByFormIdTest.java new file mode 100644 index 0000000..eb18a44 --- /dev/null +++ b/src/test/java/edu/stanford/protege/webprotege/forms/FormDataByFormIdTest.java @@ -0,0 +1,45 @@ +package edu.stanford.protege.webprotege.forms; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.guava.GuavaModule; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.json.JacksonTester; +import org.springframework.core.ResolvableType; + +import java.io.IOException; +import java.io.StringReader; +import java.util.Collections; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class FormDataByFormIdTest { + + private JacksonTester tester; + + @BeforeEach + void setUp() { + var objectMapper = new ObjectMapper(); + objectMapper.registerModule(new GuavaModule()); + tester = new JacksonTester<>(FormDataByFormIdTest.class, + ResolvableType.forClass(FormDataByFormId.class), objectMapper); + } + + @Test + void shouldSerialize() throws IOException { + var written = tester.write(new FormDataByFormId(Collections.emptyMap())); + assertThat(written).isEqualToJson("{}"); + } + + @Test + void shouldDeserialize() throws IOException { + var read = tester.read(new StringReader(""" + { + } + """)); + read.assertThat().isNotNull(); + } +} \ No newline at end of file diff --git a/src/test/java/edu/stanford/protege/webprotege/forms/FormId_TestCase.java b/src/test/java/edu/stanford/protege/webprotege/forms/FormId_TestCase.java index 0d9bf7d..8f0c26d 100644 --- a/src/test/java/edu/stanford/protege/webprotege/forms/FormId_TestCase.java +++ b/src/test/java/edu/stanford/protege/webprotege/forms/FormId_TestCase.java @@ -1,9 +1,16 @@ package edu.stanford.protege.webprotege.forms; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.guava.GuavaModule; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.boot.test.json.JacksonTester; +import org.springframework.core.ResolvableType; + +import java.io.IOException; +import java.io.StringReader; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; @@ -54,4 +61,23 @@ public void shouldImplementToString() { assertThat(formId.toString(), startsWith("FormId")); } + @Test + void shouldSerialize() throws IOException { + var objectMapper = new ObjectMapper(); + objectMapper.registerModule(new GuavaModule()); + var tester = new JacksonTester<>(FormId_TestCase.class, + ResolvableType.forClass(FormId.class), objectMapper); + var formId = FormId.generate(); + var written = tester.write(formId); + assertThat(written.getJson(), equalTo("\"" + formId.getId() + "\"")); + } + @Test + void shouldDeserialize() throws IOException { + var objectMapper = new ObjectMapper(); + objectMapper.registerModule(new GuavaModule()); + var tester = new JacksonTester<>(FormId_TestCase.class, + ResolvableType.forClass(FormId.class), objectMapper); + var read = tester.read(new StringReader("\"12345678-1234-1234-1234-123456789abc\"")); + assertThat(read.getObject(), equalTo(FormId.valueOf("12345678-1234-1234-1234-123456789abc"))); + } }