Containerization feature of SO
[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.containing;
25 import static com.github.tomakehurst.wiremock.client.WireMock.put;
26 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
28 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
29 import static org.hamcrest.MatcherAssert.assertThat;
30 import static org.hamcrest.Matchers.containsString;
31 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
32 import static org.junit.Assert.assertEquals;
33 import static org.mockito.Matchers.any;
34 import static org.mockito.Mockito.doReturn;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.spy;
37 import static org.mockito.Mockito.when;
38
39 import java.io.IOException;
40 import java.net.URI;
41 import java.net.URISyntaxException;
42 import java.nio.file.Files;
43 import java.nio.file.Paths;
44 import java.util.Optional;
45
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.onap.so.client.aai.AAIQueryClient;
52 import org.onap.so.client.graphinventory.Format;
53 import org.onap.so.client.aai.entities.CustomQuery;
54 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPayloadException;
55 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException;
56 import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriNotFoundException;
57
58 import com.github.tomakehurst.wiremock.junit.WireMockRule;
59
60 public class ServiceInstanceUriTest {
61
62         private final static String AAI_JSON_FILE_LOCATION = "src/test/resources/__files/aai/resources/";
63         
64         @Rule
65         public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8443));
66         
67         @Rule
68         public final ExpectedException exception = ExpectedException.none();
69          
70         @Test
71         public void found() throws IOException {
72                 final String content = new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "service-instance-pathed-query.json")));
73                  
74                 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
75                 final Optional<String> result = instance.extractRelatedLink(content);
76                 final String expected = "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
77                 assertEquals("result is equal", expected, result.get());
78                 
79         }
80         
81         @Test
82         public void oneKey() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
83                  
84                 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
85                 ServiceInstanceUri spy = spy(instance);
86                 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getSerivceInstance(any(Object.class));
87                 
88                 final URI result = spy.build();
89                 final URI expected = UriBuilder.fromPath("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").build();
90                 assertEquals("result is equal", expected, result);
91                 
92         }
93         
94         @Test
95         public void oneKeyQueryParams() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
96                  
97                 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
98                 ServiceInstanceUri spy = spy(instance);
99                 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").when(spy).getSerivceInstance(any(Object.class));
100                 
101                 final URI result = spy.resourceVersion("1234").build();
102                 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build();
103                 assertEquals("result is equal", expected, result);
104                 
105         }
106         
107         @Test
108         public void oneKeyEncoded() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
109                  
110                 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
111                 ServiceInstanceUri spy = spy(instance);
112                 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").when(spy).getSerivceInstance(any(Object.class));
113                 
114                 final URI result = spy.build();
115                 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%20space").build();
116                 assertEquals("result is equal", expected, result);
117                 
118         }
119         
120         @Test
121         public void oneKeyGetKeys() throws IOException, URISyntaxException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
122                  
123                 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
124                 ServiceInstanceUri spy = spy(instance);
125                 doReturn("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3%28space").when(spy).getSerivceInstance(any(Object.class));
126                 
127                 assertThat(spy.getURIKeys().values(), contains("key1", "key2", "key3(space"));
128                 
129         }
130         @Test
131         public void oneKeyClone() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
132                 ServiceInstanceUri instance = new ServiceInstanceUri("key1");
133                 ServiceInstanceUri spy = spy(instance);
134                 String uri = "/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3";
135                 doReturn(uri).when(spy).getSerivceInstance(any(Object.class));
136                 doReturn(Optional.of(uri)).when(spy).getCachedValue();
137                 final URI result = spy.resourceVersion("1234").clone().build();
138                 final URI expected = UriBuilder.fromUri("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3?resource-version=1234").build();
139                 assertEquals("result is equal", expected, result);
140         }
141         
142         @Test
143         public void threeKey() throws IOException {
144                  
145                 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
146                 final URI result = instance.build();
147                 final URI expected = UriBuilder.fromPath("/business/customers/customer/key1/service-subscriptions/service-subscription/key2/service-instances/service-instance/key3").build();
148                 assertEquals("result is equal", expected, result);
149                 
150         }
151         
152         @Test
153         public void notfound() throws IOException, GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
154                 final String content = new String(Files.readAllBytes(Paths.get(AAI_JSON_FILE_LOCATION + "empty-query-result.json")));
155                  
156                 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
157                 ServiceInstanceUri spy = spy(instance);
158                 AAIQueryClient mockQueryClient = mock(AAIQueryClient.class);
159                 when(mockQueryClient.query(any(Format.class), any(CustomQuery.class))).thenReturn(content);
160                 when(spy.getQueryClient()).thenReturn(mockQueryClient);
161                 exception.expect(GraphInventoryUriComputationException.class);
162                 spy.build();
163                 
164         }
165         
166         @Test
167         public void cloneTest() {
168                 ServiceInstanceUri instance = new ServiceInstanceUri("key1", "key2", "key3");
169                 final URI result = instance.build();
170                 final URI result2 = instance.clone().queryParam("something", "new").build();
171                 assertEquals("uris are not equal", false, result.toString().equals(result2.toString()));
172                 
173         }
174         
175         @Test
176         public void noVertexFound() throws GraphInventoryUriNotFoundException, GraphInventoryPayloadException {
177                 ServiceInstanceUri instance = new ServiceInstanceUri("key3");
178                 ServiceInstanceUri spy = spy(instance);
179                 AAIQueryClient client = mock(AAIQueryClient.class);
180                 when(client.query(any(Format.class), any(CustomQuery.class))).thenReturn("{\"results\":[]}");
181                 doReturn(client).when(spy).getQueryClient();
182                 stubFor(put(urlMatching("/aai/v[0-9]+/query.*")) 
183                                 .withRequestBody(containing("key3")) 
184                                 .willReturn(aResponse() 
185                                         .withStatus(400) 
186                                         .withHeader("Content-Type", "application/json") 
187                                         .withBodyFile("")));
188                 exception.expect(GraphInventoryUriComputationException.class);
189                 exception.expectMessage(containsString("NotFoundException"));
190                 spy.build();    
191         }
192 }