2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.aai.parsers.relationship;
23 import static org.hamcrest.Matchers.hasProperty;
24 import static org.hamcrest.Matchers.is;
25 import static org.junit.Assert.assertEquals;
27 import java.io.FileInputStream;
28 import java.io.IOException;
30 import java.net.URISyntaxException;
32 import org.apache.commons.io.IOUtils;
33 import org.junit.BeforeClass;
34 import org.junit.Ignore;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
39 import org.openecomp.aai.exceptions.AAIException;
40 import org.openecomp.aai.introspection.Introspector;
41 import org.openecomp.aai.introspection.Loader;
42 import org.openecomp.aai.introspection.LoaderFactory;
43 import org.openecomp.aai.introspection.ModelType;
44 import org.openecomp.aai.introspection.Version;
45 import org.openecomp.aai.parsers.exceptions.AAIIdentityMapParseException;
46 import org.openecomp.aai.parsers.exceptions.AmbiguousMapAAIException;
47 import org.openecomp.aai.serialization.queryformats.QueryFormatTestHelper;
48 import org.openecomp.aai.util.AAIConstants;
50 public class RelationshipToURITest {
54 private final ModelType modelType = ModelType.MOXY;
55 private final Version version10 = Version.v10;
56 private final Version version9 = Version.v9;
59 public ExpectedException thrown = ExpectedException.none();
62 public static void setup() throws NoSuchFieldException, SecurityException, Exception {
63 System.setProperty("AJSC_HOME", ".");
64 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
65 QueryFormatTestHelper.setFinalStatic(AAIConstants.class.getField("AAI_HOME_ETC_OXM"), "src/test/resources/org/openecomp/aai/introspection/");
70 public void onlyLink() throws AAIException, URISyntaxException, IOException {
71 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
72 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-related-link.json"));
73 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
75 RelationshipToURI parse = new RelationshipToURI(loader, obj);
77 URI uri = parse.getUri();
79 assertEquals("related-link is equal", expected.getPath(), uri.getPath());
83 public void onlyData() throws AAIException, URISyntaxException, IOException {
84 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
85 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-relationship-data.json"));
86 URI expected = new URI("/network/test-objects/test-object/key1");
88 RelationshipToURI parse = new RelationshipToURI(loader, obj);
90 URI uri = parse.getUri();
92 assertEquals("related-link is equal", expected, uri);
96 public void failV10() throws AAIException, URISyntaxException, IOException {
97 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
98 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
99 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
101 thrown.expect(AAIIdentityMapParseException.class);
102 thrown.expect(hasProperty("code", is("AAI_3000")));
103 RelationshipToURI parse = new RelationshipToURI(loader, obj);
106 URI uri = parse.getUri();
111 public void successV9() throws AAIException, URISyntaxException, IOException {
112 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
113 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
114 URI expected = new URI("/network/test-objects/test-object/key2");
116 RelationshipToURI parse = new RelationshipToURI(loader, obj);
119 URI uri = parse.getUri();
121 assertEquals("related-link is equal", expected, uri);
127 public void failV9() throws AAIException, URISyntaxException, IOException {
128 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
129 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
130 URI expected = new URI("/network/test-objects/test-object/key1");
132 thrown.expect(AAIIdentityMapParseException.class);
133 thrown.expect(hasProperty("code", is("AAI_3000")));
134 RelationshipToURI parse = new RelationshipToURI(loader, obj);
137 URI uri = parse.getUri();
142 public void successV10() throws AAIException, URISyntaxException, IOException {
143 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
144 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
145 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
147 RelationshipToURI parse = new RelationshipToURI(loader, obj);
150 URI uri = parse.getUri();
152 assertEquals("related-link is equal", expected, uri);
158 public void ambiguousRelationship() throws AAIException, URISyntaxException, IOException {
159 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
160 Introspector obj = loader.unmarshal("relationship", this.getJsonString("ambiguous-relationship.json"));
161 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
163 thrown.expect(AmbiguousMapAAIException.class);
164 thrown.expect(hasProperty("code", is("AAI_6146")));
166 RelationshipToURI parse = new RelationshipToURI(loader, obj);
168 URI uri = parse.getUri();
170 assertEquals("related-link is equal", expected, uri);
177 public void moreItemsThanRequired() throws AAIException, URISyntaxException, IOException {
178 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
179 Introspector obj = loader.unmarshal("relationship", this.getJsonString("too-many-items-relationship.json"));
180 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
182 RelationshipToURI parse = new RelationshipToURI(loader, obj);
184 URI uri = parse.getUri();
186 assertEquals("related-link is equal", expected.toString(), uri.toString());
191 public void twoTopLevelNodes() throws AAIException, URISyntaxException, IOException {
192 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
193 Introspector obj = loader.unmarshal("relationship", this.getJsonString("two-top-level-relationship.json"));
194 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
196 thrown.expect(AmbiguousMapAAIException.class);
197 thrown.expect(hasProperty("code", is("AAI_6146")));
199 RelationshipToURI parse = new RelationshipToURI(loader, obj);
201 URI uri = parse.getUri();
203 assertEquals("related-link is equal", expected, uri);
208 public void topLevelWithTwoKeys() throws AAIException, URISyntaxException, IOException {
209 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
210 Introspector obj = loader.unmarshal("relationship", this.getJsonString("top-level-two-keys-relationship.json"));
211 URI expected = new URI("/cloud-infrastructure/cloud-regions/cloud-region/key1/key2/availability-zones/availability-zone/key3");
213 RelationshipToURI parse = new RelationshipToURI(loader, obj);
215 URI uri = parse.getUri();
217 assertEquals("related-link is equal", expected.toString(), uri.toString());
222 private String getJsonString(String filename) throws IOException {
225 FileInputStream is = new FileInputStream("src/test/resources/org/openecomp/aai/parsers/relationship/" + filename);
226 String s = IOUtils.toString(is, "UTF-8");
227 IOUtils.closeQuietly(is);