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.stubFor;
26 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
27 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
28 import static org.hamcrest.MatcherAssert.assertThat;
29 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
30 import static org.junit.Assert.assertEquals;
31 import static org.mockito.ArgumentMatchers.any;
32 import static org.mockito.Mockito.doReturn;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.spy;
35 import static org.mockito.Mockito.when;
37 import java.io.IOException;
39 import java.net.URISyntaxException;
40 import java.nio.file.Files;
41 import java.nio.file.Paths;
42 import java.util.Optional;
44 import javax.ws.rs.NotFoundException;
45 import javax.ws.rs.core.UriBuilder;
47 import org.junit.Before;
48 import org.junit.Rule;
49 import org.junit.Test;
50 import org.junit.rules.ExpectedException;
51 import org.junit.runner.RunWith;
52 import org.mockito.ArgumentMatchers;
53 import org.mockito.InjectMocks;
54 import org.mockito.Spy;
55 import org.mockito.junit.MockitoJUnitRunner;
56 import org.onap.so.client.aai.AAIClient;
57 import org.onap.so.client.aai.AAIResourcesClient;
58 import org.onap.so.client.aai.entities.AAIResultWrapper;
59 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
60 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException;
61 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException;
62 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException;
64 import com.github.tomakehurst.wiremock.junit.WireMockRule;
66 @RunWith(MockitoJUnitRunner.class)
67 public class ServiceInstanceUriTest {
69 private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/resources/";
72 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
75 public final ExpectedException exception = ExpectedException.none();
78 public AAIClient client;
81 public AAIResourcesClient aaiClient = new AAIResourcesClient();
84 public void beforeTest() {
85 doReturn(new DefaultAAIPropertiesImpl(wireMockRule.port())).when(client).getRestProperties();
88 public void found() throws IOException {
89 final String content = new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
91 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
92 final Optional<String> result = instance.extractRelatedLink(content);
93 final String expected = "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
94 assertEquals("result is equal", expected, result.get());
99 public void oneKey() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
101 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
102 ServiceInstanceUri spy = spy(instance);
103 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getObjectById(any(Object.class));
105 final URI result = spy.build();
106 final URI expected = UriBuilder.fromPath("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").build();
107 assertEquals("result is equal", expected, result);
112 public void oneKeyQueryParams() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
114 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
115 ServiceInstanceUri spy = spy(instance);
116 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getObjectById(any(Object.class));
118 final URI result = spy.resourceVersion("1234").build();
119 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build();
120 assertEquals("result is equal", expected, result);
125 public void oneKeyEncoded() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
127 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
128 ServiceInstanceUri spy = spy(instance);
129 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").when(spy).getObjectById(any(Object.class));
131 final URI result = spy.build();
132 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").build();
133 assertEquals("result is equal", expected, result);
138 public void oneKeyGetKeys() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
140 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
141 ServiceInstanceUri spy = spy(instance);
142 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%28space").when(spy).getObjectById(any(Object.class));
144 assertThat(spy.getURIKeys().values(), contains("key1", "key2", "key3(space"));
148 public void oneKeyClone() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
149 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
150 ServiceInstanceUri spy = spy(instance);
151 String uri = "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
152 doReturn(uri).when(spy).getObjectById(any(Object.class));
153 doReturn(Optional.of(uri)).when(spy).getCachedValue();
154 final URI result = spy.resourceVersion("1234").clone().build();
155 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build();
156 assertEquals("result is equal", expected, result);
160 public void threeKey() throws IOException {
162 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
163 final URI result = instance.build();
164 final URI expected = UriBuilder.fromPath("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").build();
165 assertEquals("result is equal", expected, result);
170 public void notfound() throws IOException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
171 final String content = new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "empty-query-result.json")));
173 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
174 ServiceInstanceUri spy = spy(instance);
175 AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
176 AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
177 when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class), ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
178 when(wrapper.getJson()).thenReturn(content);
179 when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
180 exception.expect(GraphInventoryUriComputationException.class);
186 public void cloneTest() {
187 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
188 final URI result = instance.build();
189 final URI result2 = instance.clone().queryParam("something", "new").build();
190 assertEquals("uris are not equal", false, result.toString().equals(result2.toString()));
195 public void noVertexFound() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
196 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
197 ServiceInstanceUri spy = spy(instance);
198 AAIResourcesClient client = aaiClient;
199 doReturn(client).when(spy).getResourcesClient();
200 stubFor(get(urlPathMatching("/aai/v[0-9]+/nodes/service-instances/service-instance/key3"))
201 .willReturn(aResponse()
203 .withHeader("Content-Type", "application/json")
205 exception.expect(NotFoundException.class);