e983d00755f611b3f4e5a32317f3215d1f0e78d2
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / parsers / uri / URIToRelationshipObjectTest.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.assertTrue;
26
27 import java.io.UnsupportedEncodingException;
28 import java.net.MalformedURLException;
29 import java.net.URI;
30 import java.net.URISyntaxException;
31
32 import javax.annotation.PostConstruct;
33 import javax.ws.rs.core.UriBuilder;
34 import javax.xml.bind.JAXBException;
35
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
44 public class URIToRelationshipObjectTest extends AAISetup {
45
46     private SchemaVersion latest;
47     private Loader loader;
48
49     @Rule
50     public ExpectedException thrown = ExpectedException.none();
51
52     @PostConstruct
53     public void createLoader() {
54         latest = schemaVersions.getDefaultVersion();
55         loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, latest);
56     }
57
58     /**
59      * Uri.
60      *
61      * @throws JAXBException the JAXB exception
62      * @throws AAIException the AAI exception
63      * @throws IllegalArgumentException the illegal argument exception
64      * @throws UnsupportedEncodingException the unsupported encoding exception
65      * @throws URISyntaxException
66      * @throws MalformedURLException the malformed URL exception
67      */
68     @Test
69     public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException,
70             URISyntaxException {
71
72         URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion()
73                 + "/cloud-infrastructure/cloud-regions/cloud-region/mycloudregionowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3")
74                 .build();
75         URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
76         Introspector result = parse.getResult();
77         String expected = "\\{\"related-to\":\"l-interface\",\"related-link\":\"/aai/" + latest
78                 + "/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\"\\}\\]\\}";
79         assertTrue("blah", result.marshal(false).matches(expected));
80
81     }
82
83     /**
84      * Uri no version.
85      *
86      * @throws JAXBException the JAXB exception
87      * @throws AAIException the AAI exception
88      * @throws IllegalArgumentException the illegal argument exception
89      * @throws UnsupportedEncodingException the unsupported encoding exception
90      * @throws URISyntaxException
91      * @throws MalformedURLException the malformed URL exception
92      */
93     @Test
94     public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException,
95             UnsupportedEncodingException, URISyntaxException {
96         URI uri = UriBuilder.fromPath(
97                 "/cloud-infrastructure/cloud-regions/cloud-region/mycloudregionowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3")
98                 .build();
99         URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
100         Introspector result = parse.getResult();
101         String expected = "\\{\"related-to\":\"l-interface\",\"related-link\":\"/aai/" + latest
102                 + "/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\"\\}\\]\\}";
103         assertTrue("blah", result.marshal(false).matches(expected));
104
105     }
106
107     /**
108      * Double key relationship.
109      *
110      * @throws JAXBException the JAXB exception
111      * @throws AAIException the AAI exception
112      * @throws IllegalArgumentException the illegal argument exception
113      * @throws UnsupportedEncodingException the unsupported encoding exception
114      * @throws URISyntaxException
115      * @throws MalformedURLException the malformed URL exception
116      */
117     @Test
118     public void doubleKeyRelationship() throws JAXBException, AAIException, IllegalArgumentException,
119             UnsupportedEncodingException, URISyntaxException {
120         URI uri =
121                 UriBuilder
122                         .fromPath("/aai/" + latest
123                                 + "/cloud-infrastructure/complexes/complex/key1/ctag-pools/ctag-pool/key2/key3/")
124                         .build();
125         URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
126         Introspector result = parse.getResult();
127         String expected = "\\{\"related-to\":\"ctag-pool\",\"related-link\":\"/aai/" + latest
128                 + "/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\"\\}\\]\\}";
129         assertTrue("blah", result.marshal(false).matches(expected));
130
131     }
132
133     /**
134      * Uri with non string key.
135      *
136      * @throws JAXBException the JAXB exception
137      * @throws AAIException the AAI exception
138      * @throws IllegalArgumentException the illegal argument exception
139      * @throws UnsupportedEncodingException the unsupported encoding exception
140      * @throws URISyntaxException
141      * @throws MalformedURLException the malformed URL exception
142      */
143     @Test
144     public void uriWithNonStringKey() throws JAXBException, AAIException, IllegalArgumentException,
145             UnsupportedEncodingException, URISyntaxException {
146         URI uri =
147                 UriBuilder
148                         .fromPath("/aai/" + latest
149                                 + "/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/144")
150                         .build();
151         URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
152         Introspector result = parse.getResult();
153         String expected = "\\{\"related-to\":\"cvlan-tag\",\"related-link\":\"/aai/" + latest
154                 + "/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\"\\}\\]\\}";
155         assertTrue("blah", result.marshal(false).matches(expected));
156     }
157
158     /**
159      * Bad URI.
160      *
161      * @throws JAXBException the JAXB exception
162      * @throws AAIException the AAI exception
163      * @throws IllegalArgumentException the illegal argument exception
164      * @throws UnsupportedEncodingException the unsupported encoding exception
165      */
166     @Test
167     public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
168         URI uri = UriBuilder.fromPath("/aai/" + loader.getVersion()
169                 + "/cloud-infrastructure/cloud-regions/cloud-region/mycloudregionowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interadsfaces/l-interface/key3")
170                 .build();
171
172         thrown.expect(AAIException.class);
173         thrown.expect(hasProperty("code", is("AAI_3000")));
174
175         URIToObject parse = new URIToObject(loader, uri);
176
177     }
178 }