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