-
Notifications
You must be signed in to change notification settings - Fork 2
/
SingularObjectContextTest.java
122 lines (109 loc) · 6.19 KB
/
SingularObjectContextTest.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
* version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jsonld.deserialization;
import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
import cz.cvut.kbss.jsonld.environment.Generator;
import cz.cvut.kbss.jsonld.environment.Vocabulary;
import cz.cvut.kbss.jsonld.environment.model.Employee;
import cz.cvut.kbss.jsonld.environment.model.Organization;
import cz.cvut.kbss.jsonld.environment.model.Person;
import cz.cvut.kbss.jsonld.environment.model.User;
import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.net.URI;
import java.util.Collections;
import java.util.Map;
import static org.junit.Assert.*;
public class SingularObjectContextTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void setFieldValueSetsFieldValueOnInstance() throws Exception {
final SingularObjectContext<Person> ctx = new SingularObjectContext<>(new Person(),
BeanAnnotationProcessor.mapSerializableFields(
Person.class), Collections.emptyMap());
final String testFirstName = "John";
ctx.setFieldValue(Person.class.getDeclaredField("firstName"), testFirstName);
assertEquals(testFirstName, ctx.getInstance().getFirstName());
}
@Test
public void setFieldValueThrowsDeserializationExceptionWhenInvalidTypeIsUsedAsFieldValue() throws Exception {
final Integer invalidValue = 117;
thrown.expect(JsonLdDeserializationException.class);
thrown.expectMessage("Type mismatch. Cannot set value " + invalidValue + " of type " + invalidValue.getClass() +
" on field " + User.class.getDeclaredField("admin"));
final SingularObjectContext<Person> ctx = new SingularObjectContext<>(new User(),
BeanAnnotationProcessor.mapSerializableFields(
User.class), Collections.emptyMap());
ctx.setFieldValue(User.class.getDeclaredField("admin"), invalidValue);
}
@Test
public void setFieldValueSetsReferenceToAlreadyVisitedObjectWhenObjectIdIsPassedIn() throws Exception {
final Organization org = Generator.generateOrganization();
final Map<String, Object> knownInstances = Collections.singletonMap(org.getUri().toString(), org);
final SingularObjectContext<Employee> ctx = new SingularObjectContext<>(new Employee(),
BeanAnnotationProcessor.mapSerializableFields(Employee.class),
knownInstances);
ctx.setFieldValue(Employee.class.getDeclaredField("employer"), org.getUri().toString());
assertNotNull(ctx.getInstance().getEmployer());
assertSame(org, ctx.getInstance().getEmployer());
}
@Test
public void setFieldValueThrowsDeserializationExceptionWhenUnknownObjectIdIsPassedIn() throws Exception {
final Organization org = Generator.generateOrganization();
thrown.expect(JsonLdDeserializationException.class);
thrown.expectMessage("Type mismatch. Cannot set value " + org.getUri().toString() + " of type " + String.class +
" on field " + Employee.class.getDeclaredField("employer"));
final SingularObjectContext<Employee> ctx = new SingularObjectContext<>(new Employee(),
BeanAnnotationProcessor.mapSerializableFields(Employee.class),
Collections.emptyMap());
ctx.setFieldValue(Employee.class.getDeclaredField("employer"), org.getUri().toString());
}
@Test
public void setFieldValueThrowsDeserializationExceptionWhenIdOfInstanceWithInvalidTypeIsPassedIn()
throws Exception {
final User u = Generator.generateUser();
final Map<String, Object> knownInstances = Collections.singletonMap(u.getUri().toString(), u);
thrown.expect(JsonLdDeserializationException.class);
thrown.expectMessage("Type mismatch. Cannot set value " + u + " of type " + u.getClass() + " on field " +
Employee.class.getDeclaredField("employer"));
final SingularObjectContext<Employee> ctx = new SingularObjectContext<>(new Employee(),
BeanAnnotationProcessor.mapSerializableFields(Employee.class),
knownInstances);
ctx.setFieldValue(Employee.class.getDeclaredField("employer"), u.getUri().toString());
}
@Test
public void setFieldValueHandlesConversionFromStringToUri() throws Exception {
final URI id = Generator.generateUri();
final SingularObjectContext<Person> ctx = new SingularObjectContext<>(new Person(),
BeanAnnotationProcessor.mapSerializableFields(
Person.class), Collections.emptyMap());
ctx.setFieldValue(Person.class.getDeclaredField("uri"), id.toString());
assertEquals(id, ctx.getInstance().getUri());
}
@Test
public void isPropertyMappedReturnsTrueForUnmappedPropertyWhenClassContainsPropertiesField() {
final SingularObjectContext<Person> ctx = new SingularObjectContext<>(new Person(),
BeanAnnotationProcessor.mapSerializableFields(Person.class), Collections.emptyMap());
assertTrue(ctx.isPropertyMapped(Vocabulary.IS_ADMIN));
}
@Test
public void isPropertyMappedReturnsFalseForUnknownProperty() {
final SingularObjectContext<Organization> ctx = new SingularObjectContext<>(new Organization(),
BeanAnnotationProcessor.mapSerializableFields(
Organization.class), Collections.emptyMap());
assertFalse(ctx.isPropertyMapped(Vocabulary.IS_ADMIN));
}
}