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;
32 import org.onap.aai.setup.SchemaVersion;
33 import org.springframework.test.annotation.DirtiesContext;
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 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
45 public class RelationshipToURITest extends AAISetup {
47 private final ModelType modelType = ModelType.MOXY;
48 private final SchemaVersion version10 = new SchemaVersion("v10");
52 public ExpectedException thrown = ExpectedException.none();
55 public void onlyLink() throws AAIException, URISyntaxException, IOException {
56 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
57 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-related-link.json"));
58 URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
60 RelationshipToURI parse = new RelationshipToURI(loader, obj);
62 URI uri = parse.getUri();
64 assertEquals("related-link is equal", expected.getPath(), uri.getPath());
68 public void onlyData() throws AAIException, URISyntaxException, IOException {
69 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
70 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-relationship-data.json"));
71 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1");
73 RelationshipToURI parse = new RelationshipToURI(loader, obj);
75 URI uri = parse.getUri();
77 assertEquals("related-link is equal", expected, uri);
81 public void failV10() throws AAIException, URISyntaxException, IOException {
82 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
83 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
84 URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
86 thrown.expect(AAIIdentityMapParseException.class);
87 thrown.expect(hasProperty("code", is("AAI_3000")));
88 RelationshipToURI parse = new RelationshipToURI(loader, obj);
89 URI uri = parse.getUri();
94 public void failNothingToParse() throws AAIException, URISyntaxException, IOException {
95 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
96 Introspector obj = loader.unmarshal("relationship", this.getJsonString("nothing-to-parse.json"));
97 URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
99 thrown.expect(AAIIdentityMapParseException.class);
100 thrown.expect(hasProperty("code", is("AAI_3000")));
101 RelationshipToURI parse = new RelationshipToURI(loader, obj);
103 URI uri = parse.getUri();
108 public void successV10() throws AAIException, URISyntaxException, IOException {
109 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
110 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
111 URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
113 RelationshipToURI parse = new RelationshipToURI(loader, obj);
116 URI uri = parse.getUri();
118 assertEquals("related-link is equal", expected, uri);
124 public void ambiguousRelationship() throws AAIException, URISyntaxException, IOException {
125 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
126 Introspector obj = loader.unmarshal("relationship", this.getJsonString("ambiguous-relationship.json"));
127 URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
129 thrown.expect(AmbiguousMapAAIException.class);
130 thrown.expect(hasProperty("code", is("AAI_6146")));
132 RelationshipToURI parse = new RelationshipToURI(loader, obj);
134 URI uri = parse.getUri();
136 assertEquals("related-link is equal", expected, uri);
143 public void moreItemsThanRequired() throws AAIException, URISyntaxException, IOException {
144 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
145 Introspector obj = loader.unmarshal("relationship", this.getJsonString("too-many-items-relationship.json"));
146 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
148 RelationshipToURI parse = new RelationshipToURI(loader, obj);
150 URI uri = parse.getUri();
152 assertEquals("related-link is equal", expected.toString(), uri.toString());
157 public void twoTopLevelNodes() throws AAIException, URISyntaxException, IOException {
158 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
159 Introspector obj = loader.unmarshal("relationship", this.getJsonString("two-top-level-relationship.json"));
160 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
162 thrown.expect(AmbiguousMapAAIException.class);
163 thrown.expect(hasProperty("code", is("AAI_6146")));
165 RelationshipToURI parse = new RelationshipToURI(loader, obj);
167 URI uri = parse.getUri();
169 assertEquals("related-link is equal", expected, uri);
174 public void topLevelWithTwoKeys() throws AAIException, URISyntaxException, IOException {
175 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
176 Introspector obj = loader.unmarshal("relationship", this.getJsonString("top-level-two-keys-relationship.json"));
177 URI expected = new URI("/cloud-infrastructure/cloud-regions/cloud-region/key1/key2/availability-zones/availability-zone/key3");
179 RelationshipToURI parse = new RelationshipToURI(loader, obj);
181 URI uri = parse.getUri();
183 assertEquals("related-link is equal", expected.toString(), uri.toString());
188 private String getJsonString(String filename) throws IOException {
191 FileInputStream is = new FileInputStream("src/test/resources/bundleconfig-local/etc/relationship/" + filename);
192 String s = IOUtils.toString(is, "UTF-8");
193 IOUtils.closeQuietly(is);