2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.parsers.uri;
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.ws.rs.core.UriBuilder;
35 import javax.xml.bind.JAXBException;
36 import java.io.UnsupportedEncodingException;
38 import java.util.HashMap;
40 import static org.hamcrest.Matchers.hasProperty;
41 import static org.hamcrest.Matchers.is;
42 import static org.junit.Assert.assertEquals;
46 public class URIToObjectTest extends AAISetup {
48 private Version version = Version.v10;
49 private Version currentVersion = AAIProperties.LATEST;
50 private Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version);
53 public ExpectedException thrown = ExpectedException.none();
58 * @throws JAXBException the JAXB exception
59 * @throws AAIException the AAI exception
60 * @throws IllegalArgumentException the illegal argument exception
61 * @throws UnsupportedEncodingException the unsupported encoding exception
64 public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
65 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();
66 URIToObject parse = new URIToObject(loader, uri);
67 Introspector result = parse.getTopEntity();
68 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\"}]}}]}}]}}";
69 String topEntity = "cloud-region";
70 String entity = "l-interface";
72 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
79 * @throws JAXBException the JAXB exception
80 * @throws AAIException the AAI exception
81 * @throws IllegalArgumentException the illegal argument exception
82 * @throws UnsupportedEncodingException the unsupported encoding exception
83 * @throws AAIUnknownObjectException
86 public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, AAIUnknownObjectException {
87 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
88 HashMap<String, Introspector> relatedObjects = new HashMap<>();
89 Introspector tenantObj = this.loader.introspectorFromName("tenant");
90 tenantObj.setValue("tenant-id", "key1");
91 tenantObj.setValue("tenant-name", "name1");
92 relatedObjects.put(tenantObj.getObjectId(), tenantObj);
93 Introspector vserverObj = this.loader.introspectorFromName("vserver");
94 vserverObj.setValue("vserver-id", "key2");
95 vserverObj.setValue("vserver-name", "name2");
96 relatedObjects.put(vserverObj.getObjectId(), vserverObj);
98 URIToObject parse = new URIToObject(loader, uri, relatedObjects);
99 Introspector result = parse.getTopEntity();
100 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\"}]}}]}}]}}";
101 String topEntity = "cloud-region";
102 String entity = "l-interface";
104 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
113 * @throws JAXBException the JAXB exception
114 * @throws AAIException the AAI exception
115 * @throws IllegalArgumentException the illegal argument exception
116 * @throws UnsupportedEncodingException the unsupported encoding exception
119 public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
120 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();
122 thrown.expect(AAIException.class);
123 thrown.expect(hasProperty("code", is("AAI_3000")));
125 new URIToObject(loader, uri);
129 * Starts with valid namespace.
131 * @throws JAXBException the JAXB exception
132 * @throws AAIException the AAI exception
133 * @throws IllegalArgumentException the illegal argument exception
134 * @throws UnsupportedEncodingException the unsupported encoding exception
137 public void startsWithValidNamespace() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
138 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
139 URIToObject parse = new URIToObject(loader, uri);
140 Introspector result = parse.getTopEntity();
141 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\"}]}}]}}]}}";
142 String topEntity = "cloud-region";
143 String entity = "l-interface";
145 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
151 * @throws JAXBException the JAXB exception
152 * @throws AAIException the AAI exception
153 * @throws IllegalArgumentException the illegal argument exception
154 * @throws UnsupportedEncodingException the unsupported encoding exception
157 public void singleTopLevel() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
158 URI uri = UriBuilder.fromPath("/network/generic-vnfs/generic-vnf/key1").build();
159 URIToObject parse = new URIToObject(loader, uri);
160 Introspector result = parse.getTopEntity();
161 String expected = "{\"vnf-id\":\"key1\"}";
163 String topEntity = "generic-vnf";
164 String entity = "generic-vnf";
166 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
173 * @throws JAXBException the JAXB exception
174 * @throws AAIException the AAI exception
175 * @throws IllegalArgumentException the illegal argument exception
176 * @throws UnsupportedEncodingException the unsupported encoding exception
180 public void namingExceptions() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
181 URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655").build();
182 URIToObject parse = new URIToObject(loader, uri);
183 Introspector result = parse.getTopEntity();
184 String expected = "{\"vnf-id\":\"key1\",\"port-groups\":{\"port-group\":[{\"interface-id\":\"key2\",\"cvlan-tags\":{\"cvlan-tag-entry\":[{\"cvlan-tag\":655}]}}]}}";
185 String topEntity = "vce";
186 String entity = "cvlan-tag";
188 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
195 * @throws IllegalArgumentException the illegal argument exception
196 * @throws UnsupportedEncodingException the unsupported encoding exception
197 * @throws AAIException the AAI exception
201 public void noListObject() throws IllegalArgumentException, UnsupportedEncodingException, AAIException {
202 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();
203 URIToObject parse = new URIToObject(loader, uri);
204 Introspector result = parse.getTopEntity();
205 String topEntity = "vpls-pe";
206 String entity = "l3-interface-ipv4-address-list";
207 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\"}]}]}}]}}";
208 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
213 public void relativePath() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
214 URI uri = UriBuilder.fromPath("./l-interfaces/l-interface/key1").build();
215 URIToObject parse = new URIToObject(loader, uri);
216 Introspector result = parse.getEntity();
217 String expected = "{\"interface-name\":\"key1\"}";
219 String topEntity = "l-interface";
220 String entity = "l-interface";
222 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
229 * @param json the json
230 * @param parse the parse
231 * @param expected the expected
232 * @param topEntity the top entity
233 * @param entity the entity
234 * @param version the version
236 public void testSet(String json, URIToObject parse, String expected, String topEntity, String entity, Version version) {
237 assertEquals("blah", expected, json);
239 assertEquals("top entity", topEntity, parse.getTopEntityName());
241 assertEquals("entity", entity, parse.getEntityName());
243 assertEquals("entity object", entity, parse.getEntity().getDbName());
245 assertEquals("parent list object", 1, parse.getParentList().size());
247 assertEquals("object version", version, parse.getObjectVersion());