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;
51 public class RelationshipToURITest {
55 private final ModelType modelType = ModelType.MOXY;
56 private final Version version10 = Version.v10;
57 private final Version version9 = Version.v9;
60 public ExpectedException thrown = ExpectedException.none();
63 public static void setup() throws NoSuchFieldException, SecurityException, Exception {
64 System.setProperty("AJSC_HOME", ".");
65 System.setProperty("BUNDLECONFIG_DIR", "src/test/resources/bundleconfig-local");
66 QueryFormatTestHelper.setFinalStatic(AAIConstants.class.getField("AAI_HOME_ETC_OXM"), "src/test/resources/org/openecomp/aai/introspection/");
71 public void onlyLink() throws AAIException, URISyntaxException, IOException {
72 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
73 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-related-link.json"));
74 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
76 RelationshipToURI parse = new RelationshipToURI(loader, obj);
78 URI uri = parse.getUri();
80 assertEquals("related-link is equal", expected.getPath(), uri.getPath());
84 public void onlyData() throws AAIException, URISyntaxException, IOException {
85 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
86 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-relationship-data.json"));
87 URI expected = new URI("/network/test-objects/test-object/key1");
89 RelationshipToURI parse = new RelationshipToURI(loader, obj);
91 URI uri = parse.getUri();
93 assertEquals("related-link is equal", expected, uri);
97 public void failV10() throws AAIException, URISyntaxException, IOException {
98 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
99 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
100 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
102 thrown.expect(AAIIdentityMapParseException.class);
103 thrown.expect(hasProperty("code", is("AAI_3000")));
104 RelationshipToURI parse = new RelationshipToURI(loader, obj);
107 URI uri = parse.getUri();
112 public void successV9() throws AAIException, URISyntaxException, IOException {
113 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
114 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
115 URI expected = new URI("/network/test-objects/test-object/key2");
117 RelationshipToURI parse = new RelationshipToURI(loader, obj);
120 URI uri = parse.getUri();
122 assertEquals("related-link is equal", expected, uri);
128 public void failV9() throws AAIException, URISyntaxException, IOException {
129 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
130 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
131 URI expected = new URI("/network/test-objects/test-object/key1");
133 thrown.expect(AAIIdentityMapParseException.class);
134 thrown.expect(hasProperty("code", is("AAI_3000")));
135 RelationshipToURI parse = new RelationshipToURI(loader, obj);
138 URI uri = parse.getUri();
143 public void successV10() throws AAIException, URISyntaxException, IOException {
144 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
145 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
146 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
148 RelationshipToURI parse = new RelationshipToURI(loader, obj);
151 URI uri = parse.getUri();
153 assertEquals("related-link is equal", expected, uri);
159 public void ambiguousRelationship() throws AAIException, URISyntaxException, IOException {
160 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
161 Introspector obj = loader.unmarshal("relationship", this.getJsonString("ambiguous-relationship.json"));
162 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
164 thrown.expect(AmbiguousMapAAIException.class);
165 thrown.expect(hasProperty("code", is("AAI_6146")));
167 RelationshipToURI parse = new RelationshipToURI(loader, obj);
169 URI uri = parse.getUri();
171 assertEquals("related-link is equal", expected, uri);
178 public void moreItemsThanRequired() throws AAIException, URISyntaxException, IOException {
179 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
180 Introspector obj = loader.unmarshal("relationship", this.getJsonString("too-many-items-relationship.json"));
181 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
183 RelationshipToURI parse = new RelationshipToURI(loader, obj);
185 URI uri = parse.getUri();
187 assertEquals("related-link is equal", expected.toString(), uri.toString());
192 public void twoTopLevelNodes() throws AAIException, URISyntaxException, IOException {
193 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
194 Introspector obj = loader.unmarshal("relationship", this.getJsonString("two-top-level-relationship.json"));
195 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
197 thrown.expect(AmbiguousMapAAIException.class);
198 thrown.expect(hasProperty("code", is("AAI_6146")));
200 RelationshipToURI parse = new RelationshipToURI(loader, obj);
202 URI uri = parse.getUri();
204 assertEquals("related-link is equal", expected, uri);
209 public void topLevelWithTwoKeys() throws AAIException, URISyntaxException, IOException {
210 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
211 Introspector obj = loader.unmarshal("relationship", this.getJsonString("top-level-two-keys-relationship.json"));
212 URI expected = new URI("/cloud-infrastructure/cloud-regions/cloud-region/key1/key2/availability-zones/availability-zone/key3");
214 RelationshipToURI parse = new RelationshipToURI(loader, obj);
216 URI uri = parse.getUri();
218 assertEquals("related-link is equal", expected.toString(), uri.toString());
223 private String getJsonString(String filename) throws IOException {
226 FileInputStream is = new FileInputStream("src/test/resources/org/openecomp/aai/parsers/relationship/" + filename);
227 String s = IOUtils.toString(is, "UTF-8");
228 IOUtils.closeQuietly(is);