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