71d65b6f45341743b08263cd366a6eb63576e92d
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.aai.entities.uri;
22
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.times;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.when;
37 import java.io.ByteArrayInputStream;
38 import java.io.ByteArrayOutputStream;
39 import java.io.IOException;
40 import java.io.ObjectInputStream;
41 import java.io.ObjectOutputStream;
42 import java.net.URI;
43 import java.net.URISyntaxException;
44 import java.nio.file.Files;
45 import java.nio.file.Paths;
46 import java.util.Optional;
47 import javax.ws.rs.NotFoundException;
48 import javax.ws.rs.core.UriBuilder;
49 import org.junit.Before;
50 import org.junit.Rule;
51 import org.junit.Test;
52 import org.junit.rules.ExpectedException;
53 import org.junit.runner.RunWith;
54 import org.mockito.ArgumentMatchers;
55 import org.mockito.InjectMocks;
56 import org.mockito.Spy;
57 import org.mockito.junit.MockitoJUnitRunner;
58 import org.onap.so.client.aai.AAIClient;
59 import org.onap.so.client.aai.AAIObjectPlurals;
60 import org.onap.so.client.aai.AAIResourcesClient;
61 import org.onap.so.client.aai.entities.AAIResultWrapper;
62 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
63 import org.onap.so.client.graphinventory.entities.uri.HttpAwareUri;
64 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException;
65 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException;
66 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException;
67 import com.github.tomakehurst.wiremock.junit.WireMockRule;
68
69 @RunWith(MockitoJUnitRunner.class)
70 public class ServiceInstanceUriTest {
71
72     private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/resources/";
73
74     @Rule
75     public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
76
77     @Rule
78     public final ExpectedException exception = ExpectedException.none();
79
80     @Spy
81     public AAIClient client;
82
83     @InjectMocks
84     public AAIResourcesClient aaiClient = new AAIResourcesClient();
85
86     @Before
87     public void beforeTest() {
88         doReturn(new DefaultAAIPropertiesImpl(wireMockRule.port())).when(client).getRestProperties();
89     }
90
91     @Test
92     public void found() throws IOException {
93         final String content = new String(
94                 Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
95
96         ServiceInstanceUri instance = new ServiceInstanceUri("key3");
97         final Optional<String> result = instance.extractRelatedLink(content);
98         final String expected =
99                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
100         assertEquals("result is equal", expected, result.get());
101
102     }
103
104     @Test
105     public void oneKey()
106             throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
107
108         ServiceInstanceUri instance = new ServiceInstanceUri("key1");
109         ServiceInstanceUri spy = spy(instance);
110         doReturn(
111                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
112                         .when(spy).getObjectById(any(Object.class));
113
114         final URI result = spy.locateAndBuild();
115         final URI expected = UriBuilder.fromPath(
116                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
117                 .build();
118         assertEquals("result is equal", expected, result);
119
120     }
121
122     @Test
123     public void oneKeyQueryParams()
124             throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
125
126         ServiceInstanceUri instance = new ServiceInstanceUri("key1");
127         ServiceInstanceUri spy = spy(instance);
128         doReturn(
129                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
130                         .when(spy).getObjectById(any(Object.class));
131
132         final URI result = ((HttpAwareUri) spy.resourceVersion("1234")).locateAndBuild();
133         final URI expected = UriBuilder.fromUri(
134                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234")
135                 .build();
136         assertEquals("result is equal", expected, result);
137
138     }
139
140     @Test
141     public void oneKeyEncoded()
142             throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
143
144         ServiceInstanceUri instance = new ServiceInstanceUri("key1");
145         ServiceInstanceUri spy = spy(instance);
146         doReturn(
147                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space")
148                         .when(spy).getObjectById(any(Object.class));
149
150         final URI result = spy.locateAndBuild();
151         final URI expected = UriBuilder.fromUri(
152                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space")
153                 .build();
154         assertEquals("result is equal", expected, result);
155
156     }
157
158     @Test
159     public void oneKeyGetKeys()
160             throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
161
162         ServiceInstanceUri instance = new ServiceInstanceUri("key1");
163         ServiceInstanceUri spy = spy(instance);
164         doReturn(
165                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%28space")
166                         .when(spy).getObjectById(any(Object.class));
167
168         assertThat(spy.getURIKeys().values(), contains("key1", "key2", "key3(space"));
169
170     }
171
172     @Test
173     public void oneKeyClone() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
174         ServiceInstanceUri instance = new ServiceInstanceUri("key1");
175         ServiceInstanceUri spy = spy(instance);
176         String uri =
177                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
178         doReturn(Optional.of(uri)).when(spy).getCachedValue();
179         final URI result = ((HttpAwareUri) spy.resourceVersion("1234").clone()).locateAndBuild();
180         final URI expected = UriBuilder.fromUri(
181                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234")
182                 .build();
183         assertEquals("result is equal", expected, result);
184     }
185
186     @Test
187     public void threeKey() throws IOException {
188
189         ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
190         final URI result = instance.build();
191         final URI expected = UriBuilder.fromPath(
192                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
193                 .build();
194         assertEquals("result is equal", expected, result);
195
196     }
197
198     @Test
199     public void notfound() throws IOException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
200         final String content =
201                 new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "empty-query-result.json")));
202
203         ServiceInstanceUri instance = new ServiceInstanceUri("key3");
204         ServiceInstanceUri spy = spy(instance);
205         AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
206         AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
207         when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class),
208                 ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
209         when(wrapper.getJson()).thenReturn(content);
210         when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
211         exception.expect(GraphInventoryUriComputationException.class);
212         spy.locateAndBuild();
213
214     }
215
216     @Test
217     public void cloneTest() {
218         ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
219         final URI result = instance.build();
220         final URI result2 = instance.clone().queryParam("something", "new").build();
221         assertEquals("uris are not equal", false, result.toString().equals(result2.toString()));
222
223     }
224
225     @Test
226     public void noVertexFound() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
227         ServiceInstanceUri instance = new ServiceInstanceUri("key3");
228         ServiceInstanceUri spy = spy(instance);
229         AAIResourcesClient client = aaiClient;
230         doReturn(client).when(spy).getResourcesClient();
231         wireMockRule
232                 .stubFor(get(urlPathMatching("/aai/v[0-9]+/nodes/service-instances/service-instance/key3")).willReturn(
233                         aResponse().withStatus(404).withHeader("Content-Type", "application/json").withBodyFile("")));
234         exception.expect(NotFoundException.class);
235         spy.locateAndBuild();
236     }
237
238     @Test
239     public void serializeTest() throws IOException, ClassNotFoundException, GraphInventoryUriNotFoundException,
240             GraphInventoryPayloadException {
241         ServiceInstanceUri instance = new ServiceInstanceUri("key3");
242         final String content = new String(
243                 Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
244
245         ServiceInstanceUri spy = spy(instance);
246         AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
247         AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
248         when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class),
249                 ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
250         when(wrapper.getJson()).thenReturn(content);
251         when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
252         spy.locateAndBuild();
253         instance = spy.clone();
254         ByteArrayOutputStream bos = new ByteArrayOutputStream();
255
256         ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos);
257         objectOutputStream.writeObject(instance);
258         objectOutputStream.flush();
259         objectOutputStream.close();
260
261         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
262
263         ObjectInputStream objectInputStream = new ObjectInputStream(bis);
264         ServiceInstanceUri e2 = (ServiceInstanceUri) objectInputStream.readObject();
265         objectInputStream.close();
266
267         ServiceInstanceUri spy2 = spy(e2);
268
269         assertEquals(spy2.build().toString(), instance.build().toString());
270
271         // use the cached value do not call out to external system
272         verify(spy2, times(0)).getResourcesClient();
273     }
274
275     @Test
276     public void relatedToTest() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
277         ServiceInstanceUri instance = new ServiceInstanceUri("key1");
278         ServiceInstanceUri spy = spy(instance);
279         doReturn(
280                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
281                         .when(spy).getObjectById(any(Object.class));
282
283         final URI result = spy.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", "my-vnf-name").build();
284         final URI expected = UriBuilder.fromUri(
285                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3/related-to/generic-vnfs?vnf-name=my-vnf-name")
286                 .build();
287         assertEquals("result is equal", expected, result);
288     }
289
290     @Test
291     public void relatedToEqualityTestBeforeBuildTest()
292             throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
293         ServiceInstanceUri instance = new ServiceInstanceUri("key1");
294         ServiceInstanceUri spy = spy(instance);
295
296         final AAIPluralResourceUri result =
297                 spy.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", "my-vnf-name");
298
299         assertEquals("result is equal", result, result);
300     }
301 }