contains-other-v no longer implies delete-other-v
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / engines / query / GraphTraversalQueryEngineTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.onap.aai.serialization.engines.query;
24
25 import static org.junit.Assert.*;
26
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
32 import org.apache.tinkerpop.gremlin.structure.Graph;
33 import org.apache.tinkerpop.gremlin.structure.T;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
36 import org.junit.Test;
37 import org.onap.aai.exceptions.AAIException;
38 import org.onap.aai.serialization.db.EdgeRules;
39
40 public class GraphTraversalQueryEngineTest {
41
42         @Test
43         public void testFindDeletable() throws AAIException {
44                 //setup
45                 EdgeRules rules = EdgeRules.getInstance("/dbedgerules/DbEdgeRules_test.json");
46                 
47                 Graph graph = TinkerGraph.open();
48                 Vertex parent = graph.addVertex(T.id, "00", "aai-node-type", "test-parent");
49                 Vertex child = graph.addVertex(T.id, "10", "aai-node-type", "test-child");
50                 Vertex cousin = graph.addVertex(T.id, "20", "aai-node-type", "test-cousin");
51                 Vertex grandchild = graph.addVertex(T.id, "30", "aai-node-type", "test-grandchild");
52
53                 GraphTraversalSource g = graph.traversal();
54                 
55                 rules.addTreeEdge(g, parent, child); //delete-other-v=none, no cascade
56                 rules.addTreeEdge(g, child, grandchild); //d-o-v=out, yes from child
57                 rules.addEdge(g, cousin, child); //d-o-v=out, yes from cousin
58                 
59                 List<Vertex> parentExpected = new ArrayList<>(Arrays.asList(parent));
60                 List<Vertex> childExpected = new ArrayList<>(Arrays.asList(child, grandchild));
61                 List<Vertex> cousinExpected = new ArrayList<>(Arrays.asList(cousin, child, grandchild));
62                 
63                 GraphTraversalQueryEngine engine = new GraphTraversalQueryEngine(g);
64                 
65                 //tests
66                 List<Vertex> parentDeletes = engine.findDeletable(parent);
67                 assertTrue(parentExpected.containsAll(parentDeletes) && parentDeletes.containsAll(parentExpected));
68                 
69                 List<Vertex> childDeletes = engine.findDeletable(child);
70                 assertTrue(childExpected.containsAll(childDeletes) && childDeletes.containsAll(childExpected));
71                 
72                 List<Vertex> cousinDeletes = engine.findDeletable(cousin);
73                 assertTrue(cousinExpected.containsAll(cousinDeletes) && cousinDeletes.containsAll(cousinExpected));
74         }
75 }