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