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 static org.hamcrest.Matchers.hasProperty;
24 import static org.hamcrest.Matchers.is;
25 import static org.junit.Assert.assertEquals;
27 import java.io.UnsupportedEncodingException;
29 import java.util.HashMap;
31 import javax.ws.rs.core.UriBuilder;
32 import javax.xml.bind.JAXBException;
34 import org.junit.BeforeClass;
35 import org.junit.Ignore;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
40 import org.openecomp.aai.db.props.AAIProperties;
41 import org.openecomp.aai.exceptions.AAIException;
42 import org.openecomp.aai.introspection.Introspector;
43 import org.openecomp.aai.introspection.Loader;
44 import org.openecomp.aai.introspection.LoaderFactory;
45 import org.openecomp.aai.introspection.ModelType;
46 import org.openecomp.aai.introspection.Version;
47 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
51 public class URIToObjectTest {
53 private Version version = Version.v8;
54 private Version currentVersion = AAIProperties.LATEST;
55 private Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version);
57 public ExpectedException thrown = ExpectedException.none();
63 public static void configure() {
64 System.setProperty("AJSC_HOME", ".");
65 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
72 * @throws JAXBException the JAXB exception
73 * @throws AAIException the AAI exception
74 * @throws IllegalArgumentException the illegal argument exception
75 * @throws UnsupportedEncodingException the unsupported encoding exception
78 public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
79 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();
80 URIToObject parse = new URIToObject(loader, uri);
81 Introspector result = parse.getTopEntity();
82 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\"}]}}]}}]}}";
83 String topEntity = "cloud-region";
84 String entity = "l-interface";
86 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
93 * @throws JAXBException the JAXB exception
94 * @throws AAIException the AAI exception
95 * @throws IllegalArgumentException the illegal argument exception
96 * @throws UnsupportedEncodingException the unsupported encoding exception
97 * @throws AAIUnknownObjectException
100 public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, AAIUnknownObjectException {
101 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
102 HashMap<String, Introspector> relatedObjects = new HashMap<>();
103 Introspector tenantObj = this.loader.introspectorFromName("tenant");
104 tenantObj.setValue("tenant-id", "key1");
105 tenantObj.setValue("tenant-name", "name1");
106 relatedObjects.put(tenantObj.getObjectId(), tenantObj);
107 Introspector vserverObj = this.loader.introspectorFromName("vserver");
108 vserverObj.setValue("vserver-id", "key2");
109 vserverObj.setValue("vserver-name", "name2");
110 relatedObjects.put(vserverObj.getObjectId(), vserverObj);
112 URIToObject parse = new URIToObject(loader, uri, relatedObjects);
113 Introspector result = parse.getTopEntity();
114 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\"}]}}]}}]}}";
115 String topEntity = "cloud-region";
116 String entity = "l-interface";
118 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
127 * @throws JAXBException the JAXB exception
128 * @throws AAIException the AAI exception
129 * @throws IllegalArgumentException the illegal argument exception
130 * @throws UnsupportedEncodingException the unsupported encoding exception
133 public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
134 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();
136 thrown.expect(AAIException.class);
137 thrown.expect(hasProperty("code", is("AAI_3000")));
139 new URIToObject(loader, uri);
143 * Starts with valid namespace.
145 * @throws JAXBException the JAXB exception
146 * @throws AAIException the AAI exception
147 * @throws IllegalArgumentException the illegal argument exception
148 * @throws UnsupportedEncodingException the unsupported encoding exception
151 public void startsWithValidNamespace() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
152 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
153 URIToObject parse = new URIToObject(loader, uri);
154 Introspector result = parse.getTopEntity();
155 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\"}]}}]}}]}}";
156 String topEntity = "cloud-region";
157 String entity = "l-interface";
159 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
165 * @throws JAXBException the JAXB exception
166 * @throws AAIException the AAI exception
167 * @throws IllegalArgumentException the illegal argument exception
168 * @throws UnsupportedEncodingException the unsupported encoding exception
171 public void singleTopLevel() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
172 URI uri = UriBuilder.fromPath("/network/generic-vnfs/generic-vnf/key1").build();
173 URIToObject parse = new URIToObject(loader, uri);
174 Introspector result = parse.getTopEntity();
175 String expected = "{\"vnf-id\":\"key1\"}";
177 String topEntity = "generic-vnf";
178 String entity = "generic-vnf";
180 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
187 * @throws JAXBException the JAXB exception
188 * @throws AAIException the AAI exception
189 * @throws IllegalArgumentException the illegal argument exception
190 * @throws UnsupportedEncodingException the unsupported encoding exception
194 public void namingExceptions() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
195 URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655").build();
196 URIToObject parse = new URIToObject(loader, uri);
197 Introspector result = parse.getTopEntity();
198 String expected = "{\"vnf-id\":\"key1\",\"port-groups\":{\"port-group\":[{\"interface-id\":\"key2\",\"cvlan-tags\":{\"cvlan-tag-entry\":[{\"cvlan-tag\":655}]}}]}}";
199 String topEntity = "vce";
200 String entity = "cvlan-tag";
202 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
209 * @throws IllegalArgumentException the illegal argument exception
210 * @throws UnsupportedEncodingException the unsupported encoding exception
211 * @throws AAIException the AAI exception
215 public void noListObject() throws IllegalArgumentException, UnsupportedEncodingException, AAIException {
216 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();
217 URIToObject parse = new URIToObject(loader, uri);
218 Introspector result = parse.getTopEntity();
219 String topEntity = "vpls-pe";
220 String entity = "l3-interface-ipv4-address-list";
221 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\"}]}]}}]}}";
222 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
227 public void relativePath() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
228 URI uri = UriBuilder.fromPath("./l-interfaces/l-interface/key1").build();
229 URIToObject parse = new URIToObject(loader, uri);
230 Introspector result = parse.getEntity();
231 String expected = "{\"interface-name\":\"key1\"}";
233 String topEntity = "l-interface";
234 String entity = "l-interface";
236 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
243 * @param json the json
244 * @param parse the parse
245 * @param expected the expected
246 * @param topEntity the top entity
247 * @param entity the entity
248 * @param version the version
250 public void testSet(String json, URIToObject parse, String expected, String topEntity, String entity, Version version) {
251 assertEquals("blah", expected, json);
253 assertEquals("top entity", topEntity, parse.getTopEntityName());
255 assertEquals("entity", entity, parse.getEntityName());
257 assertEquals("entity object", entity, parse.getEntity().getDbName());
259 assertEquals("parent list object", 1, parse.getParentList().size());
261 assertEquals("object version", version, parse.getObjectVersion());