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.uri;
23 import static org.hamcrest.Matchers.hasProperty;
24 import static org.hamcrest.Matchers.is;
25 import static org.junit.Assert.assertEquals;
27 import java.io.UnsupportedEncodingException;
29 import java.util.HashMap;
31 import javax.annotation.PostConstruct;
32 import javax.ws.rs.core.UriBuilder;
33 import javax.xml.bind.JAXBException;
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;
48 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
49 public class URIToObjectTest extends AAISetup {
51 private SchemaVersion version;
52 private SchemaVersion currentVersion;
53 private Loader loader;
56 public ExpectedException thrown = ExpectedException.none();
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
67 public void createLoader() {
68 version = schemaVersions.getRelatedLinkVersion();
69 currentVersion = schemaVersions.getDefaultVersion();
70 loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getRelatedLinkVersion());
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")
78 URIToObject parse = new URIToObject(loader, uri);
79 Introspector result = parse.getTopEntity();
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";
85 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
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
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")
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);
114 URIToObject parse = new URIToObject(loader, uri, relatedObjects);
115 Introspector result = parse.getTopEntity();
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";
121 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
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
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")
139 thrown.expect(AAIException.class);
140 thrown.expect(hasProperty("code", is("AAI_3000")));
142 new URIToObject(loader, uri);
146 * Starts with valid namespace.
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
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")
159 URIToObject parse = new URIToObject(loader, uri);
160 Introspector result = parse.getTopEntity();
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";
166 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
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
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\"}";
185 String topEntity = "generic-vnf";
186 String entity = "generic-vnf";
188 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
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
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")
206 URIToObject parse = new URIToObject(loader, uri);
207 Introspector result = parse.getTopEntity();
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";
213 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
220 * @throws IllegalArgumentException the illegal argument exception
221 * @throws UnsupportedEncodingException the unsupported encoding exception
222 * @throws AAIException the AAI exception
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")
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";
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);
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\"}";
248 String topEntity = "l-interface";
249 String entity = "l-interface";
251 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
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
265 public void testSet(String json, URIToObject parse, String expected, String topEntity, String entity,
266 SchemaVersion version) {
267 assertEquals("blah", expected, json);
269 assertEquals("top entity", topEntity, parse.getTopEntityName());
271 assertEquals("entity", entity, parse.getEntityName());
273 assertEquals("entity object", entity, parse.getEntity().getDbName());
275 assertEquals("parent list object", 1, parse.getParentList().size());
277 assertEquals("object version", version, parse.getObjectVersion());