757fbc1ff728946a0e820a563935af9debf6b11d
[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.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.onap.aai.AAISetup;
26 import org.onap.aai.exceptions.AAIException;
27 import org.onap.aai.parsers.exceptions.DoesNotStartWithValidNamespaceException;
28 import org.onap.aai.db.props.AAIProperties;
29 import org.onap.aai.introspection.*;
30
31 import javax.annotation.PostConstruct;
32 import javax.ws.rs.core.UriBuilder;
33 import javax.xml.bind.JAXBException;
34 import java.io.UnsupportedEncodingException;
35 import java.net.URI;
36
37 import static org.hamcrest.Matchers.hasProperty;
38 import static org.hamcrest.Matchers.is;
39 import static org.junit.Assert.assertEquals;
40
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() + "/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 }