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.Matchers.containsString;
30 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
31 import static org.junit.Assert.assertEquals;
32 import static org.mockito.ArgumentMatchers.any;
33 import static org.mockito.Mockito.doReturn;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.spy;
36 import static org.mockito.Mockito.when;
38 import java.io.IOException;
40 import java.net.URISyntaxException;
41 import java.nio.file.Files;
42 import java.nio.file.Paths;
43 import java.util.Optional;
45 import javax.ws.rs.NotFoundException;
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.mockito.ArgumentMatchers;
52 import org.onap.so.client.aai.AAIResourcesClient;
53 import org.onap.so.client.aai.entities.AAIResultWrapper;
54 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
55 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException;
56 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException;
57 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException;
59 import com.github.tomakehurst.wiremock.junit.WireMockRule;
61 public class ServiceInstanceUriTest {
63 private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/resources/";
66 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8443));
69 public final ExpectedException exception = ExpectedException.none();
72 public void found() throws IOException {
73 final String content = new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
75 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
76 final Optional<String> result = instance.extractRelatedLink(content);
77 final String expected = "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
78 assertEquals("result is equal", expected, result.get());
83 public void oneKey() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
85 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
86 ServiceInstanceUri spy = spy(instance);
87 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getObjectById(any(Object.class));
89 final URI result = spy.build();
90 final URI expected = UriBuilder.fromPath("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").build();
91 assertEquals("result is equal", expected, result);
96 public void oneKeyQueryParams() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
98 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
99 ServiceInstanceUri spy = spy(instance);
100 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getObjectById(any(Object.class));
102 final URI result = spy.resourceVersion("1234").build();
103 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build();
104 assertEquals("result is equal", expected, result);
109 public void oneKeyEncoded() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
111 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
112 ServiceInstanceUri spy = spy(instance);
113 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").when(spy).getObjectById(any(Object.class));
115 final URI result = spy.build();
116 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").build();
117 assertEquals("result is equal", expected, result);
122 public void oneKeyGetKeys() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
124 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
125 ServiceInstanceUri spy = spy(instance);
126 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%28space").when(spy).getObjectById(any(Object.class));
128 assertThat(spy.getURIKeys().values(), contains("key1", "key2", "key3(space"));
132 public void oneKeyClone() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
133 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
134 ServiceInstanceUri spy = spy(instance);
135 String uri = "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
136 doReturn(uri).when(spy).getObjectById(any(Object.class));
137 doReturn(Optional.of(uri)).when(spy).getCachedValue();
138 final URI result = spy.resourceVersion("1234").clone().build();
139 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build();
140 assertEquals("result is equal", expected, result);
144 public void threeKey() throws IOException {
146 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
147 final URI result = instance.build();
148 final URI expected = UriBuilder.fromPath("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").build();
149 assertEquals("result is equal", expected, result);
154 public void notfound() throws IOException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
155 final String content = new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "empty-query-result.json")));
157 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
158 ServiceInstanceUri spy = spy(instance);
159 AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
160 AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
161 when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class), ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
162 when(wrapper.getJson()).thenReturn(content);
163 when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
164 exception.expect(GraphInventoryUriComputationException.class);
170 public void cloneTest() {
171 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
172 final URI result = instance.build();
173 final URI result2 = instance.clone().queryParam("something", "new").build();
174 assertEquals("uris are not equal", false, result.toString().equals(result2.toString()));
179 public void noVertexFound() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
180 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
181 ServiceInstanceUri spy = spy(instance);
182 AAIResourcesClient client = createClient();
183 doReturn(client).when(spy).getResourcesClient();
184 stubFor(get(urlPathMatching("/aai/v[0-9]+/nodes/service-instances/service-instance/key3"))
185 .willReturn(aResponse()
187 .withHeader("Content-Type", "application/json")
189 exception.expect(NotFoundException.class);
193 private AAIResourcesClient createClient() {
194 AAIResourcesClient client = spy(new AAIResourcesClient());
195 doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties();