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