junits for HttpClientRedirectStrategy
[so.git] / cloudify-client / src / test / java / org / onap / so / cloudify / connector / http / HttpClientRedirectStrategyTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : SO
4  * ================================================================================
5  * Copyright (C) 2018 Nokia.
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 package org.onap.so.cloudify.connector.http;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.mockito.BDDMockito.given;
24 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
25 import static org.mockito.Mockito.mock;
26
27 import java.net.URI;
28 import java.net.URISyntaxException;
29 import org.apache.http.HttpRequest;
30 import org.apache.http.HttpResponse;
31 import org.apache.http.HttpStatus;
32 import org.apache.http.ProtocolException;
33 import org.apache.http.client.methods.HttpDelete;
34 import org.apache.http.client.methods.HttpGet;
35 import org.apache.http.client.methods.HttpHead;
36 import org.apache.http.client.methods.HttpOptions;
37 import org.apache.http.client.methods.HttpPatch;
38 import org.apache.http.client.methods.HttpPost;
39 import org.apache.http.client.methods.HttpPut;
40 import org.apache.http.client.methods.HttpTrace;
41 import org.apache.http.client.methods.HttpUriRequest;
42 import org.apache.http.protocol.HttpContext;
43 import org.junit.Test;
44
45 public class HttpClientRedirectStrategyTest {
46
47     private HttpClientRedirectStrategy httpClientRedirectStrategy = new HttpClientRedirectStrategy();
48
49     @Test
50     public void isRedirectable_shouldReturnFalse_forNonRedirectableHttpMethods() {
51         assertThat(httpClientRedirectStrategy.isRedirectable(HttpPost.METHOD_NAME)).isFalse();
52         assertThat(httpClientRedirectStrategy.isRedirectable(HttpPatch.METHOD_NAME)).isFalse();
53         assertThat(httpClientRedirectStrategy.isRedirectable(HttpPut.METHOD_NAME)).isFalse();
54         assertThat(httpClientRedirectStrategy.isRedirectable(HttpOptions.METHOD_NAME)).isFalse();
55         assertThat(httpClientRedirectStrategy.isRedirectable(HttpTrace.METHOD_NAME)).isFalse();
56     }
57
58     @Test
59     public void isRedirectable_shouldReturnTrue_forRedirectableHttpMethods() {
60         assertThat(httpClientRedirectStrategy.isRedirectable(HttpGet.METHOD_NAME)).isTrue();
61         assertThat(httpClientRedirectStrategy.isRedirectable(HttpDelete.METHOD_NAME)).isTrue();
62         assertThat(httpClientRedirectStrategy.isRedirectable(HttpHead.METHOD_NAME)).isTrue();
63     }
64
65     @Test
66     public void getRedirect_shouldReturnHttpHeadUriRequest() throws URISyntaxException, ProtocolException {
67         assertHttpUriRequestFor(HttpHead.METHOD_NAME, HttpHead.class);
68     }
69
70     @Test
71     public void getRedirect_shouldReturnHttpGetUriRequest() throws URISyntaxException, ProtocolException {
72         assertHttpUriRequestFor(HttpGet.METHOD_NAME, HttpGet.class);
73     }
74
75     private void assertHttpUriRequestFor(String methodName, Class<? extends HttpUriRequest> expectedHttpMethodClass)
76         throws URISyntaxException, ProtocolException {
77         // GIVEN
78         HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS);
79         given(request.getRequestLine().getMethod()).willReturn(methodName);
80         HttpResponse response = null;
81         HttpContext context = null;
82         URI expectedUri = new URI("http://localhost/host");
83         // WHEN
84         HttpUriRequest httpUriRequest = new TestableHttpClientRedirectStrategy(expectedUri)
85             .getRedirect(request, response, context);
86         // THEN
87         assertThat(httpUriRequest).isInstanceOf(expectedHttpMethodClass);
88         assertThat(httpUriRequest.getURI()).isEqualTo(expectedUri);
89     }
90
91     @Test
92     public void getRedirect_shouldReturnHttpGetUri_byDefault() throws URISyntaxException, ProtocolException {
93         // GIVEN
94         HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS);
95         given(request.getRequestLine().getMethod()).willReturn(HttpPost.METHOD_NAME);
96         HttpResponse response = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
97         given(response.getStatusLine().getStatusCode()).willReturn(HttpStatus.SC_ACCEPTED);
98         URI expectedUri = new URI("http://localhost/host");
99         HttpContext context = null;
100         // WHEN
101         HttpUriRequest httpUriRequest = new TestableHttpClientRedirectStrategy(expectedUri)
102             .getRedirect(request, response, context);
103         // THEN
104         assertThat(httpUriRequest).isInstanceOf(HttpGet.class);
105         assertThat(httpUriRequest.getURI()).isEqualTo(expectedUri);
106     }
107
108     @Test
109     public void getRedirect_shouldCopyHttpRequestAndSetNewUri_forMovedTemporarilyStatus() throws URISyntaxException, ProtocolException {
110         assertHttpRequestIsCopied(HttpStatus.SC_MOVED_TEMPORARILY);
111     }
112
113     @Test
114     public void getRedirect_shouldCopyHttpRequestAndSetNewUri_forTemporaryRedirectStatus() throws URISyntaxException, ProtocolException {
115         assertHttpRequestIsCopied(HttpStatus.SC_TEMPORARY_REDIRECT);
116     }
117
118     private void assertHttpRequestIsCopied(int expectedHttpStatus) throws URISyntaxException, ProtocolException {
119         // GIVEN
120         HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS);
121         given(request.getRequestLine().getMethod()).willReturn(HttpGet.METHOD_NAME);
122         given(request.getRequestLine().getUri()).willReturn("http://hostname");
123         HttpResponse response = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
124         given(response.getStatusLine().getStatusCode()).willReturn(expectedHttpStatus);
125         URI expectedUri = new URI("http://localhost/host");
126         HttpContext context = null;
127         // WHEN
128         HttpUriRequest httpUriRequest = new TestableHttpClientRedirectStrategy(expectedUri)
129             .getRedirect(request, response, context);
130         // THEN
131         assertThat(httpUriRequest).isInstanceOf(HttpGet.class);
132         assertThat(httpUriRequest.getURI()).isEqualTo(expectedUri);
133     }
134
135     private static class TestableHttpClientRedirectStrategy extends HttpClientRedirectStrategy {
136
137         private final URI expectedUri;
138
139         public TestableHttpClientRedirectStrategy(URI expectedUri) {
140             this.expectedUri = expectedUri;
141         }
142         @Override
143         public URI getLocationURI(HttpRequest request, HttpResponse response, HttpContext context) {
144             return expectedUri;
145         }
146     }
147 }