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