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