4d0ec6caa0e3be764b6fab441c7b35b4dd9c26db
[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.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;
29
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;
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 public class URIToRelationshipObjectTest extends AAISetup {
43
44     private SchemaVersion latest ;
45     private Loader loader;
46
47     @Rule
48     public ExpectedException thrown = ExpectedException.none();
49
50     @PostConstruct
51     public void createLoader(){
52         latest = schemaVersions.getDefaultVersion();
53         loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, latest);
54     }
55     /**
56      * Uri.
57      *
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
64      */
65     @Test
66     public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, URISyntaxException {
67         
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));
73         
74     }
75     
76     /**
77      * Uri no version.
78      *
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
85      */
86     @Test
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));
93
94         
95     }
96
97     /**
98      * Double key relationship.
99      *
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
106      */
107     @Test
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));
114
115     }
116     
117     /**
118      * Uri with non string key.
119      *
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
126      */
127     @Test
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));
134     }
135     /**
136      * Bad URI.
137      *
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
142      */
143     @Test
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();
146         
147         thrown.expect(AAIException.class);
148         thrown.expect(hasProperty("code",  is("AAI_3000")));
149         
150         URIToObject parse = new URIToObject(loader, uri);
151         
152     }
153 }