Merge "Remove the apache commons-lang dependency in aai-common"
[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
21 package org.onap.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.Ignore;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.onap.aai.AAISetup;
38 import org.onap.aai.exceptions.AAIException;
39 import org.onap.aai.introspection.Introspector;
40 import org.onap.aai.introspection.Loader;
41 import org.onap.aai.introspection.ModelType;
42 import org.onap.aai.parsers.exceptions.AAIIdentityMapParseException;
43 import org.onap.aai.parsers.exceptions.AmbiguousMapAAIException;
44 import org.onap.aai.setup.SchemaVersion;
45 import org.springframework.test.annotation.DirtiesContext;
46
47 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
48 public class RelationshipToURITest extends AAISetup {
49
50     private final ModelType modelType = ModelType.MOXY;
51     private final SchemaVersion version10 = new SchemaVersion("v10");
52
53     @Rule
54     public ExpectedException thrown = ExpectedException.none();
55
56     @Test
57     public void onlyLink() throws AAIException, URISyntaxException, IOException {
58         Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
59         Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-related-link.json"));
60         URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
61
62         RelationshipToURI parse = new RelationshipToURI(loader, obj);
63
64         URI uri = parse.getUri();
65
66         assertEquals("related-link is equal", expected.getPath(), uri.getPath());
67     }
68
69     @Test
70     public void onlyData() throws AAIException, URISyntaxException, IOException {
71         Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
72         Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-relationship-data.json"));
73         URI expected = new URI("/network/generic-vnfs/generic-vnf/key1");
74
75         RelationshipToURI parse = new RelationshipToURI(loader, obj);
76
77         URI uri = parse.getUri();
78
79         assertEquals("related-link is equal", expected, uri);
80     }
81
82     @Test
83     public void failV10() throws AAIException, URISyntaxException, IOException {
84         Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
85         Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
86         URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
87
88         thrown.expect(AAIIdentityMapParseException.class);
89         thrown.expect(hasProperty("code", is("AAI_3000")));
90         RelationshipToURI parse = new RelationshipToURI(loader, obj);
91         URI uri = parse.getUri();
92
93     }
94
95     @Test
96     public void failNothingToParse() throws AAIException, URISyntaxException, IOException {
97         Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
98         Introspector obj = loader.unmarshal("relationship", this.getJsonString("nothing-to-parse.json"));
99         URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
100
101         thrown.expect(AAIIdentityMapParseException.class);
102         thrown.expect(hasProperty("code", is("AAI_3000")));
103         RelationshipToURI parse = new RelationshipToURI(loader, obj);
104
105         URI uri = parse.getUri();
106
107     }
108
109     @Test
110     public void successV10() throws AAIException, URISyntaxException, IOException {
111         Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
112         Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
113         URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
114
115         RelationshipToURI parse = new RelationshipToURI(loader, obj);
116
117         URI uri = parse.getUri();
118
119         assertEquals("related-link is equal", expected, uri);
120
121     }
122
123     @Test
124     public void ambiguousRelationship() throws AAIException, URISyntaxException, IOException {
125         Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
126         Introspector obj = loader.unmarshal("relationship", this.getJsonString("ambiguous-relationship.json"));
127         URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
128
129         thrown.expect(AmbiguousMapAAIException.class);
130         thrown.expect(hasProperty("code", is("AAI_6146")));
131
132         RelationshipToURI parse = new RelationshipToURI(loader, obj);
133
134         URI uri = parse.getUri();
135
136         assertEquals("related-link is equal", expected, uri);
137
138     }
139
140     @Ignore
141     @Test
142     public void moreItemsThanRequired() throws AAIException, URISyntaxException, IOException {
143         Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
144         Introspector obj = loader.unmarshal("relationship", this.getJsonString("too-many-items-relationship.json"));
145         URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
146
147         RelationshipToURI parse = new RelationshipToURI(loader, obj);
148
149         URI uri = parse.getUri();
150
151         assertEquals("related-link is equal", expected.toString(), uri.toString());
152
153     }
154
155     @Test
156     public void twoTopLevelNodes() throws AAIException, URISyntaxException, IOException {
157         Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
158         Introspector obj = loader.unmarshal("relationship", this.getJsonString("two-top-level-relationship.json"));
159         URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
160
161         thrown.expect(AmbiguousMapAAIException.class);
162         thrown.expect(hasProperty("code", is("AAI_6146")));
163
164         RelationshipToURI parse = new RelationshipToURI(loader, obj);
165
166         URI uri = parse.getUri();
167
168         assertEquals("related-link is equal", expected, uri);
169
170     }
171
172     @Test
173     public void topLevelWithTwoKeys() throws AAIException, URISyntaxException, IOException {
174         Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
175         Introspector obj = loader.unmarshal("relationship", this.getJsonString("top-level-two-keys-relationship.json"));
176         URI expected = new URI(
177                 "/cloud-infrastructure/cloud-regions/cloud-region/key1/key2/availability-zones/availability-zone/key3");
178
179         RelationshipToURI parse = new RelationshipToURI(loader, obj);
180
181         URI uri = parse.getUri();
182
183         assertEquals("related-link is equal", expected.toString(), uri.toString());
184
185     }
186
187     private String getJsonString(String filename) throws IOException {
188
189         FileInputStream is = new FileInputStream("src/test/resources/bundleconfig-local/etc/relationship/" + filename);
190         String s = IOUtils.toString(is, "UTF-8");
191         IOUtils.closeQuietly(is);
192
193         return s;
194     }
195 }