Sync up the changes for v15
[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 import org.onap.aai.setup.SchemaVersion;
33 import org.springframework.test.annotation.DirtiesContext;
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 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
45 public class RelationshipToURITest extends AAISetup {
46
47         private final ModelType modelType = ModelType.MOXY;
48         private final SchemaVersion version10 = new SchemaVersion("v10");
49         
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
116                 URI uri = parse.getUri();
117                 
118                 assertEquals("related-link is equal", expected, uri);
119
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
141         @Ignore
142         @Test
143         public void moreItemsThanRequired() throws AAIException, URISyntaxException, IOException {
144                 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
145                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("too-many-items-relationship.json"));
146                 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
147                 
148                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
149
150                 URI uri = parse.getUri();
151                 
152                 assertEquals("related-link is equal", expected.toString(), uri.toString());
153                 
154         }
155         
156         @Test
157         public void twoTopLevelNodes() throws AAIException, URISyntaxException, IOException {
158                 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
159                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("two-top-level-relationship.json"));
160                 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
161                 
162                 thrown.expect(AmbiguousMapAAIException.class);
163                 thrown.expect(hasProperty("code", is("AAI_6146")));
164                 
165                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
166                 
167                 URI uri = parse.getUri();
168                 
169                 assertEquals("related-link is equal", expected, uri);
170                 
171         }
172         
173         @Test
174         public void topLevelWithTwoKeys() throws AAIException, URISyntaxException, IOException {
175                 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
176                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("top-level-two-keys-relationship.json"));
177                 URI expected = new URI("/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         
188         private String getJsonString(String filename) throws IOException {
189                 
190                 
191                 FileInputStream is = new FileInputStream("src/test/resources/bundleconfig-local/etc/relationship/" + filename);
192                 String s =  IOUtils.toString(is, "UTF-8"); 
193                 IOUtils.closeQuietly(is);
194                 
195                 return s;
196         }
197 }