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