Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
132 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 62 additions & 17 deletions
79
...va/cz/cvut/kbss/jsonld/jackson/environment/deserialization/JsonLdDeserializationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters