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.assertTrue;
27 import java.io.UnsupportedEncodingException;
28 import java.net.MalformedURLException;
30 import java.net.URISyntaxException;
32 import javax.annotation.PostConstruct;
33 import javax.ws.rs.core.UriBuilder;
34 import javax.xml.bind.JAXBException;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.onap.aai.AAISetup;
40 import org.onap.aai.exceptions.AAIException;
41 import org.onap.aai.introspection.*;
42 import org.onap.aai.setup.SchemaVersion;
43 import org.springframework.test.annotation.DirtiesContext;
45 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
46 public class URIToRelationshipObjectTest extends AAISetup {
48 private SchemaVersion latest;
49 private Loader loader;
52 public ExpectedException thrown = ExpectedException.none();
55 public void createLoader() {
56 latest = schemaVersions.getDefaultVersion();
57 loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, latest);
63 * @throws JAXBException the JAXB exception
64 * @throws AAIException the AAI exception
65 * @throws IllegalArgumentException the illegal argument exception
66 * @throws UnsupportedEncodingException the unsupported encoding exception
67 * @throws URISyntaxException
68 * @throws MalformedURLException the malformed URL exception
71 public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException,
74 URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion()
75 + "/cloud-infrastructure/cloud-regions/cloud-region/mycloudregionowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3")
77 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
78 Introspector result = parse.getResult();
79 String expected = "\\{\"related-to\":\"l-interface\",\"related-link\":\"/aai/" + latest
80 + "/cloud-infrastructure/cloud-regions/cloud-region/mycloudregionowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3\",\"relationship-data\":\\[\\{\"relationship-key\":\"cloud-region.cloud-owner\",\"relationship-value\":\"mycloudregionowner\"\\},\\{\"relationship-key\":\"cloud-region.cloud-region-id\",\"relationship-value\":\"mycloudregionid\"\\},\\{\"relationship-key\":\"tenant.tenant-id\",\"relationship-value\":\"key1\"\\},\\{\"relationship-key\":\"vserver.vserver-id\",\"relationship-value\":\"key2\"\\},\\{\"relationship-key\":\"l-interface.interface-name\",\"relationship-value\":\"key3\"\\}\\]\\}";
81 assertTrue("blah", result.marshal(false).matches(expected));
88 * @throws JAXBException the JAXB exception
89 * @throws AAIException the AAI exception
90 * @throws IllegalArgumentException the illegal argument exception
91 * @throws UnsupportedEncodingException the unsupported encoding exception
92 * @throws URISyntaxException
93 * @throws MalformedURLException the malformed URL exception
96 public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException,
97 UnsupportedEncodingException, URISyntaxException {
98 URI uri = UriBuilder.fromPath(
99 "/cloud-infrastructure/cloud-regions/cloud-region/mycloudregionowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3")
101 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
102 Introspector result = parse.getResult();
103 String expected = "\\{\"related-to\":\"l-interface\",\"related-link\":\"/aai/" + latest
104 + "/cloud-infrastructure/cloud-regions/cloud-region/mycloudregionowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3\",\"relationship-data\":\\[\\{\"relationship-key\":\"cloud-region.cloud-owner\",\"relationship-value\":\"mycloudregionowner\"\\},\\{\"relationship-key\":\"cloud-region.cloud-region-id\",\"relationship-value\":\"mycloudregionid\"\\},\\{\"relationship-key\":\"tenant.tenant-id\",\"relationship-value\":\"key1\"\\},\\{\"relationship-key\":\"vserver.vserver-id\",\"relationship-value\":\"key2\"\\},\\{\"relationship-key\":\"l-interface.interface-name\",\"relationship-value\":\"key3\"\\}\\]\\}";
105 assertTrue("blah", result.marshal(false).matches(expected));
110 * Double key relationship.
112 * @throws JAXBException the JAXB exception
113 * @throws AAIException the AAI exception
114 * @throws IllegalArgumentException the illegal argument exception
115 * @throws UnsupportedEncodingException the unsupported encoding exception
116 * @throws URISyntaxException
117 * @throws MalformedURLException the malformed URL exception
120 public void doubleKeyRelationship() throws JAXBException, AAIException, IllegalArgumentException,
121 UnsupportedEncodingException, URISyntaxException {
124 .fromPath("/aai/" + latest
125 + "/cloud-infrastructure/complexes/complex/key1/ctag-pools/ctag-pool/key2/key3/")
127 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
128 Introspector result = parse.getResult();
129 String expected = "\\{\"related-to\":\"ctag-pool\",\"related-link\":\"/aai/" + latest
130 + "/cloud-infrastructure/complexes/complex/key1/ctag-pools/ctag-pool/key2/key3\",\"relationship-data\":\\[\\{\"relationship-key\":\"complex.physical-location-id\",\"relationship-value\":\"key1\"\\},\\{\"relationship-key\":\"ctag-pool.target-pe\",\"relationship-value\":\"key2\"\\},\\{\"relationship-key\":\"ctag-pool.availability-zone-name\",\"relationship-value\":\"key3\"\\}\\]\\}";
131 assertTrue("blah", result.marshal(false).matches(expected));
136 * Uri with non string key.
138 * @throws JAXBException the JAXB exception
139 * @throws AAIException the AAI exception
140 * @throws IllegalArgumentException the illegal argument exception
141 * @throws UnsupportedEncodingException the unsupported encoding exception
142 * @throws URISyntaxException
143 * @throws MalformedURLException the malformed URL exception
146 public void uriWithNonStringKey() throws JAXBException, AAIException, IllegalArgumentException,
147 UnsupportedEncodingException, URISyntaxException {
150 .fromPath("/aai/" + latest
151 + "/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/144")
153 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
154 Introspector result = parse.getResult();
155 String expected = "\\{\"related-to\":\"cvlan-tag\",\"related-link\":\"/aai/" + latest
156 + "/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/144\",\"relationship-data\":\\[\\{\"relationship-key\":\"vce.vnf-id\",\"relationship-value\":\"key1\"\\},\\{\"relationship-key\":\"port-group.interface-id\",\"relationship-value\":\"key2\"\\},\\{\"relationship-key\":\"cvlan-tag.cvlan-tag\",\"relationship-value\":\"144\"\\}\\]\\}";
157 assertTrue("blah", result.marshal(false).matches(expected));
163 * @throws JAXBException the JAXB exception
164 * @throws AAIException the AAI exception
165 * @throws IllegalArgumentException the illegal argument exception
166 * @throws UnsupportedEncodingException the unsupported encoding exception
169 public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
170 URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion()
171 + "/cloud-infrastructure/cloud-regions/cloud-region/mycloudregionowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interadsfaces/l-interface/key3")
174 thrown.expect(AAIException.class);
175 thrown.expect(hasProperty("code", is("AAI_3000")));
177 URIToObject parse = new URIToObject(loader, uri);