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;
36 import java.io.IOException;
38 import java.net.URISyntaxException;
39 import java.nio.file.Files;
40 import java.nio.file.Paths;
41 import java.util.Optional;
43 import javax.ws.rs.NotFoundException;
44 import javax.ws.rs.core.UriBuilder;
46 import org.junit.Before;
47 import org.junit.Rule;
48 import org.junit.Test;
49 import org.junit.rules.ExpectedException;
50 import org.junit.runner.RunWith;
51 import org.mockito.ArgumentMatchers;
52 import org.mockito.InjectMocks;
53 import org.mockito.Spy;
54 import org.mockito.junit.MockitoJUnitRunner;
55 import org.onap.so.client.aai.AAIClient;
56 import org.onap.so.client.aai.AAIResourcesClient;
57 import org.onap.so.client.aai.entities.AAIResultWrapper;
58 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
59 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException;
60 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException;
61 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException;
63 import com.github.tomakehurst.wiremock.junit.WireMockRule;
65 @RunWith(MockitoJUnitRunner.class)
66 public class ServiceInstanceUriTest {
68 private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/resources/";
71 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
74 public final ExpectedException exception = ExpectedException.none();
77 public AAIClient client;
80 public AAIResourcesClient aaiClient = new AAIResourcesClient();
83 public void beforeTest() {
84 doReturn(new DefaultAAIPropertiesImpl(wireMockRule.port())).when(client).getRestProperties();
87 public void found() throws IOException {
88 final String content = new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
90 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
91 final Optional<String> result = instance.extractRelatedLink(content);
92 final String expected = "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
93 assertEquals("result is equal", expected, result.get());
98 public void oneKey() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
100 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
101 ServiceInstanceUri spy = spy(instance);
102 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getObjectById(any(Object.class));
104 final URI result = spy.build();
105 final URI expected = UriBuilder.fromPath("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").build();
106 assertEquals("result is equal", expected, result);
111 public void oneKeyQueryParams() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
113 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
114 ServiceInstanceUri spy = spy(instance);
115 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getObjectById(any(Object.class));
117 final URI result = spy.resourceVersion("1234").build();
118 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build();
119 assertEquals("result is equal", expected, result);
124 public void oneKeyEncoded() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
126 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
127 ServiceInstanceUri spy = spy(instance);
128 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").when(spy).getObjectById(any(Object.class));
130 final URI result = spy.build();
131 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").build();
132 assertEquals("result is equal", expected, result);
137 public void oneKeyGetKeys() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
139 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
140 ServiceInstanceUri spy = spy(instance);
141 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%28space").when(spy).getObjectById(any(Object.class));
143 assertThat(spy.getURIKeys().values(), contains("key1", "key2", "key3(space"));
147 public void oneKeyClone() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
148 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
149 ServiceInstanceUri spy = spy(instance);
150 String uri = "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
151 doReturn(uri).when(spy).getObjectById(any(Object.class));
152 doReturn(Optional.of(uri)).when(spy).getCachedValue();
153 final URI result = spy.resourceVersion("1234").clone().build();
154 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build();
155 assertEquals("result is equal", expected, result);
159 public void threeKey() throws IOException {
161 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
162 final URI result = instance.build();
163 final URI expected = UriBuilder.fromPath("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").build();
164 assertEquals("result is equal", expected, result);
169 public void notfound() throws IOException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
170 final String content = new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "empty-query-result.json")));
172 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
173 ServiceInstanceUri spy = spy(instance);
174 AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
175 AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
176 when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class), ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
177 when(wrapper.getJson()).thenReturn(content);
178 when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
179 exception.expect(GraphInventoryUriComputationException.class);
185 public void cloneTest() {
186 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
187 final URI result = instance.build();
188 final URI result2 = instance.clone().queryParam("something", "new").build();
189 assertEquals("uris are not equal", false, result.toString().equals(result2.toString()));
194 public void noVertexFound() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
195 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
196 ServiceInstanceUri spy = spy(instance);
197 AAIResourcesClient client = aaiClient;
198 doReturn(client).when(spy).getResourcesClient();
199 wireMockRule.stubFor(get(urlPathMatching("/aai/v[0-9]+/nodes/service-instances/service-instance/key3"))
200 .willReturn(aResponse()
202 .withHeader("Content-Type", "application/json")
204 exception.expect(NotFoundException.class);