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.db.props.AAIProperties;
39 import org.onap.aai.exceptions.AAIException;
40 import org.onap.aai.introspection.*;
41 import org.onap.aai.parsers.exceptions.DoesNotStartWithValidNamespaceException;
43 public class URIToDBKeyTest extends AAISetup {
45 private Loader loader;
48 public ExpectedException thrown = ExpectedException.none();
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
59 public void createLoader() {
60 loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
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")
68 URIToDBKey parse = new URIToDBKey(loader, uri);
69 Object result = parse.getResult();
71 String expected = "cloud-region/tenant/vserver/l-interface";
73 assertEquals("blah", expected, result);
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
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")
91 URIToDBKey parse = new URIToDBKey(loader, uri);
92 Object result = parse.getResult();
94 String expected = "cloud-region/tenant/vserver/l-interface";
96 assertEquals("blah", expected, result);
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
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")
114 thrown.expect(AAIException.class);
115 thrown.expect(hasProperty("code", is("AAI_3001")));
117 new URIToDBKey(loader, uri);
121 * NotValid namespace.
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
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")
134 thrown.expect(DoesNotStartWithValidNamespaceException.class);
135 URIToDBKey parse = new URIToDBKey(loader, uri);
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
147 public void noValidTokens()
148 throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
149 URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion() + "/cloud/blah/blah").build();
151 thrown.expect(AAIException.class);
152 thrown.expect(hasProperty("code", is("AAI_3000")));
154 new URIToDBKey(loader, uri);
158 * Starts with valid namespace.
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
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")
172 URIToDBKey parse = new URIToDBKey(loader, uri);
173 Object result = parse.getResult();
175 String expected = "cloud-region/tenant/vserver/l-interface";
177 assertEquals("blah", expected, result);
183 * @throws IllegalArgumentException the illegal argument exception
184 * @throws AAIException the AAI exception
185 * @throws UnsupportedEncodingException the unsupported encoding exception
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")
191 URIToDBKey parse = new URIToDBKey(loader, uri);
192 Object result = parse.getResult();
194 String expected = "vce/port-group/cvlan-tag";
196 assertEquals("blah", expected, result);