Replaced all tabs with spaces in java and pom.xml
[so.git] / cloudify-client / src / test / java / org / onap / so / cloudify / connector / http / HttpClientRedirectStrategyTest.java
1 /*
2  * ============LICENSE_START======================================================= ONAP : SO
3  * ================================================================================ Copyright (C) 2018 Nokia.
4  * ============================================================================= Licensed under the Apache License,
5  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
12  * specific language governing permissions and limitations under the License.
13  * ============LICENSE_END=========================================================
14  */
15 package org.onap.so.cloudify.connector.http;
16
17 import static org.assertj.core.api.Assertions.assertThat;
18 import static org.mockito.BDDMockito.given;
19 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
20 import static org.mockito.Mockito.mock;
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import org.apache.http.HttpRequest;
24 import org.apache.http.HttpResponse;
25 import org.apache.http.HttpStatus;
26 import org.apache.http.ProtocolException;
27 import org.apache.http.client.methods.HttpDelete;
28 import org.apache.http.client.methods.HttpGet;
29 import org.apache.http.client.methods.HttpHead;
30 import org.apache.http.client.methods.HttpOptions;
31 import org.apache.http.client.methods.HttpPatch;
32 import org.apache.http.client.methods.HttpPost;
33 import org.apache.http.client.methods.HttpPut;
34 import org.apache.http.client.methods.HttpTrace;
35 import org.apache.http.client.methods.HttpUriRequest;
36 import org.apache.http.protocol.HttpContext;
37 import org.junit.Test;
38
39 public class HttpClientRedirectStrategyTest {
40
41     private HttpClientRedirectStrategy httpClientRedirectStrategy = new HttpClientRedirectStrategy();
42
43     @Test
44     public void isRedirectable_shouldReturnFalse_forNonRedirectableHttpMethods() {
45         assertThat(httpClientRedirectStrategy.isRedirectable(HttpPost.METHOD_NAME)).isFalse();
46         assertThat(httpClientRedirectStrategy.isRedirectable(HttpPatch.METHOD_NAME)).isFalse();
47         assertThat(httpClientRedirectStrategy.isRedirectable(HttpPut.METHOD_NAME)).isFalse();
48         assertThat(httpClientRedirectStrategy.isRedirectable(HttpOptions.METHOD_NAME)).isFalse();
49         assertThat(httpClientRedirectStrategy.isRedirectable(HttpTrace.METHOD_NAME)).isFalse();
50     }
51
52     @Test
53     public void isRedirectable_shouldReturnTrue_forRedirectableHttpMethods() {
54         assertThat(httpClientRedirectStrategy.isRedirectable(HttpGet.METHOD_NAME)).isTrue();
55         assertThat(httpClientRedirectStrategy.isRedirectable(HttpDelete.METHOD_NAME)).isTrue();
56         assertThat(httpClientRedirectStrategy.isRedirectable(HttpHead.METHOD_NAME)).isTrue();
57     }
58
59     @Test
60     public void getRedirect_shouldReturnHttpHeadUriRequest() throws URISyntaxException, ProtocolException {
61         assertHttpUriRequestFor(HttpHead.METHOD_NAME, HttpHead.class);
62     }
63
64     @Test
65     public void getRedirect_shouldReturnHttpGetUriRequest() throws URISyntaxException, ProtocolException {
66         assertHttpUriRequestFor(HttpGet.METHOD_NAME, HttpGet.class);
67     }
68
69     private void assertHttpUriRequestFor(String methodName, Class<? extends HttpUriRequest> expectedHttpMethodClass)
70             throws URISyntaxException, ProtocolException {
71         // GIVEN
72         HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS);
73         given(request.getRequestLine().getMethod()).willReturn(methodName);
74         HttpResponse response = null;
75         HttpContext context = null;
76         URI expectedUri = new URI("http://localhost/host");
77         // WHEN
78         HttpUriRequest httpUriRequest =
79                 new TestableHttpClientRedirectStrategy(expectedUri).getRedirect(request, response, context);
80         // THEN
81         assertThat(httpUriRequest).isInstanceOf(expectedHttpMethodClass);
82         assertThat(httpUriRequest.getURI()).isEqualTo(expectedUri);
83     }
84
85     @Test
86     public void getRedirect_shouldReturnHttpGetUri_byDefault() throws URISyntaxException, ProtocolException {
87         // GIVEN
88         HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS);
89         given(request.getRequestLine().getMethod()).willReturn(HttpPost.METHOD_NAME);
90         HttpResponse response = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
91         given(response.getStatusLine().getStatusCode()).willReturn(HttpStatus.SC_ACCEPTED);
92         URI expectedUri = new URI("http://localhost/host");
93         HttpContext context = null;
94         // WHEN
95         HttpUriRequest httpUriRequest =
96                 new TestableHttpClientRedirectStrategy(expectedUri).getRedirect(request, response, context);
97         // THEN
98         assertThat(httpUriRequest).isInstanceOf(HttpGet.class);
99         assertThat(httpUriRequest.getURI()).isEqualTo(expectedUri);
100     }
101
102     @Test
103     public void getRedirect_shouldCopyHttpRequestAndSetNewUri_forMovedTemporarilyStatus()
104             throws URISyntaxException, ProtocolException {
105         assertHttpRequestIsCopied(HttpStatus.SC_MOVED_TEMPORARILY);
106     }
107
108     @Test
109     public void getRedirect_shouldCopyHttpRequestAndSetNewUri_forTemporaryRedirectStatus()
110             throws URISyntaxException, ProtocolException {
111         assertHttpRequestIsCopied(HttpStatus.SC_TEMPORARY_REDIRECT);
112     }
113
114     private void assertHttpRequestIsCopied(int expectedHttpStatus) throws URISyntaxException, ProtocolException {
115         // GIVEN
116         HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS);
117         given(request.getRequestLine().getMethod()).willReturn(HttpGet.METHOD_NAME);
118         given(request.getRequestLine().getUri()).willReturn("http://hostname");
119         HttpResponse response = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
120         given(response.getStatusLine().getStatusCode()).willReturn(expectedHttpStatus);
121         URI expectedUri = new URI("http://localhost/host");
122         HttpContext context = null;
123         // WHEN
124         HttpUriRequest httpUriRequest =
125                 new TestableHttpClientRedirectStrategy(expectedUri).getRedirect(request, response, context);
126         // THEN
127         assertThat(httpUriRequest).isInstanceOf(HttpGet.class);
128         assertThat(httpUriRequest.getURI()).isEqualTo(expectedUri);
129     }
130
131     private static class TestableHttpClientRedirectStrategy extends HttpClientRedirectStrategy {
132
133         private final URI expectedUri;
134
135         public TestableHttpClientRedirectStrategy(URI expectedUri) {
136             this.expectedUri = expectedUri;
137         }
138
139         @Override
140         public URI getLocationURI(HttpRequest request, HttpResponse response, HttpContext context) {
141             return expectedUri;
142         }
143     }
144 }