Reduce the number of problems in aai-common by removing unused imports
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / parsers / uri / URIToObjectTest.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.uri;
22
23 import static org.hamcrest.Matchers.hasProperty;
24 import static org.hamcrest.Matchers.is;
25 import static org.junit.Assert.assertEquals;
26
27 import java.io.UnsupportedEncodingException;
28 import java.net.URI;
29 import java.util.HashMap;
30
31 import javax.annotation.PostConstruct;
32 import javax.ws.rs.core.UriBuilder;
33 import javax.xml.bind.JAXBException;
34
35 import org.junit.Ignore;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.onap.aai.AAISetup;
40 import org.onap.aai.exceptions.AAIException;
41 import org.onap.aai.introspection.*;
42 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
43 import org.onap.aai.setup.SchemaVersion;
44 import org.springframework.test.annotation.DirtiesContext;
45
46 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
47 public class URIToObjectTest extends AAISetup {
48
49     private SchemaVersion version;
50     private SchemaVersion currentVersion;
51     private Loader loader;
52
53     @Rule
54     public ExpectedException thrown = ExpectedException.none();
55
56     /**
57      * Uri.
58      *
59      * @throws JAXBException the JAXB exception
60      * @throws AAIException the AAI exception
61      * @throws IllegalArgumentException the illegal argument exception
62      * @throws UnsupportedEncodingException the unsupported encoding exception
63      */
64     @PostConstruct
65     public void createLoader() {
66         version = schemaVersions.getRelatedLinkVersion();
67         currentVersion = schemaVersions.getDefaultVersion();
68         loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getRelatedLinkVersion());
69     }
70
71     @Test
72     public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
73         URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion()
74                 + "/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3")
75                 .build();
76         URIToObject parse = new URIToObject(loader, uri);
77         Introspector result = parse.getTopEntity();
78         String expected =
79                 "{\"cloud-owner\":\"mycloudowner\",\"cloud-region-id\":\"mycloudregionid\",\"tenants\":{\"tenant\":[{\"tenant-id\":\"key1\",\"vservers\":{\"vserver\":[{\"vserver-id\":\"key2\",\"l-interfaces\":{\"l-interface\":[{\"interface-name\":\"key3\"}]}}]}}]}}";
80         String topEntity = "cloud-region";
81         String entity = "l-interface";
82
83         testSet(result.marshal(false), parse, expected, topEntity, entity, version);
84
85     }
86
87     /**
88      * Uri no version.
89      *
90      * @throws JAXBException the JAXB exception
91      * @throws AAIException the AAI exception
92      * @throws IllegalArgumentException the illegal argument exception
93      * @throws UnsupportedEncodingException the unsupported encoding exception
94      * @throws AAIUnknownObjectException
95      */
96     @Test
97     public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException,
98             UnsupportedEncodingException, AAIUnknownObjectException {
99         URI uri = UriBuilder.fromPath(
100                 "/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3")
101                 .build();
102         HashMap<String, Introspector> relatedObjects = new HashMap<>();
103         Introspector tenantObj = this.loader.introspectorFromName("tenant");
104         tenantObj.setValue("tenant-id", "key1");
105         tenantObj.setValue("tenant-name", "name1");
106         relatedObjects.put(tenantObj.getObjectId(), tenantObj);
107         Introspector vserverObj = this.loader.introspectorFromName("vserver");
108         vserverObj.setValue("vserver-id", "key2");
109         vserverObj.setValue("vserver-name", "name2");
110         relatedObjects.put(vserverObj.getObjectId(), vserverObj);
111
112         URIToObject parse = new URIToObject(loader, uri, relatedObjects);
113         Introspector result = parse.getTopEntity();
114         String expected =
115                 "{\"cloud-owner\":\"mycloudowner\",\"cloud-region-id\":\"mycloudregionid\",\"tenants\":{\"tenant\":[{\"tenant-id\":\"key1\",\"tenant-name\":\"name1\",\"vservers\":{\"vserver\":[{\"vserver-id\":\"key2\",\"vserver-name\":\"name2\",\"l-interfaces\":{\"l-interface\":[{\"interface-name\":\"key3\"}]}}]}}]}}";
116         String topEntity = "cloud-region";
117         String entity = "l-interface";
118
119         testSet(result.marshal(false), parse, expected, topEntity, entity, version);
120
121     }
122
123     /**
124      * Bad URI.
125      *
126      * @throws JAXBException the JAXB exception
127      * @throws AAIException the AAI exception
128      * @throws IllegalArgumentException the illegal argument exception
129      * @throws UnsupportedEncodingException the unsupported encoding exception
130      */
131     @Test
132     public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
133         URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion()
134                 + "/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interadsfaces/l-interface/key3")
135                 .build();
136
137         thrown.expect(AAIException.class);
138         thrown.expect(hasProperty("code", is("AAI_3000")));
139
140         new URIToObject(loader, uri);
141     }
142
143     /**
144      * Starts with valid namespace.
145      *
146      * @throws JAXBException the JAXB exception
147      * @throws AAIException the AAI exception
148      * @throws IllegalArgumentException the illegal argument exception
149      * @throws UnsupportedEncodingException the unsupported encoding exception
150      */
151     @Test
152     public void startsWithValidNamespace()
153             throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
154         URI uri = UriBuilder.fromPath(
155                 "/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3")
156                 .build();
157         URIToObject parse = new URIToObject(loader, uri);
158         Introspector result = parse.getTopEntity();
159         String expected =
160                 "{\"cloud-owner\":\"mycloudowner\",\"cloud-region-id\":\"mycloudregionid\",\"tenants\":{\"tenant\":[{\"tenant-id\":\"key1\",\"vservers\":{\"vserver\":[{\"vserver-id\":\"key2\",\"l-interfaces\":{\"l-interface\":[{\"interface-name\":\"key3\"}]}}]}}]}}";
161         String topEntity = "cloud-region";
162         String entity = "l-interface";
163
164         testSet(result.marshal(false), parse, expected, topEntity, entity, version);
165     }
166
167     /**
168      * Single top level.
169      *
170      * @throws JAXBException the JAXB exception
171      * @throws AAIException the AAI exception
172      * @throws IllegalArgumentException the illegal argument exception
173      * @throws UnsupportedEncodingException the unsupported encoding exception
174      */
175     @Test
176     public void singleTopLevel()
177             throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
178         URI uri = UriBuilder.fromPath("/network/generic-vnfs/generic-vnf/key1").build();
179         URIToObject parse = new URIToObject(loader, uri);
180         Introspector result = parse.getTopEntity();
181         String expected = "{\"vnf-id\":\"key1\"}";
182
183         String topEntity = "generic-vnf";
184         String entity = "generic-vnf";
185
186         testSet(result.marshal(false), parse, expected, topEntity, entity, version);
187
188     }
189
190     /**
191      * Naming exceptions.
192      *
193      * @throws JAXBException the JAXB exception
194      * @throws AAIException the AAI exception
195      * @throws IllegalArgumentException the illegal argument exception
196      * @throws UnsupportedEncodingException the unsupported encoding exception
197      */
198     @Test
199     @Ignore
200     public void namingExceptions()
201             throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
202         URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655")
203                 .build();
204         URIToObject parse = new URIToObject(loader, uri);
205         Introspector result = parse.getTopEntity();
206         String expected =
207                 "{\"vnf-id\":\"key1\",\"port-groups\":{\"port-group\":[{\"interface-id\":\"key2\",\"cvlan-tags\":{\"cvlan-tag-entry\":[{\"cvlan-tag\":655}]}}]}}";
208         String topEntity = "vce";
209         String entity = "cvlan-tag";
210
211         testSet(result.marshal(false), parse, expected, topEntity, entity, version);
212
213     }
214
215     /**
216      * No list object.
217      *
218      * @throws IllegalArgumentException the illegal argument exception
219      * @throws UnsupportedEncodingException the unsupported encoding exception
220      * @throws AAIException the AAI exception
221      */
222     @Test
223     @Ignore
224     public void noListObject() throws IllegalArgumentException, UnsupportedEncodingException, AAIException {
225         URI uri = UriBuilder.fromPath(
226                 "/aai/v6/network/vpls-pes/vpls-pe/0e6189fd-9257-49b9-a3be-d7ba980ccfc9/lag-interfaces/lag-interface/8ae5aa76-d597-4382-b219-04f266fe5e37/l-interfaces/l-interface/9e141d03-467b-437f-b4eb-b3133ec1e205/l3-interface-ipv4-address-list/8f19f0ea-a81f-488e-8d5c-9b7b53696c11")
227                 .build();
228         URIToObject parse = new URIToObject(loader, uri);
229         Introspector result = parse.getTopEntity();
230         String topEntity = "vpls-pe";
231         String entity = "l3-interface-ipv4-address-list";
232         String expected =
233                 "{\"equipment-name\":\"0e6189fd-9257-49b9-a3be-d7ba980ccfc9\",\"lag-interfaces\":{\"lag-interface\":[{\"interface-name\":\"8ae5aa76-d597-4382-b219-04f266fe5e37\",\"l-interfaces\":{\"l-interface\":[{\"interface-name\":\"9e141d03-467b-437f-b4eb-b3133ec1e205\",\"l3-interface-ipv4-address-list\":[{\"l3-interface-ipv4-address\":\"8f19f0ea-a81f-488e-8d5c-9b7b53696c11\"}]}]}}]}}";
234         testSet(result.marshal(false), parse, expected, topEntity, entity, version);
235
236     }
237
238     @Test
239     public void relativePath()
240             throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
241         URI uri = UriBuilder.fromPath("./l-interfaces/l-interface/key1").build();
242         URIToObject parse = new URIToObject(loader, uri);
243         Introspector result = parse.getEntity();
244         String expected = "{\"interface-name\":\"key1\"}";
245
246         String topEntity = "l-interface";
247         String entity = "l-interface";
248
249         testSet(result.marshal(false), parse, expected, topEntity, entity, version);
250
251     }
252
253     /**
254      * Test set.
255      *
256      * @param json the json
257      * @param parse the parse
258      * @param expected the expected
259      * @param topEntity the top entity
260      * @param entity the entity
261      * @param version the version
262      */
263     public void testSet(String json, URIToObject parse, String expected, String topEntity, String entity,
264             SchemaVersion version) {
265         assertEquals("blah", expected, json);
266
267         assertEquals("top entity", topEntity, parse.getTopEntityName());
268
269         assertEquals("entity", entity, parse.getEntityName());
270
271         assertEquals("entity object", entity, parse.getEntity().getDbName());
272
273         assertEquals("parent list object", 1, parse.getParentList().size());
274
275         assertEquals("object version", version, parse.getObjectVersion());
276     }
277 }