0cba621c2d6b3fd0cf685d3e6b6a52fba4612e0a
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / serialization / db / EdgeSerializerTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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 package org.onap.aai.serialization.db;
21
22 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
23 import org.apache.tinkerpop.gremlin.structure.Edge;
24 import org.apache.tinkerpop.gremlin.structure.Graph;
25 import org.apache.tinkerpop.gremlin.structure.T;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.onap.aai.AAISetup;
32 import org.onap.aai.db.props.AAIProperties;
33 import org.onap.aai.exceptions.AAIException;
34 import org.onap.aai.serialization.db.exceptions.EdgeMultiplicityException;
35 import org.springframework.beans.factory.annotation.Autowired;
36
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertTrue;
39
40 public class EdgeSerializerTest extends AAISetup {
41         @Autowired
42         EdgeSerializer rules;
43         
44         @Rule
45         public ExpectedException thrown = ExpectedException.none();
46         
47         @Test
48         public void addTreeEdgeTest() throws AAIException {
49                 Graph graph = TinkerGraph.open();
50                 Vertex v1 = graph.addVertex(T.id, "1", "aai-node-type", "cloud-region");
51                 Vertex v2 = graph.addVertex(T.id, "10", "aai-node-type", "tenant");
52                 GraphTraversalSource g = graph.traversal();
53                 rules.addTreeEdge(g, v1, v2);
54                 assertEquals(true, g.V(v1).in("org.onap.relationships.inventory.BelongsTo").has("aai-node-type", "tenant").hasNext());
55
56                 Vertex v3 = graph.addVertex(T.id, "2", "aai-node-type", "cloud-region");
57                 assertEquals(null, rules.addTreeEdgeIfPossible(g, v3, v2));
58         }
59
60         @Test
61         public void addCousinEdgeTest() throws AAIException {
62                 Graph graph = TinkerGraph.open();
63                 Vertex v1 = graph.addVertex(T.id, "1", "aai-node-type", "flavor");
64                 Vertex v2 = graph.addVertex(T.id, "10", "aai-node-type", "vserver");
65                 GraphTraversalSource g = graph.traversal();
66                 rules.addEdge(g, v1, v2);
67                 assertEquals(true, g.V(v2).out("org.onap.relationships.inventory.Uses").has("aai-node-type", "flavor").hasNext());
68
69                 Vertex v3 = graph.addVertex(T.id, "2", "aai-node-type", "flavor");
70                 assertEquals(null, rules.addEdgeIfPossible(g, v3, v2));
71         }
72
73         @Test
74         public void multiplicityViolationTest() throws AAIException {
75                 thrown.expect(EdgeMultiplicityException.class);
76                 thrown.expectMessage("multiplicity rule violated: only one edge can exist with label: org.onap.relationships.inventory.Uses between vf-module and volume-group");
77
78                 Graph graph = TinkerGraph.open();
79                 Vertex v1 = graph.addVertex(T.id, "1", "aai-node-type", "vf-module");
80                 Vertex v2 = graph.addVertex(T.id, "10", "aai-node-type", "volume-group");
81                 GraphTraversalSource g = graph.traversal();
82
83                 rules.addEdge(g, v2, v1);
84                 Vertex v3 = graph.addVertex(T.id, "3", "aai-node-type", "vf-module");
85                 rules.addEdge(g, v2, v3);
86         }
87         
88         @Test
89         public void addEdgeVerifyAAIUUIDCousinTest() throws AAIException {
90                 Graph graph = TinkerGraph.open();
91                 Vertex v1 = graph.addVertex(T.id, "1", "aai-node-type", "flavor");
92                 Vertex v2 = graph.addVertex(T.id, "10", "aai-node-type", "vserver");
93                 GraphTraversalSource g = graph.traversal();
94                 Edge e = rules.addEdge(g, v1, v2);
95                 assertTrue(e.property(AAIProperties.AAI_UUID).isPresent());
96                 //assertTrue(e.property(AAIProperties.AAI_UUID).value().toString().matches("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"));
97         }
98
99         @Test
100         public void addEdgeVerifyAAIUUIDTreeTest() throws AAIException {
101                 Graph graph = TinkerGraph.open();
102                 Vertex v1 = graph.addVertex(T.id, "1", "aai-node-type", "tenant");
103                 Vertex v2 = graph.addVertex(T.id, "10", "aai-node-type", "vserver");
104                 GraphTraversalSource g = graph.traversal();
105                 Edge e = rules.addTreeEdge(g, v1, v2);
106                 assertTrue(e.property(AAIProperties.AAI_UUID).isPresent());
107                 //assertTrue(e.property(AAIProperties.AAI_UUID).value().toString().matches("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"));
108         }
109
110 }