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