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