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