AAI-1523 Batch reformat aai-core
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / parsers / query / UniqueURIQueryTest.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
21 package org.onap.aai.parsers.query;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.io.UnsupportedEncodingException;
26 import java.net.URI;
27
28 import javax.ws.rs.core.UriBuilder;
29
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
31 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
34 import org.junit.Ignore;
35 import org.junit.Test;
36 import org.onap.aai.AAISetup;
37 import org.onap.aai.exceptions.AAIException;
38 import org.onap.aai.introspection.ModelType;
39 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
40 import org.onap.aai.serialization.engines.QueryStyle;
41 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
42 import org.onap.aai.setup.SchemaVersion;
43
44 @Ignore
45 public class UniqueURIQueryTest extends AAISetup {
46
47     private TransactionalGraphEngine dbEngine;
48     private SchemaVersion version;
49     private DynamicJAXBContext context = nodeIngestor.getContextForVersion(version);
50
51     /**
52      * Parent query.
53      *
54      * @throws UnsupportedEncodingException the unsupported encoding exception
55      * @throws AAIException the AAI exception
56      */
57     @Test
58     public void parentQuery() throws UnsupportedEncodingException, AAIException {
59         version = new SchemaVersion("v10");
60         dbEngine = new JanusGraphDBEngine(QueryStyle.GREMLIN_UNIQUE,
61                 loaderFactory.createLoaderForVersion(ModelType.MOXY, version), false);
62         URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1").build();
63         String key = "complex/key1";
64         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
65         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
66         String parentResultType = "";
67         String resultType = "complex";
68         String containerType = "";
69
70         testSet(query, expected, expected, parentResultType, resultType, containerType);
71
72     }
73
74     /**
75      * Parent plural query.
76      *
77      * @throws UnsupportedEncodingException the unsupported encoding exception
78      * @throws AAIException the AAI exception
79      */
80     @Test
81     public void parentPluralQuery() throws UnsupportedEncodingException, AAIException {
82         URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes").build();
83         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
84         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-node-type", "complex");
85         String parentResultType = "";
86         String resultType = "complex";
87         String containerType = "complexes";
88
89         testSet(query, expected, expected, parentResultType, resultType, containerType);
90
91     }
92
93     /**
94      * Child query.
95      *
96      * @throws UnsupportedEncodingException the unsupported encoding exception
97      * @throws AAIException the AAI exception
98      */
99     @Test
100     public void childQuery() throws UnsupportedEncodingException, AAIException {
101         URI uri = UriBuilder.fromPath("cloud-infrastructure/complexes/complex/key1/ctag-pools/ctag-pool/key2/key3")
102                 .build();
103         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
104         String parentKey = "complex/key1";
105         String key = parentKey + "/ctag-pool/key2/key3";
106         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
107         GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", parentKey);
108         String parentResultType = "complex";
109         String resultType = "ctag-pool";
110         String containerType = "";
111
112         testSet(query, expected, parentExpected, parentResultType, resultType, containerType);
113
114     }
115
116     /**
117      * Naming exceptions.
118      *
119      * @throws UnsupportedEncodingException the unsupported encoding exception
120      * @throws AAIException the AAI exception
121      */
122     @Test
123     public void namingExceptions() throws UnsupportedEncodingException, AAIException {
124         URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655")
125                 .build();
126         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
127         String parentKey = "vce/key1/port-group/key2";
128         String key = parentKey + "/cvlan-tag/655";
129         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
130         GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", parentKey);
131         String parentResultType = "port-group";
132         String resultType = "cvlan-tag";
133         String containerType = "";
134
135         testSet(query, expected, parentExpected, parentResultType, resultType, containerType);
136
137     }
138
139     /**
140      * Gets the all.
141      *
142      * @return the all
143      * @throws UnsupportedEncodingException the unsupported encoding exception
144      * @throws AAIException the AAI exception
145      */
146     @Test
147     public void getAll() throws UnsupportedEncodingException, AAIException {
148         String parentURI = "network/vces/vce/key1/port-groups/port-group/key2";
149         String parentKey = "vce/key1/port-group/key2";
150         URI uri = UriBuilder.fromPath(parentURI + "/cvlan-tags").build();
151         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
152         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", parentKey)
153                 .in("org.onap.relationships.inventory.BelongsTo").has("aai-node-type", "cvlan-tag");
154         GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", parentKey);
155         String parentResultType = "port-group";
156         String resultType = "cvlan-tag";
157         String containerType = "cvlan-tags";
158
159         testSet(query, expected, parentExpected, parentResultType, resultType, containerType);
160
161     }
162
163     /**
164      * Test set.
165      *
166      * @param query the query
167      * @param expected the expected
168      * @param parentExpected the parent expected
169      * @param parentResultType the parent result type
170      * @param resultType the result type
171      * @param containerType the container type
172      */
173     public void testSet(QueryParser query, GraphTraversal<Vertex, Vertex> expected,
174             GraphTraversal<Vertex, Vertex> parentExpected, String parentResultType, String resultType,
175             String containerType) {
176         assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
177         assertEquals("parent gremlin query should be " + parentExpected, parentExpected,
178                 query.getQueryBuilder().getParentQuery().getQuery());
179         assertEquals("parent result type should be " + parentResultType, parentResultType, query.getParentResultType());
180         assertEquals("result type should be " + resultType, resultType, query.getResultType());
181         assertEquals("container type should be " + containerType, containerType, query.getContainerType());
182     }
183 }