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.AAIObjectPlurals;
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.HttpAwareUri;
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.locateAndBuild();
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 = ((HttpAwareUri) spy.resourceVersion("1234")).locateAndBuild();
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.locateAndBuild();
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(Optional.of(uri)).when(spy).getCachedValue();
179 final URI result = ((HttpAwareUri) spy.resourceVersion("1234").clone()).locateAndBuild();
180 final URI expected = UriBuilder.fromUri(
181 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234")
183 assertEquals("result is equal", expected, result);
187 public void threeKey() throws IOException {
189 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
190 final URI result = instance.build();
191 final URI expected = UriBuilder.fromPath(
192 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
194 assertEquals("result is equal", expected, result);
199 public void notfound() throws IOException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
200 final String content =
201 new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "empty-query-result.json")));
203 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
204 ServiceInstanceUri spy = spy(instance);
205 AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
206 AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
207 when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class),
208 ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
209 when(wrapper.getJson()).thenReturn(content);
210 when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
211 exception.expect(GraphInventoryUriComputationException.class);
212 spy.locateAndBuild();
217 public void cloneTest() {
218 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
219 final URI result = instance.build();
220 final URI result2 = instance.clone().queryParam("something", "new").build();
221 assertEquals("uris are not equal", false, result.toString().equals(result2.toString()));
226 public void noVertexFound() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
227 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
228 ServiceInstanceUri spy = spy(instance);
229 AAIResourcesClient client = aaiClient;
230 doReturn(client).when(spy).getResourcesClient();
232 .stubFor(get(urlPathMatching("/aai/v[0-9]+/nodes/service-instances/service-instance/key3")).willReturn(
233 aResponse().withStatus(404).withHeader("Content-Type", "application/json").withBodyFile("")));
234 exception.expect(NotFoundException.class);
235 spy.locateAndBuild();
239 public void serializeTest() throws IOException, ClassNotFoundException, GraphInventoryUriNotFoundException,
240 GraphInventoryPayloadException {
241 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
242 final String content = new String(
243 Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
245 ServiceInstanceUri spy = spy(instance);
246 AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
247 AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
248 when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class),
249 ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
250 when(wrapper.getJson()).thenReturn(content);
251 when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
252 spy.locateAndBuild();
253 instance = spy.clone();
254 ByteArrayOutputStream bos = new ByteArrayOutputStream();
256 ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos);
257 objectOutputStream.writeObject(instance);
258 objectOutputStream.flush();
259 objectOutputStream.close();
261 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
263 ObjectInputStream objectInputStream = new ObjectInputStream(bis);
264 ServiceInstanceUri e2 = (ServiceInstanceUri) objectInputStream.readObject();
265 objectInputStream.close();
267 ServiceInstanceUri spy2 = spy(e2);
269 assertEquals(spy2.build().toString(), instance.build().toString());
271 // use the cached value do not call out to external system
272 verify(spy2, times(0)).getResourcesClient();
276 public void relatedToTest() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
277 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
278 ServiceInstanceUri spy = spy(instance);
280 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
281 .when(spy).getObjectById(any(Object.class));
283 final URI result = spy.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", "my-vnf-name").build();
284 final URI expected = UriBuilder.fromUri(
285 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3/related-to/generic-vnfs?vnf-name=my-vnf-name")
287 assertEquals("result is equal", expected, result);
291 public void relatedToEqualityTestBeforeBuildTest()
292 throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
293 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
294 ServiceInstanceUri spy = spy(instance);
296 final AAIPluralResourceUri result =
297 spy.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", "my-vnf-name");
299 assertEquals("result is equal", result, result);