/
Neo4jRichVersionFactoryTest.java
166 lines (130 loc) · 6.34 KB
/
Neo4jRichVersionFactoryTest.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package edu.berkeley.ground.dao.models.neo4j;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import edu.berkeley.ground.dao.Neo4jTest;
import edu.berkeley.ground.model.models.RichVersion;
import edu.berkeley.ground.model.models.Tag;
import edu.berkeley.ground.model.versions.GroundType;
import edu.berkeley.ground.exceptions.GroundException;
import static org.junit.Assert.*;
public class Neo4jRichVersionFactoryTest extends Neo4jTest {
public Neo4jRichVersionFactoryTest() throws GroundException {
super();
}
@Test
public void testReference() throws GroundException {
try {
/* Create a NodeVersion because Neo4j's rich version factory looks for an existing version with this id */
long testNodeId = super.factories.getNodeFactory().create("testNode", null, new HashMap<>())
.getId();
long id = super.createNodeVersion(testNodeId);
String testReference = "http://www.google.com";
Map<String, String> parameters = new HashMap<>();
parameters.put("http", "GET");
super.richVersionFactory.insertIntoDatabase(id, new HashMap<>(), -1, testReference, parameters);
RichVersion retrieved = super.richVersionFactory.retrieveFromDatabase(id);
assertEquals(id, retrieved.getId());
assertEquals(testReference, retrieved.getReference());
Map<String, String> retrievedParams = retrieved.getParameters();
for (String key : parameters.keySet()) {
assert (retrievedParams).containsKey(key);
assertEquals(parameters.get(key), retrievedParams.get(key));
}
super.neo4jClient.commit();
} finally {
super.neo4jClient.abort();
}
}
@Test
public void testTags() throws GroundException {
try {
/* Create a NodeVersion because Neo4j's rich version factory looks for an existing version with this id */
long testNodeId = super.factories.getNodeFactory().create("testNode", null, new HashMap<>())
.getId();
long id = super.createNodeVersion(testNodeId);
Map<String, Tag> tags = new HashMap<>();
tags.put("justkey", new Tag(-1, "justkey", null, null));
tags.put("withintvalue", new Tag(-1, "withintvalue", 1, GroundType.INTEGER));
tags.put("withstringvalue", new Tag(-1, "withstringvalue", "1", GroundType.STRING));
tags.put("withboolvalue", new Tag(-1, "withboolvalue", true, GroundType.BOOLEAN));
super.richVersionFactory.insertIntoDatabase(id, tags, -1, null, new HashMap<>());
RichVersion retrieved = super.richVersionFactory.retrieveFromDatabase(id);
assertEquals(id, retrieved.getId());
assertEquals(tags.size(), retrieved.getTags().size());
Map<String, Tag> retrievedTags = retrieved.getTags();
for (String key : tags.keySet()) {
assert (retrievedTags).containsKey(key);
assertEquals(tags.get(key), retrievedTags.get(key));
assertEquals(retrieved.getId(), retrievedTags.get(key).getId());
}
super.neo4jClient.commit();
} finally {
super.neo4jClient.abort();
}
}
@Test
public void testStructureVersionConformation() throws GroundException {
try {
/* Create a NodeVersion because Neo4j's rich version factory looks for an existing * version with this id */
long testNodeId = super.factories.getNodeFactory().create("testNode", null, new HashMap<>())
.getId();
long id = super.createNodeVersion(testNodeId);
String structureName = "testStructure";
long structureId = super.factories.getStructureFactory().create(structureName, null,
new HashMap<>()).getId();
Map<String, GroundType> structureVersionAttributes = new HashMap<>();
structureVersionAttributes.put("intfield", GroundType.INTEGER);
structureVersionAttributes.put("boolfield", GroundType.BOOLEAN);
structureVersionAttributes.put("strfield", GroundType.STRING);
long structureVersionId = super.factories.getStructureVersionFactory().create(
structureId, structureVersionAttributes, new ArrayList<>()).getId();
Map<String, Tag> tags = new HashMap<>();
tags.put("intfield", new Tag(-1, "intfield", 1, GroundType.INTEGER));
tags.put("strfield", new Tag(-1, "strfield", "1", GroundType.STRING));
tags.put("boolfield", new Tag(-1, "boolfield", true, GroundType.BOOLEAN));
super.richVersionFactory.insertIntoDatabase(id, tags, structureVersionId, null,
new HashMap<>());
RichVersion retrieved = super.richVersionFactory.retrieveFromDatabase(id);
assertEquals(retrieved.getStructureVersionId(), structureVersionId);
super.neo4jClient.commit();
} finally {
super.neo4jClient.abort();
}
}
@Test(expected = GroundException.class)
public void testStructureVersionFails() throws GroundException {
long structureVersionId = -1;
try {
/* Create a NodeVersion because Neo4j's rich version factory looks for an existing version with this id */
long testNodeId = super.factories.getNodeFactory().create("testNode", null, new HashMap<>())
.getId();
long id = super.createNodeVersion(testNodeId);
// none of these operations should fail
try {
String structureName = "testStructure";
long structureId = super.factories.getStructureFactory().create(structureName, null,
new HashMap<>()).getId();
Map<String, GroundType> structureVersionAttributes = new HashMap<>();
structureVersionAttributes.put("intfield", GroundType.INTEGER);
structureVersionAttributes.put("boolfield", GroundType.BOOLEAN);
structureVersionAttributes.put("strfield", GroundType.STRING);
structureVersionId = super.factories.getStructureVersionFactory().create(
structureId, structureVersionAttributes, new ArrayList<>()).getId();
} catch (GroundException ge) {
fail(ge.getMessage());
}
Map<String, Tag> tags = new HashMap<>();
tags.put("intfield", new Tag(-1, "intfield", 1, GroundType.INTEGER));
tags.put("intfield", new Tag(-1, "strfield", "1", GroundType.STRING));
tags.put("intfield", new Tag(-1, "boolfield", true, GroundType.BOOLEAN));
// this should fail
super.richVersionFactory.insertIntoDatabase(id, tags, structureVersionId, null,
new HashMap<>());
super.neo4jClient.commit();
} finally {
super.neo4jClient.abort();
}
}
}