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.containing;
25 import static com.github.tomakehurst.wiremock.client.WireMock.put;
26 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
28 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
29 import static org.hamcrest.MatcherAssert.assertThat;
30 import static org.hamcrest.Matchers.containsString;
31 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
32 import static org.junit.Assert.assertEquals;
33 import static org.mockito.Matchers.any;
34 import static org.mockito.Mockito.doReturn;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.spy;
37 import static org.mockito.Mockito.when;
39 import java.io.IOException;
41 import java.net.URISyntaxException;
42 import java.nio.file.Files;
43 import java.nio.file.Paths;
44 import java.util.Optional;
46 import javax.ws.rs.core.UriBuilder;
48 import org.junit.Rule;
49 import org.junit.Test;
50 import org.junit.rules.ExpectedException;
51 import org.onap.so.client.aai.AAIQueryClient;
52 import org.onap.so.client.graphinventory.Format;
53 import org.onap.so.client.aai.entities.CustomQuery;
54 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException;
55 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException;
56 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException;
58 import com.github.tomakehurst.wiremock.junit.WireMockRule;
60 public class ServiceInstanceUriTest {
62 private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/resources/";
65 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8443));
68 public final ExpectedException exception = ExpectedException.none();
71 public void found() throws IOException {
72 final String content = new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
74 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
75 final Optional<String> result = instance.extractRelatedLink(content);
76 final String expected = "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
77 assertEquals("result is equal", expected, result.get());
82 public void oneKey() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
84 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
85 ServiceInstanceUri spy = spy(instance);
86 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getSerivceInstance(any(Object.class));
88 final URI result = spy.build();
89 final URI expected = UriBuilder.fromPath("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").build();
90 assertEquals("result is equal", expected, result);
95 public void oneKeyQueryParams() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
97 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
98 ServiceInstanceUri spy = spy(instance);
99 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getSerivceInstance(any(Object.class));
101 final URI result = spy.resourceVersion("1234").build();
102 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build();
103 assertEquals("result is equal", expected, result);
108 public void oneKeyEncoded() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
110 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
111 ServiceInstanceUri spy = spy(instance);
112 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").when(spy).getSerivceInstance(any(Object.class));
114 final URI result = spy.build();
115 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").build();
116 assertEquals("result is equal", expected, result);
121 public void oneKeyGetKeys() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
123 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
124 ServiceInstanceUri spy = spy(instance);
125 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%28space").when(spy).getSerivceInstance(any(Object.class));
127 assertThat(spy.getURIKeys().values(), contains("key1", "key2", "key3(space"));
131 public void oneKeyClone() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
132 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
133 ServiceInstanceUri spy = spy(instance);
134 String uri = "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
135 doReturn(uri).when(spy).getSerivceInstance(any(Object.class));
136 doReturn(Optional.of(uri)).when(spy).getCachedValue();
137 final URI result = spy.resourceVersion("1234").clone().build();
138 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build();
139 assertEquals("result is equal", expected, result);
143 public void threeKey() throws IOException {
145 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
146 final URI result = instance.build();
147 final URI expected = UriBuilder.fromPath("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").build();
148 assertEquals("result is equal", expected, result);
153 public void notfound() throws IOException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
154 final String content = new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "empty-query-result.json")));
156 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
157 ServiceInstanceUri spy = spy(instance);
158 AAIQueryClient mockQueryClient = mock(AAIQueryClient.class);
159 when(mockQueryClient.query(any(Format.class), any(CustomQuery.class))).thenReturn(content);
160 when(spy.getQueryClient()).thenReturn(mockQueryClient);
161 exception.expect(GraphInventoryUriComputationException.class);
167 public void cloneTest() {
168 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
169 final URI result = instance.build();
170 final URI result2 = instance.clone().queryParam("something", "new").build();
171 assertEquals("uris are not equal", false, result.toString().equals(result2.toString()));
176 public void noVertexFound() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
177 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
178 ServiceInstanceUri spy = spy(instance);
179 AAIQueryClient client = mock(AAIQueryClient.class);
180 when(client.query(any(Format.class), any(CustomQuery.class))).thenReturn("{\"results\":[]}");
181 doReturn(client).when(spy).getQueryClient();
182 stubFor(put(urlMatching("/aai/v[0-9]+/query.*"))
183 .withRequestBody(containing("key3"))
184 .willReturn(aResponse()
186 .withHeader("Content-Type", "application/json")
188 exception.expect(GraphInventoryUriComputationException.class);
189 exception.expectMessage(containsString("NotFoundException"));