Skip to content

Commit

Permalink
Serialization fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhorridge committed Apr 13, 2024
1 parent 1814956 commit b1d0f65
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,10 +15,7 @@
* Stanford Center for Biomedical Informatics Research
* 2021-05-24
*/
public class FormDataByFormId {


private Map<FormId, FormData> map;
public record FormDataByFormId(Map<FormId, FormData> map) {

/**
* Map FormIds to FormData. This map should not contain null keys, but it
Expand All @@ -26,7 +25,15 @@ public FormDataByFormId(Map<FormId, FormData> map) {
this.map = new LinkedHashMap<>(checkNotNull(map));
}

private FormDataByFormId() {
@JsonCreator
public static FormDataByFormId valueOf(Map<FormId, FormData> map) {
return new FormDataByFormId(map);
}

@Override
@JsonValue
public Map<FormId, FormData> map() {
return new LinkedHashMap<>(map);
}

@Nonnull
Expand Down Expand Up @@ -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 + '}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static FormPageRequestIndex create(@Nonnull ImmutableList<FormPageRequest
checkNotNull(pageRequests);
Map<Key, FormPageRequest> 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));
Expand All @@ -42,7 +42,7 @@ public static FormPageRequestIndex create(@Nonnull ImmutableList<FormPageRequest
public PageRequest getPageRequest(FormSubject formSubject, FormRegionId id, FormPageRequest.SourceType sourceType) {
var formPageRequest = indexMap.get(Key.get(formSubject, id, sourceType));
if (formPageRequest != null) {
return formPageRequest.getPageRequest();
return formPageRequest.pageRequest();
}
else {
return PageRequest.requestPageWithSize(1, FormPageRequest.DEFAULT_PAGE_SIZE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* 2019-11-30
*/
@JsonSubTypes({@Type(EntityNameControlData.class), @Type(FormData.class), @Type(GridControlData.class), @Type(ImageControlData.class), @Type(MultiChoiceControlData.class), @Type(NumberControlData.class), @Type(SingleChoiceControlData.class), @Type(TextControlData.class)})
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
public interface FormControlData {

<R> R accept(@Nonnull FormControlDataVisitorEx<R> visitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<FormDataByFormId> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -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")));
}
}

0 comments on commit b1d0f65

Please sign in to comment.