Enhancements for the aai-common library
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / parsers / query / LegacyQueryTest.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 org.junit.Ignore;
24 import org.junit.Test;
25 import org.onap.aai.AAISetup;
26 import org.onap.aai.exceptions.AAIException;
27 import org.onap.aai.introspection.ModelType;
28 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
29 import org.onap.aai.serialization.engines.QueryStyle;
30 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
31 import org.onap.aai.setup.SchemaVersion;
32
33 import javax.ws.rs.core.UriBuilder;
34 import javax.xml.bind.JAXBException;
35 import java.io.UnsupportedEncodingException;
36 import java.net.URI;
37
38 import static org.junit.Assert.assertEquals;
39
40 @Ignore
41 public class LegacyQueryTest extends AAISetup {
42
43     private TransactionalGraphEngine dbEngine;
44     private SchemaVersion version;
45
46     public void setup() {
47         version = new SchemaVersion("v10");
48         dbEngine = new JanusGraphDBEngine(QueryStyle.GREMLIN_TRAVERSAL,
49                 loaderFactory.createLoaderForVersion(ModelType.MOXY, version), false);
50     }
51
52     /**
53      * Parent query.
54      *
55      * @throws JAXBException the JAXB exception
56      * @throws UnsupportedEncodingException the unsupported encoding exception
57      * @throws AAIException the AAI exception
58      */
59     @Test
60     public void parentQuery() throws UnsupportedEncodingException, AAIException {
61
62         URI uri = UriBuilder.fromPath("cloud-infrastructure/pservers/pserver/key1").build();
63
64         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
65
66         String expected = ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
67         assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
68         assertEquals("parent gremlin query should be equal to normal query", expected,
69                 query.getQueryBuilder().getParentQuery().getQuery());
70         assertEquals("result type should be pserver", "pserver", query.getResultType());
71
72     }
73
74     /**
75      * Child query.
76      *
77      * @throws JAXBException the JAXB exception
78      * @throws UnsupportedEncodingException the unsupported encoding exception
79      * @throws AAIException the AAI exception
80      */
81     @Test
82     public void childQuery() throws UnsupportedEncodingException, AAIException {
83         URI uri = UriBuilder.fromPath("cloud-infrastructure/pservers/pserver/key1/lag-interfaces/lag-interface/key2")
84                 .build();
85         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
86
87         String expected = ".has('hostname', 'key1').has('aai-node-type', 'pserver')"
88                 + ".out('hasLAGInterface').has('aai-node-type', 'lag-interface')" + ".has('interface-name', 'key2')";
89         String parentExpected = ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
90         assertEquals("gremlin query should be for node", expected, query.getQueryBuilder().getQuery());
91         assertEquals("parent gremlin query should be for parent", parentExpected,
92                 query.getQueryBuilder().getParentQuery().getQuery());
93         assertEquals("result type should be lag-interface", "lag-interface", query.getResultType());
94     }
95
96     /**
97      * Naming exceptions.
98      *
99      * @throws JAXBException the JAXB exception
100      * @throws UnsupportedEncodingException the unsupported encoding exception
101      * @throws AAIException the AAI exception
102      */
103     @Test
104     public void namingExceptions() throws UnsupportedEncodingException, AAIException {
105         URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655")
106                 .build();
107
108         QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
109         String expected = ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
110                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
111                 + ".has('interface-id', 'key2')"
112                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
113                 + ".has('cvlan-tag', 655)";
114         String expectedParent = ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
115                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
116                 + ".has('interface-id', 'key2')";
117         assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
118         assertEquals("parent gremlin query should be equal the query for port group", expectedParent,
119                 query.getQueryBuilder().getParentQuery().getQuery());
120         assertEquals("result type should be cvlan-tag", "cvlan-tag", query.getResultType());
121
122     }
123
124 }