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