From 44abe28675b5150e18c2f728401d781322b375c1 Mon Sep 17 00:00:00 2001 From: Tomasz Gwozdecki Date: Tue, 18 Sep 2018 03:33:10 -0400 Subject: junits for HttpClientRedirectStrategy -Added new tests for HttpClientRedirectStrategy -Tests for method getRedirect Change-Id: Ica45eac1cbf65170b5b9c84f002b05884c36f5a0 Issue-ID: SO-814 Signed-off-by: Tomasz Gwozdecki --- .../http/HttpClientRedirectStrategyTest.java | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'cloudify-client/src/test/java/org') diff --git a/cloudify-client/src/test/java/org/onap/so/cloudify/connector/http/HttpClientRedirectStrategyTest.java b/cloudify-client/src/test/java/org/onap/so/cloudify/connector/http/HttpClientRedirectStrategyTest.java index bfe7f164b2..9a05602b34 100644 --- a/cloudify-client/src/test/java/org/onap/so/cloudify/connector/http/HttpClientRedirectStrategyTest.java +++ b/cloudify-client/src/test/java/org/onap/so/cloudify/connector/http/HttpClientRedirectStrategyTest.java @@ -20,7 +20,15 @@ package org.onap.so.cloudify.connector.http; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import java.net.URI; +import java.net.URISyntaxException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.ProtocolException; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpHead; @@ -29,6 +37,8 @@ import org.apache.http.client.methods.HttpPatch; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpTrace; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.protocol.HttpContext; import org.junit.Test; public class HttpClientRedirectStrategyTest { @@ -50,4 +60,44 @@ public class HttpClientRedirectStrategyTest { assertThat(httpClientRedirectStrategy.isRedirectable(HttpDelete.METHOD_NAME)).isTrue(); assertThat(httpClientRedirectStrategy.isRedirectable(HttpHead.METHOD_NAME)).isTrue(); } + + @Test + public void getRedirect_shouldReturnHttpHeadUriRequest() throws URISyntaxException, ProtocolException { + assertHttpUriRequestFor(HttpHead.METHOD_NAME, HttpHead.class); + } + + @Test + public void getRedirect_shouldReturnHttpGetUriRequest() throws URISyntaxException, ProtocolException { + assertHttpUriRequestFor(HttpGet.METHOD_NAME, HttpGet.class); + } + + private void assertHttpUriRequestFor(String methodName, Class expectedHttpMethodClass) + throws URISyntaxException, ProtocolException { + // GIVEN + HttpRequest request = mock(HttpRequest.class, RETURNS_DEEP_STUBS); + given(request.getRequestLine().getMethod()).willReturn(methodName); + HttpResponse response = null; + HttpContext context = null; + URI expectedUri = new URI("http://localhost/host"); + // WHEN + HttpUriRequest httpUriRequest = new TestableHttpClientRedirectStrategy(expectedUri) + .getRedirect(request, response, context); + // THEN + assertThat(httpUriRequest).isInstanceOf(expectedHttpMethodClass); + assertThat(httpUriRequest.getURI()).isEqualTo(expectedUri); + } + + private static class TestableHttpClientRedirectStrategy extends HttpClientRedirectStrategy { + + private final URI expectedUri; + + public TestableHttpClientRedirectStrategy(URI expectedUri) { + this.expectedUri = expectedUri; + } + + @Override + public URI getLocationURI(HttpRequest request, HttpResponse response, HttpContext context) { + return expectedUri; + } + } } \ No newline at end of file -- cgit 1.2.3-korg