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