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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
/**
* ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
* Copyright © 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*
* ECOMP is a trademark and service mark of AT&T Intellectual Property.
*/
package org.onap.aai.query.builder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.junit.Test;
import org.onap.aai.db.props.AAIProperties;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.serialization.db.EdgeType;
public class TraversalQueryTest extends QueryBuilderTestAbstraction {
@Override
protected QueryBuilder<Edge> getNewEdgeTraversal(Vertex v) {
return new TraversalQuery<>(loader, g, v, testEdgeRules);
}
@Override
protected QueryBuilder<Edge> getNewEdgeTraversal() {
return new TraversalQuery<>(loader, g, testEdgeRules);
}
@Override
protected QueryBuilder<Vertex> getNewVertexTraversal(Vertex v) {
return new TraversalQuery<>(loader, g, v, testEdgeRules);
}
@Override
protected QueryBuilder<Vertex> getNewVertexTraversal() {
return new TraversalQuery<>(loader, g, testEdgeRules);
}
@Test
public void unionQuery() {
QueryBuilder<Vertex> tQ = new TraversalQuery<>(loader, g);
QueryBuilder<Vertex> tQ2 = new TraversalQuery<>(loader, g);
QueryBuilder<Vertex> tQ3 = new TraversalQuery<>(loader, g);
tQ.union(
tQ2.getVerticesByProperty("test1", "value1"),
tQ3.getVerticesByIndexedProperty("test2", "value2"));
GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start()
.union(__.has("test1", "value1"),__.has("test2", "value2"));
assertEquals("they are equal", expected, tQ.getQuery());
g.tx().rollback();
}
@Test
public void traversalClones() throws UnsupportedEncodingException, AAIException, URISyntaxException {
QueryBuilder<Vertex> tQ = new TraversalQuery<>(loader, g);
QueryBuilder<Vertex> builder = tQ.createQueryFromURI(new URI("network/test-objects/test-object/key1")).getQueryBuilder();
GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("vnf-id", "key1").has("aai-node-type", "test-object");
GraphTraversal<Vertex, Vertex> containerExpected = __.<Vertex>start().has("aai-node-type", "test-object");
assertEquals("query object", expected.toString(), builder.getQuery().toString());
assertEquals("container query object", containerExpected.toString(), builder.getContainerQuery().getQuery().toString());
g.tx().rollback();
}
@Test
public void nestedTraversalClones() throws UnsupportedEncodingException, AAIException, URISyntaxException {
QueryBuilder<Vertex> tQ = new TraversalQuery<>(loader, g);
QueryBuilder<Vertex> builder = tQ.createQueryFromURI(new URI("network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2")).getQueryBuilder();
GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("vnf-id", "key1").has("aai-node-type", "generic-vnf").out("hasLInterface").has(AAIProperties.NODE_TYPE, "l-interface").has("interface-name", "key2");
GraphTraversal<Vertex, Vertex> containerExpected = __.<Vertex>start().has("vnf-id", "key1").has("aai-node-type", "generic-vnf").out("hasLInterface").has(AAIProperties.NODE_TYPE, "l-interface");
assertEquals("query object", expected.toString(), builder.getQuery().toString());
assertEquals("container query object", containerExpected.toString(), builder.getContainerQuery().getQuery().toString());
g.tx().rollback();
}
@Test
public void abstractEdgeToVertexTraversalTest() throws AAIException {
Vertex gvnf = g.addV("aai-node-type","generic-vnf","vnf-id","gvnf").next();
Vertex vnfc1 = g.addV("aai-node-type","vnfc","vnfc-name","a-name").next();
testEdgeRules.addEdge(g, gvnf, vnfc1);
QueryBuilder<Vertex> tQ = getNewVertexTraversal(gvnf);
tQ.createEdgeTraversal(EdgeType.COUSIN, "vnf", "vnfc");
List<Vertex> list = tQ.toList();
assertEquals("Has 1 vertexes ", 1, list.size());
assertTrue("Has vertex on the default edge ", list.contains(vnfc1));
g.tx().rollback();
}
@Test
public void abstractEdgeToVertexTraversalSingleOutRuleTest() throws AAIException {
Vertex vce = g.addV("aai-node-type","vce","vnf-id","vce").next();
Vertex vnfc1 = g.addV("aai-node-type","vnfc","vnfc-name","a-name").next();
testEdgeRules.addEdge(g, vce, vnfc1);
QueryBuilder<Vertex> tQ1 = new TraversalQuery<>(loader, g, vce, testEdgeRules);
tQ1.createEdgeTraversal(EdgeType.COUSIN, "vnf", "vnfc");
QueryBuilder<Vertex> tQ2 = new TraversalQuery<>(loader, g, vnfc1, testEdgeRules);
tQ2.createEdgeTraversal(EdgeType.COUSIN, "vnfc", "vnf");
List<Vertex> list1 = tQ1.toList();
List<Vertex> list2 = tQ2.toList();
assertEquals("1 - Has 1 vertexes ", 1, list1.size());
assertTrue("1 - traversal results in vnfc ", list1.contains(vnfc1));
assertEquals("2 - Has 1 vertexes ", 1, list2.size());
assertTrue("2 - traversal results in vce ", list2.contains(vce));
g.tx().rollback();
}
@Test
public void abstractEdgeToVertexTraversalSingleInRuleTest() throws AAIException {
Vertex vce = g.addV("aai-node-type","vce","vnf-id","vce").next();
Vertex pserver = g.addV("aai-node-type","pserver","hostname","a-name").next();
testEdgeRules.addEdge(g, vce, pserver);
QueryBuilder<Vertex> tQ1 = new TraversalQuery<>(loader, g, vce, testEdgeRules);
tQ1.createEdgeTraversal(EdgeType.COUSIN, "vnf", "pserver");
List<Vertex> list = tQ1.toList();
assertEquals("1 - Has 1 vertexes ", 1, list.size());
assertTrue("1 - traversal results in vnfc ", list.contains(pserver));
g.tx().rollback();
}
@Test
public void abstractEdgeToVertexMultiRuleTraversalTest() throws AAIException {
Vertex gvnf = g.addV("aai-node-type","generic-vnf","vnf-id","gvnf").next();
Vertex vnfc1 = g.addV("aai-node-type","vnfc","vnfc-name","a-name").next();
Vertex vnfc2 = g.addV("aai-node-type","vnfc","vnfc-name","b-name").next();
testEdgeRules.addEdge(g, gvnf, vnfc1);
testEdgeRules.addEdge(g, gvnf, vnfc2, "re-uses");
QueryBuilder<Vertex> tQ = getNewVertexTraversal(gvnf);
tQ.createEdgeTraversal(EdgeType.COUSIN, "vnf", "vnfc");
List<Vertex> list = tQ.toList();
assertEquals("Has 2 vertexes ", 2, list.size());
assertTrue("Has vertex on the default edge ", list.contains(vnfc1));
assertTrue("Has vertex on the re-uses edge ", list.contains(vnfc2));
g.tx().rollback();
}
@Test
public void abstractEdgeToVertexMultiRuleOutTraversalTest() throws AAIException {
Vertex gvnf = g.addV("aai-node-type","generic-vnf","vnf-id","gvnf").next();
Vertex pserver = g.addV("aai-node-type","pserver","hostname","a-name").next();
testEdgeRules.addEdge(g, gvnf, pserver);
testEdgeRules.addEdge(g, gvnf, pserver, "generic-vnf-pserver-B");
QueryBuilder<Vertex> tQ = getNewVertexTraversal(gvnf);
tQ.createEdgeTraversal(EdgeType.COUSIN, "vnf", "pserver");
List<Vertex> list = tQ.toList();
assertEquals("Has 2 vertexes ", 2, list.size());
assertTrue("result has pserver ", list.contains(pserver));
g.tx().rollback();
}
@Test
public void abstractEdgeToVertexMultiRuleInTraversalTest() throws AAIException {
Vertex gvnf = g.addV("aai-node-type","generic-vnf","vnf-id","gvnf").next();
Vertex complex = g.addV("aai-node-type","complex","physical-location-id","a-name").next();
testEdgeRules.addEdge(g, gvnf, complex);
testEdgeRules.addEdge(g, gvnf, complex, "complex-generic-vnf-B");
QueryBuilder<Vertex> tQ = getNewVertexTraversal(gvnf);
tQ.createEdgeTraversal(EdgeType.COUSIN, "vnf", "complex");
List<Vertex> list = tQ.toList();
assertEquals("Has 2 vertexes ", 2, list.size());
assertTrue("result has pserver ", list.contains(complex));
g.tx().rollback();
}
}
|