7e70f2d5f4c065072b6bd3479c04ffb3f7ddf1a8
[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.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;
37
38 import java.io.IOException;
39 import java.net.URI;
40 import java.net.URISyntaxException;
41 import java.nio.file.Files;
42 import java.nio.file.Paths;
43 import java.util.Optional;
44
45 import javax.ws.rs.NotFoundException;
46 import javax.ws.rs.core.UriBuilder;
47
48 import org.junit.Rule;
49 import org.junit.Test;
50 import org.junit.rules.ExpectedException;
51 import org.mockito.Matchers;
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;
58
59 import com.github.tomakehurst.wiremock.junit.WireMockRule;
60
61 public class ServiceInstanceUriTest {
62
63         private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/resources/";
64         
65         @Rule
66         public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8443));
67         
68         @Rule
69         public final ExpectedException exception = ExpectedException.none();
70          
71         @Test
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")));
74                  
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());
79                 
80         }
81         
82         @Test
83         public void oneKey() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
84                  
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));
88                 
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);
92                 
93         }
94         
95         @Test
96         public void oneKeyQueryParams() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
97                  
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));
101                 
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);
105                 
106         }
107         
108         @Test
109         public void oneKeyEncoded() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
110                  
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));
114                 
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);
118                 
119         }
120         
121         @Test
122         public void oneKeyGetKeys() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
123                  
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));
127                 
128                 assertThat(spy.getURIKeys().values(), contains("key1", "key2", "key3(space"));
129                 
130         }
131         @Test
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);
141         }
142         
143         @Test
144         public void threeKey() throws IOException {
145                  
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);
150                 
151         }
152         
153         @Test
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")));
156                  
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(Matchers.<AAIResourceUri>any(AAIResourceUri.class), Matchers.<Class<NotFoundException>>any())).thenReturn(wrapper);
162                 when(wrapper.getJson()).thenReturn(content);
163                 when(spy.getResourcesClient()).thenReturn(mockResourcesClient);
164                 exception.expect(GraphInventoryUriComputationException.class);
165                 spy.build();
166                 
167         }
168         
169         @Test
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()));
175                 
176         }
177         
178         @Test
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() 
186                                         .withStatus(404) 
187                                         .withHeader("Content-Type", "application/json") 
188                                         .withBodyFile("")));
189                 exception.expect(NotFoundException.class);
190                 spy.build();    
191         }
192         
193         private AAIResourcesClient createClient() {
194                 AAIResourcesClient client = spy(new AAIResourcesClient());
195                 doReturn(new DefaultAAIPropertiesImpl()).when(client).getRestProperties();
196                 return client;
197         }
198 }