EdgeRules throws descriptive error on invalid rule
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / query / builder / TraversalQueryTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.aai.query.builder;
22
23 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
24 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
25 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
26 import org.apache.tinkerpop.gremlin.structure.Vertex;
27 import org.junit.Before;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.openecomp.aai.AAISetup;
32 import org.openecomp.aai.db.props.AAIProperties;
33 import org.openecomp.aai.exceptions.AAIException;
34 import org.openecomp.aai.introspection.Loader;
35 import org.openecomp.aai.introspection.LoaderFactory;
36 import org.openecomp.aai.introspection.ModelType;
37
38 import java.io.UnsupportedEncodingException;
39 import java.net.URI;
40 import java.net.URISyntaxException;
41
42 import static org.junit.Assert.assertEquals;
43
44 public class TraversalQueryTest extends AAISetup {
45
46         private Loader loader;
47
48         @Mock
49         private GraphTraversalSource g;
50
51         @Before
52         public void configure() throws Exception {
53                 loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
54         }
55         
56         @Test
57         public void unionQuery() {
58                 TraversalQuery<Vertex> tQ = new TraversalQuery<>(loader, g);
59                 TraversalQuery<Vertex> tQ2 = new TraversalQuery<>(loader, g);
60                 TraversalQuery<Vertex> tQ3 = new TraversalQuery<>(loader, g);
61                 tQ.union(
62                                 tQ2.getVerticesByProperty("test1", "value1"),
63                                 tQ3.getVerticesByIndexedProperty("test2", "value2"));
64                 
65                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start()
66                                 .union(__.has("test1", "value1"),__.has("test2", "value2"));
67                 
68                 assertEquals("they are equal", expected, tQ.getQuery());
69                 
70         }
71
72         @Ignore
73         @Test
74         public void traversalClones() throws UnsupportedEncodingException, AAIException, URISyntaxException {
75                 TraversalQuery<Vertex> tQ = new TraversalQuery<>(loader, g);
76                 QueryBuilder<Vertex> builder = tQ.createQueryFromURI(new URI("network/test-objects/test-object/key1")).getQueryBuilder();
77                 GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("vnf-id", "key1").has("aai-node-type", "test-object");
78                 GraphTraversal<Vertex, Vertex> containerExpected = __.<Vertex>start().has("aai-node-type", "test-object");
79                 
80                 assertEquals("query object", expected.toString(), builder.getQuery().toString());
81                 assertEquals("container query object", containerExpected.toString(), builder.getContainerQuery().getQuery().toString());
82                 
83         }
84
85         @Ignore
86         @Test
87         public void nestedTraversalClones() throws UnsupportedEncodingException, AAIException, URISyntaxException {
88                 
89                 TraversalQuery<Vertex> tQ = new TraversalQuery<>(loader, g);
90                 QueryBuilder<Vertex> builder = tQ.createQueryFromURI(new URI("network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2")).getQueryBuilder();
91                 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");
92                 GraphTraversal<Vertex, Vertex> containerExpected = __.<Vertex>start().has("vnf-id", "key1").has("aai-node-type", "generic-vnf").out("hasLInterface").has(AAIProperties.NODE_TYPE, "l-interface");
93                 
94                 assertEquals("query object", expected.toString(), builder.getQuery().toString());
95                 assertEquals("container query object", containerExpected.toString(), builder.getContainerQuery().getQuery().toString());
96                 
97         }
98         
99         
100         
101 }