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.onap.so.client.aai.entities.uri;
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.get;
25 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
26 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
27 import static org.hamcrest.MatcherAssert.assertThat;
28 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
29 import static org.junit.Assert.assertEquals;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.Mockito.doReturn;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.spy;
34 import static org.mockito.Mockito.when;
35 import java.io.IOException;
37 import java.net.URISyntaxException;
38 import java.nio.file.Files;
39 import java.nio.file.Paths;
40 import java.util.Optional;
41 import javax.ws.rs.NotFoundException;
42 import javax.ws.rs.core.UriBuilder;
43 import org.junit.Before;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.junit.rules.ExpectedException;
47 import org.junit.runner.RunWith;
48 import org.mockito.ArgumentMatchers;
49 import org.mockito.InjectMocks;
50 import org.mockito.Spy;
51 import org.mockito.junit.MockitoJUnitRunner;
52 import org.onap.so.client.aai.AAIClient;
53 import org.onap.so.client.aai.AAIResourcesClient;
54 import org.onap.so.client.aai.entities.AAIResultWrapper;
55 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
56 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException;
57 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException;
58 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException;
59 import com.github.tomakehurst.wiremock.junit.WireMockRule;
61 @RunWith(MockitoJUnitRunner.class)
62 public class ServiceInstanceUriTest {
64 private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/resources/";
67 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
70 public final ExpectedException exception = ExpectedException.none();
73 public AAIClient client;
76 public AAIResourcesClient aaiClient = new AAIResourcesClient();
79 public void beforeTest() {
80 doReturn(new DefaultAAIPropertiesImpl(wireMockRule.port())).when(client).getRestProperties();
84 public void found() throws IOException {
85 final String content = new String(
86 Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
88 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
89 final Optional<String> result = instance.extractRelatedLink(content);
90 final String expected =
91 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
92 assertEquals("result is equal", expected, result.get());
98 throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
100 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
101 ServiceInstanceUri spy = spy(instance);
103 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
104 .when(spy).getObjectById(any(Object.class));
106 final URI result = spy.build();
107 final URI expected = UriBuilder.fromPath(
108 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
110 assertEquals("result is equal", expected, result);
115 public void oneKeyQueryParams()
116 throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
118 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
119 ServiceInstanceUri spy = spy(instance);
121 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
122 .when(spy).getObjectById(any(Object.class));
124 final URI result = spy.resourceVersion("1234").build();
125 final URI expected = UriBuilder.fromUri(
126 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234")
128 assertEquals("result is equal", expected, result);
133 public void oneKeyEncoded()
134 throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
136 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
137 ServiceInstanceUri spy = spy(instance);
139 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space")
140 .when(spy).getObjectById(any(Object.class));
142 final URI result = spy.build();
143 final URI expected = UriBuilder.fromUri(
144 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space")
146 assertEquals("result is equal", expected, result);
151 public void oneKeyGetKeys()
152 throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
154 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
155 ServiceInstanceUri spy = spy(instance);
157 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%28space")
158 .when(spy).getObjectById(any(Object.class));
160 assertThat(spy.getURIKeys().values(), contains("key1", "key2", "key3(space"));
165 public void oneKeyClone() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
166 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
167 ServiceInstanceUri spy = spy(instance);
169 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
170 doReturn(uri).when(spy).getObjectById(any(Object.class));
171 doReturn(Optional.of(uri)).when(spy).getCachedValue();
172 final URI result = spy.resourceVersion("1234").clone().build();
173 final URI expected = UriBuilder.fromUri(
174 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234")
176 assertEquals("result is equal", expected, result);
180 public void threeKey() throws IOException {
182 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
183 final URI result = instance.build();
184 final URI expected = UriBuilder.fromPath(
185 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
187 assertEquals("result is equal", expected, result);
192 public void notfound() throws IOException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
193 final String content =
194 new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "empty-query-result.json")));
196 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
197 ServiceInstanceUri spy = spy(instance);
198 AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
199 AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
200 when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class),
201 ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
202 when(wrapper.getJson()).thenReturn(content);
203 when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
204 exception.expect(GraphInventoryUriComputationException.class);
210 public void cloneTest() {
211 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
212 final URI result = instance.build();
213 final URI result2 = instance.clone().queryParam("something", "new").build();
214 assertEquals("uris are not equal", false, result.toString().equals(result2.toString()));
219 public void noVertexFound() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
220 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
221 ServiceInstanceUri spy = spy(instance);
222 AAIResourcesClient client = aaiClient;
223 doReturn(client).when(spy).getResourcesClient();
225 .stubFor(get(urlPathMatching("/aai/v[0-9]+/nodes/service-instances/service-instance/key3")).willReturn(
226 aResponse().withStatus(404).withHeader("Content-Type", "application/json").withBodyFile("")));
227 exception.expect(NotFoundException.class);