e8296665778b8f294af760e622c5a93de2f890cc
[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.when;
35 import java.io.IOException;
36 import java.net.URI;
37 import java.net.URISyntaxException;
38 import java.nio.file.Files;
39 import java.nio.file.Paths;
40 import java.util.Optional;
41 import javax.ws.rs.NotFoundException;
42 import javax.ws.rs.core.UriBuilder;
43 import org.junit.Before;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.junit.rules.ExpectedException;
47 import org.junit.runner.RunWith;
48 import org.mockito.ArgumentMatchers;
49 import org.mockito.InjectMocks;
50 import org.mockito.Spy;
51 import org.mockito.junit.MockitoJUnitRunner;
52 import org.onap.so.client.aai.AAIClient;
53 import org.onap.so.client.aai.AAIResourcesClient;
54 import org.onap.so.client.aai.entities.AAIResultWrapper;
55 import org.onap.so.client.defaultproperties.DefaultAAIPropertiesImpl;
56 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException;
57 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException;
58 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException;
59 import com.github.tomakehurst.wiremock.junit.WireMockRule;
60
61 @RunWith(MockitoJUnitRunner.class)
62 public class ServiceInstanceUriTest {
63
64     private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/resources/";
65
66     @Rule
67     public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
68
69     @Rule
70     public final ExpectedException exception = ExpectedException.none();
71
72     @Spy
73     public AAIClient client;
74
75     @InjectMocks
76     public AAIResourcesClient aaiClient = new AAIResourcesClient();
77
78     @Before
79     public void beforeTest() {
80         doReturn(new DefaultAAIPropertiesImpl(wireMockRule.port())).when(client).getRestProperties();
81     }
82
83     @Test
84     public void found() throws IOException {
85         final String content = new String(
86                 Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
87
88         ServiceInstanceUri instance = new ServiceInstanceUri("key3");
89         final Optional<String> result = instance.extractRelatedLink(content);
90         final String expected =
91                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
92         assertEquals("result is equal", expected, result.get());
93
94     }
95
96     @Test
97     public void oneKey()
98             throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
99
100         ServiceInstanceUri instance = new ServiceInstanceUri("key1");
101         ServiceInstanceUri spy = spy(instance);
102         doReturn(
103                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
104                         .when(spy).getObjectById(any(Object.class));
105
106         final URI result = spy.build();
107         final URI expected = UriBuilder.fromPath(
108                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
109                 .build();
110         assertEquals("result is equal", expected, result);
111
112     }
113
114     @Test
115     public void oneKeyQueryParams()
116             throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
117
118         ServiceInstanceUri instance = new ServiceInstanceUri("key1");
119         ServiceInstanceUri spy = spy(instance);
120         doReturn(
121                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
122                         .when(spy).getObjectById(any(Object.class));
123
124         final URI result = spy.resourceVersion("1234").build();
125         final URI expected = UriBuilder.fromUri(
126                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234")
127                 .build();
128         assertEquals("result is equal", expected, result);
129
130     }
131
132     @Test
133     public void oneKeyEncoded()
134             throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
135
136         ServiceInstanceUri instance = new ServiceInstanceUri("key1");
137         ServiceInstanceUri spy = spy(instance);
138         doReturn(
139                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space")
140                         .when(spy).getObjectById(any(Object.class));
141
142         final URI result = spy.build();
143         final URI expected = UriBuilder.fromUri(
144                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space")
145                 .build();
146         assertEquals("result is equal", expected, result);
147
148     }
149
150     @Test
151     public void oneKeyGetKeys()
152             throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
153
154         ServiceInstanceUri instance = new ServiceInstanceUri("key1");
155         ServiceInstanceUri spy = spy(instance);
156         doReturn(
157                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%28space")
158                         .when(spy).getObjectById(any(Object.class));
159
160         assertThat(spy.getURIKeys().values(), contains("key1", "key2", "key3(space"));
161
162     }
163
164     @Test
165     public void oneKeyClone() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
166         ServiceInstanceUri instance = new ServiceInstanceUri("key1");
167         ServiceInstanceUri spy = spy(instance);
168         String uri =
169                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
170         doReturn(uri).when(spy).getObjectById(any(Object.class));
171         doReturn(Optional.of(uri)).when(spy).getCachedValue();
172         final URI result = spy.resourceVersion("1234").clone().build();
173         final URI expected = UriBuilder.fromUri(
174                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234")
175                 .build();
176         assertEquals("result is equal", expected, result);
177     }
178
179     @Test
180     public void threeKey() throws IOException {
181
182         ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
183         final URI result = instance.build();
184         final URI expected = UriBuilder.fromPath(
185                 "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3")
186                 .build();
187         assertEquals("result is equal", expected, result);
188
189     }
190
191     @Test
192     public void notfound() throws IOException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
193         final String content =
194                 new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "empty-query-result.json")));
195
196         ServiceInstanceUri instance = new ServiceInstanceUri("key3");
197         ServiceInstanceUri spy = spy(instance);
198         AAIResourcesClient mockResourcesClient = mock(AAIResourcesClient.class);
199         AAIResultWrapper wrapper = mock(AAIResultWrapper.class);
200         when(mockResourcesClient.get(ArgumentMatchers.<AAIResourceUri>any(AAIResourceUri.class),
201                 ArgumentMatchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
202         when(wrapper.getJson()).thenReturn(content);
203         when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
204         exception.expect(GraphInventoryUriComputationException.class);
205         spy.build();
206
207     }
208
209     @Test
210     public void cloneTest() {
211         ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
212         final URI result = instance.build();
213         final URI result2 = instance.clone().queryParam("something", "new").build();
214         assertEquals("uris are not equal", false, result.toString().equals(result2.toString()));
215
216     }
217
218     @Test
219     public void noVertexFound() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
220         ServiceInstanceUri instance = new ServiceInstanceUri("key3");
221         ServiceInstanceUri spy = spy(instance);
222         AAIResourcesClient client = aaiClient;
223         doReturn(client).when(spy).getResourcesClient();
224         wireMockRule
225                 .stubFor(get(urlPathMatching("/aai/v[0-9]+/nodes/service-instances/service-instance/key3")).willReturn(
226                         aResponse().withStatus(404).withHeader("Content-Type", "application/json").withBodyFile("")));
227         exception.expect(NotFoundException.class);
228         spy.build();
229     }
230 }