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.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
39 import org.openecomp.aai.db.props.AAIProperties;
40 import org.openecomp.aai.exceptions.AAIException;
41 import org.openecomp.aai.introspection.Introspector;
42 import org.openecomp.aai.introspection.Loader;
43 import org.openecomp.aai.introspection.LoaderFactory;
44 import org.openecomp.aai.introspection.ModelType;
45 import org.openecomp.aai.introspection.Version;
46 import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
50 public class URIToObjectTest {
52 private Version version = Version.v8;
53 private Version currentVersion = AAIProperties.LATEST;
54 private Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version);
56 public ExpectedException thrown = ExpectedException.none();
62 public static void configure() {
63 System.setProperty("AJSC_HOME", "./src/test/resources/");
64 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
71 * @throws JAXBException the JAXB exception
72 * @throws AAIException the AAI exception
73 * @throws IllegalArgumentException the illegal argument exception
74 * @throws UnsupportedEncodingException the unsupported encoding exception
77 public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
78 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();
79 URIToObject parse = new URIToObject(loader, uri);
80 Introspector result = parse.getTopEntity();
81 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\"}]}}]}}]}}";
82 String topEntity = "cloud-region";
83 String entity = "l-interface";
85 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
92 * @throws JAXBException the JAXB exception
93 * @throws AAIException the AAI exception
94 * @throws IllegalArgumentException the illegal argument exception
95 * @throws UnsupportedEncodingException the unsupported encoding exception
96 * @throws AAIUnknownObjectException
99 public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, AAIUnknownObjectException {
100 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
101 HashMap<String, Introspector> relatedObjects = new HashMap<>();
102 Introspector tenantObj = this.loader.introspectorFromName("tenant");
103 tenantObj.setValue("tenant-id", "key1");
104 tenantObj.setValue("tenant-name", "name1");
105 relatedObjects.put(tenantObj.getObjectId(), tenantObj);
106 Introspector vserverObj = this.loader.introspectorFromName("vserver");
107 vserverObj.setValue("vserver-id", "key2");
108 vserverObj.setValue("vserver-name", "name2");
109 relatedObjects.put(vserverObj.getObjectId(), vserverObj);
111 URIToObject parse = new URIToObject(loader, uri, relatedObjects);
112 Introspector result = parse.getTopEntity();
113 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\"}]}}]}}]}}";
114 String topEntity = "cloud-region";
115 String entity = "l-interface";
117 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
126 * @throws JAXBException the JAXB exception
127 * @throws AAIException the AAI exception
128 * @throws IllegalArgumentException the illegal argument exception
129 * @throws UnsupportedEncodingException the unsupported encoding exception
132 public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
133 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();
135 thrown.expect(AAIException.class);
136 thrown.expect(hasProperty("code", is("AAI_3000")));
138 new URIToObject(loader, uri);
142 * Starts with valid namespace.
144 * @throws JAXBException the JAXB exception
145 * @throws AAIException the AAI exception
146 * @throws IllegalArgumentException the illegal argument exception
147 * @throws UnsupportedEncodingException the unsupported encoding exception
150 public void startsWithValidNamespace() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
151 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
152 URIToObject parse = new URIToObject(loader, uri);
153 Introspector result = parse.getTopEntity();
154 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\"}]}}]}}]}}";
155 String topEntity = "cloud-region";
156 String entity = "l-interface";
158 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
164 * @throws JAXBException the JAXB exception
165 * @throws AAIException the AAI exception
166 * @throws IllegalArgumentException the illegal argument exception
167 * @throws UnsupportedEncodingException the unsupported encoding exception
170 public void singleTopLevel() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
171 URI uri = UriBuilder.fromPath("/network/generic-vnfs/generic-vnf/key1").build();
172 URIToObject parse = new URIToObject(loader, uri);
173 Introspector result = parse.getTopEntity();
174 String expected = "{\"vnf-id\":\"key1\"}";
176 String topEntity = "generic-vnf";
177 String entity = "generic-vnf";
179 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
186 * @throws JAXBException the JAXB exception
187 * @throws AAIException the AAI exception
188 * @throws IllegalArgumentException the illegal argument exception
189 * @throws UnsupportedEncodingException the unsupported encoding exception
192 public void namingExceptions() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
193 URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655").build();
194 URIToObject parse = new URIToObject(loader, uri);
195 Introspector result = parse.getTopEntity();
196 String expected = "{\"vnf-id\":\"key1\",\"port-groups\":{\"port-group\":[{\"interface-id\":\"key2\",\"cvlan-tags\":{\"cvlan-tag-entry\":[{\"cvlan-tag\":655}]}}]}}";
197 String topEntity = "vce";
198 String entity = "cvlan-tag";
200 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
207 * @throws IllegalArgumentException the illegal argument exception
208 * @throws UnsupportedEncodingException the unsupported encoding exception
209 * @throws AAIException the AAI exception
212 public void noListObject() throws IllegalArgumentException, UnsupportedEncodingException, AAIException {
213 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();
214 URIToObject parse = new URIToObject(loader, uri);
215 Introspector result = parse.getTopEntity();
216 String topEntity = "vpls-pe";
217 String entity = "l3-interface-ipv4-address-list";
218 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\"}]}]}}]}}";
219 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
224 public void relativePath() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
225 URI uri = UriBuilder.fromPath("./l-interfaces/l-interface/key1").build();
226 URIToObject parse = new URIToObject(loader, uri);
227 Introspector result = parse.getEntity();
228 String expected = "{\"interface-name\":\"key1\"}";
230 String topEntity = "l-interface";
231 String entity = "l-interface";
233 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
240 * @param json the json
241 * @param parse the parse
242 * @param expected the expected
243 * @param topEntity the top entity
244 * @param entity the entity
245 * @param version the version
247 public void testSet(String json, URIToObject parse, String expected, String topEntity, String entity, Version version) {
248 assertEquals("blah", expected, json);
250 assertEquals("top entity", topEntity, parse.getTopEntityName());
252 assertEquals("entity", entity, parse.getEntityName());
254 assertEquals("entity object", entity, parse.getEntity().getDbName());
256 assertEquals("parent list object", 1, parse.getParentList().size());
258 assertEquals("object version", version, parse.getObjectVersion());