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