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.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
38 import org.openecomp.aai.exceptions.AAIException;
39 import org.openecomp.aai.introspection.Introspector;
40 import org.openecomp.aai.introspection.Loader;
41 import org.openecomp.aai.introspection.LoaderFactory;
42 import org.openecomp.aai.introspection.ModelType;
43 import org.openecomp.aai.introspection.Version;
44 import org.openecomp.aai.parsers.exceptions.AAIIdentityMapParseException;
45 import org.openecomp.aai.parsers.exceptions.AmbiguousMapAAIException;
46 import org.openecomp.aai.serialization.queryformats.QueryFormatTestHelper;
47 import org.openecomp.aai.util.AAIConstants;
49 public class RelationshipToURITest {
53 private final ModelType modelType = ModelType.MOXY;
54 private final Version version10 = Version.v10;
55 private final Version version9 = Version.v9;
58 public ExpectedException thrown = ExpectedException.none();
61 public static void setup() throws NoSuchFieldException, SecurityException, Exception {
62 System.setProperty("AJSC_HOME", ".");
63 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
64 QueryFormatTestHelper.setFinalStatic(AAIConstants.class.getField("AAI_HOME_ETC_OXM"), "src/test/resources/org/openecomp/aai/introspection/");
69 public void onlyLink() throws AAIException, URISyntaxException, IOException {
70 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
71 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-related-link.json"));
72 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
74 RelationshipToURI parse = new RelationshipToURI(loader, obj);
76 URI uri = parse.getUri();
78 assertEquals("related-link is equal", expected.getPath(), uri.getPath());
82 public void onlyData() throws AAIException, URISyntaxException, IOException {
83 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
84 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-relationship-data.json"));
85 URI expected = new URI("/network/test-objects/test-object/key1");
87 RelationshipToURI parse = new RelationshipToURI(loader, obj);
89 URI uri = parse.getUri();
91 assertEquals("related-link is equal", expected, uri);
95 public void failV10() throws AAIException, URISyntaxException, IOException {
96 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
97 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
98 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
100 thrown.expect(AAIIdentityMapParseException.class);
101 thrown.expect(hasProperty("code", is("AAI_3000")));
102 RelationshipToURI parse = new RelationshipToURI(loader, obj);
105 URI uri = parse.getUri();
110 public void successV9() throws AAIException, URISyntaxException, IOException {
111 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
112 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
113 URI expected = new URI("/network/test-objects/test-object/key2");
115 RelationshipToURI parse = new RelationshipToURI(loader, obj);
118 URI uri = parse.getUri();
120 assertEquals("related-link is equal", expected, uri);
126 public void failV9() throws AAIException, URISyntaxException, IOException {
127 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
128 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
129 URI expected = new URI("/network/test-objects/test-object/key1");
131 thrown.expect(AAIIdentityMapParseException.class);
132 thrown.expect(hasProperty("code", is("AAI_3000")));
133 RelationshipToURI parse = new RelationshipToURI(loader, obj);
136 URI uri = parse.getUri();
141 public void successV10() throws AAIException, URISyntaxException, IOException {
142 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
143 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
144 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
146 RelationshipToURI parse = new RelationshipToURI(loader, obj);
149 URI uri = parse.getUri();
151 assertEquals("related-link is equal", expected, uri);
157 public void ambiguousRelationship() throws AAIException, URISyntaxException, IOException {
158 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
159 Introspector obj = loader.unmarshal("relationship", this.getJsonString("ambiguous-relationship.json"));
160 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
162 thrown.expect(AmbiguousMapAAIException.class);
163 thrown.expect(hasProperty("code", is("AAI_6146")));
165 RelationshipToURI parse = new RelationshipToURI(loader, obj);
167 URI uri = parse.getUri();
169 assertEquals("related-link is equal", expected, uri);
175 public void moreItemsThanRequired() throws AAIException, URISyntaxException, IOException {
176 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
177 Introspector obj = loader.unmarshal("relationship", this.getJsonString("too-many-items-relationship.json"));
178 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
180 RelationshipToURI parse = new RelationshipToURI(loader, obj);
182 URI uri = parse.getUri();
184 assertEquals("related-link is equal", expected.toString(), uri.toString());
189 public void twoTopLevelNodes() throws AAIException, URISyntaxException, IOException {
190 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
191 Introspector obj = loader.unmarshal("relationship", this.getJsonString("two-top-level-relationship.json"));
192 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
194 thrown.expect(AmbiguousMapAAIException.class);
195 thrown.expect(hasProperty("code", is("AAI_6146")));
197 RelationshipToURI parse = new RelationshipToURI(loader, obj);
199 URI uri = parse.getUri();
201 assertEquals("related-link is equal", expected, uri);
206 public void topLevelWithTwoKeys() throws AAIException, URISyntaxException, IOException {
207 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
208 Introspector obj = loader.unmarshal("relationship", this.getJsonString("top-level-two-keys-relationship.json"));
209 URI expected = new URI("/cloud-infrastructure/cloud-regions/cloud-region/key1/key2/availability-zones/availability-zone/key3");
211 RelationshipToURI parse = new RelationshipToURI(loader, obj);
213 URI uri = parse.getUri();
215 assertEquals("related-link is equal", expected.toString(), uri.toString());
220 private String getJsonString(String filename) throws IOException {
223 FileInputStream is = new FileInputStream("src/test/resources/org/openecomp/aai/parsers/relationship/" + filename);
224 String s = IOUtils.toString(is, "UTF-8");
225 IOUtils.closeQuietly(is);