2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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=========================================================
21 package org.openecomp.aai.parsers.uri;
23 import org.junit.Ignore;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.openecomp.aai.AAISetup;
28 import org.openecomp.aai.db.props.AAIProperties;
29 import org.openecomp.aai.exceptions.AAIException;
30 import org.openecomp.aai.introspection.*;
31 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
33 import javax.ws.rs.core.UriBuilder;
34 import javax.xml.bind.JAXBException;
35 import java.io.UnsupportedEncodingException;
37 import java.util.HashMap;
39 import static org.hamcrest.Matchers.hasProperty;
40 import static org.hamcrest.Matchers.is;
41 import static org.junit.Assert.assertEquals;
45 public class URIToObjectTest extends AAISetup {
47 private Version version = Version.v10;
48 private Version currentVersion = AAIProperties.LATEST;
49 private Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version);
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 uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
64 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();
65 URIToObject parse = new URIToObject(loader, uri);
66 Introspector result = parse.getTopEntity();
67 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\"}]}}]}}]}}";
68 String topEntity = "cloud-region";
69 String entity = "l-interface";
71 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
78 * @throws JAXBException the JAXB exception
79 * @throws AAIException the AAI exception
80 * @throws IllegalArgumentException the illegal argument exception
81 * @throws UnsupportedEncodingException the unsupported encoding exception
82 * @throws AAIUnknownObjectException
85 public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, AAIUnknownObjectException {
86 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
87 HashMap<String, Introspector> relatedObjects = new HashMap<>();
88 Introspector tenantObj = this.loader.introspectorFromName("tenant");
89 tenantObj.setValue("tenant-id", "key1");
90 tenantObj.setValue("tenant-name", "name1");
91 relatedObjects.put(tenantObj.getObjectId(), tenantObj);
92 Introspector vserverObj = this.loader.introspectorFromName("vserver");
93 vserverObj.setValue("vserver-id", "key2");
94 vserverObj.setValue("vserver-name", "name2");
95 relatedObjects.put(vserverObj.getObjectId(), vserverObj);
97 URIToObject parse = new URIToObject(loader, uri, relatedObjects);
98 Introspector result = parse.getTopEntity();
99 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\"}]}}]}}]}}";
100 String topEntity = "cloud-region";
101 String entity = "l-interface";
103 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
112 * @throws JAXBException the JAXB exception
113 * @throws AAIException the AAI exception
114 * @throws IllegalArgumentException the illegal argument exception
115 * @throws UnsupportedEncodingException the unsupported encoding exception
118 public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
119 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();
121 thrown.expect(AAIException.class);
122 thrown.expect(hasProperty("code", is("AAI_3000")));
124 new URIToObject(loader, uri);
128 * Starts with valid namespace.
130 * @throws JAXBException the JAXB exception
131 * @throws AAIException the AAI exception
132 * @throws IllegalArgumentException the illegal argument exception
133 * @throws UnsupportedEncodingException the unsupported encoding exception
136 public void startsWithValidNamespace() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
137 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
138 URIToObject parse = new URIToObject(loader, uri);
139 Introspector result = parse.getTopEntity();
140 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\"}]}}]}}]}}";
141 String topEntity = "cloud-region";
142 String entity = "l-interface";
144 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
150 * @throws JAXBException the JAXB exception
151 * @throws AAIException the AAI exception
152 * @throws IllegalArgumentException the illegal argument exception
153 * @throws UnsupportedEncodingException the unsupported encoding exception
156 public void singleTopLevel() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
157 URI uri = UriBuilder.fromPath("/network/generic-vnfs/generic-vnf/key1").build();
158 URIToObject parse = new URIToObject(loader, uri);
159 Introspector result = parse.getTopEntity();
160 String expected = "{\"vnf-id\":\"key1\"}";
162 String topEntity = "generic-vnf";
163 String entity = "generic-vnf";
165 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
172 * @throws JAXBException the JAXB exception
173 * @throws AAIException the AAI exception
174 * @throws IllegalArgumentException the illegal argument exception
175 * @throws UnsupportedEncodingException the unsupported encoding exception
179 public void namingExceptions() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
180 URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655").build();
181 URIToObject parse = new URIToObject(loader, uri);
182 Introspector result = parse.getTopEntity();
183 String expected = "{\"vnf-id\":\"key1\",\"port-groups\":{\"port-group\":[{\"interface-id\":\"key2\",\"cvlan-tags\":{\"cvlan-tag-entry\":[{\"cvlan-tag\":655}]}}]}}";
184 String topEntity = "vce";
185 String entity = "cvlan-tag";
187 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
194 * @throws IllegalArgumentException the illegal argument exception
195 * @throws UnsupportedEncodingException the unsupported encoding exception
196 * @throws AAIException the AAI exception
200 public void noListObject() throws IllegalArgumentException, UnsupportedEncodingException, AAIException {
201 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();
202 URIToObject parse = new URIToObject(loader, uri);
203 Introspector result = parse.getTopEntity();
204 String topEntity = "vpls-pe";
205 String entity = "l3-interface-ipv4-address-list";
206 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\"}]}]}}]}}";
207 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
212 public void relativePath() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
213 URI uri = UriBuilder.fromPath("./l-interfaces/l-interface/key1").build();
214 URIToObject parse = new URIToObject(loader, uri);
215 Introspector result = parse.getEntity();
216 String expected = "{\"interface-name\":\"key1\"}";
218 String topEntity = "l-interface";
219 String entity = "l-interface";
221 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
228 * @param json the json
229 * @param parse the parse
230 * @param expected the expected
231 * @param topEntity the top entity
232 * @param entity the entity
233 * @param version the version
235 public void testSet(String json, URIToObject parse, String expected, String topEntity, String entity, Version version) {
236 assertEquals("blah", expected, json);
238 assertEquals("top entity", topEntity, parse.getTopEntityName());
240 assertEquals("entity", entity, parse.getEntityName());
242 assertEquals("entity object", entity, parse.getEntity().getDbName());
244 assertEquals("parent list object", 1, parse.getParentList().size());
246 assertEquals("object version", version, parse.getObjectVersion());