2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.parsers.query;
23 import static org.junit.Assert.assertEquals;
25 import java.io.StringReader;
26 import java.io.UnsupportedEncodingException;
28 import javax.xml.bind.JAXBException;
29 import javax.xml.bind.Unmarshaller;
30 import javax.xml.transform.stream.StreamSource;
32 import org.eclipse.persistence.dynamic.DynamicEntity;
33 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
34 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
35 import org.junit.Before;
36 import org.junit.Ignore;
37 import org.junit.Test;
38 import org.onap.aai.AAISetup;
39 import org.onap.aai.exceptions.AAIException;
40 import org.onap.aai.introspection.*;
41 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
42 import org.onap.aai.serialization.engines.QueryStyle;
43 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
44 import org.onap.aai.setup.SchemaVersion;
47 public class RelationshipQueryTest extends AAISetup {
49 private TransactionalGraphEngine dbEngine;
50 private SchemaVersion version;
51 private DynamicJAXBContext context = nodeIngestor.getContextForVersion(version);
55 version = new SchemaVersion("v10");
56 dbEngine = new JanusGraphDBEngine(QueryStyle.GREMLIN_TRAVERSAL,
57 loaderFactory.createLoaderForVersion(ModelType.MOXY, version), false);
63 * @throws JAXBException the JAXB exception
64 * @throws UnsupportedEncodingException the unsupported encoding exception
65 * @throws AAIException the AAI exception
68 public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
70 String content = "{" + "\"related-to\" : \"pserver\"," + "\"relationship-data\" : [{"
71 + "\"relationship-key\" : \"pserver.hostname\"," + "\"relationship-value\" : \"key1\"" + "}]" + "}";
73 Unmarshaller unmarshaller = context.createUnmarshaller();
74 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
75 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
76 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
77 Object obj = context.newDynamicEntity("Relationship");
79 DynamicEntity entity = (DynamicEntity) unmarshaller
80 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
82 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
83 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
85 String expected = ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
86 assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
87 assertEquals("parent gremlin query should be equal to normal query", expected,
88 query.getQueryBuilder().getParentQuery().getQuery());
89 assertEquals("result type should be pserver", "pserver", query.getResultType());
96 * @throws JAXBException the JAXB exception
97 * @throws UnsupportedEncodingException the unsupported encoding exception
98 * @throws AAIException the AAI exception
102 public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
103 String content = "{" + "\"related-to\" : \"lag-interface\"," + "\"relationship-data\" : [{"
104 + "\"relationship-key\" : \"pserver.hostname\"," + "\"relationship-value\" : \"key1\"" + "}, {"
105 + "\"relationship-key\" : \"lag-interface.interface-name\"," + "\"relationship-value\" : \"key2\""
108 Unmarshaller unmarshaller = context.createUnmarshaller();
109 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
110 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
111 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
112 Object obj = context.newDynamicEntity("Relationship");
114 DynamicEntity entity = (DynamicEntity) unmarshaller
115 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
117 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
118 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
120 ".has('hostname', 'key1').has('aai-node-type', 'pserver').out('hasLAGInterface').has('aai-node-type', 'lag-interface')"
121 + ".has('interface-name', 'key2')";
122 String parentExpected = ".has('hostname', 'key1').has('aai-node-type', 'pserver')";
123 assertEquals("gremlin query should be for node", expected, query.getQueryBuilder().getQuery());
124 assertEquals("parent gremlin query should be for parent", parentExpected,
125 query.getQueryBuilder().getParentQuery().getQuery());
126 assertEquals("result type should be lag-interface", "lag-interface", query.getResultType());
132 * @throws JAXBException the JAXB exception
133 * @throws UnsupportedEncodingException the unsupported encoding exception
134 * @throws AAIException the AAI exception
138 public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
139 String content = "{" + "\"related-to\" : \"cvlan-tag\"," + "\"relationship-data\" : [{"
140 + "\"relationship-key\" : \"vce.vnf-id\"," + "\"relationship-value\" : \"key1\"" + "}, {"
141 + "\"relationship-key\" : \"port-group.interface-id\"," + "\"relationship-value\" : \"key2\"" + "},{"
142 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\"," + "\"relationship-value\" : \"655\"" + "}]" + "}";
144 Unmarshaller unmarshaller = context.createUnmarshaller();
145 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
146 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
147 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
148 Object obj = context.newDynamicEntity("Relationship");
150 DynamicEntity entity = (DynamicEntity) unmarshaller
151 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
153 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
154 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
156 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
157 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2').in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'cvlan-tag')"
158 + ".has('cvlan-tag', 655)";
159 String expectedParent =
160 ".has('vnf-id', 'key1').has('aai-node-type', 'vce').in('org.onap.relationships.inventory.BelongsTo')"
161 + ".has('aai-node-type', 'port-group').has('interface-id', 'key2')";
163 assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
164 assertEquals("parent gremlin query should be equal the query for port group", expectedParent,
165 query.getQueryBuilder().getParentQuery().getQuery());
166 assertEquals("result type should be cvlan-tag", "cvlan-tag", query.getResultType());
173 * @throws JAXBException the JAXB exception
174 * @throws UnsupportedEncodingException the unsupported encoding exception
175 * @throws AAIException the AAI exception
179 public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
180 String content = "{" + "\"related-to\" : \"ctag-pool\"," + "\"relationship-data\" : [{"
181 + "\"relationship-key\" : \"complex.physical-location-id\"," + "\"relationship-value\" : \"key1\""
182 + " }, { " + "\"relationship-key\" : \"ctag-pool.target-pe\"," + " \"relationship-value\" : \"key2\""
183 + " },{" + "\"relationship-key\" : \"ctag-pool.availability-zone-name\","
184 + "\"relationship-value\" : \"key3\"" + "}]" + "}";
186 Unmarshaller unmarshaller = context.createUnmarshaller();
187 unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
188 unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
189 unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
190 Object obj = context.newDynamicEntity("Relationship");
192 DynamicEntity entity = (DynamicEntity) unmarshaller
193 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
195 Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
196 QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
198 String expected = ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')"
199 + ".in('org.onap.relationships.inventory.BelongsTo').has('aai-node-type', 'ctag-pool')"
200 + ".has('target-pe', 'key2')" + ".has('availability-zone-name', 'key3')";
201 String expectedParent = ".has('physical-location-id', 'key1').has('aai-node-type', 'complex')";
203 assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
204 assertEquals("parent gremlin query should be equal the query for port group", expectedParent,
205 query.getQueryBuilder().getParentQuery().getQuery());
206 assertEquals("result type should be ctag-pool", "ctag-pool", query.getResultType());