Skip to content

Commit

Permalink
Testing JSON-LD deserialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Aug 1, 2016
1 parent dc2a3f0 commit f8eec95
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 26 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Expand Up @@ -20,6 +20,7 @@
<org.mockito.version>1.10.19</org.mockito.version>
<ch.qos.logback.version>1.1.6</ch.qos.logback.version>
<com.fasterxml.jackson.version>2.8.1</com.fasterxml.jackson.version>
<com.github.jsonld-java.version>0.8.3</com.github.jsonld-java.version>
</properties>

<repositories>
Expand All @@ -43,6 +44,12 @@
<version>0.0.2</version>
</dependency>

<dependency>
<groupId>com.github.jsonld-java</groupId>
<artifactId>jsonld-java</artifactId>
<version>${com.github.jsonld-java.version}</version>
</dependency>

<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
@@ -1,53 +1,98 @@
package cz.cvut.kbss.jsonld.jackson.environment.deserialization;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import cz.cvut.kbss.jsonld.jackson.JsonLdModule;
import cz.cvut.kbss.jsonld.jackson.environment.Environment;
import cz.cvut.kbss.jsonld.jackson.environment.model.Employee;
import cz.cvut.kbss.jsonld.jackson.environment.model.Organization;
import cz.cvut.kbss.jsonld.jackson.environment.model.User;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;

public class JsonLdDeserializationTest {

private static final URI HALSEY_URI = URI
.create("http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#Catherine+Halsey");
private static final URI LASKY_URI = URI
.create("http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#Thomas+Lasky");
private static final URI PALMER_URI = URI
.create("http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#Sarah+Palmer");

private static final Map<URI, User> USERS = initUsers();

private static final URI ORG_URI = URI.create("http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#UNSC");
private static final String ORG_NAME = "UNSC";
private static final String[] ORG_BRANDS = {"Spartan-II", "Mjolnir IV"};

private ObjectMapper objectMapper;

private static Map<URI, User> initUsers() {
final Map<URI, User> map = new HashMap<>();
map.put(HALSEY_URI, new User(HALSEY_URI, "Catherine", "Halsey", "halsey@unsc.org", true));
map.put(LASKY_URI, new User(LASKY_URI, "Thomas", "Lasky", "lasky@unsc.org", false));
map.put(PALMER_URI, new User(PALMER_URI, "Sarah", "Palmer", "palmer@unsc.org", false));
return map;
}

@Before
public void setUp() {
this.objectMapper = new ObjectMapper();
objectMapper.registerModule(new JsonLdModule());
}

@Ignore
@Test
public void testDeserializeInstanceWithDataProperties() throws Exception {
final String input = Environment.readData("objectWithDataProperties.json");
final User expected = new User();
expected.setUri(URI.create("http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#787088083"));
expected.setFirstName("Catherine");
expected.setLastName("Halsey");
expected.setUsername("halsey@unsc.org");
expected.setAdmin(true);
final User result = objectMapper.readValue(input, User.class);
assertNotNull(result);
assertEquals(expected.getUri(), result.getUri());
assertEquals(expected.getFirstName(), result.getFirstName());
assertEquals(expected.getLastName(), result.getLastName());
assertEquals(expected.getUsername(), result.getUsername());
assertEquals(expected.getAdmin(), result.getAdmin());
final User expected = USERS.get(HALSEY_URI);
verifyUserAttributes(expected, result);
}

private void verifyUserAttributes(User expected, User actual) {
assertEquals(expected.getUri(), actual.getUri());
assertEquals(expected.getFirstName(), actual.getFirstName());
assertEquals(expected.getLastName(), actual.getLastName());
assertEquals(expected.getUsername(), actual.getUsername());
assertEquals(expected.getAdmin(), actual.getAdmin());
}

@Ignore
@Test
public void testDeserializeInstanceWithSingularObjectProperty() throws Exception {
final String input = Environment.readData("objectWithSingularReference.json");
final Employee result = objectMapper.readValue(input, Employee.class);
// TODO
verifyUserAttributes(USERS.get(HALSEY_URI), result);
assertNotNull(result.getEmployer());
verifyOrganizationAttributes(result.getEmployer());
}

private void verifyOrganizationAttributes(Organization actual) {
assertEquals(ORG_URI, actual.getUri());
assertEquals(ORG_NAME, actual.getName());
assertNotNull(actual.getDateCreated());
for (String b : ORG_BRANDS) {
assertTrue(actual.getBrands().contains(b));
}
}

@Test
public void testDeserializeCollectionOfInstances() throws Exception {
final String input = Environment.readData("collectionOfInstances.json");
final List<Employee> result = objectMapper.readValue(input, new TypeReference<List<Employee>>() {
});
assertNotNull(result);
assertFalse(result.isEmpty());
result.forEach(e -> {
final User expected = USERS.get(e.getUri());
verifyUserAttributes(expected, e);
});
}
}
Expand Up @@ -5,6 +5,8 @@
import cz.cvut.kbss.jopa.model.annotations.OWLDataProperty;
import cz.cvut.kbss.jsonld.jackson.environment.Vocabulary;

import java.net.URI;

@OWLClass(iri = Vocabulary.USER)
public class User extends Person {

Expand All @@ -14,6 +16,17 @@ public class User extends Person {
@OWLAnnotationProperty(iri = Vocabulary.IS_ADMIN)
private Boolean admin;

public User() {
}

public User(URI uri, String firstName, String lastName, String username, Boolean admin) {
setUri(uri);
setFirstName(firstName);
setLastName(lastName);
this.username = username;
this.admin = admin;
}

public String getUsername() {
return username;
}
Expand Down
38 changes: 38 additions & 0 deletions src/test/resources/collectionOfInstances.json
@@ -0,0 +1,38 @@
[
{
"@id": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#Catherine+Halsey",
"@type": [
"http://onto.fel.cvut.cz/ontologies/ufo/Person",
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/Employee",
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/User"
],
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/isAdmin": true,
"http://xmlns.com/foaf/0.1/accountName": "halsey@unsc.org",
"http://xmlns.com/foaf/0.1/firstName": "Catherine",
"http://xmlns.com/foaf/0.1/lastName": "Halsey"
},
{
"@id": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#Thomas+Lasky",
"@type": [
"http://onto.fel.cvut.cz/ontologies/ufo/Person",
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/Employee",
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/User"
],
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/isAdmin": false,
"http://xmlns.com/foaf/0.1/accountName": "lasky@unsc.org",
"http://xmlns.com/foaf/0.1/firstName": "Thomas",
"http://xmlns.com/foaf/0.1/lastName": "Lasky"
},
{
"@id": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#Sarah+Palmer",
"@type": [
"http://onto.fel.cvut.cz/ontologies/ufo/Person",
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/Employee",
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/User"
],
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/isAdmin": false,
"http://xmlns.com/foaf/0.1/accountName": "palmer@unsc.org",
"http://xmlns.com/foaf/0.1/firstName": "Sarah",
"http://xmlns.com/foaf/0.1/lastName": "Palmer"
}
]
2 changes: 1 addition & 1 deletion src/test/resources/objectWithDataProperties.json
Expand Up @@ -5,7 +5,7 @@
"accountName": "http://xmlns.com/foaf/0.1/accountName",
"isAdmin": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/isAdmin"
},
"@id": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#787088083",
"@id": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#Catherine+Halsey",
"@type": [
"http://onto.fel.cvut.cz/ontologies/ufo/Person",
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/User"
Expand Down
19 changes: 11 additions & 8 deletions src/test/resources/objectWithSingularReference.json
Expand Up @@ -5,21 +5,24 @@
"accountName": "http://xmlns.com/foaf/0.1/accountName",
"isAdmin": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/isAdmin"
},
"@id": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#-697813743",
"@id": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#Catherine+Halsey",
"@type": [
"http://onto.fel.cvut.cz/ontologies/ufo/Person",
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/Employee",
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/User"
],
"isAdmin": false,
"isAdmin": true,
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/isMemberOf": {
"@id": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#-82218522",
"@id": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld#UNSC",
"@type": "http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/Organization",
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/brand": "Brandy0",
"http://krizik.felk.cvut.cz/ontologies/jaxb-jsonld/brand": [
"Spartan-II",
"Mjolnir IV"
],
"http://purl.org/dc/terms/created": "Fri Jul 29 14:52:04 CEST 2016",
"http://www.w3.org/2000/01/rdf-schema#label": "Organization291065182"
"http://www.w3.org/2000/01/rdf-schema#label": "UNSC"
},
"accountName": "user647104078",
"firstName": "FirstName647104078",
"lastName": "LastName647104078"
"accountName": "halsey@unsc.org",
"firstName": "Catherine",
"lastName": "Halsey"
}

0 comments on commit f8eec95

Please sign in to comment.