2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.parsers.uri;
23 import static org.hamcrest.Matchers.hasProperty;
24 import static org.hamcrest.Matchers.is;
25 import static org.junit.Assert.assertEquals;
27 import java.io.UnsupportedEncodingException;
30 import javax.annotation.PostConstruct;
31 import javax.ws.rs.core.UriBuilder;
32 import javax.xml.bind.JAXBException;
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;
42 public class URIToDBKeyTest extends AAISetup {
44 private Loader loader;
47 public ExpectedException thrown = ExpectedException.none();
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
58 public void createLoader() {
59 loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
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")
67 URIToDBKey parse = new URIToDBKey(loader, uri);
68 Object result = parse.getResult();
70 String expected = "cloud-region/tenant/vserver/l-interface";
72 assertEquals("blah", expected, result);
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
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")
90 URIToDBKey parse = new URIToDBKey(loader, uri);
91 Object result = parse.getResult();
93 String expected = "cloud-region/tenant/vserver/l-interface";
95 assertEquals("blah", expected, result);
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
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")
113 thrown.expect(AAIException.class);
114 thrown.expect(hasProperty("code", is("AAI_3001")));
116 new URIToDBKey(loader, uri);
120 * NotValid namespace.
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
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")
133 thrown.expect(DoesNotStartWithValidNamespaceException.class);
134 URIToDBKey parse = new URIToDBKey(loader, uri);
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
146 public void noValidTokens()
147 throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
148 URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion() + "/cloud/blah/blah").build();
150 thrown.expect(AAIException.class);
151 thrown.expect(hasProperty("code", is("AAI_3000")));
153 new URIToDBKey(loader, uri);
157 * Starts with valid namespace.
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
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")
171 URIToDBKey parse = new URIToDBKey(loader, uri);
172 Object result = parse.getResult();
174 String expected = "cloud-region/tenant/vserver/l-interface";
176 assertEquals("blah", expected, result);
182 * @throws IllegalArgumentException the illegal argument exception
183 * @throws AAIException the AAI exception
184 * @throws UnsupportedEncodingException the unsupported encoding exception
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")
190 URIToDBKey parse = new URIToDBKey(loader, uri);
191 Object result = parse.getResult();
193 String expected = "vce/port-group/cvlan-tag";
195 assertEquals("blah", expected, result);