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.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;
46 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
47 public class URIToObjectTest extends AAISetup {
49 private SchemaVersion version;
50 private SchemaVersion currentVersion;
51 private Loader loader;
54 public ExpectedException thrown = ExpectedException.none();
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
65 public void createLoader() {
66 version = schemaVersions.getRelatedLinkVersion();
67 currentVersion = schemaVersions.getDefaultVersion();
68 loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getRelatedLinkVersion());
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")
76 URIToObject parse = new URIToObject(loader, uri);
77 Introspector result = parse.getTopEntity();
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";
83 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
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
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")
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);
112 URIToObject parse = new URIToObject(loader, uri, relatedObjects);
113 Introspector result = parse.getTopEntity();
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";
119 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
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
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")
137 thrown.expect(AAIException.class);
138 thrown.expect(hasProperty("code", is("AAI_3000")));
140 new URIToObject(loader, uri);
144 * Starts with valid namespace.
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
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")
157 URIToObject parse = new URIToObject(loader, uri);
158 Introspector result = parse.getTopEntity();
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";
164 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
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
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\"}";
183 String topEntity = "generic-vnf";
184 String entity = "generic-vnf";
186 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
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
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")
204 URIToObject parse = new URIToObject(loader, uri);
205 Introspector result = parse.getTopEntity();
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";
211 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
218 * @throws IllegalArgumentException the illegal argument exception
219 * @throws UnsupportedEncodingException the unsupported encoding exception
220 * @throws AAIException the AAI exception
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")
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";
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);
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\"}";
246 String topEntity = "l-interface";
247 String entity = "l-interface";
249 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
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
263 public void testSet(String json, URIToObject parse, String expected, String topEntity, String entity,
264 SchemaVersion version) {
265 assertEquals("blah", expected, json);
267 assertEquals("top entity", topEntity, parse.getTopEntityName());
269 assertEquals("entity", entity, parse.getEntityName());
271 assertEquals("entity object", entity, parse.getEntity().getDbName());
273 assertEquals("parent list object", 1, parse.getParentList().size());
275 assertEquals("object version", version, parse.getObjectVersion());