Initial commit with all the necessary files
[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.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
38
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;
47
48
49
50 public class URIToObjectTest {
51
52         private Version version = Version.v8;
53         private Version currentVersion = AAIProperties.LATEST;
54         private Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version);
55         @Rule
56         public ExpectedException thrown = ExpectedException.none();
57         
58         /**
59          * Configure.
60          */
61         @BeforeClass
62         public static void configure() {
63                 System.setProperty("AJSC_HOME", "./src/test/resources/");
64                 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
65         }
66         
67         
68         /**
69          * Uri.
70          *
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
75          */
76         @Test
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";
84                 
85                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
86
87         }
88         
89         /**
90          * Uri no version.
91          *
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 
97          */
98         @Test
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);
110
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";
116                 
117                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
118
119                 
120         }
121         
122
123         /**
124          * Bad URI.
125          *
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
130          */
131         @Test
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();
134                 
135                 thrown.expect(AAIException.class);
136                 thrown.expect(hasProperty("code",  is("AAI_3000")));
137                 
138                 new URIToObject(loader, uri);
139         }
140         
141         /**
142          * Starts with valid namespace.
143          *
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
148          */
149         @Test
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";
157                 
158                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
159         }
160         
161         /**
162          * Single top level.
163          *
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
168          */
169         @Test
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\"}";
175                 
176                 String topEntity = "generic-vnf";
177                 String entity = "generic-vnf";
178                 
179                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
180
181         }
182         
183         /**
184          * Naming exceptions.
185          *
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
190          */
191         @Test
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";
199                 
200                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
201
202     }
203         
204         /**
205          * No list object.
206          *
207          * @throws IllegalArgumentException the illegal argument exception
208          * @throws UnsupportedEncodingException the unsupported encoding exception
209          * @throws AAIException the AAI exception
210          */
211         @Test
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);
220                 
221         }
222         
223         @Test
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\"}";
229                 
230                 String topEntity = "l-interface";
231                 String entity = "l-interface";
232                 
233                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
234
235         }
236         
237         /**
238          * Test set.
239          *
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
246          */
247         public void testSet(String json, URIToObject parse, String expected, String topEntity, String entity, Version version) {
248                 assertEquals("blah", expected, json);
249                 
250                 assertEquals("top entity", topEntity, parse.getTopEntityName());
251
252                 assertEquals("entity", entity, parse.getEntityName());
253
254                 assertEquals("entity object", entity, parse.getEntity().getDbName());
255                 
256                 assertEquals("parent list object", 1, parse.getParentList().size());
257                 
258                 assertEquals("object version", version, parse.getObjectVersion());
259         }
260 }