2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.parsers.relationship;
24 import org.apache.commons.io.IOUtils;
25 import org.junit.Ignore;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.onap.aai.AAISetup;
30 import org.onap.aai.exceptions.AAIException;
31 import org.onap.aai.introspection.*;
32 import org.onap.aai.parsers.exceptions.AAIIdentityMapParseException;
33 import org.onap.aai.parsers.exceptions.AmbiguousMapAAIException;
35 import java.io.FileInputStream;
36 import java.io.IOException;
38 import java.net.URISyntaxException;
40 import static org.hamcrest.Matchers.hasProperty;
41 import static org.hamcrest.Matchers.is;
42 import static org.junit.Assert.assertEquals;
44 public class RelationshipToURITest extends AAISetup {
46 private final ModelType modelType = ModelType.MOXY;
47 private final Version version10 = Version.v10;
48 private final Version version9 = Version.v9;
51 public ExpectedException thrown = ExpectedException.none();
54 public void onlyLink() throws AAIException, URISyntaxException, IOException {
55 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
56 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-related-link.json"));
57 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
59 RelationshipToURI parse = new RelationshipToURI(loader, obj);
61 URI uri = parse.getUri();
63 assertEquals("related-link is equal", expected.getPath(), uri.getPath());
67 public void onlyData() throws AAIException, URISyntaxException, IOException {
68 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
69 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-relationship-data.json"));
70 URI expected = new URI("/network/test-objects/test-object/key1");
72 RelationshipToURI parse = new RelationshipToURI(loader, obj);
74 URI uri = parse.getUri();
76 assertEquals("related-link is equal", expected, uri);
80 public void failV10() throws AAIException, URISyntaxException, IOException {
81 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
82 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
83 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
85 thrown.expect(AAIIdentityMapParseException.class);
86 thrown.expect(hasProperty("code", is("AAI_3000")));
87 RelationshipToURI parse = new RelationshipToURI(loader, obj);
88 URI uri = parse.getUri();
93 public void successV9() throws AAIException, URISyntaxException, IOException {
94 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
95 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
96 URI expected = new URI("/network/test-objects/test-object/key2");
98 RelationshipToURI parse = new RelationshipToURI(loader, obj);
99 URI uri = parse.getUri();
101 assertEquals("related-link is equal", expected, uri);
107 public void failV9() throws AAIException, URISyntaxException, IOException {
108 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
109 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
110 URI expected = new URI("/network/test-objects/test-object/key1");
112 thrown.expect(AAIIdentityMapParseException.class);
113 thrown.expect(hasProperty("code", is("AAI_3000")));
114 RelationshipToURI parse = new RelationshipToURI(loader, obj);
117 URI uri = parse.getUri();
122 public void failNothingToParse() throws AAIException, URISyntaxException, IOException {
123 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
124 Introspector obj = loader.unmarshal("relationship", this.getJsonString("nothing-to-parse.json"));
125 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
127 thrown.expect(AAIIdentityMapParseException.class);
128 thrown.expect(hasProperty("code", is("AAI_3000")));
129 RelationshipToURI parse = new RelationshipToURI(loader, obj);
131 URI uri = parse.getUri();
136 public void successV10() throws AAIException, URISyntaxException, IOException {
137 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
138 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
139 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
141 RelationshipToURI parse = new RelationshipToURI(loader, obj);
144 URI uri = parse.getUri();
146 assertEquals("related-link is equal", expected, uri);
152 public void ambiguousRelationship() throws AAIException, URISyntaxException, IOException {
153 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
154 Introspector obj = loader.unmarshal("relationship", this.getJsonString("ambiguous-relationship.json"));
155 URI expected = new URI("/aai/v10/network/test-objects/test-object/key1");
157 thrown.expect(AmbiguousMapAAIException.class);
158 thrown.expect(hasProperty("code", is("AAI_6146")));
160 RelationshipToURI parse = new RelationshipToURI(loader, obj);
162 URI uri = parse.getUri();
164 assertEquals("related-link is equal", expected, uri);
171 public void moreItemsThanRequired() throws AAIException, URISyntaxException, IOException {
172 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
173 Introspector obj = loader.unmarshal("relationship", this.getJsonString("too-many-items-relationship.json"));
174 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
176 RelationshipToURI parse = new RelationshipToURI(loader, obj);
178 URI uri = parse.getUri();
180 assertEquals("related-link is equal", expected.toString(), uri.toString());
185 public void twoTopLevelNodes() throws AAIException, URISyntaxException, IOException {
186 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
187 Introspector obj = loader.unmarshal("relationship", this.getJsonString("two-top-level-relationship.json"));
188 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
190 thrown.expect(AmbiguousMapAAIException.class);
191 thrown.expect(hasProperty("code", is("AAI_6146")));
193 RelationshipToURI parse = new RelationshipToURI(loader, obj);
195 URI uri = parse.getUri();
197 assertEquals("related-link is equal", expected, uri);
202 public void topLevelWithTwoKeys() throws AAIException, URISyntaxException, IOException {
203 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
204 Introspector obj = loader.unmarshal("relationship", this.getJsonString("top-level-two-keys-relationship.json"));
205 URI expected = new URI("/cloud-infrastructure/cloud-regions/cloud-region/key1/key2/availability-zones/availability-zone/key3");
207 RelationshipToURI parse = new RelationshipToURI(loader, obj);
209 URI uri = parse.getUri();
211 assertEquals("related-link is equal", expected.toString(), uri.toString());
216 private String getJsonString(String filename) throws IOException {
219 FileInputStream is = new FileInputStream("src/test/resources/bundleconfig-local/etc/relationship/" + filename);
220 String s = IOUtils.toString(is, "UTF-8");
221 IOUtils.closeQuietly(is);