3bbc00fdeefbe3062b533feba2bb0214340a4e66
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / parsers / uri / URIToObjectTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.aai.parsers.uri;
22
23 import static org.hamcrest.Matchers.hasProperty;
24 import static org.hamcrest.Matchers.is;
25 import static org.junit.Assert.assertEquals;
26
27 import java.io.UnsupportedEncodingException;
28 import java.net.URI;
29 import java.util.HashMap;
30
31 import javax.ws.rs.core.UriBuilder;
32 import javax.xml.bind.JAXBException;
33
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;
39
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;
48
49
50 @Ignore
51 public class URIToObjectTest {
52
53         private Version version = Version.v8;
54         private Version currentVersion = AAIProperties.LATEST;
55         private Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version);
56         @Rule
57         public ExpectedException thrown = ExpectedException.none();
58         
59         /**
60          * Configure.
61          */
62         @BeforeClass
63         public static void configure() {
64                 System.setProperty("AJSC_HOME", ".");
65                 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
66         }
67         
68         
69         /**
70          * Uri.
71          *
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
76          */
77         @Test
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";
85                 
86                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
87
88         }
89         
90         /**
91          * Uri no version.
92          *
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 
98          */
99         @Test
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);
111
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";
117                 
118                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
119
120                 
121         }
122         
123
124         /**
125          * Bad URI.
126          *
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
131          */
132         @Test
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();
135                 
136                 thrown.expect(AAIException.class);
137                 thrown.expect(hasProperty("code",  is("AAI_3000")));
138                 
139                 new URIToObject(loader, uri);
140         }
141         
142         /**
143          * Starts with valid namespace.
144          *
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
149          */
150         @Test
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";
158                 
159                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
160         }
161         
162         /**
163          * Single top level.
164          *
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
169          */
170         @Test
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\"}";
176                 
177                 String topEntity = "generic-vnf";
178                 String entity = "generic-vnf";
179                 
180                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
181
182         }
183         
184         /**
185          * Naming exceptions.
186          *
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
191          */
192         @Test
193         @Ignore
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";
201                 
202                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
203
204     }
205         
206         /**
207          * No list object.
208          *
209          * @throws IllegalArgumentException the illegal argument exception
210          * @throws UnsupportedEncodingException the unsupported encoding exception
211          * @throws AAIException the AAI exception
212          */
213         @Test
214         @Ignore
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);
223                 
224         }
225         
226         @Test
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\"}";
232                 
233                 String topEntity = "l-interface";
234                 String entity = "l-interface";
235                 
236                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
237
238         }
239         
240         /**
241          * Test set.
242          *
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
249          */
250         public void testSet(String json, URIToObject parse, String expected, String topEntity, String entity, Version version) {
251                 assertEquals("blah", expected, json);
252                 
253                 assertEquals("top entity", topEntity, parse.getTopEntityName());
254
255                 assertEquals("entity", entity, parse.getEntityName());
256
257                 assertEquals("entity object", entity, parse.getEntity().getDbName());
258                 
259                 assertEquals("parent list object", 1, parse.getParentList().size());
260                 
261                 assertEquals("object version", version, parse.getObjectVersion());
262         }
263 }