Skip to content

Commit

Permalink
Make type property "@type" for PrimitiveFormControlDataDto
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhorridge committed Jul 3, 2024
1 parent 5929251 commit 7c4442c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.Optional;

@JsonSubTypes({@Type(EntityFormControlDataDto.class), @Type(LiteralFormControlDataDto.class), @Type(IriFormControlDataDto.class)})
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
public abstract class PrimitiveFormControlDataDto implements Comparable<PrimitiveFormControlDataDto> {

public static final int BEFORE = -1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package edu.stanford.protege.webprotege.forms.data;

import com.google.common.collect.ImmutableMap;
import edu.stanford.protege.webprotege.entity.*;
import edu.stanford.protege.webprotege.jackson.WebProtegeJacksonApplication;
import org.junit.jupiter.api.Test;
import org.semanticweb.owlapi.model.IRI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.*;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.context.annotation.Import;
import uk.ac.manchester.cs.owl.owlapi.*;

import java.io.*;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Matthew Horridge
* Stanford Center for Biomedical Informatics Research
* 2024-07-03
*/
@JsonTest
@AutoConfigureJsonTesters
@Import(WebProtegeJacksonApplication.class)
public class PrimitiveFormControlDataDtoTest {

@Autowired
private JacksonTester<PrimitiveFormControlDataDto> tester;

@Test
public void shouldSerializeEntityData() throws IOException {
var data = PrimitiveFormControlDataDto.get(OWLClassData.get(new OWLClassImpl(IRI.create("http://example.org/A")), ImmutableMap.of()));
var written = tester.write(data);
System.out.println(written.getJson());
assertThat(written).hasJsonPathStringValue("['@type']", "EntityFormControlDataDto");
assertThat(written).hasJsonPathValue("entity");
}

@Test
void shouldDeserializeEntityData() throws IOException {
var expected = PrimitiveFormControlDataDto.get(OWLClassData.get(new OWLClassImpl(IRI.create("http://example.org/A")), ImmutableMap.of()));
var json = """
{"@type":"EntityFormControlDataDto","entity":{"@type":"ClassData","iri":"http://example.org/A"}}
""";
var read = tester.read(new StringReader(json));
assertThat(read.getObject()).isEqualTo(expected);
}

@Test
public void shouldSerializeIriData() throws IOException {
var data = PrimitiveFormControlDataDto.get(IRIData.get(IRI.create("http://example.org/A"), ImmutableMap.of()));
var written = tester.write(data);
System.out.println(written.getJson());
assertThat(written).hasJsonPathStringValue("['@type']", "IriFormControlDataDto");
assertThat(written).hasJsonPathValue("iri");
}

@Test
void shouldDeserializeIriData() throws IOException {
var expected = PrimitiveFormControlDataDto.get(IRIData.get(IRI.create("http://example.org/A"), ImmutableMap.of()));
var json = """
{"@type":"IriFormControlDataDto","iri":{"@type":"IRIData","iri":"http://example.org/A"}}
""";
var read = tester.read(new StringReader(json));
assertThat(read.getObject()).isEqualTo(expected);
}

@Test
public void shouldSerializeLiteralData() throws IOException {
var data = PrimitiveFormControlDataDto.get(OWLLiteralData.get(new OWLLiteralImpl("abc", "en", null)));
var written = tester.write(data);
System.out.println(written.getJson());
assertThat(written).hasJsonPathStringValue("['@type']", "LiteralFormControlDataDto");
assertThat(written).hasJsonPathValue("literal");
}

@Test
void shouldDeserializeLiteralData() throws IOException {
var expected = PrimitiveFormControlDataDto.get(OWLLiteralData.get(new OWLLiteralImpl("abc", "en", null)));
var json = """
{"@type":"LiteralFormControlDataDto","literal":{"@type":"LiteralData","value":"abc","lang":"en"}}
""";
var read = tester.read(new StringReader(json));
assertThat(read.getObject()).isEqualTo(expected);
}
}

0 comments on commit 7c4442c

Please sign in to comment.