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