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.times;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.when;
37 import java.io.ByteArrayInputStream;
38 import java.io.ByteArrayOutputStream;
39 import java.io.IOException;
40 import java.io.ObjectInputStream;
41 import java.io.ObjectOutputStream;
43 import java.net.URISyntaxException;
44 import java.nio.file.Files;
45 import java.nio.file.Paths;
46 import java.util.Optional;
47 import javax.ws.rs.NotFoundException;
48 import javax.ws.rs.core.UriBuilder;
49 import org.junit.Before;
50 import org.junit.Rule;
51 import org.junit.Test;
52 import org.junit.rules.ExpectedException;
53 import org.junit.runner.RunWith;
54 import org.mockito.ArgumentMatchers;
55 import org.mockito.InjectMocks;
56 import org.mockito.Spy;
57 import org.mockito.junit.MockitoJUnitRunner;
58 import org.onap.so.client.aai.AAIClient;
59 import org.onap.so.client.aai.AAIObjectType;
60 import org.onap.so.client.aai.AAIResourcesClient;
61 import org.onap.so.client.aai.entities.AAIResultWrapper;
62 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
63 import org.onap.so.client.graphinventory.entities.uri.Depth;
64 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException;
65 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException;
66 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException;
67 import com.github.tomakehurst.wiremock.junit.WireMockRule;
69 @RunWith(MockitoJUnitRunner.class)
70 public class ServiceInstanceUriTest {
72 private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/resources/";
75 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
78 public final ExpectedException exception = ExpectedException.none();
81 public AAIClient client;
84 public AAIResourcesClient aaiClient = new AAIResourcesClient();
87 public void beforeTest() {
88 doReturn(new DefaultAAIPropertiesImpl(wireMockRule.port())).when(client).getRestProperties();
92 public void found() throws IOException {
93 final String content = new String(
94 Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
96 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
97 final Optional<String> result = instance.extractRelatedLink(content);
98 final String expected =
99 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
100 assertEquals("result is equal", expected, result.get());
106 throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
108 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
109 ServiceInstanceUri spy = spy(instance);
111 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
112 .when(spy).getObjectById(any(Object.class));
114 final URI result = spy.build();
115 final URI expected = UriBuilder.fromPath(
116 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
118 assertEquals("result is equal", expected, result);
123 public void oneKeyQueryParams()
124 throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
126 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
127 ServiceInstanceUri spy = spy(instance);
129 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
130 .when(spy).getObjectById(any(Object.class));
132 final URI result = spy.resourceVersion("1234").build();
133 final URI expected = UriBuilder.fromUri(
134 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234")
136 assertEquals("result is equal", expected, result);
141 public void oneKeyEncoded()
142 throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
144 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
145 ServiceInstanceUri spy = spy(instance);
147 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space")
148 .when(spy).getObjectById(any(Object.class));
150 final URI result = spy.build();
151 final URI expected = UriBuilder.fromUri(
152 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space")
154 assertEquals("result is equal", expected, result);
159 public void oneKeyGetKeys()
160 throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
162 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
163 ServiceInstanceUri spy = spy(instance);
165 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%28space")
166 .when(spy).getObjectById(any(Object.class));
168 assertThat(spy.getURIKeys().values(), contains("key1", "key2", "key3(space"));
173 public void oneKeyClone() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
174 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
175 ServiceInstanceUri spy = spy(instance);
177 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
178 doReturn(uri).when(spy).getObjectById(any(Object.class));
179 doReturn(Optional.of(uri)).when(spy).getCachedValue();
180 final URI result = spy.resourceVersion("1234").clone().build();
181 final URI expected = UriBuilder.fromUri(
182 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234")
184 assertEquals("result is equal", expected, result);
188 public void threeKey() throws IOException {
190 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
191 final URI result = instance.build();
192 final URI expected = UriBuilder.fromPath(
193 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
195 assertEquals("result is equal", expected, result);
200 public void notfound() throws IOException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
201 final String content =
202 new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "empty-query-result.json")));
204 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
205 ServiceInstanceUri spy = spy(instance);
206 AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
207 AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
208 when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class),
209 ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
210 when(wrapper.getJson()).thenReturn(content);
211 when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
212 exception.expect(GraphInventoryUriComputationException.class);
218 public void cloneTest() {
219 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
220 final URI result = instance.build();
221 final URI result2 = instance.clone().queryParam("something", "new").build();
222 assertEquals("uris are not equal", false, result.toString().equals(result2.toString()));
227 public void noVertexFound() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
228 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
229 ServiceInstanceUri spy = spy(instance);
230 AAIResourcesClient client = aaiClient;
231 doReturn(client).when(spy).getResourcesClient();
233 .stubFor(get(urlPathMatching("/aai/v[0-9]+/nodes/service-instances/service-instance/key3")).willReturn(
234 aResponse().withStatus(404).withHeader("Content-Type", "application/json").withBodyFile("")));
235 exception.expect(NotFoundException.class);
240 public void serializeTest() throws IOException, ClassNotFoundException, GraphInventoryUriNotFoundException,
241 GraphInventoryPayloadException {
242 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
243 final String content = new String(
244 Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
246 ServiceInstanceUri spy = spy(instance);
247 AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
248 AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
249 when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class),
250 ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
251 when(wrapper.getJson()).thenReturn(content);
252 when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
254 instance = spy.clone();
255 ByteArrayOutputStream bos = new ByteArrayOutputStream();
257 ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos);
258 objectOutputStream.writeObject(instance);
259 objectOutputStream.flush();
260 objectOutputStream.close();
262 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
264 ObjectInputStream objectInputStream = new ObjectInputStream(bis);
265 ServiceInstanceUri e2 = (ServiceInstanceUri) objectInputStream.readObject();
266 objectInputStream.close();
268 ServiceInstanceUri spy2 = spy(e2);
270 assertEquals(spy2.build().toString(), instance.build().toString());
272 // use the cached value do not call out to external system
273 verify(spy2, times(0)).getResourcesClient();