Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / parsers / uri / URIToRelationshipObjectTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.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.ws.rs.core.UriBuilder;
33 import javax.xml.bind.JAXBException;
34
35 import org.junit.BeforeClass;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39
40 import org.openecomp.aai.db.props.AAIProperties;
41 import org.openecomp.aai.exceptions.AAIException;
42 import org.openecomp.aai.introspection.Introspector;
43 import org.openecomp.aai.introspection.Loader;
44 import org.openecomp.aai.introspection.LoaderFactory;
45 import org.openecomp.aai.introspection.ModelType;
46 import org.openecomp.aai.introspection.Version;
47
48
49 public class URIToRelationshipObjectTest {
50
51         private Version latest = AAIProperties.LATEST;
52         private Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, latest);
53
54         @Rule
55         public ExpectedException thrown = ExpectedException.none();
56         
57         /**
58          * Configure.
59          */
60         @BeforeClass
61         public static void configure() {
62                 System.setProperty("AJSC_HOME", "./src/test/resources/");
63                 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
64         }
65         
66         /**
67          * Uri.
68          *
69          * @throws JAXBException the JAXB exception
70          * @throws AAIException the AAI exception
71          * @throws IllegalArgumentException the illegal argument exception
72          * @throws UnsupportedEncodingException the unsupported encoding exception
73          * @throws URISyntaxException 
74          * @throws MalformedURLException the malformed URL exception
75          */
76         @Test
77     public void uri() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, URISyntaxException {
78                 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();
79                 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
80                 Introspector result = parse.getResult();
81                 String expected = "\\{\"related-to\":\"l-interface\",\"related-link\":\".*?:8443/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\"\\}\\]\\}";
82                 assertTrue("blah", result.marshal(false).matches(expected));
83                 
84         }
85         
86         /**
87          * Uri no version.
88          *
89          * @throws JAXBException the JAXB exception
90          * @throws AAIException the AAI exception
91          * @throws IllegalArgumentException the illegal argument exception
92          * @throws UnsupportedEncodingException the unsupported encoding exception
93          * @throws URISyntaxException 
94          * @throws MalformedURLException the malformed URL exception
95          */
96         @Test
97     public void uriNoVersion() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, URISyntaxException {
98                 URI uri = UriBuilder.fromPath("/cloud-infrastructure/cloud-regions/cloud-region/mycloudregionowner/mycloudregionid/tenants/tenant/key1/vservers/vserver/key2/l-interfaces/l-interface/key3").build();
99                 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
100                 Introspector result = parse.getResult();
101                 String expected = "\\{\"related-to\":\"l-interface\",\"related-link\":\".*?:8443/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\"\\}\\]\\}";
102                 assertTrue("blah", result.marshal(false).matches(expected));
103
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, UnsupportedEncodingException, URISyntaxException {
119                 URI uri = UriBuilder.fromPath("/aai/" + latest + "/cloud-infrastructure/complexes/complex/key1/ctag-pools/ctag-pool/key2/key3/").build();
120                 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
121                 Introspector result = parse.getResult();
122                 String expected = "\\{\"related-to\":\"ctag-pool\",\"related-link\":\".*?:8443/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\"\\}\\]\\}";
123                 assertTrue("blah", result.marshal(false).matches(expected));
124
125         }
126         
127         /**
128          * Uri with non string key.
129          *
130          * @throws JAXBException the JAXB exception
131          * @throws AAIException the AAI exception
132          * @throws IllegalArgumentException the illegal argument exception
133          * @throws UnsupportedEncodingException the unsupported encoding exception
134          * @throws URISyntaxException 
135          * @throws MalformedURLException the malformed URL exception
136          */
137         @Test
138         public void uriWithNonStringKey() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException, URISyntaxException {
139                 URI uri = UriBuilder.fromPath("/aai/" + latest + "/network/vces/vce/key1/port-groups/port-group/key2/cvlan-tags/cvlan-tag/144").build();
140                 URIToRelationshipObject parse = new URIToRelationshipObject(loader, uri);
141                 Introspector result = parse.getResult();
142                 String expected = "\\{\"related-to\":\"cvlan-tag\",\"related-link\":\".*?:8443/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\"\\}\\]\\}";
143                 assertTrue("blah", result.marshal(false).matches(expected));
144         }
145         /**
146          * Bad URI.
147          *
148          * @throws JAXBException the JAXB exception
149          * @throws AAIException the AAI exception
150          * @throws IllegalArgumentException the illegal argument exception
151          * @throws UnsupportedEncodingException the unsupported encoding exception
152          */
153         @Test
154     public void badURI() throws JAXBException, AAIException, IllegalArgumentException, UnsupportedEncodingException {
155                 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();
156                 
157                 thrown.expect(AAIException.class);
158                 thrown.expect(hasProperty("code",  is("AAI_3000")));
159                 
160                 URIToObject parse = new URIToObject(loader, uri);
161                 
162         }
163 }