4302de616a363338f3d2b321c7e9703db0edffee
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / parsers / query / LegacyQueryTest.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.parsers.query;
22
23 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.openecomp.aai.exceptions.AAIException;
27 import org.openecomp.aai.introspection.LoaderFactory;
28 import org.openecomp.aai.introspection.ModelInjestor;
29 import org.openecomp.aai.introspection.ModelType;
30 import org.openecomp.aai.introspection.Version;
31 import org.openecomp.aai.serialization.engines.QueryStyle;
32 import org.openecomp.aai.serialization.engines.TitanDBEngine;
33 import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
34
35 import javax.ws.rs.core.UriBuilder;
36 import javax.xml.bind.JAXBException;
37 import java.io.UnsupportedEncodingException;
38 import java.net.URI;
39
40 import static org.junit.Assert.assertEquals;
41
42
43 public class LegacyQueryTest {
44         private ModelInjestor injestor = ModelInjestor.getInstance();
45         private TransactionalGraphEngine dbEngine =
46                         new TitanDBEngine(QueryStyle.GREMLIN_TRAVERSAL,
47                                 LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.v8),
48                                 false); 
49         private final Version version = Version.v8;
50         private DynamicJAXBContext context = injestor.getContextForVersion(version);
51         
52         
53         /**
54          * Configure.
55          */
56         @BeforeClass
57         public static void configure() {
58                 System.setProperty("AJSC_HOME", "./src/test/resources/");
59                 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
60         }
61         
62         /**
63          * Parent query.
64          *
65          * @throws JAXBException the JAXB exception
66          * @throws UnsupportedEncodingException the unsupported encoding exception
67          * @throws AAIException the AAI exception
68          */
69         @Test
70     public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
71                 
72                 URI uri = UriBuilder.fromPath("cloud-infrastructure/pservers/pserver/key1").build();
73
74                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
75
76                 String expected = 
77                                 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
78                 assertEquals(
79                                 "gremlin query should be " + expected,
80                                 expected,
81                                 query.getQueryBuilder().getQuery());
82                 assertEquals(
83                                 "parent gremlin query should be equal to normal query",
84                                 expected,
85                                 query.getQueryBuilder().getParentQuery().getQuery());
86                 assertEquals(
87                                 "result type should be pserver",
88                                 "pserver",
89                                 query.getResultType());
90                 
91     }
92
93         /**
94          * Child query.
95          *
96          * @throws JAXBException the JAXB exception
97          * @throws UnsupportedEncodingException the unsupported encoding exception
98          * @throws AAIException the AAI exception
99          */
100         @Test
101     public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
102                 URI uri = UriBuilder.fromPath("cloud-infrastructure/pservers/pserver/key1/lag-interfaces/lag-interface/key2").build();
103                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
104
105                 String expected =
106                                 ".has('hostname', 'key1').has('aai-node-type', 'pserver')"
107                                 + ".in('tosca.relationships.BindsTo').has('aai-node-type', 'lag-interface')"
108                                 + ".has('interface-name', 'key2')";
109                 String parentExpected = 
110                                 ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
111                 assertEquals(
112                                 "gremlin query should be for node",
113                                 expected,
114                                 query.getQueryBuilder().getQuery());
115                 assertEquals(
116                                 "parent gremlin query should be for parent",
117                                 parentExpected,
118                                 query.getQueryBuilder().getParentQuery().getQuery());
119                 assertEquals(
120                                 "result type should be lag-interface",
121                                 "lag-interface",
122                                 query.getResultType());
123     }
124         
125         /**
126          * Naming exceptions.
127          *
128          * @throws JAXBException the JAXB exception
129          * @throws UnsupportedEncodingException the unsupported encoding exception
130          * @throws AAIException the AAI exception
131          */
132         @Test
133     public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
134                 URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655").build();
135         
136                 QueryParser query = dbEngine.getQueryBuilder().createQueryFromURI(uri);
137                 String expected = 
138                                 ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
139                                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
140                                 + ".has('interface-id', 'key2')"
141                                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
142                                 + ".has('cvlan-tag', 655)";
143                 String expectedParent = 
144                                                 ".has('vnf-id', 'key1').has('aai-node-type', 'vce')"
145                                                 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'port-group')"
146                                                 + ".has('interface-id', 'key2')";
147                 assertEquals(
148                                 "gremlin query should be " + expected,
149                                 expected,
150                                 query.getQueryBuilder().getQuery());
151                 assertEquals(
152                                 "parent gremlin query should be equal the query for port group",
153                                 expectedParent,
154                                 query.getQueryBuilder().getParentQuery().getQuery());
155                 assertEquals(
156                                 "result type should be cvlan-tag",
157                                 "cvlan-tag",
158                                 query.getResultType());
159                 
160     }
161         
162 }