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