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=========================================================
20 package org.onap.aai.parsers.uri;
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.introspection.*;
28 import org.onap.aai.setup.SchemaVersion;
30 import javax.annotation.PostConstruct;
31 import javax.ws.rs.core.UriBuilder;
32 import javax.xml.bind.JAXBException;
33 import java.io.UnsupportedEncodingException;
34 import java.net.MalformedURLException;
36 import java.net.URISyntaxException;
38 import static org.hamcrest.Matchers.hasProperty;
39 import static org.hamcrest.Matchers.is;
40 import static org.junit.Assert.assertTrue;
42 public class URIToRelationshipObjectTest extends AAISetup {
44 private SchemaVersion latest ;
45 private Loader loader;
48 public ExpectedException thrown = ExpectedException.none();
51 public void createLoader(){
52 latest = schemaVersions.getDefaultVersion();
53 loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, latest);
58 * @throws JAXBException the JAXB exception
59 * @throws AAIException the AAI exception
60 * @throws IllegalArgumentException the illegal argument exception
61 * @throws UnsupportedEncodingException the unsupported encoding exception
62 * @throws URISyntaxException
63 * @throws MalformedURLException the malformed URL exception
66 public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, URISyntaxException {
68 URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion() + "/cloud-infrastructure/cloud-regions/cloud-region/mycloudregionowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
69 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
70 Introspector result = parse.getResult();
71 String expected = "\\{\"related-to\":\"l-interface\",\"related-link\":\"/aai/" + latest + "/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\"\\}\\]\\}";
72 assertTrue("blah", result.marshal(false).matches(expected));
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
83 * @throws URISyntaxException
84 * @throws MalformedURLException the malformed URL exception
87 public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, URISyntaxException {
88 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudregionowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
89 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
90 Introspector result = parse.getResult();
91 String expected = "\\{\"related-to\":\"l-interface\",\"related-link\":\"/aai/" + latest + "/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\"\\}\\]\\}";
92 assertTrue("blah", result.marshal(false).matches(expected));
98 * Double key relationship.
100 * @throws JAXBException the JAXB exception
101 * @throws AAIException the AAI exception
102 * @throws IllegalArgumentException the illegal argument exception
103 * @throws UnsupportedEncodingException the unsupported encoding exception
104 * @throws URISyntaxException
105 * @throws MalformedURLException the malformed URL exception
108 public void doubleKeyRelationship() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, URISyntaxException {
109 URI uri = UriBuilder.fromPath("/aai/" + latest + "/cloud-infrastructure/complexes/complex/key1/ctag-pools/ctag-pool/key2/key3/").build();
110 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
111 Introspector result = parse.getResult();
112 String expected = "\\{\"related-to\":\"ctag-pool\",\"related-link\":\"/aai/" + latest + "/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\"\\}\\]\\}";
113 assertTrue("blah", result.marshal(false).matches(expected));
118 * Uri with non string key.
120 * @throws JAXBException the JAXB exception
121 * @throws AAIException the AAI exception
122 * @throws IllegalArgumentException the illegal argument exception
123 * @throws UnsupportedEncodingException the unsupported encoding exception
124 * @throws URISyntaxException
125 * @throws MalformedURLException the malformed URL exception
128 public void uriWithNonStringKey() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, URISyntaxException {
129 URI uri = UriBuilder.fromPath("/aai/" + latest + "/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/144").build();
130 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
131 Introspector result = parse.getResult();
132 String expected = "\\{\"related-to\":\"cvlan-tag\",\"related-link\":\"/aai/" + latest + "/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\"\\}\\]\\}";
133 assertTrue("blah", result.marshal(false).matches(expected));
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
144 public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
145 URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion() + "/cloud-infrastructure/cloud-regions/cloud-region/mycloudregionowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interadsfaces/l-interface/key3").build();
147 thrown.expect(AAIException.class);
148 thrown.expect(hasProperty("code", is("AAI_3000")));
150 URIToObject parse = new URIToObject(loader, uri);