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