9a05602b348d35a313f8473ce3f521473bedf491
[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.ProtocolException;
32 import org.apache.http.client.methods.HttpDelete;
33 import org.apache.http.client.methods.HttpGet;
34 import org.apache.http.client.methods.HttpHead;
35 import org.apache.http.client.methods.HttpOptions;
36 import org.apache.http.client.methods.HttpPatch;
37 import org.apache.http.client.methods.HttpPost;
38 import org.apache.http.client.methods.HttpPut;
39 import org.apache.http.client.methods.HttpTrace;
40 import org.apache.http.client.methods.HttpUriRequest;
41 import org.apache.http.protocol.HttpContext;
42 import org.junit.Test;
43
44 public class HttpClientRedirectStrategyTest {
45
46     private HttpClientRedirectStrategy httpClientRedirectStrategy = new HttpClientRedirectStrategy();
47
48     @Test
49     public void isRedirectable_shouldReturnFalse_forNonRedirectableHttpMethods() {
50         assertThat(httpClientRedirectStrategy.isRedirectable(HttpPost.METHOD_NAME)).isFalse();
51         assertThat(httpClientRedirectStrategy.isRedirectable(HttpPatch.METHOD_NAME)).isFalse();
52         assertThat(httpClientRedirectStrategy.isRedirectable(HttpPut.METHOD_NAME)).isFalse();
53         assertThat(httpClientRedirectStrategy.isRedirectable(HttpOptions.METHOD_NAME)).isFalse();
54         assertThat(httpClientRedirectStrategy.isRedirectable(HttpTrace.METHOD_NAME)).isFalse();
55     }
56
57     @Test
58     public void isRedirectable_shouldReturnTrue_forRedirectableHttpMethods() {
59         assertThat(httpClientRedirectStrategy.isRedirectable(HttpGet.METHOD_NAME)).isTrue();
60         assertThat(httpClientRedirectStrategy.isRedirectable(HttpDelete.METHOD_NAME)).isTrue();
61         assertThat(httpClientRedirectStrategy.isRedirectable(HttpHead.METHOD_NAME)).isTrue();
62     }
63
64     @Test
65     public void getRedirect_shouldReturnHttpHeadUriRequest() throws URISyntaxException, ProtocolException {
66         assertHttpUriRequestFor(HttpHead.METHOD_NAME, HttpHead.class);
67     }
68
69     @Test
70     public void getRedirect_shouldReturnHttpGetUriRequest() throws URISyntaxException, ProtocolException {
71         assertHttpUriRequestFor(HttpGet.METHOD_NAME, HttpGet.class);
72     }
73
74     private void assertHttpUriRequestFor(String methodName, Class<? extends HttpUriRequest> expectedHttpMethodClass)
75         throws URISyntaxException, ProtocolException {
76         // GIVEN
77         HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS);
78         given(request.getRequestLine().getMethod()).willReturn(methodName);
79         HttpResponse response = null;
80         HttpContext context = null;
81         URI expectedUri = new URI("http://localhost/host");
82         // WHEN
83         HttpUriRequest httpUriRequest = new TestableHttpClientRedirectStrategy(expectedUri)
84             .getRedirect(request, response, context);
85         // THEN
86         assertThat(httpUriRequest).isInstanceOf(expectedHttpMethodClass);
87         assertThat(httpUriRequest.getURI()).isEqualTo(expectedUri);
88     }
89
90     private static class TestableHttpClientRedirectStrategy extends HttpClientRedirectStrategy {
91
92         private final URI expectedUri;
93
94         public TestableHttpClientRedirectStrategy(URI expectedUri) {
95             this.expectedUri = expectedUri;
96         }
97
98         @Override
99         public URI getLocationURI(HttpRequest request, HttpResponse response, HttpContext context) {
100             return expectedUri;
101         }
102     }
103 }