Update the license for 2017-2018 license
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / parsers / uri / URIToDBKeyTest.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.junit.Ignore;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.onap.aai.AAISetup;
27 import org.onap.aai.exceptions.AAIException;
28 import org.onap.aai.parsers.exceptions.DoesNotStartWithValidNamespaceException;
29 import org.onap.aai.db.props.AAIProperties;
30 import org.onap.aai.introspection.*;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
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
38 import static org.hamcrest.Matchers.hasProperty;
39 import static org.hamcrest.Matchers.is;
40 import static org.junit.Assert.assertEquals;
41
42
43
44 @PrepareForTest(ModelInjestor.class)
45 public class URIToDBKeyTest extends AAISetup {
46
47         private Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
48
49         @Rule
50         public ExpectedException thrown = ExpectedException.none();
51         
52         /**
53          * Uri.
54          *
55          * @throws JAXBException the JAXB exception
56          * @throws AAIException the AAI exception
57          * @throws IllegalArgumentException the illegal argument exception
58          * @throws UnsupportedEncodingException the unsupported encoding exception
59          */
60         @Test
61     public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
62                 URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion() + "/cloud-infrastructure/cloud-regions/cloud-region/cloudOwner-key/cloudRegion-key/tenants/tenant/tenantId-key/vservers/vserver/vserverId-key/l-interfaces/l-interface/key3").build();
63                 URIToDBKey parse = new URIToDBKey(loader, uri);
64                 Object result = parse.getResult();
65
66                 String expected = "cloud-region/tenant/vserver/l-interface";
67                 
68                 assertEquals("blah", expected, result);
69                 
70         }
71         
72         /**
73          * Uri no version.
74          *
75          * @throws JAXBException the JAXB exception
76          * @throws AAIException the AAI exception
77          * @throws IllegalArgumentException the illegal argument exception
78          * @throws UnsupportedEncodingException the unsupported encoding exception
79          */
80         @Test
81     public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
82                 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/cloudOwner-key/cloudRegion-key/tenants/tenant/tenantId-key/vservers/vserver/vserverId-key/l-interfaces/l-interface/key3").build();
83                 URIToDBKey parse = new URIToDBKey(loader, uri);
84                 Object result = parse.getResult();
85                 
86                 String expected = "cloud-region/tenant/vserver/l-interface";
87                 
88                 assertEquals("blah", expected, result);
89                 
90         }
91         
92
93         /**
94          * Bad URI.
95          *
96          * @throws JAXBException the JAXB exception
97          * @throws AAIException the AAI exception
98          * @throws IllegalArgumentException the illegal argument exception
99          * @throws UnsupportedEncodingException the unsupported encoding exception
100          */
101         @Test
102     public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
103                 URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion() + "/cloud-infrastructure/tenants/tenant/key1/vservers/vserver/key2/l-interadsfaces/l-interface/key3").build();
104                 
105                 thrown.expect(AAIException.class);
106                 thrown.expect(hasProperty("code",  is("AAI_3001")));
107                 
108                 new URIToDBKey(loader, uri);
109         }
110         
111         /**
112          * NotValid namespace.
113          *
114          * @throws JAXBException the JAXB exception
115          * @throws DoesNotStartWithValidNamespaceException the AAI exception
116          * @throws IllegalArgumentException the illegal argument exception
117          * @throws UnsupportedEncodingException the unsupported encoding exception
118          */
119         @Test
120     public void notValidNamespace() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
121                 URI uri = UriBuilder.fromPath("/cloud-region/cloud-regions/cloud-region/cloudOwner-key/cloudRegion-key/tenants/tenant/tenantId-key/vservers/vserver/vserverId-key/l-interfaces/l-interface/key3").build();
122                 thrown.expect(DoesNotStartWithValidNamespaceException.class);
123                 URIToDBKey parse = new URIToDBKey(loader, uri);
124         }
125         
126         
127         /**
128          * No valid tokens.
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 noValidTokens() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
137                 URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion() + "/cloud/blah/blah").build();
138                 
139                 thrown.expect(AAIException.class);
140                 thrown.expect(hasProperty("code",  is("AAI_3000")));
141                 
142                 new URIToDBKey(loader, uri);
143         }
144         
145         /**
146          * Starts with valid namespace.
147          *
148          * @throws JAXBException the JAXB exception
149          * @throws AAIException the AAI exception
150          * @throws IllegalArgumentException the illegal argument exception
151          * @throws UnsupportedEncodingException the unsupported encoding exception
152          */
153         @Test
154     public void startsWithValidNamespace() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
155                 URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion() + "/cloud-infrastructure/cloud-regions/cloud-region/cloudOwner-key/cloudRegion-key/tenants/tenant/tenantId-key/vservers/vserver/vserverId-key/l-interfaces/l-interface/key3").build();
156                 
157                 URIToDBKey parse = new URIToDBKey(loader, uri);
158                 Object result = parse.getResult();
159
160                 String expected = "cloud-region/tenant/vserver/l-interface";
161                 
162                 assertEquals("blah", expected, result);
163         }
164         
165         /**
166          * Naming exceptions.
167          *
168          * @throws IllegalArgumentException the illegal argument exception
169          * @throws AAIException the AAI exception
170          * @throws UnsupportedEncodingException the unsupported encoding exception
171          */
172         @Test
173     public void namingExceptions() throws IllegalArgumentException, AAIException, UnsupportedEncodingException {
174                 URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655").build();
175                 URIToDBKey parse = new URIToDBKey(loader, uri);
176                 Object result = parse.getResult();
177
178                 String expected = "vce/port-group/cvlan-tag";
179                 
180                 assertEquals("blah", expected, result);
181                 
182     }
183                 
184 }