Containerization feature of SO
[so.git] / cloudify-client / src / test / java / org / onap / so / cloudify / connector / http / HttpClientConnectorTest.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.cloudify.connector.http;
22
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.post;
25 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
26 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
27
28 import com.github.tomakehurst.wiremock.http.Fault;
29 import com.github.tomakehurst.wiremock.junit.WireMockRule;
30 import static com.github.tomakehurst.wiremock.client.WireMock.get;
31 import static com.github.tomakehurst.wiremock.client.WireMock.delete;
32 import static com.github.tomakehurst.wiremock.client.WireMock.put;
33 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
34 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
35 import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
36 import static com.github.tomakehurst.wiremock.client.WireMock.verify;
37 import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
38 import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor;
39 import static com.github.tomakehurst.wiremock.client.WireMock.deleteRequestedFor;
40 import org.apache.http.HttpStatus;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.rules.ExpectedException;
44 import static org.hamcrest.CoreMatchers.*;
45 import static org.junit.Assert.assertEquals;
46
47 import java.io.ByteArrayInputStream;
48 import java.io.InputStream;
49 import java.nio.charset.StandardCharsets;
50
51 import org.onap.so.cloudify.base.client.CloudifyConnectException;
52 import org.onap.so.cloudify.base.client.CloudifyRequest;
53 import org.onap.so.cloudify.base.client.CloudifyResponseException;
54 import org.onap.so.cloudify.base.client.Entity;
55 import org.onap.so.cloudify.base.client.HttpMethod;
56 import org.onap.so.cloudify.v3.model.Deployment;
57
58 public class HttpClientConnectorTest {
59         
60         @Rule
61         public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
62         
63         @Rule
64         public ExpectedException thrown = ExpectedException.none();
65
66         @Test
67         public void sunnyDay_POST(){                    
68                 wireMockRule.stubFor(post(urlPathEqualTo("/testUrl")).willReturn(aResponse()
69                                 .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK)));
70                 int port = wireMockRule.port();
71                 HttpClientConnector conector = new HttpClientConnector();
72                 CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>();
73                 Deployment deployment = new Deployment();
74                 deployment.setId("id");
75                 request.entity(deployment, "application/json");
76                 request.endpoint("http://localhost:"+port+"/testUrl");
77                 request.setBasicAuthentication("USER","PASSWORD");
78                 request.header("Content-Type","application/json");
79                 request.method(HttpMethod.POST);
80                 conector.request(request);
81                 verify(postRequestedFor(urlEqualTo("/testUrl")));
82         }
83
84
85         @Test
86         public void sunnyDay_GET(){                     
87                 wireMockRule.stubFor(get(urlPathEqualTo("/testUrl")).willReturn(aResponse()
88                                 .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK)));
89                 int port = wireMockRule.port();
90                 HttpClientConnector conector = new HttpClientConnector();
91                 CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>();
92                 request.endpoint("http://localhost:"+port+"/testUrl");
93                 request.setBasicAuthentication("USER","PASSWORD");
94                 request.method(HttpMethod.GET);
95                 conector.request(request);
96                 verify(getRequestedFor(urlEqualTo("/testUrl")));
97         }
98         
99         @Test
100         public void sunnyDay_PUT(){                     
101                 wireMockRule.stubFor(put(urlPathEqualTo("/testUrl")).willReturn(aResponse()
102                                 .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK)));
103                 int port = wireMockRule.port();
104                 HttpClientConnector conector = new HttpClientConnector();
105                 CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>();
106                 request.endpoint("http://localhost:"+port+"/testUrl");
107                 request.setBasicAuthentication("USER","PASSWORD");
108                 request.method(HttpMethod.PUT);
109                 conector.request(request);
110                 verify(putRequestedFor(urlEqualTo("/testUrl")));
111         }
112         
113         
114         @Test
115         public void sunnyDay_DELETE(){                  
116                 wireMockRule.stubFor(delete(urlPathEqualTo("/testUrl")).willReturn(aResponse()
117                                 .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK)));
118                 int port = wireMockRule.port();
119                 HttpClientConnector conector = new HttpClientConnector();
120                 CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>();
121                 request.endpoint("http://localhost:"+port+"/testUrl");
122                 request.setBasicAuthentication("USER","PASSWORD");
123                 request.method(HttpMethod.DELETE);
124                 conector.request(request);
125                 verify(deleteRequestedFor(urlEqualTo("/testUrl")));
126         }
127         
128         
129         @Test
130         public void rainyDay_PATCH(){                    
131                 thrown.expect(HttpClientException.class);
132                 thrown.expectMessage("Unrecognized HTTP Method: PATCH");
133                 HttpClientConnector conector = new HttpClientConnector();
134                 CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>();
135                 request.endpoint("http://localhost:123123/testUrl");
136                 request.setBasicAuthentication("USER","PASSWORD");
137                 request.method(HttpMethod.PATCH);
138                 conector.request(request);
139         
140         }
141         
142         @Test
143         public void rainyDayRunTimeException(){ 
144                 wireMockRule.stubFor(post(urlEqualTo("/503")).willReturn(
145                 aResponse().withStatus(503).withHeader("Content-Type", "text/plain").withBody("failure")));
146                 thrown.expect(RuntimeException.class);
147                 thrown.expectMessage("Unexpected client exception");
148                 HttpClientConnector conector = new HttpClientConnector();
149                 CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>();
150                 request.endpoint("http://localhost:123123/503");
151                 request.setBasicAuthentication("USER","PASSWORD");
152                 request.method(HttpMethod.POST);
153                 conector.request(request);
154         
155         }
156         
157         @Test
158         public void rainyDayBadUri() {
159                 wireMockRule.stubFor(post(urlPathEqualTo("/testUrl")).willReturn(aResponse()
160                                 .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK)));
161                 thrown.expect(HttpClientException.class);
162                 int port = wireMockRule.port();
163                 HttpClientConnector conector = new HttpClientConnector();
164                 CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>();
165                 Deployment deployment = new Deployment();
166                 deployment.setId("id");
167                 request.entity(deployment, "application/json");
168                 request.endpoint("(@#$@(#*$&asfasdf");
169                 request.setBasicAuthentication("USER","PASSWORD");
170                 request.header("Content-Type","application/json");
171                 request.method(HttpMethod.POST);
172                 conector.request(request);
173         }
174
175         @Test
176         public void sunnyDayWithJsonEntity_POST(){                      
177                 wireMockRule.stubFor(post(urlPathEqualTo("/testUrl")).willReturn(aResponse()
178                                 .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK)));
179                 int port = wireMockRule.port();
180                 HttpClientConnector conector = new HttpClientConnector();
181                 
182                 Deployment deployment = new Deployment();
183                 deployment.setId("id");
184
185                 CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(null, HttpMethod.POST, "/", Entity.json(deployment), null);
186
187                 request.endpoint("http://localhost:"+port);
188                 request.path("testUrl");
189                 request.header("Content-Type","application/json");
190                 request.header("Content-Type",  null);
191                 
192                 request.returnType(Deployment.class);
193                 assertEquals(Deployment.class, request.returnType());
194                 
195                 Entity<Deployment> t = request.json(deployment);
196                 assertEquals(t.getEntity().getId(), "id");
197
198                 request.queryParam("test", "one").queryParam("test",  "two");
199                 
200                 conector.request(request);
201
202                 verify(postRequestedFor(urlEqualTo("/testUrl?test=two")));
203         }
204
205         @Test
206         public void sunnyDayWithStreamEntity_POST() {                   
207                 wireMockRule.stubFor(post(urlPathEqualTo("/testUrl")).willReturn(aResponse()
208                                 .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_OK)));
209                 int port = wireMockRule.port();
210                 HttpClientConnector conector = new HttpClientConnector();
211
212                 InputStream is = new ByteArrayInputStream("{}".getBytes(StandardCharsets.UTF_8));
213
214                 CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>(null, HttpMethod.POST, "/testUrl", Entity.stream(is), null);
215                 
216                 request.endpoint("http://localhost:"+port);
217                 request.setBasicAuthentication("USER","PASSWORD");
218                 request.header("Content-Type","application/json");
219
220                 conector.request(request);
221                 verify(postRequestedFor(urlEqualTo("/testUrl")));
222         }
223
224         @Test
225         public void rainyDayGarbageData(){                      
226                 wireMockRule.stubFor(get(urlPathEqualTo("/testUrl")).willReturn(
227                                 aResponse().withFault(Fault.RANDOM_DATA_THEN_CLOSE)));
228                 thrown.expect(CloudifyConnectException.class);
229                 int port = wireMockRule.port();
230                 HttpClientConnector conector = new HttpClientConnector();
231                 CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>();
232                 request.endpoint("http://localhost:"+port+"/testUrl");
233                 request.setBasicAuthentication("USER","PASSWORD");
234                 request.method(HttpMethod.GET);
235                 conector.request(request);
236         }
237
238         @Test
239         public void rainyDayEmptyResponse(){
240                 wireMockRule.stubFor(get(urlPathEqualTo("/testUrl")).willReturn(aResponse()
241                                 .withHeader("Content-Type", "application/json").withBody("TEST").withStatus(HttpStatus.SC_NOT_FOUND)));
242                 
243                 thrown.expect(HttpClientException.class);
244                 int port = wireMockRule.port();
245                 HttpClientConnector conector = new HttpClientConnector();
246                 CloudifyRequest<Deployment> request = new CloudifyRequest<Deployment>();
247                 request.endpoint("http://localhost:"+port+"/testUrl");
248                 request.setBasicAuthentication("USER","PASSWORD");
249                 request.method(HttpMethod.GET);
250                 conector.request(request);  // gets down to "Get here on an error response (4XX-5XX)", then tries to throw a CloudifyResponseException, which calls getEntity, which tries to parse an HTML error page as a JSON, which causes the HttpClientException.
251         }
252         
253
254 }