-
Notifications
You must be signed in to change notification settings - Fork 2
/
CompactedJsonLdSerializerTest.java
80 lines (66 loc) · 2.84 KB
/
CompactedJsonLdSerializerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cz.cvut.kbss.jsonld.serialization;
import com.github.jsonldjava.utils.JsonUtils;
import cz.cvut.kbss.jsonld.environment.Generator;
import cz.cvut.kbss.jsonld.environment.model.Employee;
import cz.cvut.kbss.jsonld.environment.model.Organization;
import cz.cvut.kbss.jsonld.environment.model.User;
import cz.cvut.kbss.jsonld.serialization.util.BufferedJsonSerializer;
import org.junit.Before;
import org.junit.Test;
import java.util.Set;
import static org.junit.Assert.assertNotNull;
public class CompactedJsonLdSerializerTest {
private BufferedJsonSerializer jsonWriter;
private JsonLdSerializer serializer;
@Before
public void setUp() {
this.jsonWriter = new BufferedJsonSerializer();
this.serializer = new CompactedJsonLdSerializer(jsonWriter);
}
// The following tests only verify validity of the output JSON-LD, no structure checks are performed
@Test
public void testSerializeObjectWithDataProperties() throws Exception {
final User user = Generator.generateUser();
serializer.serialize(user);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
assertNotNull(jsonObject);
}
@Test
public void testSerializeCollectionOfObjects() throws Exception {
final Set<User> users = Generator.generateUsers();
serializer.serialize(users);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
assertNotNull(jsonObject);
}
@Test
public void testSerializeObjectWithSingularReference() throws Exception {
final Employee employee = Generator.generateEmployee();
serializer.serialize(employee);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
assertNotNull(jsonObject);
}
@Test
public void testSerializeObjectWithPluralReference() throws Exception {
final Organization org = Generator.generateOrganization();
generateEmployees(org, false); // No backward references for this test
serializer.serialize(org);
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
assertNotNull(jsonObject);
}
private void generateEmployees(Organization org, boolean withBackwardReference) {
for (int i = 0; i < Generator.randomCount(10); i++) {
final Employee emp = Generator.generateEmployee();
emp.setEmployer(withBackwardReference ? org : null);
org.addEmployee(emp);
}
}
@Test
public void testSerializeObjectWithBackwardReferences() throws Exception {
final Organization org = Generator.generateOrganization();
generateEmployees(org, true);
serializer.serialize(org);
System.out.println(jsonWriter.getResult());
Object jsonObject = JsonUtils.fromString(jsonWriter.getResult());
assertNotNull(jsonObject);
}
}