7656661bbad4d13634122edd44e8582edf219b08
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.parsers.uri;
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.exceptions.AAIException;
30 import org.onap.aai.parsers.exceptions.DoesNotStartWithValidNamespaceException;
31 import org.onap.aai.db.props.AAIProperties;
32 import org.onap.aai.introspection.*;
33 import org.powermock.core.classloader.annotations.PrepareForTest;
34
35 import javax.ws.rs.core.UriBuilder;
36 import javax.xml.bind.JAXBException;
37 import java.io.UnsupportedEncodingException;
38 import java.net.URI;
39
40 import static org.hamcrest.Matchers.hasProperty;
41 import static org.hamcrest.Matchers.is;
42 import static org.junit.Assert.assertEquals;
43
44
45
46 @PrepareForTest(ModelInjestor.class)
47 public class URIToDBKeyTest extends AAISetup {
48
49         private Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST);
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/cloudOwner-key/cloudRegion-key/tenants/tenant/tenantId-key/vservers/vserver/vserverId-key/l-interfaces/l-interface/key3").build();
65                 URIToDBKey parse = new URIToDBKey(loader, uri);
66                 Object result = parse.getResult();
67
68                 String expected = "cloud-region/tenant/vserver/l-interface";
69                 
70                 assertEquals("blah", expected, result);
71                 
72         }
73         
74         /**
75          * Uri no version.
76          *
77          * @throws JAXBException the JAXB exception
78          * @throws AAIException the AAI exception
79          * @throws IllegalArgumentException the illegal argument exception
80          * @throws UnsupportedEncodingException the unsupported encoding exception
81          */
82         @Test
83     public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
84                 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();
85                 URIToDBKey parse = new URIToDBKey(loader, uri);
86                 Object result = parse.getResult();
87                 
88                 String expected = "cloud-region/tenant/vserver/l-interface";
89                 
90                 assertEquals("blah", expected, result);
91                 
92         }
93         
94
95         /**
96          * Bad URI.
97          *
98          * @throws JAXBException the JAXB exception
99          * @throws AAIException the AAI exception
100          * @throws IllegalArgumentException the illegal argument exception
101          * @throws UnsupportedEncodingException the unsupported encoding exception
102          */
103         @Test
104     public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
105                 URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion() + "/cloud-infrastructure/tenants/tenant/key1/vservers/vserver/key2/l-interadsfaces/l-interface/key3").build();
106                 
107                 thrown.expect(AAIException.class);
108                 thrown.expect(hasProperty("code",  is("AAI_3001")));
109                 
110                 new URIToDBKey(loader, uri);
111         }
112         
113         /**
114          * NotValid namespace.
115          *
116          * @throws JAXBException the JAXB exception
117          * @throws DoesNotStartWithValidNamespaceException the AAI exception
118          * @throws IllegalArgumentException the illegal argument exception
119          * @throws UnsupportedEncodingException the unsupported encoding exception
120          */
121         @Test
122     public void notValidNamespace() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
123                 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();
124                 thrown.expect(DoesNotStartWithValidNamespaceException.class);
125                 URIToDBKey parse = new URIToDBKey(loader, uri);
126         }
127         
128         
129         /**
130          * No valid tokens.
131          *
132          * @throws JAXBException the JAXB exception
133          * @throws AAIException the AAI exception
134          * @throws IllegalArgumentException the illegal argument exception
135          * @throws UnsupportedEncodingException the unsupported encoding exception
136          */
137         @Test
138     public void noValidTokens() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
139                 URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion() + "/cloud/blah/blah").build();
140                 
141                 thrown.expect(AAIException.class);
142                 thrown.expect(hasProperty("code",  is("AAI_3000")));
143                 
144                 new URIToDBKey(loader, uri);
145         }
146         
147         /**
148          * Starts with valid namespace.
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 startsWithValidNamespace() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
157                 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();
158                 
159                 URIToDBKey parse = new URIToDBKey(loader, uri);
160                 Object result = parse.getResult();
161
162                 String expected = "cloud-region/tenant/vserver/l-interface";
163                 
164                 assertEquals("blah", expected, result);
165         }
166         
167         /**
168          * Naming exceptions.
169          *
170          * @throws IllegalArgumentException the illegal argument exception
171          * @throws AAIException the AAI exception
172          * @throws UnsupportedEncodingException the unsupported encoding exception
173          */
174         @Test
175     public void namingExceptions() throws IllegalArgumentException, AAIException, UnsupportedEncodingException {
176                 URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655").build();
177                 URIToDBKey parse = new URIToDBKey(loader, uri);
178                 Object result = parse.getResult();
179
180                 String expected = "vce/port-group/cvlan-tag";
181                 
182                 assertEquals("blah", expected, result);
183                 
184     }
185                 
186 }