-
Notifications
You must be signed in to change notification settings - Fork 2
/
PropertiesInstanceContextTest.java
130 lines (113 loc) · 5.26 KB
/
PropertiesInstanceContextTest.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
123
124
125
126
127
128
129
130
/**
* Copyright (C) 2017 Czech Technical University in Prague
*
* 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.
*
* 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.jopa.model.annotations.Properties;
import cz.cvut.kbss.jsonld.environment.Vocabulary;
import cz.cvut.kbss.jsonld.environment.model.Person;
import cz.cvut.kbss.jsonld.exception.JsonLdDeserializationException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class PropertiesInstanceContextTest {
private static final String VALUE = "halsey@unsc.org";
private Field personProperties;
@Rule
public ExpectedException thrown = ExpectedException.none();
@Before
public void setUp() throws Exception {
this.personProperties = Person.class.getDeclaredField("properties");
}
@Test
public void addItemInsertsCollectionIntoPropertiesMapAndAddsValueToIt() {
final Map<String, Set<String>> map = new HashMap<>();
final InstanceContext<Map> ctx = new PropertiesInstanceContext(map, Vocabulary.USERNAME, personProperties);
ctx.addItem(VALUE);
assertTrue(map.containsKey(Vocabulary.USERNAME));
assertEquals(1, map.get(Vocabulary.USERNAME).size());
assertTrue(map.get(Vocabulary.USERNAME).contains(VALUE));
}
@Test
public void addItemReusesPropertyCollectionWhenItIsAlreadyPresentInPropertiesMap() {
final Map<String, Set<String>> map = new HashMap<>();
map.put(Vocabulary.USERNAME, new HashSet<>());
map.get(Vocabulary.USERNAME).add(VALUE);
final InstanceContext<Map> ctx = new PropertiesInstanceContext(map, Vocabulary.USERNAME, personProperties);
final String newValue = "halsey@oni.org";
ctx.addItem(newValue);
assertEquals(2, map.get(Vocabulary.USERNAME).size());
assertTrue(map.get(Vocabulary.USERNAME).contains(VALUE));
assertTrue(map.get(Vocabulary.USERNAME).contains(newValue));
}
@Test
public void addItemPutsSingleValueIntoPropertiesMapWhenMapIsConfiguredAsSingleValued() throws Exception {
final Map<?, ?> map = new HashMap<>();
final InstanceContext<Map> ctx = new PropertiesInstanceContext(map, Vocabulary.USERNAME,
SingleValued.class.getDeclaredField("properties"));
ctx.addItem(VALUE);
assertEquals(VALUE, map.get(Vocabulary.USERNAME));
}
private static class SingleValued {
@Properties
private Map<String, String> properties;
}
@Test
public void addItemConvertsPropertyIdentifierToCorrectType() throws Exception {
final Map<URI, Set<?>> map = new HashMap<>();
final InstanceContext<Map> ctx = new PropertiesInstanceContext(map, Vocabulary.USERNAME,
TypedProperties.class.getDeclaredField("properties"));
ctx.addItem(VALUE);
assertTrue(map.containsKey(URI.create(Vocabulary.USERNAME)));
}
private static class TypedProperties {
@Properties
private Map<URI, Set<?>> properties;
}
@Test
public void addItemInsertsValuesOfCorrectTypes() throws Exception {
final Map<URI, Set<?>> map = new HashMap<>();
final InstanceContext<Map> ctx = new PropertiesInstanceContext(map, Vocabulary.IS_ADMIN,
TypedProperties.class.getDeclaredField("properties"));
ctx.addItem(true);
assertEquals(Boolean.TRUE, map.get(URI.create(Vocabulary.IS_ADMIN)).iterator().next());
}
@Test
public void addItemConvertsValuesToCorrectType() {
final Map<String, Set<String>> map = new HashMap<>();
final InstanceContext<Map> ctx = new PropertiesInstanceContext(map, Vocabulary.IS_ADMIN, personProperties);
ctx.addItem(true);
assertEquals(Boolean.TRUE.toString(), map.get(Vocabulary.IS_ADMIN).iterator().next());
}
@Test
public void addItemThrowsExceptionWhenMultipleItemsForSingularPropertyAreAdded() throws Exception {
final Map<String, String> map = new HashMap<>();
final InstanceContext<Map> ctx = new PropertiesInstanceContext(map, Vocabulary.USERNAME,
SingleValued.class.getDeclaredField("properties"));
thrown.expect(JsonLdDeserializationException.class);
thrown.expectMessage(
containsString("Encountered multiple values of property " + Vocabulary.USERNAME));
ctx.addItem(VALUE);
ctx.addItem("halsey@oni.org");
}
}