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