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=========================================================
20 package org.onap.aai.parsers.uri;
22 import org.onap.aai.setup.SchemaVersion;
24 import org.junit.Ignore;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.onap.aai.AAISetup;
29 import org.onap.aai.db.props.AAIProperties;
30 import org.onap.aai.exceptions.AAIException;
31 import org.onap.aai.introspection.*;
32 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
34 import javax.annotation.PostConstruct;
35 import javax.ws.rs.core.UriBuilder;
36 import javax.xml.bind.JAXBException;
37 import java.io.UnsupportedEncodingException;
39 import java.util.HashMap;
41 import static org.hamcrest.Matchers.hasProperty;
42 import static org.hamcrest.Matchers.is;
43 import static org.junit.Assert.assertEquals;
45 public class URIToObjectTest extends AAISetup {
47 private SchemaVersion version ;
48 private SchemaVersion currentVersion;
49 private Loader loader ;
52 public ExpectedException thrown = ExpectedException.none();
57 * @throws JAXBException the JAXB exception
58 * @throws AAIException the AAI exception
59 * @throws IllegalArgumentException the illegal argument exception
60 * @throws UnsupportedEncodingException the unsupported encoding exception
63 public void createLoader(){
64 version = schemaVersions.getRelatedLinkVersion();
65 currentVersion = schemaVersions.getDefaultVersion();
66 loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getRelatedLinkVersion());
70 public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
71 URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion() + "/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
72 URIToObject parse = new URIToObject(loader, uri);
73 Introspector result = parse.getTopEntity();
74 String expected = "{\"cloud-owner\":\"mycloudowner\",\"cloud-region-id\":\"mycloudregionid\",\"tenants\":{\"tenant\":[{\"tenant-id\":\"key1\",\"vservers\":{\"vserver\":[{\"vserver-id\":\"key2\",\"l-interfaces\":{\"l-interface\":[{\"interface-name\":\"key3\"}]}}]}}]}}";
75 String topEntity = "cloud-region";
76 String entity = "l-interface";
78 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
85 * @throws JAXBException the JAXB exception
86 * @throws AAIException the AAI exception
87 * @throws IllegalArgumentException the illegal argument exception
88 * @throws UnsupportedEncodingException the unsupported encoding exception
89 * @throws AAIUnknownObjectException
92 public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, AAIUnknownObjectException {
93 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
94 HashMap<String, Introspector> relatedObjects = new HashMap<>();
95 Introspector tenantObj = this.loader.introspectorFromName("tenant");
96 tenantObj.setValue("tenant-id", "key1");
97 tenantObj.setValue("tenant-name", "name1");
98 relatedObjects.put(tenantObj.getObjectId(), tenantObj);
99 Introspector vserverObj = this.loader.introspectorFromName("vserver");
100 vserverObj.setValue("vserver-id", "key2");
101 vserverObj.setValue("vserver-name", "name2");
102 relatedObjects.put(vserverObj.getObjectId(), vserverObj);
104 URIToObject parse = new URIToObject(loader, uri, relatedObjects);
105 Introspector result = parse.getTopEntity();
106 String expected = "{\"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\"}]}}]}}]}}";
107 String topEntity = "cloud-region";
108 String entity = "l-interface";
110 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
119 * @throws JAXBException the JAXB exception
120 * @throws AAIException the AAI exception
121 * @throws IllegalArgumentException the illegal argument exception
122 * @throws UnsupportedEncodingException the unsupported encoding exception
125 public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
126 URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion() + "/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interadsfaces/l-interface/key3").build();
128 thrown.expect(AAIException.class);
129 thrown.expect(hasProperty("code", is("AAI_3000")));
131 new URIToObject(loader, uri);
135 * Starts with valid namespace.
137 * @throws JAXBException the JAXB exception
138 * @throws AAIException the AAI exception
139 * @throws IllegalArgumentException the illegal argument exception
140 * @throws UnsupportedEncodingException the unsupported encoding exception
143 public void startsWithValidNamespace() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
144 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
145 URIToObject parse = new URIToObject(loader, uri);
146 Introspector result = parse.getTopEntity();
147 String expected = "{\"cloud-owner\":\"mycloudowner\",\"cloud-region-id\":\"mycloudregionid\",\"tenants\":{\"tenant\":[{\"tenant-id\":\"key1\",\"vservers\":{\"vserver\":[{\"vserver-id\":\"key2\",\"l-interfaces\":{\"l-interface\":[{\"interface-name\":\"key3\"}]}}]}}]}}";
148 String topEntity = "cloud-region";
149 String entity = "l-interface";
151 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
157 * @throws JAXBException the JAXB exception
158 * @throws AAIException the AAI exception
159 * @throws IllegalArgumentException the illegal argument exception
160 * @throws UnsupportedEncodingException the unsupported encoding exception
163 public void singleTopLevel() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
164 URI uri = UriBuilder.fromPath("/network/generic-vnfs/generic-vnf/key1").build();
165 URIToObject parse = new URIToObject(loader, uri);
166 Introspector result = parse.getTopEntity();
167 String expected = "{\"vnf-id\":\"key1\"}";
169 String topEntity = "generic-vnf";
170 String entity = "generic-vnf";
172 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
179 * @throws JAXBException the JAXB exception
180 * @throws AAIException the AAI exception
181 * @throws IllegalArgumentException the illegal argument exception
182 * @throws UnsupportedEncodingException the unsupported encoding exception
186 public void namingExceptions() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
187 URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655").build();
188 URIToObject parse = new URIToObject(loader, uri);
189 Introspector result = parse.getTopEntity();
190 String expected = "{\"vnf-id\":\"key1\",\"port-groups\":{\"port-group\":[{\"interface-id\":\"key2\",\"cvlan-tags\":{\"cvlan-tag-entry\":[{\"cvlan-tag\":655}]}}]}}";
191 String topEntity = "vce";
192 String entity = "cvlan-tag";
194 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
201 * @throws IllegalArgumentException the illegal argument exception
202 * @throws UnsupportedEncodingException the unsupported encoding exception
203 * @throws AAIException the AAI exception
207 public void noListObject() throws IllegalArgumentException, UnsupportedEncodingException, AAIException {
208 URI uri = UriBuilder.fromPath("/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").build();
209 URIToObject parse = new URIToObject(loader, uri);
210 Introspector result = parse.getTopEntity();
211 String topEntity = "vpls-pe";
212 String entity = "l3-interface-ipv4-address-list";
213 String expected = "{\"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\"}]}]}}]}}";
214 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
219 public void relativePath() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
220 URI uri = UriBuilder.fromPath("./l-interfaces/l-interface/key1").build();
221 URIToObject parse = new URIToObject(loader, uri);
222 Introspector result = parse.getEntity();
223 String expected = "{\"interface-name\":\"key1\"}";
225 String topEntity = "l-interface";
226 String entity = "l-interface";
228 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
235 * @param json the json
236 * @param parse the parse
237 * @param expected the expected
238 * @param topEntity the top entity
239 * @param entity the entity
240 * @param version the version
242 public void testSet(String json, URIToObject parse, String expected, String topEntity, String entity, SchemaVersion version) {
243 assertEquals("blah", expected, json);
245 assertEquals("top entity", topEntity, parse.getTopEntityName());
247 assertEquals("entity", entity, parse.getEntityName());
249 assertEquals("entity object", entity, parse.getEntity().getDbName());
251 assertEquals("parent list object", 1, parse.getParentList().size());
253 assertEquals("object version", version, parse.getObjectVersion());