Merge "[AAI] Fix doc config files"
[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
21 package org.onap.aai.parsers.uri;
22
23 import static org.hamcrest.Matchers.hasProperty;
24 import static org.hamcrest.Matchers.is;
25 import static org.junit.Assert.assertEquals;
26
27 import java.io.UnsupportedEncodingException;
28 import java.net.URI;
29
30 import javax.annotation.PostConstruct;
31 import javax.ws.rs.core.UriBuilder;
32 import javax.xml.bind.JAXBException;
33
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.onap.aai.AAISetup;
38 import org.onap.aai.exceptions.AAIException;
39 import org.onap.aai.introspection.*;
40 import org.onap.aai.parsers.exceptions.DoesNotStartWithValidNamespaceException;
41
42 public class URIToDBKeyTest extends AAISetup {
43
44     private Loader loader;
45
46     @Rule
47     public ExpectedException thrown = ExpectedException.none();
48
49     /**
50      * Uri.
51      *
52      * @throws JAXBException the JAXB exception
53      * @throws AAIException the AAI exception
54      * @throws IllegalArgumentException the illegal argument exception
55      * @throws UnsupportedEncodingException the unsupported encoding exception
56      */
57     @PostConstruct
58     public void createLoader() {
59         loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
60     }
61
62     @Test
63     public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
64         URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion()
65                 + "/cloud-infrastructure/cloud-regions/cloud-region/cloudOwner-key/cloudRegion-key/tenants/tenant/tenantId-key/vservers/vserver/vserverId-key/l-interfaces/l-interface/key3")
66                 .build();
67         URIToDBKey parse = new URIToDBKey(loader, uri);
68         Object result = parse.getResult();
69
70         String expected = "cloud-region/tenant/vserver/l-interface";
71
72         assertEquals("blah", expected, result);
73
74     }
75
76     /**
77      * Uri no version.
78      *
79      * @throws JAXBException the JAXB exception
80      * @throws AAIException the AAI exception
81      * @throws IllegalArgumentException the illegal argument exception
82      * @throws UnsupportedEncodingException the unsupported encoding exception
83      */
84     @Test
85     public void uriNoVersion()
86             throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
87         URI uri = UriBuilder.fromPath(
88                 "/cloud-infrastructure/cloud-regions/cloud-region/cloudOwner-key/cloudRegion-key/tenants/tenant/tenantId-key/vservers/vserver/vserverId-key/l-interfaces/l-interface/key3")
89                 .build();
90         URIToDBKey parse = new URIToDBKey(loader, uri);
91         Object result = parse.getResult();
92
93         String expected = "cloud-region/tenant/vserver/l-interface";
94
95         assertEquals("blah", expected, result);
96
97     }
98
99     /**
100      * Bad URI.
101      *
102      * @throws JAXBException the JAXB exception
103      * @throws AAIException the AAI exception
104      * @throws IllegalArgumentException the illegal argument exception
105      * @throws UnsupportedEncodingException the unsupported encoding exception
106      */
107     @Test
108     public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
109         URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion()
110                 + "/cloud-infrastructure/tenants/tenant/key1/vservers/vserver/key2/l-interadsfaces/l-interface/key3")
111                 .build();
112
113         thrown.expect(AAIException.class);
114         thrown.expect(hasProperty("code", is("AAI_3001")));
115
116         new URIToDBKey(loader, uri);
117     }
118
119     /**
120      * NotValid namespace.
121      *
122      * @throws JAXBException the JAXB exception
123      * @throws DoesNotStartWithValidNamespaceException the AAI exception
124      * @throws IllegalArgumentException the illegal argument exception
125      * @throws UnsupportedEncodingException the unsupported encoding exception
126      */
127     @Test
128     public void notValidNamespace()
129             throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
130         URI uri = UriBuilder.fromPath(
131                 "/cloud-region/cloud-regions/cloud-region/cloudOwner-key/cloudRegion-key/tenants/tenant/tenantId-key/vservers/vserver/vserverId-key/l-interfaces/l-interface/key3")
132                 .build();
133         thrown.expect(DoesNotStartWithValidNamespaceException.class);
134         URIToDBKey parse = new URIToDBKey(loader, uri);
135     }
136
137     /**
138      * No valid tokens.
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 noValidTokens()
147             throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
148         URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion() + "/cloud/blah/blah").build();
149
150         thrown.expect(AAIException.class);
151         thrown.expect(hasProperty("code", is("AAI_3000")));
152
153         new URIToDBKey(loader, uri);
154     }
155
156     /**
157      * Starts with valid namespace.
158      *
159      * @throws JAXBException the JAXB exception
160      * @throws AAIException the AAI exception
161      * @throws IllegalArgumentException the illegal argument exception
162      * @throws UnsupportedEncodingException the unsupported encoding exception
163      */
164     @Test
165     public void startsWithValidNamespace()
166             throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
167         URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion()
168                 + "/cloud-infrastructure/cloud-regions/cloud-region/cloudOwner-key/cloudRegion-key/tenants/tenant/tenantId-key/vservers/vserver/vserverId-key/l-interfaces/l-interface/key3")
169                 .build();
170
171         URIToDBKey parse = new URIToDBKey(loader, uri);
172         Object result = parse.getResult();
173
174         String expected = "cloud-region/tenant/vserver/l-interface";
175
176         assertEquals("blah", expected, result);
177     }
178
179     /**
180      * Naming exceptions.
181      *
182      * @throws IllegalArgumentException the illegal argument exception
183      * @throws AAIException the AAI exception
184      * @throws UnsupportedEncodingException the unsupported encoding exception
185      */
186     @Test
187     public void namingExceptions() throws IllegalArgumentException, AAIException, UnsupportedEncodingException {
188         URI uri = UriBuilder.fromPath("network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/655")
189                 .build();
190         URIToDBKey parse = new URIToDBKey(loader, uri);
191         Object result = parse.getResult();
192
193         String expected = "vce/port-group/cvlan-tag";
194
195         assertEquals("blah", expected, result);
196
197     }
198
199 }