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