Skip to content

Commit

Permalink
Ironed out serialization of EntityNameControl descriptor, data and dtos
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhorridge committed Jul 2, 2024
1 parent 76fde71 commit b92f81a
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class EntityNameControlDataDto implements FormControlDataDto {

@JsonCreator
public static EntityNameControlDataDto get(@JsonProperty(PropertyNames.CONTROL) @Nonnull EntityNameControlDescriptor descriptor,
@JsonProperty(PropertyNames.ENTITY) @Nonnull OWLEntityData entityData,
@JsonProperty(PropertyNames.ENTITY) @Nullable OWLEntityData entityData,
@JsonProperty(PropertyNames.DEPTH) int depth) {
return new AutoValue_EntityNameControlDataDto(depth, descriptor, entityData);
}
Expand All @@ -30,7 +30,7 @@ public static EntityNameControlDataDto get(@JsonProperty(PropertyNames.CONTROL)
public abstract EntityNameControlDescriptor getDescriptor();

@Nullable
@JsonProperty(PropertyNames.ENTITY)
@JsonProperty(PropertyNames.VALUE)
protected abstract OWLEntityData getEntityInternal();

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import javax.annotation.Nonnull;

@JsonSubTypes({@Type(EntityNameControlDataDto.class), @Type(GridControlDataDto.class), @Type(ImageControlDataDto.class), @Type(MultiChoiceControlDataDto.class), @Type(NumberControlDataDto.class), @Type(SingleChoiceControlDataDto.class), @Type(TextControlDataDto.class), @Type(FormDataDto.class)})
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
public interface FormControlDataDto {

<R> R accept(FormControlDataDtoVisitorEx<R> visitor);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package edu.stanford.protege.webprotege.forms.field;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.*;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
Expand All @@ -25,7 +22,7 @@
*/
@JsonTypeName(EntityNameControlDescriptor.TYPE)
@AutoValue

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public abstract class EntityNameControlDescriptor implements FormControlDescriptor {

protected static final String TYPE = "ENTITY_NAME";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package edu.stanford.protege.webprotege.forms.field;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.*;
import com.google.auto.value.AutoValue;
import edu.stanford.protege.webprotege.common.LanguageMap;
import edu.stanford.protege.webprotege.criteria.CompositeRootCriteria;
Expand All @@ -16,6 +13,7 @@

@AutoValue
@JsonTypeName("EntityNameControlDescriptorDto")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public abstract class EntityNameControlDescriptorDto implements FormControlDescriptorDto {

@JsonCreator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@JsonSubTypes({@Type(EntityNameControlDescriptorDto.class), @Type(GridControlDescriptorDto.class), @Type(ImageControlDescriptorDto.class), @Type(MultiChoiceControlDescriptorDto.class), @Type(NumberControlDescriptorDto.class), @Type(SingleChoiceControlDescriptorDto.class), @Type(TextControlDescriptorDto.class), @Type(SubFormControlDescriptorDto.class)

})
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
public interface FormControlDescriptorDto {

<R> R accept(FormControlDescriptorDtoVisitor<R> visitor);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package edu.stanford.protege.webprotege.forms.data;

import edu.stanford.protege.webprotege.forms.field.*;
import edu.stanford.protege.webprotege.jackson.WebProtegeJacksonApplication;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.context.annotation.Import;
import uk.ac.manchester.cs.owl.owlapi.OWLLiteralImpl;

import java.io.*;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

@JsonTest
@Import(WebProtegeJacksonApplication.class)
class EntityNameControlDataDtoTest {

@Autowired
private JacksonTester<FormControlDataDto> tester;

@Test
void shouldSerialize() throws IOException {
var data = EntityNameControlDataDto.get(EntityNameControlDescriptor.getDefault().getDefault(),
null,
3);
var written = tester.write(data);
System.out.println(written.getJson());
assertThat(written).hasJsonPathValue("control");
assertThat(written).hasJsonPath("value");
assertThat(written).hasJsonPathValue("depth");
}

@Test
void shouldDeserialize() throws IOException {
var json = """
{
"@type": "EntityNameControlDataDto",
"depth": 3,
"control": {
"@type": "ENTITY_NAME"
},
"value": null
}
""";
var read = tester.read(new StringReader(json));
assertThat(read.getObject()).isInstanceOf(EntityNameControlDataDto.class);
var obj = (EntityNameControlDataDto) read.getObject();
assertThat(obj.getEntity()).isEmpty();
assertThat(obj.getDepth()).isEqualTo(3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package edu.stanford.protege.webprotege.forms.field;

import edu.stanford.protege.webprotege.common.LanguageMap;
import edu.stanford.protege.webprotege.criteria.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.*;
import org.springframework.boot.test.json.JacksonTester;

import java.io.IOException;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

@JsonTest
@AutoConfigureJsonTesters
class EntityNameControlDescriptorDtoTest {


@Autowired
private JacksonTester<EntityNameControlDescriptorDto> tester;

@Test
public void shouldSerializeNonEmpty() throws IOException {
var descriptor = EntityNameControlDescriptorDto.get(LanguageMap.of("en", "Hello"),
CompositeRootCriteria.get(List.of(), MultiMatchType.ALL));
var written = tester.write(descriptor);
System.out.println(written.getJson());
assertThat(written).hasJsonPathValue("placeholder");
assertThat(written).hasJsonPathValue("criteria");
}

@Test
void shouldNotSerializeEmptyFields() throws IOException {
var descriptor = EntityNameControlDescriptorDto.get(LanguageMap.empty(),
null);
var written = tester.write(descriptor);
System.out.println(written.getJson());
assertThat(written).doesNotHaveJsonPath("placeholder");
assertThat(written).doesNotHaveJsonPath("criteria");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@ class EntityNameControlDescriptorTest {
private JacksonTester<EntityNameControlDescriptor> tester;

@Test
public void shouldSerialize() throws IOException {
var descriptor = EntityNameControlDescriptor.get(LanguageMap.empty(),
public void shouldSerializeNonEmpty() throws IOException {
var descriptor = EntityNameControlDescriptor.get(LanguageMap.of("en", "Hello"),
CompositeRootCriteria.get(List.of(), MultiMatchType.ALL));
var written = tester.write(descriptor);
System.out.println(written.getJson());
assertThat(written).hasJsonPathValue("placeholder");
assertThat(written).hasJsonPathValue("criteria");
}

@Test
void shouldNotSerializeEmptyFields() throws IOException {
var descriptor = EntityNameControlDescriptor.get(LanguageMap.empty(),
null);
var written = tester.write(descriptor);
System.out.println(written.getJson());
assertThat(written).doesNotHaveJsonPath("placeholder");
assertThat(written).doesNotHaveJsonPath("criteria");
}
}

0 comments on commit b92f81a

Please sign in to comment.