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