AAI-1523 Batch reformat aai-core
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / parsers / query / UniqueRelationshipQueryTest.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.StringReader;
26 import java.io.UnsupportedEncodingException;
27
28 import javax.xml.bind.JAXBException;
29 import javax.xml.bind.Unmarshaller;
30 import javax.xml.transform.stream.StreamSource;
31
32 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
33 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
34 import org.apache.tinkerpop.gremlin.structure.Vertex;
35 import org.eclipse.persistence.dynamic.DynamicEntity;
36 import org.eclipse.persistence.jaxb.UnmarshallerProperties;
37 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
38 import org.junit.Before;
39 import org.junit.Ignore;
40 import org.junit.Test;
41 import org.onap.aai.AAISetup;
42 import org.onap.aai.exceptions.AAIException;
43 import org.onap.aai.introspection.*;
44 import org.onap.aai.nodes.NodeIngestor;
45 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
46 import org.onap.aai.serialization.engines.QueryStyle;
47 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
48 import org.onap.aai.setup.SchemaVersion;
49 import org.springframework.beans.factory.annotation.Autowired;
50
51 @Ignore
52 public class UniqueRelationshipQueryTest extends AAISetup {
53
54     @Autowired
55     private NodeIngestor ingestor;
56
57     private TransactionalGraphEngine dbEngine;
58     private SchemaVersion version;
59     private DynamicJAXBContext context = ingestor.getContextForVersion(version);
60     private Unmarshaller unmarshaller = null;
61
62     /**
63      * Setup.
64      *
65      * @throws JAXBException the JAXB exception
66      */
67     @Before
68     public void setup() throws JAXBException {
69         version = new SchemaVersion("v10");
70         dbEngine = new JanusGraphDBEngine(QueryStyle.GREMLIN_UNIQUE,
71                 loaderFactory.createLoaderForVersion(ModelType.MOXY, version), false);
72         unmarshaller = context.createUnmarshaller();
73         unmarshaller.setProperty(UnmarshallerProperties.MEDIA_TYPE, "application/json");
74         unmarshaller.setProperty(UnmarshallerProperties.JSON_INCLUDE_ROOT, false);
75         unmarshaller.setProperty(UnmarshallerProperties.JSON_WRAPPER_AS_ARRAY_NAME, true);
76     }
77
78     /**
79      * Parent query.
80      *
81      * @throws JAXBException the JAXB exception
82      * @throws UnsupportedEncodingException the unsupported encoding exception
83      * @throws AAIException the AAI exception
84      */
85     @Test
86     public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
87
88         String content = "{" + "\"related-to\" : \"pserver\"," + "\"relationship-data\" : [{"
89                 + "\"relationship-key\" : \"pserver.hostname\"," + "\"relationship-value\" : \"key1\"" + "}]" + "}";
90
91         Object obj = context.newDynamicEntity("Relationship");
92
93         DynamicEntity entity = (DynamicEntity) unmarshaller
94                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
95
96         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
97         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
98         String key = "pserver/key1";
99         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
100         String resultType = "pserver";
101         String containerType = "";
102
103         testSet(query, expected, expected, resultType, containerType);
104
105     }
106
107     /**
108      * Child query.
109      *
110      * @throws JAXBException the JAXB exception
111      * @throws UnsupportedEncodingException the unsupported encoding exception
112      * @throws AAIException the AAI exception
113      */
114     @Test
115     public void childQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
116         String content = "{" + "\"related-to\" : \"lag-interface\"," + "\"relationship-data\" : [{"
117                 + "\"relationship-key\" : \"pserver.hostname\"," + "\"relationship-value\" : \"key1\"" + "}, {"
118                 + "\"relationship-key\" : \"lag-interface.interface-name\"," + "\"relationship-value\" : \"key2\""
119                 + "}]" + "}";
120
121         Object obj = context.newDynamicEntity("Relationship");
122
123         DynamicEntity entity = (DynamicEntity) unmarshaller
124                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
125
126         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
127         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
128
129         String key = "pserver/key1/lag-interface/key2";
130         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
131         GraphTraversal<Vertex, Vertex> parentExpected = __.<Vertex>start().has("aai-unique-key", "pserver/key1");
132         String resultType = "lag-interface";
133         String containerType = "";
134
135         testSet(query, expected, parentExpected, resultType, containerType);
136     }
137
138     /**
139      * Naming exceptions.
140      *
141      * @throws JAXBException the JAXB exception
142      * @throws UnsupportedEncodingException the unsupported encoding exception
143      * @throws AAIException the AAI exception
144      */
145     @Test
146     public void namingExceptions() throws JAXBException, UnsupportedEncodingException, AAIException {
147         String content = "{" + "\"related-to\" : \"cvlan-tag\"," + "\"relationship-data\" : [{"
148                 + "\"relationship-key\" : \"vce.vnf-id\"," + "\"relationship-value\" : \"key1\"" + "}, {"
149                 + "\"relationship-key\" : \"port-group.interface-id\"," + "\"relationship-value\" : \"key2\"" + "},{"
150                 + "\"relationship-key\" : \"cvlan-tag.cvlan-tag\"," + "\"relationship-value\" : \"655\"" + "}]" + "}";
151
152         Object obj = context.newDynamicEntity("Relationship");
153
154         DynamicEntity entity = (DynamicEntity) unmarshaller
155                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
156
157         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
158         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
159         String key = "vce/key1/port-group/key2/cvlan-tag/655";
160         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
161         GraphTraversal<Vertex, Vertex> parentExpected =
162                 __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
163         String resultType = "cvlan-tag";
164         String containerType = "";
165
166         testSet(query, expected, parentExpected, resultType, containerType);
167
168     }
169
170     /**
171      * Double key.
172      *
173      * @throws JAXBException the JAXB exception
174      * @throws UnsupportedEncodingException the unsupported encoding exception
175      * @throws AAIException the AAI exception
176      */
177     @Test
178     public void doubleKey() throws JAXBException, UnsupportedEncodingException, AAIException {
179         String content = "{" + "\"related-to\" : \"service-capability\"," + "\"relationship-data\" : [{"
180                 + "\"relationship-key\" : \"service-capability.service-type\"," + "\"relationship-value\" : \"key1\""
181                 + " }, { " + "\"relationship-key\" : \"service-capability.vnf-type\","
182                 + " \"relationship-value\" : \"key2\"" + " }]" + "}";
183
184         Object obj = context.newDynamicEntity("Relationship");
185
186         DynamicEntity entity = (DynamicEntity) unmarshaller
187                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
188
189         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
190         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
191
192         String key = "service-capability/key1/key2";
193         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
194         GraphTraversal<Vertex, Vertex> parentExpected =
195                 __.<Vertex>start().has("aai-unique-key", "service-capability/key1/key2");
196         String resultType = "service-capability";
197         String containerType = "";
198
199         testSet(query, expected, parentExpected, resultType, containerType);
200
201     }
202
203     /**
204      * Short circuit.
205      *
206      * @throws JAXBException the JAXB exception
207      * @throws UnsupportedEncodingException the unsupported encoding exception
208      * @throws AAIException the AAI exception
209      */
210     @Test
211     public void shortCircuit() throws JAXBException, UnsupportedEncodingException, AAIException {
212         String content = "{" + "\"related-to\" : \"cvlan-tag\","
213                 + "\"related-link\" : \"http://mock-system-name.com:8443/aai/v6/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655\","
214                 + "\"relationship-data\" : [{" + "\"relationship-key\" : \"vce.hostname\","
215                 + "\"relationship-value\" : \"key1\"" + "}, {" + "\"relationship-key\" : \"port-group.interface-name\","
216                 + "\"relationship-value\" : \"key2\"" + "},{" + "\"relationship-key\" : \"cvlan-tag.-name\","
217                 + "\"relationship-value\" : \"655\"" + "}]" + "}";
218
219         Object obj = context.newDynamicEntity("Relationship");
220
221         DynamicEntity entity = (DynamicEntity) unmarshaller
222                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
223
224         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
225         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
226         String key = "vce/key1/port-group/key2/cvlan-tag/655";
227         GraphTraversal<Vertex, Vertex> expected = __.<Vertex>start().has("aai-unique-key", key);
228         GraphTraversal<Vertex, Vertex> parentExpected =
229                 __.<Vertex>start().has("aai-unique-key", "vce/key1/port-group/key2");
230         String resultType = "cvlan-tag";
231         String containerType = "";
232
233         testSet(query, expected, parentExpected, resultType, containerType);
234
235     }
236
237     /**
238      * Test set.
239      *
240      * @param query the query
241      * @param expected the expected
242      * @param parentExpected the parent expected
243      * @param resultType the result type
244      * @param containerType the container type
245      */
246     public void testSet(QueryParser query, GraphTraversal<Vertex, Vertex> expected,
247             GraphTraversal<Vertex, Vertex> parentExpected, String resultType, String containerType) {
248         assertEquals("gremlin query should be " + expected, expected, query.getQueryBuilder().getQuery());
249         assertEquals("parent gremlin query should be " + parentExpected, parentExpected,
250                 query.getParentQueryBuilder().getParentQuery());
251         assertEquals("result type should be " + resultType, resultType, query.getResultType());
252         assertEquals("container type should be " + containerType, containerType, query.getContainerType());
253     }
254 }