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.junit.Ignore;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.onap.aai.AAISetup;
27 import org.onap.aai.db.props.AAIProperties;
28 import org.onap.aai.exceptions.AAIException;
29 import org.onap.aai.introspection.*;
30 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
32 import javax.ws.rs.core.UriBuilder;
33 import javax.xml.bind.JAXBException;
34 import java.io.UnsupportedEncodingException;
36 import java.util.HashMap;
38 import static org.hamcrest.Matchers.hasProperty;
39 import static org.hamcrest.Matchers.is;
40 import static org.junit.Assert.assertEquals;
44 public class URIToObjectTest extends AAISetup {
46 private Version version = Version.v10;
47 private Version currentVersion = AAIProperties.LATEST;
48 private Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version);
51 public ExpectedException thrown = ExpectedException.none();
56 * @throws JAXBException the JAXB exception
57 * @throws AAIException the AAI exception
58 * @throws IllegalArgumentException the illegal argument exception
59 * @throws UnsupportedEncodingException the unsupported encoding exception
62 public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
63 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();
64 URIToObject parse = new URIToObject(loader, uri);
65 Introspector result = parse.getTopEntity();
66 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\"}]}}]}}]}}";
67 String topEntity = "cloud-region";
68 String entity = "l-interface";
70 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
77 * @throws JAXBException the JAXB exception
78 * @throws AAIException the AAI exception
79 * @throws IllegalArgumentException the illegal argument exception
80 * @throws UnsupportedEncodingException the unsupported encoding exception
81 * @throws AAIUnknownObjectException
84 public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, AAIUnknownObjectException {
85 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
86 HashMap<String, Introspector> relatedObjects = new HashMap<>();
87 Introspector tenantObj = this.loader.introspectorFromName("tenant");
88 tenantObj.setValue("tenant-id", "key1");
89 tenantObj.setValue("tenant-name", "name1");
90 relatedObjects.put(tenantObj.getObjectId(), tenantObj);
91 Introspector vserverObj = this.loader.introspectorFromName("vserver");
92 vserverObj.setValue("vserver-id", "key2");
93 vserverObj.setValue("vserver-name", "name2");
94 relatedObjects.put(vserverObj.getObjectId(), vserverObj);
96 URIToObject parse = new URIToObject(loader, uri, relatedObjects);
97 Introspector result = parse.getTopEntity();
98 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\"}]}}]}}]}}";
99 String topEntity = "cloud-region";
100 String entity = "l-interface";
102 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
111 * @throws JAXBException the JAXB exception
112 * @throws AAIException the AAI exception
113 * @throws IllegalArgumentException the illegal argument exception
114 * @throws UnsupportedEncodingException the unsupported encoding exception
117 public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
118 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();
120 thrown.expect(AAIException.class);
121 thrown.expect(hasProperty("code", is("AAI_3000")));
123 new URIToObject(loader, uri);
127 * Starts with valid namespace.
129 * @throws JAXBException the JAXB exception
130 * @throws AAIException the AAI exception
131 * @throws IllegalArgumentException the illegal argument exception
132 * @throws UnsupportedEncodingException the unsupported encoding exception
135 public void startsWithValidNamespace() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
136 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
137 URIToObject parse = new URIToObject(loader, uri);
138 Introspector result = parse.getTopEntity();
139 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\"}]}}]}}]}}";
140 String topEntity = "cloud-region";
141 String entity = "l-interface";
143 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
149 * @throws JAXBException the JAXB exception
150 * @throws AAIException the AAI exception
151 * @throws IllegalArgumentException the illegal argument exception
152 * @throws UnsupportedEncodingException the unsupported encoding exception
155 public void singleTopLevel() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
156 URI uri = UriBuilder.fromPath("/network/generic-vnfs/generic-vnf/key1").build();
157 URIToObject parse = new URIToObject(loader, uri);
158 Introspector result = parse.getTopEntity();
159 String expected = "{\"vnf-id\":\"key1\"}";
161 String topEntity = "generic-vnf";
162 String entity = "generic-vnf";
164 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
171 * @throws JAXBException the JAXB exception
172 * @throws AAIException the AAI exception
173 * @throws IllegalArgumentException the illegal argument exception
174 * @throws UnsupportedEncodingException the unsupported encoding exception
178 public void namingExceptions() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
179 URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655").build();
180 URIToObject parse = new URIToObject(loader, uri);
181 Introspector result = parse.getTopEntity();
182 String expected = "{\"vnf-id\":\"key1\",\"port-groups\":{\"port-group\":[{\"interface-id\":\"key2\",\"cvlan-tags\":{\"cvlan-tag-entry\":[{\"cvlan-tag\":655}]}}]}}";
183 String topEntity = "vce";
184 String entity = "cvlan-tag";
186 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
193 * @throws IllegalArgumentException the illegal argument exception
194 * @throws UnsupportedEncodingException the unsupported encoding exception
195 * @throws AAIException the AAI exception
199 public void noListObject() throws IllegalArgumentException, UnsupportedEncodingException, AAIException {
200 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();
201 URIToObject parse = new URIToObject(loader, uri);
202 Introspector result = parse.getTopEntity();
203 String topEntity = "vpls-pe";
204 String entity = "l3-interface-ipv4-address-list";
205 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\"}]}]}}]}}";
206 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
211 public void relativePath() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
212 URI uri = UriBuilder.fromPath("./l-interfaces/l-interface/key1").build();
213 URIToObject parse = new URIToObject(loader, uri);
214 Introspector result = parse.getEntity();
215 String expected = "{\"interface-name\":\"key1\"}";
217 String topEntity = "l-interface";
218 String entity = "l-interface";
220 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
227 * @param json the json
228 * @param parse the parse
229 * @param expected the expected
230 * @param topEntity the top entity
231 * @param entity the entity
232 * @param version the version
234 public void testSet(String json, URIToObject parse, String expected, String topEntity, String entity, Version version) {
235 assertEquals("blah", expected, json);
237 assertEquals("top entity", topEntity, parse.getTopEntityName());
239 assertEquals("entity", entity, parse.getEntityName());
241 assertEquals("entity object", entity, parse.getEntity().getDbName());
243 assertEquals("parent list object", 1, parse.getParentList().size());
245 assertEquals("object version", version, parse.getObjectVersion());