Update license files, sonar plugin and fix tests
[aai/aai-common.git] / aai-core / src / test / java / org / openecomp / aai / parsers / relationship / RelationshipToURITest.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.relationship;
22
23 import static org.hamcrest.Matchers.hasProperty;
24 import static org.hamcrest.Matchers.is;
25 import static org.junit.Assert.assertEquals;
26
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.net.URI;
30 import java.net.URISyntaxException;
31
32 import org.apache.commons.io.IOUtils;
33 import org.junit.BeforeClass;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37
38 import org.openecomp.aai.exceptions.AAIException;
39 import org.openecomp.aai.introspection.Introspector;
40 import org.openecomp.aai.introspection.Loader;
41 import org.openecomp.aai.introspection.LoaderFactory;
42 import org.openecomp.aai.introspection.ModelType;
43 import org.openecomp.aai.introspection.Version;
44 import org.openecomp.aai.parsers.exceptions.AAIIdentityMapParseException;
45 import org.openecomp.aai.parsers.exceptions.AmbiguousMapAAIException;
46 import org.openecomp.aai.serialization.queryformats.QueryFormatTestHelper;
47 import org.openecomp.aai.util.AAIConstants;
48
49 public class RelationshipToURITest {
50
51         
52         
53         private final ModelType modelType = ModelType.MOXY;
54         private final Version version10 = Version.v10;
55         private final Version version9 = Version.v9;
56         
57         @Rule
58         public ExpectedException thrown = ExpectedException.none();
59         
60         @BeforeClass
61         public static void setup() throws NoSuchFieldException, SecurityException, Exception {
62                 System.setProperty("AJSC_HOME", "./src/test/resources/");
63                 System.setProperty("BUNDLECONFIG_DIR", "bundleconfig-local");
64                 QueryFormatTestHelper.setFinalStatic(AAIConstants.class.getField("AAI_HOME_ETC_OXM"), "src/test/resources/org/openecomp/aai/introspection/");
65
66         }
67         
68         @Test
69         public void onlyLink() throws AAIException, URISyntaxException, IOException {
70                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
71                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-related-link.json"));
72                 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
73                 
74                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
75                 
76                 URI uri = parse.getUri();
77                 
78                 assertEquals("related-link is equal", expected.getPath(), uri.getPath());
79         }
80         
81         @Test
82         public void onlyData() throws AAIException, URISyntaxException, IOException {
83                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
84                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-relationship-data.json"));
85                 URI expected = new URI("/network/test-objects/test-object/key1");
86
87                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
88                 
89                 URI uri = parse.getUri();
90                 
91                 assertEquals("related-link is equal", expected, uri);
92         }
93         
94         @Test
95         public void failV10() throws AAIException, URISyntaxException, IOException {
96                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
97                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
98                 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
99                 
100                 thrown.expect(AAIIdentityMapParseException.class);
101                 thrown.expect(hasProperty("code", is("AAI_3000")));
102                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
103                 
104
105                 URI uri = parse.getUri();
106                 
107         }
108         
109         @Test
110         public void successV9() throws AAIException, URISyntaxException, IOException {
111                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
112                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
113                 URI expected = new URI("/network/test-objects/test-object/key2");
114                 
115                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
116                 
117
118                 URI uri = parse.getUri();
119                 
120                 assertEquals("related-link is equal", expected, uri);
121
122                 
123         }
124         
125         @Test
126         public void failV9() throws AAIException, URISyntaxException, IOException {
127                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
128                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
129                 URI expected = new URI("/network/test-objects/test-object/key1");
130                 
131                 thrown.expect(AAIIdentityMapParseException.class);
132                 thrown.expect(hasProperty("code", is("AAI_3000")));
133                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
134                 
135
136                 URI uri = parse.getUri();
137                 
138         }
139         
140         @Test
141         public void successV10() throws AAIException, URISyntaxException, IOException {
142                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
143                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
144                 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
145                 
146                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
147                 
148
149                 URI uri = parse.getUri();
150                 
151                 assertEquals("related-link is equal", expected, uri);
152
153                 
154         }
155         
156         @Test
157         public void ambiguousRelationship() throws AAIException, URISyntaxException, IOException {
158                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
159                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("ambiguous-relationship.json"));
160                 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
161                 
162                 thrown.expect(AmbiguousMapAAIException.class);
163                 thrown.expect(hasProperty("code", is("AAI_6146")));
164                 
165                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
166                 
167                 URI uri = parse.getUri();
168                 
169                 assertEquals("related-link is equal", expected, uri);
170
171                 
172         }
173         
174         @Test
175         public void moreItemsThanRequired() throws AAIException, URISyntaxException, IOException {
176                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
177                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("too-many-items-relationship.json"));
178                 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
179                 
180                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
181
182                 URI uri = parse.getUri();
183                 
184                 assertEquals("related-link is equal", expected.toString(), uri.toString());
185                 
186         }
187         
188         @Test
189         public void twoTopLevelNodes() throws AAIException, URISyntaxException, IOException {
190                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
191                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("two-top-level-relationship.json"));
192                 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
193                 
194                 thrown.expect(AmbiguousMapAAIException.class);
195                 thrown.expect(hasProperty("code", is("AAI_6146")));
196                 
197                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
198                 
199                 URI uri = parse.getUri();
200                 
201                 assertEquals("related-link is equal", expected, uri);
202                 
203         }
204         
205         @Test
206         public void topLevelWithTwoKeys() throws AAIException, URISyntaxException, IOException {
207                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
208                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("top-level-two-keys-relationship.json"));
209                 URI expected = new URI("/cloud-infrastructure/cloud-regions/cloud-region/key1/key2/availability-zones/availability-zone/key3");
210                 
211                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
212                 
213                 URI uri = parse.getUri();
214                 
215                 assertEquals("related-link is equal", expected.toString(), uri.toString());
216                 
217         }
218         
219         
220         private String getJsonString(String filename) throws IOException {
221                 
222                 
223                 FileInputStream is = new FileInputStream("src/test/resources/org/openecomp/aai/parsers/relationship/" + filename);
224                 String s =  IOUtils.toString(is, "UTF-8"); 
225                 IOUtils.closeQuietly(is);
226                 
227                 return s;
228         }
229 }