4f15d278897259b79b403a4365f757c392cbdf37
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.parsers.uri;
23
24 import org.junit.Ignore;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.onap.aai.AAISetup;
29 import org.onap.aai.db.props.AAIProperties;
30 import org.onap.aai.exceptions.AAIException;
31 import org.onap.aai.introspection.*;
32
33 import javax.ws.rs.core.UriBuilder;
34 import javax.xml.bind.JAXBException;
35 import java.io.UnsupportedEncodingException;
36 import java.net.MalformedURLException;
37 import java.net.URI;
38 import java.net.URISyntaxException;
39
40 import static org.hamcrest.Matchers.hasProperty;
41 import static org.hamcrest.Matchers.is;
42 import static org.junit.Assert.assertTrue;
43
44
45 public class URIToRelationshipObjectTest extends AAISetup {
46
47         private Version latest = Version.v10;
48         private Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, latest);
49
50         @Rule
51         public ExpectedException thrown = ExpectedException.none();
52
53         /**
54          * Uri.
55          *
56          * @throws JAXBException the JAXB exception
57          * @throws AAIException the AAI exception
58          * @throws IllegalArgumentException the illegal argument exception
59          * @throws UnsupportedEncodingException the unsupported encoding exception
60          * @throws URISyntaxException 
61          * @throws MalformedURLException the malformed URL exception
62          */
63         @Test
64     public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, URISyntaxException {
65                 
66                 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();
67                 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
68                 Introspector result = parse.getResult();
69                 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\"\\}\\]\\}";
70                 assertTrue("blah", result.marshal(false).matches(expected));
71                 
72         }
73         
74         /**
75          * Uri no version.
76          *
77          * @throws JAXBException the JAXB exception
78          * @throws AAIException the AAI exception
79          * @throws IllegalArgumentException the illegal argument exception
80          * @throws UnsupportedEncodingException the unsupported encoding exception
81          * @throws URISyntaxException 
82          * @throws MalformedURLException the malformed URL exception
83          */
84         @Test
85     public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, URISyntaxException {
86                 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudregionowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
87                 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
88                 Introspector result = parse.getResult();
89                 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\"\\}\\]\\}";
90                 assertTrue("blah", result.marshal(false).matches(expected));
91
92                 
93         }
94
95         /**
96          * Double key relationship.
97          *
98          * @throws JAXBException the JAXB exception
99          * @throws AAIException the AAI exception
100          * @throws IllegalArgumentException the illegal argument exception
101          * @throws UnsupportedEncodingException the unsupported encoding exception
102          * @throws URISyntaxException 
103          * @throws MalformedURLException the malformed URL exception
104          */
105         @Test
106         public void doubleKeyRelationship() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, URISyntaxException {
107                 URI uri = UriBuilder.fromPath("/aai/" + latest + "/cloud-infrastructure/complexes/complex/key1/ctag-pools/ctag-pool/key2/key3/").build();
108                 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
109                 Introspector result = parse.getResult();
110                 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\"\\}\\]\\}";
111                 assertTrue("blah", result.marshal(false).matches(expected));
112
113         }
114         
115         /**
116          * Uri with non string key.
117          *
118          * @throws JAXBException the JAXB exception
119          * @throws AAIException the AAI exception
120          * @throws IllegalArgumentException the illegal argument exception
121          * @throws UnsupportedEncodingException the unsupported encoding exception
122          * @throws URISyntaxException 
123          * @throws MalformedURLException the malformed URL exception
124          */
125         @Test
126         public void uriWithNonStringKey() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, URISyntaxException {
127                 URI uri = UriBuilder.fromPath("/aai/" + latest + "/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/144").build();
128                 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
129                 Introspector result = parse.getResult();
130                 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\"\\}\\]\\}";
131                 assertTrue("blah", result.marshal(false).matches(expected));
132         }
133         /**
134          * Bad URI.
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          */
141         @Test
142     public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
143                 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();
144                 
145                 thrown.expect(AAIException.class);
146                 thrown.expect(hasProperty("code",  is("AAI_3000")));
147                 
148                 URIToObject parse = new URIToObject(loader, uri);
149                 
150         }
151 }