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);
89 URI uri = parse.getUri();
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");
99 RelationshipToURI parse = new RelationshipToURI(loader, obj);
102 URI uri = parse.getUri();
104 assertEquals("related-link is equal", expected, uri);
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");
115 thrown.expect(AAIIdentityMapParseException.class);
116 thrown.expect(hasProperty("code", is("AAI_3000")));
117 RelationshipToURI parse = new RelationshipToURI(loader, obj);
120 URI uri = parse.getUri();
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");
130 RelationshipToURI parse = new RelationshipToURI(loader, obj);
133 URI uri = parse.getUri();
135 assertEquals("related-link is equal", expected, uri);
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");
146 thrown.expect(AmbiguousMapAAIException.class);
147 thrown.expect(hasProperty("code", is("AAI_6146")));
149 RelationshipToURI parse = new RelationshipToURI(loader, obj);
151 URI uri = parse.getUri();
153 assertEquals("related-link is equal", expected, uri);
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");
165 RelationshipToURI parse = new RelationshipToURI(loader, obj);
167 URI uri = parse.getUri();
169 assertEquals("related-link is equal", expected.toString(), uri.toString());
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");
179 thrown.expect(AmbiguousMapAAIException.class);
180 thrown.expect(hasProperty("code", is("AAI_6146")));
182 RelationshipToURI parse = new RelationshipToURI(loader, obj);
184 URI uri = parse.getUri();
186 assertEquals("related-link is equal", expected, uri);
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");
196 RelationshipToURI parse = new RelationshipToURI(loader, obj);
198 URI uri = parse.getUri();
200 assertEquals("related-link is equal", expected.toString(), uri.toString());
205 private String getJsonString(String filename) throws IOException {
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);