1 package org.onap.crud.dao.champ;
3 import org.junit.After;
4 import org.junit.Before;
5 import org.junit.Ignore;
7 import org.onap.aai.champcore.graph.impl.InMemoryChampGraphImpl;
8 import org.onap.crud.dao.GraphDao;
9 import org.onap.crud.entity.Edge;
10 import org.onap.crud.entity.Vertex;
11 import org.onap.crud.exception.CrudException;
13 import java.util.HashMap;
14 import java.util.List;
17 import static org.junit.Assert.*;
21 * This suite of tests validates the basic functionality of the {@link ChampDao}.
23 public class ChampDaoTest {
25 private static final String GRAPH_NAME = "my_test_graph";
27 private GraphDao champDao = null;
31 * Perform setup steps that must be done prior to executing each test.
36 // Create an instance of the Champ DAO, backed by the Champ library's in-memory back end
37 // for testing purposes.
38 Map<String, Object> champDaoProperties = new HashMap<String, Object>();
39 champDaoProperties.put(ChampDao.CONFIG_STORAGE_BACKEND, "in-memory");
40 champDaoProperties.put(ChampDao.CONFIG_GRAPH_NAME, GRAPH_NAME);
41 champDao = new ChampDao(new InMemoryChampGraphImpl.Builder().properties(champDaoProperties).build());
46 * Perform tear down steps that must be done after executing each test.
49 public void tearDown() {
51 // Release the Champ DAO instance that we were using for the test.
52 if (champDao != null) {
53 ((ChampDao) champDao).close();
59 * Tests the ability of the {@link ChampDao} to create a vertex.
61 * @throws CrudException
64 public void createVertexTest() throws CrudException {
66 String VERTEX_TYPE = "Test_Vertex";
68 Map<String, Object> properties = new HashMap<String, Object>();
69 properties.put("property1", "something");
70 properties.put("property2", "something else");
73 Vertex createdVertex = champDao.addVertex(VERTEX_TYPE, properties);
75 // Validate that the returned {@link Vertex} has the right label assigned to it.
76 assertTrue("Unexpected vertex type '" + createdVertex.getType() + "' returned from DAO",
77 createdVertex.getType().equals(VERTEX_TYPE));
79 // Validate that all of the properties that we provided to the DAO are in fact assigned
80 // to the {@link Vertex} that we got back.
81 assertTrue("Vertex property list returned from DAO did not contain all expected properties - expected: " +
82 properties.keySet() + " actual: " + createdVertex.getProperties().keySet(),
83 createdVertex.getProperties().keySet().containsAll(properties.keySet()));
85 // Validate that the values assigned to the properties in the returned {@link Vertex}
86 // match the ones that we provided.
87 for (String propertyKey : properties.keySet()) {
89 assertTrue(createdVertex.getProperties().get(propertyKey).equals(properties.get(propertyKey)));
95 * Tests the ability of the {@link ChampDao} to retrieve a vertex from the graph data store
96 * by its unique identifier.
98 * @throws CrudException
101 public void getVertexByIdTest() throws CrudException {
103 String VERTEX_TYPE = "Test_Vertex";
105 Map<String, Object> properties = new HashMap<String, Object>();
106 properties.put("property1", "something");
107 properties.put("property2", "something else");
109 // Create the vertex.
110 Vertex createdVertex = champDao.addVertex(VERTEX_TYPE, properties);
112 // Make sure the {@link Vertex} returned from the create method includes an id that we can
113 // use to retrieve it.
114 assertTrue("No valid id returned for the created vertex", createdVertex.getId().isPresent());
116 // Now, retrieve the {@link Vertex} by its identifier.
117 Vertex retrievedVertex = champDao.getVertex(createdVertex.getId().get(), VERTEX_TYPE);
119 // Validate that the retrieved {@link Vertex} has the right label assigned to it.
120 assertTrue("Unexpected vertex type '" + retrievedVertex.getType() + "' returned from DAO",
121 retrievedVertex.getType().equals(VERTEX_TYPE));
123 // Validate that all of the properties that we provided when we created the {@link Vertex}
124 // are present in the {@link Vertex} that we retrieved.
125 assertTrue("Vertex property list returned from DAO did not contain all expected properties - expected: " +
126 properties.keySet() + " actual: " + retrievedVertex.getProperties().keySet(),
127 retrievedVertex.getProperties().keySet().containsAll(properties.keySet()));
129 // Validate that the values assigned to the properties in the retrieved {@link Vertex}
130 // match the ones that we provided when we created it.
131 for (String propertyKey : properties.keySet()) {
133 assertTrue(retrievedVertex.getProperties().get(propertyKey).equals(properties.get(propertyKey)));
139 * Tests the ability of the {@link ChampDao} to update an already existing vertex.
141 * @throws CrudException
144 public void updateVertexTest() throws CrudException {
146 final String VERTEX_TYPE = "Test_Vertex";
148 Map<String, Object> properties = new HashMap<String, Object>();
149 properties.put("property1", "something");
150 properties.put("property2", "something else");
152 // Create the vertex.
153 Vertex createdVertex = champDao.addVertex(VERTEX_TYPE, properties);
155 // Make sure the {@link Vertex} returned from the create method includes an id that we can
156 // use to retrieve it.
157 assertTrue("No valid id returned for the created vertex", createdVertex.getId().isPresent());
159 // Modify the properties list...
160 properties.put("property3", "a new property");
161 properties.remove("property1");
163 // ...and apply it to our vertex.
164 Vertex updatedVertex = champDao.updateVertex(createdVertex.getId().get(), createdVertex.getType(), properties);
166 assertTrue("Vertex property list returned from DAO update operation did not contain all expected properties - expected: " +
167 properties.keySet() + " actual: " + updatedVertex.getProperties().keySet(),
168 updatedVertex.getProperties().keySet().containsAll(properties.keySet()));
170 // Validate that the values assigned to the properties in the updated {@link Vertex}
171 // match the ones that we provided when we created it.
172 for (String propertyKey : properties.keySet()) {
174 assertTrue("Unexpected value for property '" + propertyKey + "' - Expected: " +
175 properties.get(propertyKey) + " Actual: " +
176 updatedVertex.getProperties().get(propertyKey),
177 updatedVertex.getProperties().get(propertyKey).equals(properties.get(propertyKey)));
180 // Validate that the property that we removed is NOT in the set of properties from our
181 // updated {@link Vertex}.
182 assertFalse("Property 'property1' should no longer be associated with updated vertex",
183 updatedVertex.getProperties().containsKey("property1"));
188 * Tests the ability of the {@link ChampDao} to retrieve multiple vertices which match
189 * a particular set of supplied properties.
191 * @throws CrudException
194 public void getVerticesTest() throws CrudException {
196 final String FIRST_VERTEX_TYPE = "pserver";
197 final String SECOND_VERTEX_TYPE = "complex";
199 // Create some vertices.
201 Map<String, Object> vertex1Properties = new HashMap<String, Object>();
202 vertex1Properties.put("O/S", "Linux");
203 vertex1Properties.put("version", "6.5");
204 vertex1Properties.put("hostname", "kll0001");
205 champDao.addVertex(FIRST_VERTEX_TYPE, vertex1Properties);
207 Map<String, Object> vertex2Properties = new HashMap<String, Object>();
208 vertex2Properties.put("O/S", "Linux");
209 vertex2Properties.put("version", "6.5");
210 vertex2Properties.put("hostname", "kll0002");
211 champDao.addVertex(FIRST_VERTEX_TYPE, vertex2Properties);
213 Map<String, Object> vertex3Properties = new HashMap<String, Object>();
214 vertex3Properties.put("O/S", "Linux");
215 vertex3Properties.put("version", "7.2");
216 vertex3Properties.put("hostname", "kll0003");
217 champDao.addVertex(FIRST_VERTEX_TYPE, vertex3Properties);
219 Map<String, Object> vertex4Properties = new HashMap<String, Object>();
220 vertex4Properties.put("O/S", "Windows");
221 vertex4Properties.put("version", "10");
222 vertex4Properties.put("hostname", "Dev Laptop");
223 champDao.addVertex(FIRST_VERTEX_TYPE, vertex4Properties);
225 Map<String, Object> vertex5Properties = new HashMap<String, Object>();
226 vertex5Properties.put("Street", "Baker");
227 vertex5Properties.put("Number", "222B");
228 champDao.addVertex(SECOND_VERTEX_TYPE, vertex5Properties);
230 // Create a set of properties to use for our query.
231 Map<String, Object> queryProperties = new HashMap<String, Object>();
232 queryProperties.put("O/S", "Linux");
233 queryProperties.put("version", "6.5");
235 // Validate that we filter our 'get vertices' results by type
236 List<Vertex> allVerticesByType = champDao.getVertices(FIRST_VERTEX_TYPE, MapBuilder.builder().build());
237 for (Vertex v : allVerticesByType) {
238 assertTrue("Unexpected vertex type returned from query. Expected: " +
239 FIRST_VERTEX_TYPE + " Actual: " + v.getType(),
240 v.getType().equals(FIRST_VERTEX_TYPE));
243 // Now, request the vertices that match our parameters.
244 List<Vertex> vertices = champDao.getVertices(FIRST_VERTEX_TYPE, queryProperties);
246 // Validate that got back the expected number of vertices.
247 assertEquals(vertices.size(), 2);
249 // Validate that the vertices we got back contain the expected parameters.
250 for (Vertex v : vertices) {
252 assertTrue("Vertex from query result does not contain expected vertex 'O/S'",
253 v.getProperties().containsKey("O/S"));
254 assertTrue("Vertex from query result contains unexpected value for 'O/S' parameter - Expected: 'Linux' Actual: '" +
255 v.getProperties().get("O/S") + "'",
256 v.getProperties().get("O/S").equals("Linux"));
258 assertTrue("Vertex from query result does not contain expected vertex 'O/S'",
259 v.getProperties().containsKey("version"));
260 assertTrue("Vertex from query result contains unexpected value for 'O/S' parameter - Expected: 'Linux' Actual: '" +
261 v.getProperties().get("O/S") + "'",
262 v.getProperties().get("version").equals("6.5"));
267 public void deleteVertexTest() throws CrudException {
269 boolean deletedVertexNotFound = false;
272 Vertex createdVertex = champDao.addVertex("test_type", MapBuilder.builder()
273 .withKeyValue("O/S", "Linux")
274 .withKeyValue("version", "6.5")
275 .withKeyValue("hostname", "kll0001")
278 // Verify that we can retrieve the vertex from the graph data base.
279 Vertex retrievedVertex = champDao.getVertex(createdVertex.getId().get(), "test_type");
281 // Now, delete the vertex.
282 champDao.deleteVertex(createdVertex.getId().get(), "test_type");
284 // Now, try to retrieve it again. This time we should fail to find it.
286 champDao.getVertex(createdVertex.getId().get(), "test_type");
288 } catch (CrudException e) {
289 assertTrue(e.getMessage().contains("No vertex with id"));
290 deletedVertexNotFound = true;
293 assertTrue("Should not have been able to retrieve deleted vertex", deletedVertexNotFound);
297 public void createEdgeTest() throws CrudException {
299 String EDGE_TYPE = "has";
301 // Create the source vertex for the edge.
302 Map<String, Object> srcVertexProperties = new HashMap<String, Object>();
303 srcVertexProperties.put("O/S", "Linux");
304 srcVertexProperties.put("version", "6.5");
305 srcVertexProperties.put("hostname", "kll0001");
306 Vertex sourceVertex = champDao.addVertex("vserver", srcVertexProperties);
308 // Create the target vertex for the edge.
309 Map<String, Object> dstVertexProperties = new HashMap<String, Object>();
310 dstVertexProperties.put("O/S", "Linux");
311 dstVertexProperties.put("version", "6.5");
312 dstVertexProperties.put("hostname", "kll0002");
313 Vertex destVertex = champDao.addVertex("VNF", dstVertexProperties);
315 // Now, create the edge itself.
316 Map<String, Object> edgeProperties = new HashMap<String, Object>();
317 edgeProperties.put("prop", "val");
318 Edge createdEdge = champDao.addEdge("has", sourceVertex, destVertex, edgeProperties);
320 // Validate that the Edge object returned from the create method matches what we were
322 assertTrue("Unexpected type for Edge returned from create method. Expected: " + EDGE_TYPE
323 + " Actual: " + createdEdge.getType(),
324 createdEdge.getType().equals("has"));
325 assertTrue("Unexpected properties for Edge returned from create method. Expected: " + edgeProperties
326 + " Actual: " + createdEdge.getProperties(),
327 createdEdge.getProperties().equals(edgeProperties));
332 public void createEdgeWithMissingSrcOrTargetTest() throws CrudException {
334 String EDGE_TYPE = "has";
336 // Create the source vertex for the edge.
337 Map<String, Object> srcVertexProperties = new HashMap<String, Object>();
338 srcVertexProperties.put("O/S", "Linux");
339 srcVertexProperties.put("version", "6.5");
340 srcVertexProperties.put("hostname", "kll0001");
341 Vertex sourceVertex = champDao.addVertex("vserver", srcVertexProperties);
343 // Create the target vertex for the edge.
344 Map<String, Object> dstVertexProperties = new HashMap<String, Object>();
345 dstVertexProperties.put("O/S", "Linux");
346 dstVertexProperties.put("version", "6.5");
347 dstVertexProperties.put("hostname", "kll0002");
348 Vertex destVertex = champDao.addVertex("VNF", dstVertexProperties);
350 // Now, try creating the Edge but specify an id for the source vertex that does
352 Map<String, Object> edgeProperties = new HashMap<String, Object>();
353 edgeProperties.put("prop", "val");
355 champDao.addEdge(EDGE_TYPE, new Vertex.Builder("miss").id("99").build(), destVertex, edgeProperties);
356 } catch (CrudException e) {
357 assertTrue(e.getMessage().contains("Error creating edge - source vertex"));
360 // Now, try created the Edge with a valid source vertex, but specify an id for the
361 // target vertex that does not exist.
363 champDao.addEdge(EDGE_TYPE, sourceVertex, new Vertex.Builder("miss").id("99").build(), edgeProperties);
364 } catch (CrudException e) {
365 assertTrue(e.getMessage().contains("Error creating edge - target vertex"));
371 public void getEdgeByIdTest() throws CrudException {
373 String EDGE_TYPE = "has";
375 // Create the source vertex for the edge.
376 Map<String, Object> srcVertexProperties = new HashMap<String, Object>();
377 srcVertexProperties.put("O/S", "Linux");
378 srcVertexProperties.put("version", "6.5");
379 srcVertexProperties.put("hostname", "kll0001");
380 Vertex sourceVertex = champDao.addVertex("vserver", srcVertexProperties);
382 // Create the target vertex for the edge.
383 Map<String, Object> dstVertexProperties = new HashMap<String, Object>();
384 dstVertexProperties.put("O/S", "Linux");
385 dstVertexProperties.put("version", "6.5");
386 dstVertexProperties.put("hostname", "kll0002");
387 Vertex destVertex = champDao.addVertex("VNF", dstVertexProperties);
389 // Now, create the edge itself.
390 Map<String, Object> edgeProperties = new HashMap<String, Object>();
391 edgeProperties.put("prop", "val");
392 Edge createdEdge = champDao.addEdge("has", sourceVertex, destVertex, edgeProperties);
394 // Retrieve the edge we just created by specifying its unique identifier.
395 Edge retrievedEdge = champDao.getEdge(createdEdge.getId().get(), "has");
397 // Validate that the contents of the object that we got back matches what we thought we
399 assertTrue("Unexpected type for Edge returned from get method. Expected: " + EDGE_TYPE
400 + " Actual: " + retrievedEdge.getType(),
401 retrievedEdge.getType().equals(EDGE_TYPE));
402 assertTrue("Unexpected properties for Edge returned from get method. Expected: " + edgeProperties
403 + " Actual: " + retrievedEdge.getProperties(),
404 retrievedEdge.getProperties().equals(edgeProperties));
408 public void getEdgesTest() throws CrudException {
410 final String EDGE_TYPE_HAS = "has";
411 final String EDGE_TYPE_RUNS = "runs";
413 // Create some vertices and edges that we can query agains.
414 Vertex complex = champDao.addVertex("complex", MapBuilder.builder()
415 .withKeyValue("Province", "Ontario")
416 .withKeyValue("City", "Ottawa")
417 .withKeyValue("Street", "303 Terry Fox")
420 Vertex vserver = champDao.addVertex("vserver", MapBuilder.builder()
421 .withKeyValue("O/S", "Linux")
422 .withKeyValue("version", "6.5")
423 .withKeyValue("hostname", "kll0001")
426 Vertex vnf1 = champDao.addVertex("vserver", MapBuilder.builder()
427 .withKeyValue("Application", "OpenDaylight")
430 Vertex vnf2 = champDao.addVertex("vserver", MapBuilder.builder()
431 .withKeyValue("Application", "Cammunda")
434 Edge edge1 = champDao.addEdge(EDGE_TYPE_HAS, complex, vserver,
436 .withKeyValue("usesResource", "false")
437 .withKeyValue("hasDelTarget", "false")
440 Edge edge2 = champDao.addEdge(EDGE_TYPE_RUNS, vserver, vnf1,
442 .withKeyValue("usesResource", "false")
443 .withKeyValue("hasDelTarget", "true")
446 Edge edge3 = champDao.addEdge(EDGE_TYPE_RUNS, vserver, vnf2,
448 .withKeyValue("usesResource", "false")
449 .withKeyValue("hasDelTarget", "false")
452 // Query for all HAS edges.
453 List<Edge> hasEdges = champDao.getEdges(EDGE_TYPE_HAS, new HashMap<String, Object>());
455 assertEquals("Unexpected number of edges of type 'has' found. Expected: 1 Actual: " + hasEdges.size(),
457 assertTrue("Result of query for 'has' type edges does not contain the expected results",
458 containsEdge(edge1, hasEdges));
460 // Query for all RUNS edges.
461 List<Edge> runsEdges = champDao.getEdges(EDGE_TYPE_RUNS, new HashMap<String, Object>());
463 assertEquals("Unexpected number of edges of type 'runs' found. Expected: 2 Actual: " + runsEdges.size(),
464 runsEdges.size(), 2);
465 assertTrue("Result of query for 'runs' type edges does not contain the expected results",
466 containsEdge(edge2, runsEdges));
467 assertTrue("Result of query for 'runs' type edges does not contain the expected results",
468 containsEdge(edge2, runsEdges));
470 // Query for all HAS edges with the property 'hasDelTarget' equal to 'true'.
471 List<Edge> runsEdgesWithDelTargetTrue =
472 champDao.getEdges(EDGE_TYPE_RUNS, MapBuilder.builder()
473 .withKeyValue("hasDelTarget", "true")
476 assertEquals("Unexpected number of edges of type 'has' with 'hasDelTarget=true' found. Expected: 1 Actual: "
477 + runsEdgesWithDelTargetTrue.size(),
478 runsEdgesWithDelTargetTrue.size(), 1);
479 assertTrue("Result of query for 'runs' type edges with delTarget set to TRUE does not contain the expected results",
480 containsEdge(edge2, runsEdgesWithDelTargetTrue));
484 @Ignore // For now - pending some expected fixes to the Champ library.
485 public void updateEdgeTest() throws CrudException {
487 // Create the source vertex for the edge.
488 Vertex sourceVertex = champDao.addVertex("vserver", MapBuilder.builder()
489 .withKeyValue("O/S", "Linux")
490 .withKeyValue("version", "6.5")
491 .withKeyValue("hostname", "kll0001")
494 // Create the target vertex for the edge.
495 Vertex destVertex = champDao.addVertex("VNF", MapBuilder.builder()
496 .withKeyValue("O/S", "Linux")
497 .withKeyValue("version", "6.5")
498 .withKeyValue("hostname", "kll0002")
501 // Now, create the edge itself.
502 Edge createdEdge = champDao.addEdge("has",
506 .withKeyValue("key1", "value1")
507 .withKeyValue("key2", "value2")
508 .withKeyValue("key3", "value3")
511 // Make sure the Edge returned from the create method includes an id that we can
512 // use to retrieve it.
513 assertTrue("No valid id returned for the created edge", createdEdge.getId().isPresent());
515 // Retrieve the properties map for our edge and make some changes.
516 Map<String, Object> properties = createdEdge.getProperties();
517 properties.put("key4", "value4");
518 properties.remove("key2");
520 // Now update the edge with the new properties map.
521 Edge updatedEdge = champDao.updateEdge(createdEdge);
523 assertTrue("Edge property list returned from DAO update operation did not contain all expected properties - expected: " +
524 properties.keySet() + " actual: " + updatedEdge.getProperties().keySet(),
525 updatedEdge.getProperties().keySet().containsAll(properties.keySet()));
527 // Validate that the values assigned to the properties in the updated Edge
528 // match the ones that we provided when we created it.
529 for (String propertyKey : properties.keySet()) {
531 assertTrue("Unexpected value for property '" + propertyKey + "' - Expected: " +
532 properties.get(propertyKey) + " Actual: " +
533 updatedEdge.getProperties().get(propertyKey),
534 updatedEdge.getProperties().get(propertyKey).equals(properties.get(propertyKey)));
537 // Validate that the property that we removed is NOT in the set of properties from our
539 // *** We will leave this validation commented out for now, as the Champ library actually
540 // merges update properties instead of replacing them...
541 assertFalse("Property 'key2' should no longer be associated with updated edge",
542 updatedEdge.getProperties().containsKey("key2"));
546 public void deleteEdgeTest() throws CrudException {
548 boolean deletedEdgeNotFound = false;
550 // Create the source vertex for the edge.
551 Vertex sourceVertex = champDao.addVertex("vserver", MapBuilder.builder()
552 .withKeyValue("O/S", "Linux")
553 .withKeyValue("version", "6.5")
554 .withKeyValue("hostname", "kll0001")
557 // Create the target vertex for the edge.
558 Vertex destVertex = champDao.addVertex("VNF", MapBuilder.builder()
559 .withKeyValue("O/S", "Linux")
560 .withKeyValue("version", "6.5")
561 .withKeyValue("hostname", "kll0002")
564 // Now, create the edge itself.
565 Edge createdEdge = champDao.addEdge("has",
569 .withKeyValue("key1", "value1")
570 .withKeyValue("key2", "value2")
571 .withKeyValue("key3", "value3")
574 // Verify that we can retrieve the edge that we just created.
575 Edge retrievedEdge = champDao.getEdge(createdEdge.getId().get(), "has");
578 champDao.deleteEdge(createdEdge.getId().get(), "has");
580 // Try retrieving it again. This time we should not find it.
582 champDao.getEdge(createdEdge.getId().get(), "has");
583 } catch (CrudException e) {
585 assertTrue(e.getMessage().contains("No edge with id"));
586 deletedEdgeNotFound = true;
589 assertTrue("Should not have been able to retrieve deleted edge.", deletedEdgeNotFound);
592 private boolean containsEdge(Edge anEdge, List<Edge> edges) {
594 for (Edge e : edges) {
595 if (e.getId().isPresent() && anEdge.getId().isPresent() && (e.getId().get().equals(anEdge.getId().get()))) {
603 public static class MapBuilder {
605 private Map<String, Object> map;
607 private MapBuilder() {
608 map = new HashMap<String, Object>();
611 public static MapBuilder builder() {
612 return new MapBuilder();
615 public MapBuilder withKeyValue(String key, Object value) {
620 public Map<String, Object> build() {