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