Update the license for 2017-2018 license
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / parsers / uri / URIToObjectTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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 package org.onap.aai.parsers.uri;
21
22 import org.junit.Ignore;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.onap.aai.AAISetup;
27 import org.onap.aai.db.props.AAIProperties;
28 import org.onap.aai.exceptions.AAIException;
29 import org.onap.aai.introspection.*;
30 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
31
32 import javax.ws.rs.core.UriBuilder;
33 import javax.xml.bind.JAXBException;
34 import java.io.UnsupportedEncodingException;
35 import java.net.URI;
36 import java.util.HashMap;
37
38 import static org.hamcrest.Matchers.hasProperty;
39 import static org.hamcrest.Matchers.is;
40 import static org.junit.Assert.assertEquals;
41
42
43
44 public class URIToObjectTest extends AAISetup {
45
46         private Version version = Version.v10;
47         private Version currentVersion = AAIProperties.LATEST;
48         private Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, version);
49
50         @Rule
51         public ExpectedException thrown = ExpectedException.none();
52
53         /**
54          * Uri.
55          *
56          * @throws JAXBException the JAXB exception
57          * @throws AAIException the AAI exception
58          * @throws IllegalArgumentException the illegal argument exception
59          * @throws UnsupportedEncodingException the unsupported encoding exception
60          */
61         @Test
62     public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
63                 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();
64                 URIToObject parse = new URIToObject(loader, uri);
65                 Introspector result = parse.getTopEntity();
66                 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\"}]}}]}}]}}";
67                 String topEntity = "cloud-region";
68                 String entity = "l-interface";
69                 
70                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
71
72         }
73         
74         /**
75          * Uri no version.
76          *
77          * @throws JAXBException the JAXB exception
78          * @throws AAIException the AAI exception
79          * @throws IllegalArgumentException the illegal argument exception
80          * @throws UnsupportedEncodingException the unsupported encoding exception
81          * @throws AAIUnknownObjectException 
82          */
83         @Test
84     public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, AAIUnknownObjectException {
85                 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
86                 HashMap<String, Introspector> relatedObjects = new HashMap<>();
87                 Introspector tenantObj = this.loader.introspectorFromName("tenant");
88                 tenantObj.setValue("tenant-id", "key1");
89                 tenantObj.setValue("tenant-name", "name1");
90                 relatedObjects.put(tenantObj.getObjectId(), tenantObj);
91                 Introspector vserverObj = this.loader.introspectorFromName("vserver");
92                 vserverObj.setValue("vserver-id", "key2");
93                 vserverObj.setValue("vserver-name", "name2");
94                 relatedObjects.put(vserverObj.getObjectId(), vserverObj);
95
96                 URIToObject parse = new URIToObject(loader, uri, relatedObjects);
97                 Introspector result = parse.getTopEntity();
98                 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\"}]}}]}}]}}";
99                 String topEntity = "cloud-region";
100                 String entity = "l-interface";
101                 
102                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
103
104                 
105         }
106         
107
108         /**
109          * Bad URI.
110          *
111          * @throws JAXBException the JAXB exception
112          * @throws AAIException the AAI exception
113          * @throws IllegalArgumentException the illegal argument exception
114          * @throws UnsupportedEncodingException the unsupported encoding exception
115          */
116         @Test
117     public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
118                 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();
119                 
120                 thrown.expect(AAIException.class);
121                 thrown.expect(hasProperty("code",  is("AAI_3000")));
122                 
123                 new URIToObject(loader, uri);
124         }
125         
126         /**
127          * Starts with valid namespace.
128          *
129          * @throws JAXBException the JAXB exception
130          * @throws AAIException the AAI exception
131          * @throws IllegalArgumentException the illegal argument exception
132          * @throws UnsupportedEncodingException the unsupported encoding exception
133          */
134         @Test
135     public void startsWithValidNamespace() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
136                 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
137                 URIToObject parse = new URIToObject(loader, uri);
138                 Introspector result = parse.getTopEntity();
139                 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\"}]}}]}}]}}";
140                 String topEntity = "cloud-region";
141                 String entity = "l-interface";
142                 
143                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
144         }
145         
146         /**
147          * Single top level.
148          *
149          * @throws JAXBException the JAXB exception
150          * @throws AAIException the AAI exception
151          * @throws IllegalArgumentException the illegal argument exception
152          * @throws UnsupportedEncodingException the unsupported encoding exception
153          */
154         @Test
155     public void singleTopLevel() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
156                 URI uri = UriBuilder.fromPath("/network/generic-vnfs/generic-vnf/key1").build();
157                 URIToObject parse = new URIToObject(loader, uri);
158                 Introspector result = parse.getTopEntity();
159                 String expected = "{\"vnf-id\":\"key1\"}";
160                 
161                 String topEntity = "generic-vnf";
162                 String entity = "generic-vnf";
163                 
164                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
165
166         }
167         
168         /**
169          * Naming exceptions.
170          *
171          * @throws JAXBException the JAXB exception
172          * @throws AAIException the AAI exception
173          * @throws IllegalArgumentException the illegal argument exception
174          * @throws UnsupportedEncodingException the unsupported encoding exception
175          */
176         @Test
177         @Ignore
178     public void namingExceptions() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
179                 URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655").build();
180                 URIToObject parse = new URIToObject(loader, uri);
181                 Introspector result = parse.getTopEntity();
182                 String expected = "{\"vnf-id\":\"key1\",\"port-groups\":{\"port-group\":[{\"interface-id\":\"key2\",\"cvlan-tags\":{\"cvlan-tag-entry\":[{\"cvlan-tag\":655}]}}]}}";
183                 String topEntity = "vce";
184                 String entity = "cvlan-tag";
185                 
186                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
187
188     }
189         
190         /**
191          * No list object.
192          *
193          * @throws IllegalArgumentException the illegal argument exception
194          * @throws UnsupportedEncodingException the unsupported encoding exception
195          * @throws AAIException the AAI exception
196          */
197         @Test
198         @Ignore
199         public void noListObject() throws IllegalArgumentException, UnsupportedEncodingException, AAIException {
200                 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();
201                 URIToObject parse = new URIToObject(loader, uri);
202                 Introspector result = parse.getTopEntity();
203                 String topEntity = "vpls-pe";
204                 String entity = "l3-interface-ipv4-address-list";
205                 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\"}]}]}}]}}";
206                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
207                 
208         }
209         
210         @Test
211     public void relativePath() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
212                 URI uri = UriBuilder.fromPath("./l-interfaces/l-interface/key1").build();
213                 URIToObject parse = new URIToObject(loader, uri);
214                 Introspector result = parse.getEntity();
215                 String expected = "{\"interface-name\":\"key1\"}";
216                 
217                 String topEntity = "l-interface";
218                 String entity = "l-interface";
219                 
220                 testSet(result.marshal(false), parse, expected, topEntity, entity, version);
221
222         }
223         
224         /**
225          * Test set.
226          *
227          * @param json the json
228          * @param parse the parse
229          * @param expected the expected
230          * @param topEntity the top entity
231          * @param entity the entity
232          * @param version the version
233          */
234         public void testSet(String json, URIToObject parse, String expected, String topEntity, String entity, Version version) {
235                 assertEquals("blah", expected, json);
236                 
237                 assertEquals("top entity", topEntity, parse.getTopEntityName());
238
239                 assertEquals("entity", entity, parse.getEntityName());
240
241                 assertEquals("entity object", entity, parse.getEntity().getDbName());
242                 
243                 assertEquals("parent list object", 1, parse.getParentList().size());
244                 
245                 assertEquals("object version", version, parse.getObjectVersion());
246         }
247 }