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 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;
34 import java.io.FileInputStream;
35 import java.io.IOException;
37 import java.net.URISyntaxException;
39 import static org.hamcrest.Matchers.hasProperty;
40 import static org.hamcrest.Matchers.is;
41 import static org.junit.Assert.assertEquals;
43 public class RelationshipToURITest extends AAISetup {
45 private final ModelType modelType = ModelType.MOXY;
46 private final Version version10 = Version.v10;
47 private final Version version9 = Version.v9;
50 public ExpectedException thrown = ExpectedException.none();
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");
58 RelationshipToURI parse = new RelationshipToURI(loader, obj);
60 URI uri = parse.getUri();
62 assertEquals("related-link is equal", expected.getPath(), uri.getPath());
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");
71 RelationshipToURI parse = new RelationshipToURI(loader, obj);
73 URI uri = parse.getUri();
75 assertEquals("related-link is equal", expected, uri);
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");
84 thrown.expect(AAIIdentityMapParseException.class);
85 thrown.expect(hasProperty("code", is("AAI_3000")));
86 RelationshipToURI parse = new RelationshipToURI(loader, obj);
87 URI uri = parse.getUri();
92 public void successV9() throws AAIException, URISyntaxException, IOException {
93 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
94 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
95 URI expected = new URI("/network/test-objects/test-object/key2");
97 RelationshipToURI parse = new RelationshipToURI(loader, obj);
98 URI uri = parse.getUri();
100 assertEquals("related-link is equal", expected, uri);
106 public void failV9() throws AAIException, URISyntaxException, IOException {
107 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
108 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
109 URI expected = new URI("/network/test-objects/test-object/key1");
111 thrown.expect(AAIIdentityMapParseException.class);
112 thrown.expect(hasProperty("code", is("AAI_3000")));
113 RelationshipToURI parse = new RelationshipToURI(loader, obj);
116 URI uri = parse.getUri();
121 public void failNothingToParse() throws AAIException, URISyntaxException, IOException {
122 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
123 Introspector obj = loader.unmarshal("relationship", this.getJsonString("nothing-to-parse.json"));
124 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
126 thrown.expect(AAIIdentityMapParseException.class);
127 thrown.expect(hasProperty("code", is("AAI_3000")));
128 RelationshipToURI parse = new RelationshipToURI(loader, obj);
130 URI uri = parse.getUri();
135 public void successV10() throws AAIException, URISyntaxException, IOException {
136 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
137 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
138 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
140 RelationshipToURI parse = new RelationshipToURI(loader, obj);
143 URI uri = parse.getUri();
145 assertEquals("related-link is equal", expected, uri);
151 public void ambiguousRelationship() throws AAIException, URISyntaxException, IOException {
152 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
153 Introspector obj = loader.unmarshal("relationship", this.getJsonString("ambiguous-relationship.json"));
154 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
156 thrown.expect(AmbiguousMapAAIException.class);
157 thrown.expect(hasProperty("code", is("AAI_6146")));
159 RelationshipToURI parse = new RelationshipToURI(loader, obj);
161 URI uri = parse.getUri();
163 assertEquals("related-link is equal", expected, uri);
170 public void moreItemsThanRequired() throws AAIException, URISyntaxException, IOException {
171 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
172 Introspector obj = loader.unmarshal("relationship", this.getJsonString("too-many-items-relationship.json"));
173 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
175 RelationshipToURI parse = new RelationshipToURI(loader, obj);
177 URI uri = parse.getUri();
179 assertEquals("related-link is equal", expected.toString(), uri.toString());
184 public void twoTopLevelNodes() throws AAIException, URISyntaxException, IOException {
185 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
186 Introspector obj = loader.unmarshal("relationship", this.getJsonString("two-top-level-relationship.json"));
187 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
189 thrown.expect(AmbiguousMapAAIException.class);
190 thrown.expect(hasProperty("code", is("AAI_6146")));
192 RelationshipToURI parse = new RelationshipToURI(loader, obj);
194 URI uri = parse.getUri();
196 assertEquals("related-link is equal", expected, uri);
201 public void topLevelWithTwoKeys() throws AAIException, URISyntaxException, IOException {
202 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
203 Introspector obj = loader.unmarshal("relationship", this.getJsonString("top-level-two-keys-relationship.json"));
204 URI expected = new URI("/cloud-infrastructure/cloud-regions/cloud-region/key1/key2/availability-zones/availability-zone/key3");
206 RelationshipToURI parse = new RelationshipToURI(loader, obj);
208 URI uri = parse.getUri();
210 assertEquals("related-link is equal", expected.toString(), uri.toString());
215 private String getJsonString(String filename) throws IOException {
218 FileInputStream is = new FileInputStream("src/test/resources/bundleconfig-local/etc/relationship/" + filename);
219 String s = IOUtils.toString(is, "UTF-8");
220 IOUtils.closeQuietly(is);