AAI-1523 Batch reformat aai-core
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / parsers / query / RelationshipQueryTest.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.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;
45
46 @Ignore
47 public class RelationshipQueryTest extends AAISetup {
48
49     private TransactionalGraphEngine dbEngine;
50     private SchemaVersion version;
51     private DynamicJAXBContext context = nodeIngestor.getContextForVersion(version);
52
53     @Before
54     public void setup() {
55         version = new SchemaVersion("v10");
56         dbEngine = new JanusGraphDBEngine(QueryStyle.GREMLIN_TRAVERSAL,
57                 loaderFactory.createLoaderForVersion(ModelType.MOXY, version), false);
58     }
59
60     /**
61      * Parent query.
62      *
63      * @throws JAXBException the JAXB exception
64      * @throws UnsupportedEncodingException the unsupported encoding exception
65      * @throws AAIException the AAI exception
66      */
67     @Test
68     public void parentQuery() throws JAXBException, UnsupportedEncodingException, AAIException {
69
70         String content = "{" + "\"related-to\" : \"pserver\"," + "\"relationship-data\" : [{"
71                 + "\"relationship-key\" : \"pserver.hostname\"," + "\"relationship-value\" : \"key1\"" + "}]" + "}";
72
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");
78
79         DynamicEntity entity = (DynamicEntity) unmarshaller
80                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
81
82         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
83         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
84
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());
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     @Ignore
101     @Test
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\""
106                 + "}]" + "}";
107
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");
113
114         DynamicEntity entity = (DynamicEntity) unmarshaller
115                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
116
117         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
118         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
119         String expected =
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());
127     }
128
129     /**
130      * Naming exceptions.
131      *
132      * @throws JAXBException the JAXB exception
133      * @throws UnsupportedEncodingException the unsupported encoding exception
134      * @throws AAIException the AAI exception
135      */
136     @Ignore
137     @Test
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\"" + "}]" + "}";
143
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");
149
150         DynamicEntity entity = (DynamicEntity) unmarshaller
151                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
152
153         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
154         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
155         String expected =
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')";
162
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());
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     @Ignore
178     @Test
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\"" + "}]" + "}";
185
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");
191
192         DynamicEntity entity = (DynamicEntity) unmarshaller
193                 .unmarshal(new StreamSource(new StringReader(content)), obj.getClass()).getValue();
194
195         Introspector wrappedObj = IntrospectorFactory.newInstance(ModelType.MOXY, entity);
196         QueryParser query = dbEngine.getQueryBuilder().createQueryFromRelationship(wrappedObj);
197
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')";
202
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());
207
208     }
209
210 }