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=========================================================
21 package org.onap.aai.parsers.relationship;
23 import static org.hamcrest.Matchers.hasProperty;
24 import static org.hamcrest.Matchers.is;
25 import static org.junit.Assert.assertEquals;
27 import java.io.FileInputStream;
28 import java.io.IOException;
30 import java.net.URISyntaxException;
32 import org.apache.commons.io.IOUtils;
33 import org.junit.Ignore;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.onap.aai.AAISetup;
38 import org.onap.aai.exceptions.AAIException;
39 import org.onap.aai.introspection.Introspector;
40 import org.onap.aai.introspection.Loader;
41 import org.onap.aai.introspection.ModelType;
42 import org.onap.aai.parsers.exceptions.AAIIdentityMapParseException;
43 import org.onap.aai.parsers.exceptions.AmbiguousMapAAIException;
44 import org.onap.aai.setup.SchemaVersion;
45 import org.springframework.test.annotation.DirtiesContext;
47 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
48 public class RelationshipToURITest extends AAISetup {
50 private final ModelType modelType = ModelType.MOXY;
51 private final SchemaVersion version10 = new SchemaVersion("v10");
54 public ExpectedException thrown = ExpectedException.none();
57 public void onlyLink() throws AAIException, URISyntaxException, IOException {
58 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
59 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-related-link.json"));
60 URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
62 RelationshipToURI parse = new RelationshipToURI(loader, obj);
64 URI uri = parse.getUri();
66 assertEquals("related-link is equal", expected.getPath(), uri.getPath());
70 public void onlyData() throws AAIException, URISyntaxException, IOException {
71 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
72 Introspector obj = loader.unmarshal("relationship", this.getJsonString("only-relationship-data.json"));
73 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1");
75 RelationshipToURI parse = new RelationshipToURI(loader, obj);
77 URI uri = parse.getUri();
79 assertEquals("related-link is equal", expected, uri);
83 public void failV10() throws AAIException, URISyntaxException, IOException {
84 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
85 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-failv10-successv9.json"));
86 URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
88 thrown.expect(AAIIdentityMapParseException.class);
89 thrown.expect(hasProperty("code", is("AAI_3000")));
90 RelationshipToURI parse = new RelationshipToURI(loader, obj);
91 URI uri = parse.getUri();
96 public void failNothingToParse() throws AAIException, URISyntaxException, IOException {
97 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
98 Introspector obj = loader.unmarshal("relationship", this.getJsonString("nothing-to-parse.json"));
99 URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
101 thrown.expect(AAIIdentityMapParseException.class);
102 thrown.expect(hasProperty("code", is("AAI_3000")));
103 RelationshipToURI parse = new RelationshipToURI(loader, obj);
105 URI uri = parse.getUri();
110 public void successV10() throws AAIException, URISyntaxException, IOException {
111 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
112 Introspector obj = loader.unmarshal("relationship", this.getJsonString("both-successv10-failv9.json"));
113 URI expected = new URI("/aai/v10/network/generic-vnfs/generic-vnf/key1");
115 RelationshipToURI parse = new RelationshipToURI(loader, obj);
117 URI uri = parse.getUri();
119 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);
142 public void moreItemsThanRequired() throws AAIException, URISyntaxException, IOException {
143 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
144 Introspector obj = loader.unmarshal("relationship", this.getJsonString("too-many-items-relationship.json"));
145 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
147 RelationshipToURI parse = new RelationshipToURI(loader, obj);
149 URI uri = parse.getUri();
151 assertEquals("related-link is equal", expected.toString(), uri.toString());
156 public void twoTopLevelNodes() throws AAIException, URISyntaxException, IOException {
157 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
158 Introspector obj = loader.unmarshal("relationship", this.getJsonString("two-top-level-relationship.json"));
159 URI expected = new URI("/network/generic-vnfs/generic-vnf/key1/l-interfaces/l-interface/key2");
161 thrown.expect(AmbiguousMapAAIException.class);
162 thrown.expect(hasProperty("code", is("AAI_6146")));
164 RelationshipToURI parse = new RelationshipToURI(loader, obj);
166 URI uri = parse.getUri();
168 assertEquals("related-link is equal", expected, uri);
173 public void topLevelWithTwoKeys() throws AAIException, URISyntaxException, IOException {
174 Loader loader = loaderFactory.createLoaderForVersion(modelType, version10);
175 Introspector obj = loader.unmarshal("relationship", this.getJsonString("top-level-two-keys-relationship.json"));
176 URI expected = new URI(
177 "/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());
187 private String getJsonString(String filename) throws IOException {
189 FileInputStream is = new FileInputStream("src/test/resources/bundleconfig-local/etc/relationship/" + filename);
190 String s = IOUtils.toString(is, "UTF-8");
191 IOUtils.closeQuietly(is);