Skip to content

Commit

Permalink
Adding tests for AddEdge and RemoveEdge serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewhorridge committed Apr 18, 2024
1 parent c41bca8 commit 25965ae
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>edu.stanford.protege</groupId>
<artifactId>webprotege-jackson</artifactId>
<version>0.9.2</version>
<scope>test</scope>
</dependency>


</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.base.MoreObjects;

/**
* Matthew Horridge
* Stanford Center for Biomedical Informatics Research
* 2021-04-20
*/
@JsonTypeName("webprotege.events.graph.AddEdge")
public class AddEdge<U> extends EdgeChange<U> {

@JsonCreator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.google.common.base.MoreObjects;

/**
* Matthew Horridge
* Stanford Center for Biomedical Informatics Research
* 2021-04-20
*/
@JsonTypeName("webprotege.events.graph.RemoveEdge")
public class RemoveEdge<U> extends EdgeChange<U> {

@JsonCreator
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package edu.stanford.protege.webprotege.hierarchy;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.stanford.protege.webprotege.MockingUtils;
import edu.stanford.protege.webprotege.entity.EntityNode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.boot.test.json.JacksonTester;

import java.io.IOException;
import java.io.StringReader;

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

@JsonTest
@AutoConfigureJsonTesters
class AddEdge_Serialization_Test {

@Autowired
JacksonTester<GraphModelChange<EntityNode>> tester;

@Autowired
ObjectMapper objectMapper;

@BeforeEach
void setUp() throws JsonProcessingException {
}

@Test
void shouldSerializeAsJson() throws IOException {
var written = tester.write(new AddEdge<>(
new GraphEdge<>(
GraphNode.getForEntityNode(MockingUtils.mockOWLClassNode(),
false),
GraphNode.getForEntityNode(MockingUtils.mockOWLClassNode(),
false)
)
));
assertThat(written).hasJsonPathStringValue("type", "webprotege.events.graph.AddEdge");
assertThat(written).hasJsonPath("edge");
}

@Test
void shouldDeserializeFromJson() throws IOException {
var json = """
{"type":"webprotege.events.graph.AddEdge","edge":{"predecessor":{"userObject":{"entity":{"@type":"Class","iri":"http://stuff.com/I0"},"browserText":"<http://stuff.com/I0>","tags":[],"deprecated":false,"watches":[],"openCommentCount":0,"shortForms":[]},"sink":false},"successor":{"userObject":{"entity":{"@type":"Class","iri":"http://stuff.com/I1"},"browserText":"<http://stuff.com/I1>","tags":[],"deprecated":false,"watches":[],"openCommentCount":0,"shortForms":[]},"sink":false}}}
""";
var read = tester.readObject(new StringReader(json));
assertThat(read).isInstanceOf(AddEdge.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package edu.stanford.protege.webprotege.hierarchy;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.stanford.protege.webprotege.MockingUtils;
import edu.stanford.protege.webprotege.entity.EntityNode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters;
import org.springframework.boot.test.autoconfigure.json.JsonTest;
import org.springframework.boot.test.json.JacksonTester;

import java.io.IOException;
import java.io.StringReader;

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

/**
* Matthew Horridge
* Stanford Center for Biomedical Informatics Research
* 2024-04-18
*/
@JsonTest
@AutoConfigureJsonTesters
class RemoveEdge_Serialization_Test {

@Autowired
JacksonTester<GraphModelChange<EntityNode>> tester;

@Autowired
ObjectMapper objectMapper;

@BeforeEach
void setUp() throws JsonProcessingException {
}

@Test
void shouldSerializeAsJson() throws IOException {
var written = tester.write(new RemoveEdge<>(
new GraphEdge<>(
GraphNode.getForEntityNode(MockingUtils.mockOWLClassNode(),
false),
GraphNode.getForEntityNode(MockingUtils.mockOWLClassNode(),
false)
)
));
assertThat(written).hasJsonPathStringValue("type", "webprotege.events.graph.RemoveEdge");
assertThat(written).hasJsonPath("edge");
}

@Test
void shouldDeserializeFromJson() throws IOException {
var json = """
{"type":"webprotege.events.graph.RemoveEdge","edge":{"predecessor":{"userObject":{"entity":{"@type":"Class","iri":"http://stuff.com/I0"},"browserText":"<http://stuff.com/I0>","tags":[],"deprecated":false,"watches":[],"openCommentCount":0,"shortForms":[]},"sink":false},"successor":{"userObject":{"entity":{"@type":"Class","iri":"http://stuff.com/I1"},"browserText":"<http://stuff.com/I1>","tags":[],"deprecated":false,"watches":[],"openCommentCount":0,"shortForms":[]},"sink":false}}}
""";
var read = tester.readObject(new StringReader(json));
assertThat(read).isInstanceOf(RemoveEdge.class);
}
}

0 comments on commit 25965ae

Please sign in to comment.