1 package org.openecomp.crud.dao.champ;
3 import org.junit.After;
4 import org.junit.Before;
5 import org.junit.Ignore;
7 import org.openecomp.aai.champcore.graph.impl.InMemoryChampGraphImpl;
8 import org.openecomp.crud.dao.GraphDao;
9 import org.openecomp.crud.entity.Edge;
10 import org.openecomp.crud.entity.Vertex;
11 import org.openecomp.crud.exception.CrudException;
13 import java.util.HashMap;
14 import java.util.List;
16 import java.util.Properties;
18 import static org.junit.Assert.*;
22 * This suite of tests validates the basic functionality of the {@link ChampDao}.
24 public class ChampDaoTest {
26 private static final String GRAPH_NAME = "my_test_graph";
28 private GraphDao champDao = null;
32 * Perform setup steps that must be done prior to executing each test.
37 // Create an instance of the Champ DAO, backed by the Champ library's in-memory back end
38 // for testing purposes.
39 Map<String, Object> champDaoProperties = new HashMap<String, Object>();
40 champDaoProperties.put(ChampDao.CONFIG_STORAGE_BACKEND, "in-memory");
41 champDaoProperties.put(ChampDao.CONFIG_GRAPH_NAME, GRAPH_NAME);
42 champDao = new ChampDao(new InMemoryChampGraphImpl.Builder().properties(champDaoProperties).build());
47 * Perform tear down steps that must be done after executing each test.
50 public void tearDown() {
52 // Release the Champ DAO instance that we were using for the test.
53 if (champDao != null) {
54 ((ChampDao) champDao).close();
60 * Tests the ability of the {@link ChampDao} to create a vertex.
62 * @throws CrudException
65 public void createVertexTest() throws CrudException {
67 String VERTEX_TYPE = "Test_Vertex";
69 Map<String, Object> properties = new HashMap<String, Object>();
70 properties.put("property1", "something");
71 properties.put("property2", "something else");
74 Vertex createdVertex = champDao.addVertex(VERTEX_TYPE, properties);
76 // Validate that the returned {@link Vertex} has the right label assigned to it.
77 assertTrue("Unexpected vertex type '" + createdVertex.getType() + "' returned from DAO",
78 createdVertex.getType().equals(VERTEX_TYPE));
80 // Validate that all of the properties that we provided to the DAO are in fact assigned
81 // to the {@link Vertex} that we got back.
82 assertTrue("Vertex property list returned from DAO did not contain all expected properties - expected: " +
83 properties.keySet() + " actual: " + createdVertex.getProperties().keySet(),
84 createdVertex.getProperties().keySet().containsAll(properties.keySet()));
86 // Validate that the values assigned to the properties in the returned {@link Vertex}
87 // match the ones that we provided.
88 for (String propertyKey : properties.keySet()) {
90 assertTrue(createdVertex.getProperties().get(propertyKey).equals(properties.get(propertyKey)));
96 * Tests the ability of the {@link ChampDao} to retrieve a vertex from the graph data store
97 * by its unique identifier.
99 * @throws CrudException
102 public void getVertexByIdTest() throws CrudException {
104 String VERTEX_TYPE = "Test_Vertex";
106 Map<String, Object> properties = new HashMap<String, Object>();
107 properties.put("property1", "something");
108 properties.put("property2", "something else");
110 // Create the vertex.
111 Vertex createdVertex = champDao.addVertex(VERTEX_TYPE, properties);
113 // Make sure the {@link Vertex} returned from the create method includes an id that we can
114 // use to retrieve it.
115 assertTrue("No valid id returned for the created vertex", createdVertex.getId().isPresent());
117 // Now, retrieve the {@link Vertex} by its identifier.
118 Vertex retrievedVertex = champDao.getVertex(createdVertex.getId().get(), VERTEX_TYPE);
120 // Validate that the retrieved {@link Vertex} has the right label assigned to it.
121 assertTrue("Unexpected vertex type '" + retrievedVertex.getType() + "' returned from DAO",
122 retrievedVertex.getType().equals(VERTEX_TYPE));
124 // Validate that all of the properties that we provided when we created the {@link Vertex}
125 // are present in the {@link Vertex} that we retrieved.
126 assertTrue("Vertex property list returned from DAO did not contain all expected properties - expected: " +
127 properties.keySet() + " actual: " + retrievedVertex.getProperties().keySet(),
128 retrievedVertex.getProperties().keySet().containsAll(properties.keySet()));
130 // Validate that the values assigned to the properties in the retrieved {@link Vertex}
131 // match the ones that we provided when we created it.
132 for (String propertyKey : properties.keySet()) {
134 assertTrue(retrievedVertex.getProperties().get(propertyKey).equals(properties.get(propertyKey)));
140 * Tests the ability of the {@link ChampDao} to update an already existing vertex.
142 * @throws CrudException
145 public void updateVertexTest() throws CrudException {
147 final String VERTEX_TYPE = "Test_Vertex";
149 Map<String, Object> properties = new HashMap<String, Object>();
150 properties.put("property1", "something");
151 properties.put("property2", "something else");
153 // Create the vertex.
154 Vertex createdVertex = champDao.addVertex(VERTEX_TYPE, properties);
156 // Make sure the {@link Vertex} returned from the create method includes an id that we can
157 // use to retrieve it.
158 assertTrue("No valid id returned for the created vertex", createdVertex.getId().isPresent());
160 // Modify the properties list...
161 properties.put("property3", "a new property");
162 properties.remove("property1");
164 // ...and apply it to our vertex.
165 Vertex updatedVertex = champDao.updateVertex(createdVertex.getId().get(), createdVertex.getType(), properties);
167 assertTrue("Vertex property list returned from DAO update operation did not contain all expected properties - expected: " +
168 properties.keySet() + " actual: " + updatedVertex.getProperties().keySet(),
169 updatedVertex.getProperties().keySet().containsAll(properties.keySet()));
171 // Validate that the values assigned to the properties in the updated {@link Vertex}
172 // match the ones that we provided when we created it.
173 for (String propertyKey : properties.keySet()) {
175 assertTrue("Unexpected value for property '" + propertyKey + "' - Expected: " +
176 properties.get(propertyKey) + " Actual: " +
177 updatedVertex.getProperties().get(propertyKey),
178 updatedVertex.getProperties().get(propertyKey).equals(properties.get(propertyKey)));
181 // Validate that the property that we removed is NOT in the set of properties from our
182 // updated {@link Vertex}.
183 assertFalse("Property 'property1' should no longer be associated with updated vertex",
184 updatedVertex.getProperties().containsKey("property1"));
189 * Tests the ability of the {@link ChampDao} to retrieve multiple vertices which match
190 * a particular set of supplied properties.
192 * @throws CrudException
195 public void getVerticesTest() throws CrudException {
197 final String FIRST_VERTEX_TYPE = "pserver";
198 final String SECOND_VERTEX_TYPE = "complex";
200 // Create some vertices.
202 Map<String, Object> vertex1Properties = new HashMap<String, Object>();
203 vertex1Properties.put("O/S", "Linux");
204 vertex1Properties.put("version", "6.5");
205 vertex1Properties.put("hostname", "kll0001");
206 champDao.addVertex(FIRST_VERTEX_TYPE, vertex1Properties);
208 Map<String, Object> vertex2Properties = new HashMap<String, Object>();
209 vertex2Properties.put("O/S", "Linux");
210 vertex2Properties.put("version", "6.5");
211 vertex2Properties.put("hostname", "kll0002");
212 champDao.addVertex(FIRST_VERTEX_TYPE, vertex2Properties);
214 Map<String, Object> vertex3Properties = new HashMap<String, Object>();
215 vertex3Properties.put("O/S", "Linux");
216 vertex3Properties.put("version", "7.2");
217 vertex3Properties.put("hostname", "kll0003");
218 champDao.addVertex(FIRST_VERTEX_TYPE, vertex3Properties);
220 Map<String, Object> vertex4Properties = new HashMap<String, Object>();
221 vertex4Properties.put("O/S", "Windows");
222 vertex4Properties.put("version", "10");
223 vertex4Properties.put("hostname", "Dev Laptop");
224 champDao.addVertex(FIRST_VERTEX_TYPE, vertex4Properties);
226 Map<String, Object> vertex5Properties = new HashMap<String, Object>();
227 vertex5Properties.put("Street", "Baker");
228 vertex5Properties.put("Number", "222B");
229 champDao.addVertex(SECOND_VERTEX_TYPE, vertex5Properties);
231 // Create a set of properties to use for our query.
232 Map<String, Object> queryProperties = new HashMap<String, Object>();
233 queryProperties.put("O/S", "Linux");
234 queryProperties.put("version", "6.5");
236 // Validate that we filter our 'get vertices' results by type
237 List<Vertex> allVerticesByType = champDao.getVertices(FIRST_VERTEX_TYPE, MapBuilder.builder().build());
238 for (Vertex v : allVerticesByType) {
239 assertTrue("Unexpected vertex type returned from query. Expected: " +
240 FIRST_VERTEX_TYPE + " Actual: " + v.getType(),
241 v.getType().equals(FIRST_VERTEX_TYPE));
244 // Now, request the vertices that match our parameters.
245 List<Vertex> vertices = champDao.getVertices(FIRST_VERTEX_TYPE, queryProperties);
247 // Validate that got back the expected number of vertices.
248 assertEquals(vertices.size(), 2);
250 // Validate that the vertices we got back contain the expected parameters.
251 for (Vertex v : vertices) {
253 assertTrue("Vertex from query result does not contain expected vertex 'O/S'",
254 v.getProperties().containsKey("O/S"));
255 assertTrue("Vertex from query result contains unexpected value for 'O/S' parameter - Expected: 'Linux' Actual: '" +
256 v.getProperties().get("O/S") + "'",
257 v.getProperties().get("O/S").equals("Linux"));
259 assertTrue("Vertex from query result does not contain expected vertex 'O/S'",
260 v.getProperties().containsKey("version"));
261 assertTrue("Vertex from query result contains unexpected value for 'O/S' parameter - Expected: 'Linux' Actual: '" +
262 v.getProperties().get("O/S") + "'",
263 v.getProperties().get("version").equals("6.5"));
268 public void deleteVertexTest() throws CrudException {
270 boolean deletedVertexNotFound = false;
273 Vertex createdVertex = champDao.addVertex("test_type", MapBuilder.builder()
274 .withKeyValue("O/S", "Linux")
275 .withKeyValue("version", "6.5")
276 .withKeyValue("hostname", "kll0001")
279 // Verify that we can retrieve the vertex from the graph data base.
280 Vertex retrievedVertex = champDao.getVertex(createdVertex.getId().get(), "test_type");
282 // Now, delete the vertex.
283 champDao.deleteVertex(createdVertex.getId().get(), "test_type");
285 // Now, try to retrieve it again. This time we should fail to find it.
287 champDao.getVertex(createdVertex.getId().get(), "test_type");
289 } catch (CrudException e) {
290 assertTrue(e.getMessage().contains("No vertex with id"));
291 deletedVertexNotFound = true;
294 assertTrue("Should not have been able to retrieve deleted vertex", deletedVertexNotFound);
298 public void createEdgeTest() throws CrudException {
300 String EDGE_TYPE = "has";
302 // Create the source vertex for the edge.
303 Map<String, Object> srcVertexProperties = new HashMap<String, Object>();
304 srcVertexProperties.put("O/S", "Linux");
305 srcVertexProperties.put("version", "6.5");
306 srcVertexProperties.put("hostname", "kll0001");
307 Vertex sourceVertex = champDao.addVertex("vserver", srcVertexProperties);
309 // Create the target vertex for the edge.
310 Map<String, Object> dstVertexProperties = new HashMap<String, Object>();
311 dstVertexProperties.put("O/S", "Linux");
312 dstVertexProperties.put("version", "6.5");
313 dstVertexProperties.put("hostname", "kll0002");
314 Vertex destVertex = champDao.addVertex("VNF", dstVertexProperties);
316 // Now, create the edge itself.
317 Map<String, Object> edgeProperties = new HashMap<String, Object>();
318 edgeProperties.put("prop", "val");
319 Edge createdEdge = champDao.addEdge("has", sourceVertex, destVertex, edgeProperties);
321 // Validate that the Edge object returned from the create method matches what we were
323 assertTrue("Unexpected type for Edge returned from create method. Expected: " + EDGE_TYPE
324 + " Actual: " + createdEdge.getType(),
325 createdEdge.getType().equals("has"));
326 assertTrue("Unexpected properties for Edge returned from create method. Expected: " + edgeProperties
327 + " Actual: " + createdEdge.getProperties(),
328 createdEdge.getProperties().equals(edgeProperties));
333 public void createEdgeWithMissingSrcOrTargetTest() throws CrudException {
335 String EDGE_TYPE = "has";
337 // Create the source vertex for the edge.
338 Map<String, Object> srcVertexProperties = new HashMap<String, Object>();
339 srcVertexProperties.put("O/S", "Linux");
340 srcVertexProperties.put("version", "6.5");
341 srcVertexProperties.put("hostname", "kll0001");
342 Vertex sourceVertex = champDao.addVertex("vserver", srcVertexProperties);
344 // Create the target vertex for the edge.
345 Map<String, Object> dstVertexProperties = new HashMap<String, Object>();
346 dstVertexProperties.put("O/S", "Linux");
347 dstVertexProperties.put("version", "6.5");
348 dstVertexProperties.put("hostname", "kll0002");
349 Vertex destVertex = champDao.addVertex("VNF", dstVertexProperties);
351 // Now, try creating the Edge but specify an id for the source vertex that does
353 Map<String, Object> edgeProperties = new HashMap<String, Object>();
354 edgeProperties.put("prop", "val");
356 champDao.addEdge(EDGE_TYPE, new Vertex.Builder("miss").id("99").build(), destVertex, edgeProperties);
357 } catch (CrudException e) {
358 assertTrue(e.getMessage().contains("Error creating edge - source vertex"));
361 // Now, try created the Edge with a valid source vertex, but specify an id for the
362 // target vertex that does not exist.
364 champDao.addEdge(EDGE_TYPE, sourceVertex, new Vertex.Builder("miss").id("99").build(), edgeProperties);
365 } catch (CrudException e) {
366 assertTrue(e.getMessage().contains("Error creating edge - target vertex"));
372 public void getEdgeByIdTest() throws CrudException {
374 String EDGE_TYPE = "has";
376 // Create the source vertex for the edge.
377 Map<String, Object> srcVertexProperties = new HashMap<String, Object>();
378 srcVertexProperties.put("O/S", "Linux");
379 srcVertexProperties.put("version", "6.5");
380 srcVertexProperties.put("hostname", "kll0001");
381 Vertex sourceVertex = champDao.addVertex("vserver", srcVertexProperties);
383 // Create the target vertex for the edge.
384 Map<String, Object> dstVertexProperties = new HashMap<String, Object>();
385 dstVertexProperties.put("O/S", "Linux");
386 dstVertexProperties.put("version", "6.5");
387 dstVertexProperties.put("hostname", "kll0002");
388 Vertex destVertex = champDao.addVertex("VNF", dstVertexProperties);
390 // Now, create the edge itself.
391 Map<String, Object> edgeProperties = new HashMap<String, Object>();
392 edgeProperties.put("prop", "val");
393 Edge createdEdge = champDao.addEdge("has", sourceVertex, destVertex, edgeProperties);
395 // Retrieve the edge we just created by specifying its unique identifier.
396 Edge retrievedEdge = champDao.getEdge(createdEdge.getId().get(), "has");
398 // Validate that the contents of the object that we got back matches what we thought we
400 assertTrue("Unexpected type for Edge returned from get method. Expected: " + EDGE_TYPE
401 + " Actual: " + retrievedEdge.getType(),
402 retrievedEdge.getType().equals(EDGE_TYPE));
403 assertTrue("Unexpected properties for Edge returned from get method. Expected: " + edgeProperties
404 + " Actual: " + retrievedEdge.getProperties(),
405 retrievedEdge.getProperties().equals(edgeProperties));
409 public void getEdgesTest() throws CrudException {
411 final String EDGE_TYPE_HAS = "has";
412 final String EDGE_TYPE_RUNS = "runs";
414 // Create some vertices and edges that we can query agains.
415 Vertex complex = champDao.addVertex("complex", MapBuilder.builder()
416 .withKeyValue("Province", "Ontario")
417 .withKeyValue("City", "Ottawa")
418 .withKeyValue("Street", "303 Terry Fox")
421 Vertex vserver = champDao.addVertex("vserver", MapBuilder.builder()
422 .withKeyValue("O/S", "Linux")
423 .withKeyValue("version", "6.5")
424 .withKeyValue("hostname", "kll0001")
427 Vertex vnf1 = champDao.addVertex("vserver", MapBuilder.builder()
428 .withKeyValue("Application", "OpenDaylight")
431 Vertex vnf2 = champDao.addVertex("vserver", MapBuilder.builder()
432 .withKeyValue("Application", "Cammunda")
435 Edge edge1 = champDao.addEdge(EDGE_TYPE_HAS, complex, vserver,
437 .withKeyValue("usesResource", "false")
438 .withKeyValue("hasDelTarget", "false")
441 Edge edge2 = champDao.addEdge(EDGE_TYPE_RUNS, vserver, vnf1,
443 .withKeyValue("usesResource", "false")
444 .withKeyValue("hasDelTarget", "true")
447 Edge edge3 = champDao.addEdge(EDGE_TYPE_RUNS, vserver, vnf2,
449 .withKeyValue("usesResource", "false")
450 .withKeyValue("hasDelTarget", "false")
453 // Query for all HAS edges.
454 List<Edge> hasEdges = champDao.getEdges(EDGE_TYPE_HAS, new HashMap<String, Object>());
456 assertEquals("Unexpected number of edges of type 'has' found. Expected: 1 Actual: " + hasEdges.size(),
458 assertTrue("Result of query for 'has' type edges does not contain the expected results",
459 containsEdge(edge1, hasEdges));
461 // Query for all RUNS edges.
462 List<Edge> runsEdges = champDao.getEdges(EDGE_TYPE_RUNS, new HashMap<String, Object>());
464 assertEquals("Unexpected number of edges of type 'runs' found. Expected: 2 Actual: " + runsEdges.size(),
465 runsEdges.size(), 2);
466 assertTrue("Result of query for 'runs' type edges does not contain the expected results",
467 containsEdge(edge2, runsEdges));
468 assertTrue("Result of query for 'runs' type edges does not contain the expected results",
469 containsEdge(edge2, runsEdges));
471 // Query for all HAS edges with the property 'hasDelTarget' equal to 'true'.
472 List<Edge> runsEdgesWithDelTargetTrue =
473 champDao.getEdges(EDGE_TYPE_RUNS, MapBuilder.builder()
474 .withKeyValue("hasDelTarget", "true")
477 assertEquals("Unexpected number of edges of type 'has' with 'hasDelTarget=true' found. Expected: 1 Actual: "
478 + runsEdgesWithDelTargetTrue.size(),
479 runsEdgesWithDelTargetTrue.size(), 1);
480 assertTrue("Result of query for 'runs' type edges with delTarget set to TRUE does not contain the expected results",
481 containsEdge(edge2, runsEdgesWithDelTargetTrue));
485 @Ignore // For now - pending some expected fixes to the Champ library.
486 public void updateEdgeTest() throws CrudException {
488 // Create the source vertex for the edge.
489 Vertex sourceVertex = champDao.addVertex("vserver", MapBuilder.builder()
490 .withKeyValue("O/S", "Linux")
491 .withKeyValue("version", "6.5")
492 .withKeyValue("hostname", "kll0001")
495 // Create the target vertex for the edge.
496 Vertex destVertex = champDao.addVertex("VNF", MapBuilder.builder()
497 .withKeyValue("O/S", "Linux")
498 .withKeyValue("version", "6.5")
499 .withKeyValue("hostname", "kll0002")
502 // Now, create the edge itself.
503 Edge createdEdge = champDao.addEdge("has",
507 .withKeyValue("key1", "value1")
508 .withKeyValue("key2", "value2")
509 .withKeyValue("key3", "value3")
512 // Make sure the Edge returned from the create method includes an id that we can
513 // use to retrieve it.
514 assertTrue("No valid id returned for the created edge", createdEdge.getId().isPresent());
516 // Retrieve the properties map for our edge and make some changes.
517 Map<String, Object> properties = createdEdge.getProperties();
518 properties.put("key4", "value4");
519 properties.remove("key2");
521 // Now update the edge with the new properties map.
522 Edge updatedEdge = champDao.updateEdge(createdEdge);
524 assertTrue("Edge property list returned from DAO update operation did not contain all expected properties - expected: " +
525 properties.keySet() + " actual: " + updatedEdge.getProperties().keySet(),
526 updatedEdge.getProperties().keySet().containsAll(properties.keySet()));
528 // Validate that the values assigned to the properties in the updated Edge
529 // match the ones that we provided when we created it.
530 for (String propertyKey : properties.keySet()) {
532 assertTrue("Unexpected value for property '" + propertyKey + "' - Expected: " +
533 properties.get(propertyKey) + " Actual: " +
534 updatedEdge.getProperties().get(propertyKey),
535 updatedEdge.getProperties().get(propertyKey).equals(properties.get(propertyKey)));
538 // Validate that the property that we removed is NOT in the set of properties from our
540 // *** We will leave this validation commented out for now, as the Champ library actually
541 // merges update properties instead of replacing them...
542 // assertFalse("Property 'key2' should no longer be associated with updated edge",
543 // updatedEdge.getProperties().containsKey("key2"));
547 public void deleteEdgeTest() throws CrudException {
549 boolean deletedEdgeNotFound = false;
551 // Create the source vertex for the edge.
552 Vertex sourceVertex = champDao.addVertex("vserver", MapBuilder.builder()
553 .withKeyValue("O/S", "Linux")
554 .withKeyValue("version", "6.5")
555 .withKeyValue("hostname", "kll0001")
558 // Create the target vertex for the edge.
559 Vertex destVertex = champDao.addVertex("VNF", MapBuilder.builder()
560 .withKeyValue("O/S", "Linux")
561 .withKeyValue("version", "6.5")
562 .withKeyValue("hostname", "kll0002")
565 // Now, create the edge itself.
566 Edge createdEdge = champDao.addEdge("has",
570 .withKeyValue("key1", "value1")
571 .withKeyValue("key2", "value2")
572 .withKeyValue("key3", "value3")
575 // Verify that we can retrieve the edge that we just created.
576 Edge retrievedEdge = champDao.getEdge(createdEdge.getId().get(), "has");
579 champDao.deleteEdge(createdEdge.getId().get(), "has");
581 // Try retrieving it again. This time we should not find it.
583 champDao.getEdge(createdEdge.getId().get(), "has");
584 } catch (CrudException e) {
586 assertTrue(e.getMessage().contains("No edge with id"));
587 deletedEdgeNotFound = true;
590 assertTrue("Should not have been able to retrieve deleted edge.", deletedEdgeNotFound);
593 private boolean containsEdge(Edge anEdge, List<Edge> edges) {
595 for (Edge e : edges) {
596 if (e.getId().isPresent() && anEdge.getId().isPresent() && (e.getId().get().equals(anEdge.getId().get()))) {
604 public static class MapBuilder {
606 private Map<String, Object> map;
608 private MapBuilder() {
609 map = new HashMap<String, Object>();
612 public static MapBuilder builder() {
613 return new MapBuilder();
616 public MapBuilder withKeyValue(String key, Object value) {
621 public Map<String, Object> build() {