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