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.openecomp.mso.client.aai.entities.uri;
23 import static org.hamcrest.CoreMatchers.containsString;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
26 import static org.junit.Assert.assertEquals;
27 import static org.mockito.Matchers.any;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.spy;
31 import static org.mockito.Mockito.when;
33 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
34 import static com.github.tomakehurst.wiremock.client.WireMock.put;
35 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
36 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
37 import static com.github.tomakehurst.wiremock.client.WireMock.containing;
38 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
41 import java.io.IOException;
43 import java.net.URISyntaxException;
44 import java.nio.file.Files;
45 import java.nio.file.Paths;
46 import java.util.Optional;
48 import javax.ws.rs.BadRequestException;
49 import javax.ws.rs.core.UriBuilder;
51 import org.junit.Rule;
52 import org.junit.Test;
53 import org.junit.rules.ExpectedException;
54 import org.mockito.Matchers;
55 import org.openecomp.mso.client.aai.AAIObjectType;
56 import org.openecomp.mso.client.aai.AAIQueryClient;
57 import org.openecomp.mso.client.aai.Format;
58 import org.openecomp.mso.client.aai.entities.CustomQuery;
59 import org.openecomp.mso.client.aai.exceptions.AAIPayloadException;
60 import org.openecomp.mso.client.aai.exceptions.AAIUriComputationException;
61 import org.openecomp.mso.client.aai.exceptions.AAIUriNotFoundException;
63 import com.github.tomakehurst.wiremock.junit.WireMockRule;
65 public class ServiceInstanceUriTest {
67 private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/resources/";
70 public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8443));
73 public final ExpectedException exception = ExpectedException.none();
76 public void found() throws IOException {
77 final String content = new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
79 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
80 final Optional<String> result = instance.extractRelatedLink(content);
81 final String expected = "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
82 assertEquals("result is equal", expected, result.get());
87 public void oneKey() throws IOException, URISyntaxException, AAIUriNotFoundException, AAIPayloadException {
89 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
90 ServiceInstanceUri spy = spy(instance);
91 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getSerivceInstance(any(Object.class));
93 final URI result = spy.build();
94 final URI expected = UriBuilder.fromPath("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").build();
95 assertEquals("result is equal", expected, result);
100 public void oneKeyQueryParams() throws IOException, URISyntaxException, AAIUriNotFoundException, AAIPayloadException {
102 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
103 ServiceInstanceUri spy = spy(instance);
104 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getSerivceInstance(any(Object.class));
106 final URI result = spy.resourceVersion("1234").build();
107 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build();
108 assertEquals("result is equal", expected, result);
113 public void oneKeyEncoded() throws IOException, URISyntaxException, AAIUriNotFoundException, AAIPayloadException {
115 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
116 ServiceInstanceUri spy = spy(instance);
117 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").when(spy).getSerivceInstance(any(Object.class));
119 final URI result = spy.build();
120 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").build();
121 assertEquals("result is equal", expected, result);
126 public void oneKeyGetKeys() throws IOException, URISyntaxException, AAIUriNotFoundException, AAIPayloadException {
128 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
129 ServiceInstanceUri spy = spy(instance);
130 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%28space").when(spy).getSerivceInstance(any(Object.class));
132 assertThat(spy.getURIKeys().values(), contains("key1", "key2", "key3(space"));
136 public void oneKeyClone() throws AAIUriNotFoundException, AAIPayloadException {
137 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
138 ServiceInstanceUri spy = spy(instance);
139 String uri = "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
140 doReturn(uri).when(spy).getSerivceInstance(any(Object.class));
141 doReturn(Optional.of(uri)).when(spy).getCachedValue();
142 final URI result = spy.resourceVersion("1234").clone().build();
143 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build();
144 assertEquals("result is equal", expected, result);
148 public void threeKey() throws IOException {
150 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
151 final URI result = instance.build();
152 final URI expected = UriBuilder.fromPath("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").build();
153 assertEquals("result is equal", expected, result);
158 public void notfound() throws IOException, AAIUriNotFoundException, AAIPayloadException {
159 final String content = new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "empty-query-result.json")));
161 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
162 ServiceInstanceUri spy = spy(instance);
163 AAIQueryClient mockQueryClient = mock(AAIQueryClient.class);
164 when(mockQueryClient.query(any(Format.class), any(CustomQuery.class))).thenReturn(content);
165 when(spy.getQueryClient()).thenReturn(mockQueryClient);
166 exception.expect(AAIUriComputationException.class);
172 public void cloneTest() {
173 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
174 final URI result = instance.build();
175 final URI result2 = instance.clone().queryParam("something", "new").build();
176 assertEquals("uris are not equal", false, result.toString().equals(result2.toString()));
181 public void noVertexFound() throws AAIUriNotFoundException, AAIPayloadException {
182 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
183 ServiceInstanceUri spy = spy(instance);
184 stubFor(put(urlMatching("/aai/v[0-9]+/query.*"))
185 .withRequestBody(containing("key3"))
186 .willReturn(aResponse()
188 .withHeader("Content-Type", "application/json")
190 exception.expect(AAIUriComputationException.class);
191 exception.expectMessage(containsString("NotFoundException"));