HttpLookupUri is now serializable again
[so.git] / common / src / test / java / org / onap / so / client / aai / entities / uri / ServiceInstanceUriTest.java
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.AAIObjectType;
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.Depth;
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.build();
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 = spy.resourceVersion("1234").build();
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.build();
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(uri).when(spy).getObjectById(any(Object.class));
179         doReturn(Optional.of(uri)).when(spy).getCachedValue();
180         final URI result = spy.resourceVersion("1234").clone().build();
181         final URI expected = UriBuilder.fromUri(
182                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234")
183                 .build();
184         assertEquals("result is equal", expected, result);
185     }
186
187     @Test
188     public void threeKey() throws IOException {
189
190         ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
191         final URI result = instance.build();
192         final URI expected = UriBuilder.fromPath(
193                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
194                 .build();
195         assertEquals("result is equal", expected, result);
196
197     }
198
199     @Test
200     public void notfound() throws IOException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
201         final String content =
202                 new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "empty-query-result.json")));
203
204         ServiceInstanceUri instance = new ServiceInstanceUri("key3");
205         ServiceInstanceUri spy = spy(instance);
206         AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
207         AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
208         when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class),
209                 ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
210         when(wrapper.getJson()).thenReturn(content);
211         when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
212         exception.expect(GraphInventoryUriComputationException.class);
213         spy.build();
214
215     }
216
217     @Test
218     public void cloneTest() {
219         ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
220         final URI result = instance.build();
221         final URI result2 = instance.clone().queryParam("something", "new").build();
222         assertEquals("uris are not equal", false, result.toString().equals(result2.toString()));
223
224     }
225
226     @Test
227     public void noVertexFound() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
228         ServiceInstanceUri instance = new ServiceInstanceUri("key3");
229         ServiceInstanceUri spy = spy(instance);
230         AAIResourcesClient client = aaiClient;
231         doReturn(client).when(spy).getResourcesClient();
232         wireMockRule
233                 .stubFor(get(urlPathMatching("/aai/v[0-9]+/nodes/service-instances/service-instance/key3")).willReturn(
234                         aResponse().withStatus(404).withHeader("Content-Type", "application/json").withBodyFile("")));
235         exception.expect(NotFoundException.class);
236         spy.build();
237     }
238
239     @Test
240     public void serializeTest() throws IOException, ClassNotFoundException, GraphInventoryUriNotFoundException,
241             GraphInventoryPayloadException {
242         ServiceInstanceUri instance = new ServiceInstanceUri("key3");
243         final String content = new String(
244                 Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
245
246         ServiceInstanceUri spy = spy(instance);
247         AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
248         AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
249         when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class),
250                 ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
251         when(wrapper.getJson()).thenReturn(content);
252         when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
253         spy.build();
254         instance = spy.clone();
255         ByteArrayOutputStream bos = new ByteArrayOutputStream();
256
257         ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos);
258         objectOutputStream.writeObject(instance);
259         objectOutputStream.flush();
260         objectOutputStream.close();
261
262         ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
263
264         ObjectInputStream objectInputStream = new ObjectInputStream(bis);
265         ServiceInstanceUri e2 = (ServiceInstanceUri) objectInputStream.readObject();
266         objectInputStream.close();
267
268         ServiceInstanceUri spy2 = spy(e2);
269
270         assertEquals(spy2.build().toString(), instance.build().toString());
271
272         // use the cached value do not call out to external system
273         verify(spy2, times(0)).getResourcesClient();
274
275     }
276 }