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.aaiclient.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.aaiclient.client.aai.AAIClient;
59 import org.onap.aaiclient.client.aai.AAIResourcesClient;
60 import org.onap.aaiclient.client.aai.entities.AAIResultWrapper;
61 import org.onap.aaiclient.client.defaultproperties.DefaultAAIPropertiesImpl;
62 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types;
63 import org.onap.aaiclient.client.graphinventory.entities.uri.HttpAwareUri;
64 import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryPayloadException;
65 import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryUriComputationException;
66 import org.onap.aaiclient.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(Types.SERVICE_INSTANCE.getFragment("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(Types.SERVICE_INSTANCE.getFragment("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(Types.SERVICE_INSTANCE.getFragment("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(Types.SERVICE_INSTANCE.getFragment("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(Types.SERVICE_INSTANCE.getFragment("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(Types.SERVICE_INSTANCE.getFragment("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 notfound() throws IOException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
188 final String content =
189 new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "empty-query-result.json")));
191 ServiceInstanceUri instance = new ServiceInstanceUri(Types.SERVICE_INSTANCE.getFragment("key3"));
192 ServiceInstanceUri spy = spy(instance);
193 AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
194 AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
195 when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class),
196 ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
197 when(wrapper.getJson()).thenReturn(content);
198 when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
199 exception.expect(GraphInventoryUriComputationException.class);
200 spy.locateAndBuild();
205 public void noVertexFound() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
206 ServiceInstanceUri instance = new ServiceInstanceUri(Types.SERVICE_INSTANCE.getFragment("key3"));
207 ServiceInstanceUri spy = spy(instance);
208 AAIResourcesClient client = aaiClient;
209 doReturn(client).when(spy).getResourcesClient();
211 .stubFor(get(urlPathMatching("/aai/v[0-9]+/nodes/service-instances/service-instance/key3")).willReturn(
212 aResponse().withStatus(404).withHeader("Content-Type", "application/json").withBodyFile("")));
213 exception.expect(NotFoundException.class);
214 spy.locateAndBuild();
218 public void serializeTest() throws IOException, ClassNotFoundException, GraphInventoryUriNotFoundException,
219 GraphInventoryPayloadException {
220 ServiceInstanceUri instance = new ServiceInstanceUri(Types.SERVICE_INSTANCE.getFragment("key3"));
221 final String content = new String(
222 Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
224 ServiceInstanceUri spy = spy(instance);
225 AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
226 AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
227 when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class),
228 ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
229 when(wrapper.getJson()).thenReturn(content);
230 when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
231 spy.locateAndBuild();
232 instance = spy.clone();
233 ByteArrayOutputStream bos = new ByteArrayOutputStream();
235 ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos);
236 objectOutputStream.writeObject(instance);
237 objectOutputStream.flush();
238 objectOutputStream.close();
240 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
242 ObjectInputStream objectInputStream = new ObjectInputStream(bis);
243 ServiceInstanceUri e2 = (ServiceInstanceUri) objectInputStream.readObject();
244 objectInputStream.close();
246 ServiceInstanceUri spy2 = spy(e2);
248 assertEquals(spy2.build().toString(), instance.build().toString());
250 // use the cached value do not call out to external system
251 verify(spy2, times(0)).getResourcesClient();
255 public void relatedToTest() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
256 ServiceInstanceUri instance = new ServiceInstanceUri(Types.SERVICE_INSTANCE.getFragment("key1"));
257 ServiceInstanceUri spy = spy(instance);
259 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
260 .when(spy).getObjectById(any(Object.class));
263 spy.relatedTo(Types.GENERIC_VNFS.getFragment()).queryParam("vnf-name", "my-vnf-name").build();
264 final URI expected = UriBuilder.fromUri(
265 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3/related-to/generic-vnfs?vnf-name=my-vnf-name")
267 assertEquals("result is equal", expected, result);
271 public void relatedToEqualityTestBeforeBuildTest()
272 throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
273 ServiceInstanceUri instance = new ServiceInstanceUri(Types.SERVICE_INSTANCE.getFragment("key1"));
274 ServiceInstanceUri spy = spy(instance);
276 final AAIPluralResourceUri result =
277 spy.relatedTo(Types.GENERIC_VNFS.getFragment()).queryParam("vnf-name", "my-vnf-name");
279 assertEquals("result is equal", result, result);