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.schema.enums.ObjectMetadata;
23 import org.onap.aai.setup.SchemaVersion;
25 import org.junit.Ignore;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.onap.aai.AAISetup;
30 import org.onap.aai.db.props.AAIProperties;
31 import org.onap.aai.exceptions.AAIException;
32 import org.onap.aai.introspection.*;
33 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
34 import org.springframework.test.annotation.DirtiesContext;
36 import javax.annotation.PostConstruct;
37 import javax.ws.rs.core.UriBuilder;
38 import javax.xml.bind.JAXBException;
39 import java.io.UnsupportedEncodingException;
41 import java.util.HashMap;
43 import static org.hamcrest.Matchers.hasProperty;
44 import static org.hamcrest.Matchers.is;
45 import static org.junit.Assert.assertEquals;
47 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
48 public class URIToObjectTest extends AAISetup {
50 private SchemaVersion version ;
51 private SchemaVersion currentVersion;
52 private Loader loader ;
55 public ExpectedException thrown = ExpectedException.none();
60 * @throws JAXBException the JAXB exception
61 * @throws AAIException the AAI exception
62 * @throws IllegalArgumentException the illegal argument exception
63 * @throws UnsupportedEncodingException the unsupported encoding exception
66 public void createLoader(){
67 version = schemaVersions.getRelatedLinkVersion();
68 currentVersion = schemaVersions.getDefaultVersion();
69 loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getRelatedLinkVersion());
73 public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
74 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();
75 URIToObject parse = new URIToObject(loader, uri);
76 Introspector result = parse.getTopEntity();
77 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\"}]}}]}}]}}";
78 String topEntity = "cloud-region";
79 String entity = "l-interface";
81 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
88 * @throws JAXBException the JAXB exception
89 * @throws AAIException the AAI exception
90 * @throws IllegalArgumentException the illegal argument exception
91 * @throws UnsupportedEncodingException the unsupported encoding exception
92 * @throws AAIUnknownObjectException
95 public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, AAIUnknownObjectException {
96 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
97 HashMap<String, Introspector> relatedObjects = new HashMap<>();
98 Introspector tenantObj = this.loader.introspectorFromName("tenant");
99 tenantObj.setValue("tenant-id", "key1");
100 tenantObj.setValue("tenant-name", "name1");
101 relatedObjects.put(tenantObj.getObjectId(), tenantObj);
102 Introspector vserverObj = this.loader.introspectorFromName("vserver");
103 vserverObj.setValue("vserver-id", "key2");
104 vserverObj.setValue("vserver-name", "name2");
105 relatedObjects.put(vserverObj.getObjectId(), vserverObj);
107 URIToObject parse = new URIToObject(loader, uri, relatedObjects);
108 Introspector result = parse.getTopEntity();
109 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\"}]}}]}}]}}";
110 String topEntity = "cloud-region";
111 String entity = "l-interface";
113 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
122 * @throws JAXBException the JAXB exception
123 * @throws AAIException the AAI exception
124 * @throws IllegalArgumentException the illegal argument exception
125 * @throws UnsupportedEncodingException the unsupported encoding exception
128 public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
129 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();
131 thrown.expect(AAIException.class);
132 thrown.expect(hasProperty("code", is("AAI_3000")));
134 new URIToObject(loader, uri);
138 * Starts with valid namespace.
140 * @throws JAXBException the JAXB exception
141 * @throws AAIException the AAI exception
142 * @throws IllegalArgumentException the illegal argument exception
143 * @throws UnsupportedEncodingException the unsupported encoding exception
146 public void startsWithValidNamespace() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
147 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
148 URIToObject parse = new URIToObject(loader, uri);
149 Introspector result = parse.getTopEntity();
150 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\"}]}}]}}]}}";
151 String topEntity = "cloud-region";
152 String entity = "l-interface";
154 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
160 * @throws JAXBException the JAXB exception
161 * @throws AAIException the AAI exception
162 * @throws IllegalArgumentException the illegal argument exception
163 * @throws UnsupportedEncodingException the unsupported encoding exception
166 public void singleTopLevel() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
167 URI uri = UriBuilder.fromPath("/network/generic-vnfs/generic-vnf/key1").build();
168 URIToObject parse = new URIToObject(loader, uri);
169 Introspector result = parse.getTopEntity();
170 String expected = "{\"vnf-id\":\"key1\"}";
172 String topEntity = "generic-vnf";
173 String entity = "generic-vnf";
175 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
182 * @throws JAXBException the JAXB exception
183 * @throws AAIException the AAI exception
184 * @throws IllegalArgumentException the illegal argument exception
185 * @throws UnsupportedEncodingException the unsupported encoding exception
189 public void namingExceptions() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
190 URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655").build();
191 URIToObject parse = new URIToObject(loader, uri);
192 Introspector result = parse.getTopEntity();
193 String expected = "{\"vnf-id\":\"key1\",\"port-groups\":{\"port-group\":[{\"interface-id\":\"key2\",\"cvlan-tags\":{\"cvlan-tag-entry\":[{\"cvlan-tag\":655}]}}]}}";
194 String topEntity = "vce";
195 String entity = "cvlan-tag";
197 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
204 * @throws IllegalArgumentException the illegal argument exception
205 * @throws UnsupportedEncodingException the unsupported encoding exception
206 * @throws AAIException the AAI exception
210 public void noListObject() throws IllegalArgumentException, UnsupportedEncodingException, AAIException {
211 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();
212 URIToObject parse = new URIToObject(loader, uri);
213 Introspector result = parse.getTopEntity();
214 String topEntity = "vpls-pe";
215 String entity = "l3-interface-ipv4-address-list";
216 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\"}]}]}}]}}";
217 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
222 public void relativePath() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
223 URI uri = UriBuilder.fromPath("./l-interfaces/l-interface/key1").build();
224 URIToObject parse = new URIToObject(loader, uri);
225 Introspector result = parse.getEntity();
226 String expected = "{\"interface-name\":\"key1\"}";
228 String topEntity = "l-interface";
229 String entity = "l-interface";
231 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
238 * @param json the json
239 * @param parse the parse
240 * @param expected the expected
241 * @param topEntity the top entity
242 * @param entity the entity
243 * @param version the version
245 public void testSet(String json, URIToObject parse, String expected, String topEntity, String entity, SchemaVersion version) {
246 assertEquals("blah", expected, json);
248 assertEquals("top entity", topEntity, parse.getTopEntityName());
250 assertEquals("entity", entity, parse.getEntityName());
252 assertEquals("entity object", entity, parse.getEntity().getDbName());
254 assertEquals("parent list object", 1, parse.getParentList().size());
256 assertEquals("object version", version, parse.getObjectVersion());