2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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 package org.onap.aai.parsers.relationship;
22 import org.apache.commons.io.IOUtils;
23 import org.junit.Ignore;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.onap.aai.AAISetup;
28 import org.onap.aai.exceptions.AAIException;
29 import org.onap.aai.introspection.*;
30 import org.onap.aai.parsers.exceptions.AAIIdentityMapParseException;
31 import org.onap.aai.parsers.exceptions.AmbiguousMapAAIException;
33 import java.io.FileInputStream;
34 import java.io.IOException;
36 import java.net.URISyntaxException;
38 import static org.hamcrest.Matchers.hasProperty;
39 import static org.hamcrest.Matchers.is;
40 import static org.junit.Assert.assertEquals;
42 public class RelationshipToURITest extends AAISetup {
44 private final ModelType modelType = ModelType.MOXY;
45 private final Version version10 = Version.v10;
46 private final Version version9 = Version.v9;
49 public ExpectedException thrown = ExpectedException.none();
52 public void onlyLink() throws AAIException, URISyntaxException, IOException {
53 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
54 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-related-link.json"));
55 URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
57 RelationshipToURI parse = new RelationshipToURI(loader, obj);
59 URI uri = parse.getUri();
61 assertEquals("related-link is equal", expected.getPath(), uri.getPath());
65 public void onlyData() throws AAIException, URISyntaxException, IOException {
66 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
67 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-relationship-data.json"));
68 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1");
70 RelationshipToURI parse = new RelationshipToURI(loader, obj);
72 URI uri = parse.getUri();
74 assertEquals("related-link is equal", expected, uri);
78 public void failV10() throws AAIException, URISyntaxException, IOException {
79 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
80 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
81 URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
83 thrown.expect(AAIIdentityMapParseException.class);
84 thrown.expect(hasProperty("code", is("AAI_3000")));
85 RelationshipToURI parse = new RelationshipToURI(loader, obj);
86 URI uri = parse.getUri();
91 public void successV9() throws AAIException, URISyntaxException, IOException {
92 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
93 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
94 URI expected = new URI("/network/generic-vnfs/generic-vnf/key2");
96 RelationshipToURI parse = new RelationshipToURI(loader, obj);
97 URI uri = parse.getUri();
99 assertEquals("related-link is equal", expected, uri);
105 public void failV9() throws AAIException, URISyntaxException, IOException {
106 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version9);
107 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
108 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1");
110 thrown.expect(AAIIdentityMapParseException.class);
111 thrown.expect(hasProperty("code", is("AAI_3000")));
112 RelationshipToURI parse = new RelationshipToURI(loader, obj);
115 URI uri = parse.getUri();
120 public void failNothingToParse() throws AAIException, URISyntaxException, IOException {
121 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
122 Introspector obj = loader.unmarshal("relationship", this.getJsonString("nothing-to-parse.json"));
123 URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
125 thrown.expect(AAIIdentityMapParseException.class);
126 thrown.expect(hasProperty("code", is("AAI_3000")));
127 RelationshipToURI parse = new RelationshipToURI(loader, obj);
129 URI uri = parse.getUri();
134 public void successV10() throws AAIException, URISyntaxException, IOException {
135 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
136 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
137 URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
139 RelationshipToURI parse = new RelationshipToURI(loader, obj);
142 URI uri = parse.getUri();
144 assertEquals("related-link is equal", expected, uri);
150 public void ambiguousRelationship() throws AAIException, URISyntaxException, IOException {
151 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
152 Introspector obj = loader.unmarshal("relationship", this.getJsonString("ambiguous-relationship.json"));
153 URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
155 thrown.expect(AmbiguousMapAAIException.class);
156 thrown.expect(hasProperty("code", is("AAI_6146")));
158 RelationshipToURI parse = new RelationshipToURI(loader, obj);
160 URI uri = parse.getUri();
162 assertEquals("related-link is equal", expected, uri);
169 public void moreItemsThanRequired() throws AAIException, URISyntaxException, IOException {
170 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
171 Introspector obj = loader.unmarshal("relationship", this.getJsonString("too-many-items-relationship.json"));
172 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
174 RelationshipToURI parse = new RelationshipToURI(loader, obj);
176 URI uri = parse.getUri();
178 assertEquals("related-link is equal", expected.toString(), uri.toString());
183 public void twoTopLevelNodes() throws AAIException, URISyntaxException, IOException {
184 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
185 Introspector obj = loader.unmarshal("relationship", this.getJsonString("two-top-level-relationship.json"));
186 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
188 thrown.expect(AmbiguousMapAAIException.class);
189 thrown.expect(hasProperty("code", is("AAI_6146")));
191 RelationshipToURI parse = new RelationshipToURI(loader, obj);
193 URI uri = parse.getUri();
195 assertEquals("related-link is equal", expected, uri);
200 public void topLevelWithTwoKeys() throws AAIException, URISyntaxException, IOException {
201 Loader loader = LoaderFactory.createLoaderForVersion(modelType, version10);
202 Introspector obj = loader.unmarshal("relationship", this.getJsonString("top-level-two-keys-relationship.json"));
203 URI expected = new URI("/cloud-infrastructure/cloud-regions/cloud-region/key1/key2/availability-zones/availability-zone/key3");
205 RelationshipToURI parse = new RelationshipToURI(loader, obj);
207 URI uri = parse.getUri();
209 assertEquals("related-link is equal", expected.toString(), uri.toString());
214 private String getJsonString(String filename) throws IOException {
217 FileInputStream is = new FileInputStream("src/test/resources/bundleconfig-local/etc/relationship/" + filename);
218 String s = IOUtils.toString(is, "UTF-8");
219 IOUtils.closeQuietly(is);