EdgeRules throws descriptive error on invalid rule
[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                 
88
89                 URI uri = parse.getUri();
90                 
91         }
92         
93         @Test
94         public void successV9() throws AAIException, URISyntaxException, IOException {
95                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
96                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
97                 URI expected = new URI("/network/test-objects/test-object/key2");
98                 
99                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
100                 
101
102                 URI uri = parse.getUri();
103                 
104                 assertEquals("related-link is equal", expected, uri);
105
106                 
107         }
108         
109         @Test
110         public void failV9() throws AAIException, URISyntaxException, IOException {
111                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
112                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
113                 URI expected = new URI("/network/test-objects/test-object/key1");
114                 
115                 thrown.expect(AAIIdentityMapParseException.class);
116                 thrown.expect(hasProperty("code", is("AAI_3000")));
117                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
118                 
119
120                 URI uri = parse.getUri();
121                 
122         }
123         
124         @Test
125         public void successV10() throws AAIException, URISyntaxException, IOException {
126                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
127                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
128                 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
129                 
130                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
131                 
132
133                 URI uri = parse.getUri();
134                 
135                 assertEquals("related-link is equal", expected, uri);
136
137                 
138         }
139         
140         @Test
141         public void ambiguousRelationship() throws AAIException, URISyntaxException, IOException {
142                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
143                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("ambiguous-relationship.json"));
144                 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
145                 
146                 thrown.expect(AmbiguousMapAAIException.class);
147                 thrown.expect(hasProperty("code", is("AAI_6146")));
148                 
149                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
150                 
151                 URI uri = parse.getUri();
152                 
153                 assertEquals("related-link is equal", expected, uri);
154
155                 
156         }
157
158         @Ignore
159         @Test
160         public void moreItemsThanRequired() throws AAIException, URISyntaxException, IOException {
161                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
162                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("too-many-items-relationship.json"));
163                 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
164                 
165                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
166
167                 URI uri = parse.getUri();
168                 
169                 assertEquals("related-link is equal", expected.toString(), uri.toString());
170                 
171         }
172         
173         @Test
174         public void twoTopLevelNodes() throws AAIException, URISyntaxException, IOException {
175                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
176                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("two-top-level-relationship.json"));
177                 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
178                 
179                 thrown.expect(AmbiguousMapAAIException.class);
180                 thrown.expect(hasProperty("code", is("AAI_6146")));
181                 
182                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
183                 
184                 URI uri = parse.getUri();
185                 
186                 assertEquals("related-link is equal", expected, uri);
187                 
188         }
189         
190         @Test
191         public void topLevelWithTwoKeys() throws AAIException, URISyntaxException, IOException {
192                 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
193                 Introspector obj = loader.unmarshal("relationship", this.getJsonString("top-level-two-keys-relationship.json"));
194                 URI expected = new URI("/cloud-infrastructure/cloud-regions/cloud-region/key1/key2/availability-zones/availability-zone/key3");
195                 
196                 RelationshipToURI parse = new RelationshipToURI(loader, obj);
197                 
198                 URI uri = parse.getUri();
199                 
200                 assertEquals("related-link is equal", expected.toString(), uri.toString());
201                 
202         }
203         
204         
205         private String getJsonString(String filename) throws IOException {
206                 
207                 
208                 FileInputStream is = new FileInputStream("src/test/resources/bundleconfig-local/etc/relationship/" + filename);
209                 String s =  IOUtils.toString(is, "UTF-8"); 
210                 IOUtils.closeQuietly(is);
211                 
212                 return s;
213         }
214 }