Update license files, sonar plugin and fix tests
[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                                 "parent gremlin query should be for parent",
113                                 parentExpected,
114                                 query.getQueryBuilder().getParentQuery().getQuery());
115                 assertEquals(
116                                 "result type should be lag-interface",
117                                 "lag-interface",
118                                 query.getResultType());
119     }
120
121 }