diff options
author | Lee, Tian (tl5884) <TianL@amdocs.com> | 2018-11-21 12:02:49 +0000 |
---|---|---|
committer | Lee, Tian (tl5884) <TianL@amdocs.com> | 2018-11-21 12:11:43 +0000 |
commit | c782aa5250b32e8aa3c7ecd088f47fd87334a8ef (patch) | |
tree | 75d544b2b7a023de4fe5e96fe57eae6ef00952a1 /src/test/java/org | |
parent | ef858ed661134e651082675c091db056f8add98d (diff) |
Rewrite rest-client to use Jersey 2.x API1.4.0
This is to fix clashes with the rs-api 2.x jars that Spring Boot
requires on the classpath.
Change-Id: Ic36cdf0d650f96c54824e2f51c7f8060b2588c66
Issue-ID: AAI-1939
Signed-off-by: Lee, Tian (tl5884) <TianL@amdocs.com>
Diffstat (limited to 'src/test/java/org')
3 files changed, 243 insertions, 280 deletions
diff --git a/src/test/java/org/onap/aai/restclient/client/OperationResultTest.java b/src/test/java/org/onap/aai/restclient/client/OperationResultTest.java index 6f18f9a..c002b97 100644 --- a/src/test/java/org/onap/aai/restclient/client/OperationResultTest.java +++ b/src/test/java/org/onap/aai/restclient/client/OperationResultTest.java @@ -28,13 +28,10 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; - import org.junit.Before; import org.junit.Test; -import org.onap.aai.restclient.client.OperationResult; - -import com.sun.jersey.core.util.MultivaluedMapImpl; public class OperationResultTest { @@ -63,7 +60,7 @@ public class OperationResultTest { assertEquals(opResult.getResult(),"no content found"); assertTrue(opResult.wasSuccessful()); - MultivaluedMap<String,String> multiMap = new MultivaluedMapImpl(); + MultivaluedMap<String,String> multiMap = new MultivaluedHashMap<>(); multiMap.add("p1","v1"); multiMap.add("p2","v2"); opResult.setHeaders(multiMap); diff --git a/src/test/java/org/onap/aai/restclient/client/RestfulClientTest.java b/src/test/java/org/onap/aai/restclient/client/RestfulClientTest.java index 5eb7f1f..9fe09a1 100644 --- a/src/test/java/org/onap/aai/restclient/client/RestfulClientTest.java +++ b/src/test/java/org/onap/aai/restclient/client/RestfulClientTest.java @@ -1,8 +1,8 @@ /* * ============LICENSE_START=========================================================================================== * Copyright (c) 2017 AT&T Intellectual Property. - * Copyright (c) 2017 Amdocs - * Modification Copyright (c) 2018 IBM. + * Copyright (c) 2017 Amdocs + * Modification Copyright (c) 2018 IBM. * All rights reserved. * ===================================================================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with @@ -14,7 +14,7 @@ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. * ============LICENSE_END================================================== =========================================== - * + * * ECOMP and OpenECOMP are trademarks and service marks of AT&T Intellectual Property. */ package org.onap.aai.restclient.client; @@ -24,63 +24,59 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import javax.ws.rs.ProcessingException; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Invocation.Builder; +import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; - import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; -import org.onap.aai.restclient.client.OperationResult; -import org.onap.aai.restclient.client.RestClient; import org.onap.aai.restclient.enums.RestAuthenticationMode; import org.onap.aai.restclient.rest.RestClientBuilder; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.UniformInterfaceException; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.client.WebResource.Builder; -import com.sun.jersey.core.util.MultivaluedMapImpl; - public class RestfulClientTest { private static final String TEST_URL = "http://localhost:9000/aai/v7"; - private final MultivaluedMapImpl emptyMap = new MultivaluedMapImpl(); + private final MultivaluedMap<String, String> emptyMap = new MultivaluedHashMap<>(); private RestClientBuilder mockClientBuilder; private Client mockedClient; - private WebResource mockedWebResource; + private WebTarget mockedWebTarget; private Builder mockedBuilder; - private ClientResponse mockedClientResponse; + private Response mockedClientResponse; /** * Test case initialization - * + * * @throws Exception the exception */ - @SuppressWarnings("unchecked") @Before public void init() throws Exception { - mockedClientResponse = Mockito.mock(ClientResponse.class); + mockedClientResponse = Mockito.mock(Response.class); setResponseStatus(Response.Status.OK); - Mockito.when(mockedClientResponse.getHeaders()).thenReturn(emptyMap); - Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("hello"); + Mockito.when(mockedClientResponse.getHeaders()).thenReturn(new MultivaluedHashMap<>()); + Mockito.when(mockedClientResponse.readEntity(String.class)).thenReturn("hello"); mockedBuilder = Mockito.mock(Builder.class); - Mockito.when(mockedBuilder.get(Mockito.any(Class.class))).thenReturn(mockedClientResponse); - Mockito.when(mockedBuilder.post(Mockito.any(Class.class))).thenReturn(mockedClientResponse); - Mockito.when(mockedBuilder.put(Mockito.any(Class.class))).thenReturn(mockedClientResponse); - Mockito.when(mockedBuilder.delete(Mockito.any(Class.class))).thenReturn(mockedClientResponse); + Mockito.when(mockedBuilder.get()).thenReturn(mockedClientResponse); + Mockito.when(mockedBuilder.post(Mockito.any())).thenReturn(mockedClientResponse); + Mockito.when(mockedBuilder.put(Mockito.any())).thenReturn(mockedClientResponse); + Mockito.when(mockedBuilder.delete()).thenReturn(mockedClientResponse); Mockito.when(mockedBuilder.head()).thenReturn(mockedClientResponse); + Mockito.when(mockedBuilder.accept(Mockito.any(MediaType.class))).thenReturn(mockedBuilder); - mockedWebResource = Mockito.mock(WebResource.class); - Mockito.when(mockedWebResource.accept(Mockito.<MediaType>anyVararg())).thenReturn(mockedBuilder); + mockedWebTarget = Mockito.mock(WebTarget.class); + Mockito.when(mockedWebTarget.request()).thenReturn(mockedBuilder); mockedClient = Mockito.mock(Client.class); - Mockito.when(mockedClient.resource(Mockito.anyString())).thenReturn(mockedWebResource); + Mockito.when(mockedClient.target(Mockito.anyString())).thenReturn(mockedWebTarget); mockClientBuilder = Mockito.mock(RestClientBuilder.class); Mockito.when(mockClientBuilder.getClient()).thenReturn(mockedClient); @@ -170,24 +166,24 @@ public class RestfulClientTest { assertNull(result.getResult()); assertNull(result.getFailureCause()); } - + @Test public void validateSuccessfulPost_withMultivaluedHeader() throws Exception { RestClient restClient = buildClient(); - MultivaluedMapImpl headerMap = new MultivaluedMapImpl(); - + MultivaluedMap<String, String> headerMap = new MultivaluedHashMap<>(); + headerMap.add("txnId", "123"); headerMap.add("txnId", "456"); headerMap.add("txnId", "789"); OperationResult result = restClient.post(TEST_URL, "", headerMap, MediaType.APPLICATION_JSON_TYPE, - MediaType.APPLICATION_JSON_TYPE); + MediaType.APPLICATION_JSON_TYPE); - // capture the txnId header from the outgoing request + // capture the txnId header from the outgoing request ArgumentCaptor<String> txnIdHeaderName = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> txnIdHeaderValue = ArgumentCaptor.forClass(String.class); - + Mockito.verify(mockedBuilder, Mockito.atLeast(1)).header(txnIdHeaderName.capture(), txnIdHeaderValue.capture()); assertEquals("123;456;789", txnIdHeaderValue.getValue()); @@ -220,7 +216,7 @@ public class RestfulClientTest { @Test public void validateResourceNotFoundGet() throws Exception { setResponseStatus(Response.Status.NOT_FOUND); - Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("RNF"); + Mockito.when(mockedClientResponse.readEntity(String.class)).thenReturn("RNF"); OperationResult result = buildClient().get(TEST_URL, emptyMap, MediaType.APPLICATION_JSON_TYPE); @@ -249,7 +245,7 @@ public class RestfulClientTest { @Test public void validateHealthCheckFailureWithThrownException() throws Exception { - Mockito.when(mockedBuilder.get(Mockito.any(Class.class))).thenThrow(new IllegalArgumentException("error")); + Mockito.when(mockedBuilder.get()).thenThrow(new IllegalArgumentException("error")); boolean targetServiceHealthy = buildClient().healthCheck("http://localhost:9000/aai/util/echo", "startSerice", "targetService"); @@ -260,7 +256,7 @@ public class RestfulClientTest { @Test public void validateSuccessfulGetWithRetries() throws Exception { Mockito.when(mockedClientResponse.getStatus()).thenReturn(408).thenReturn(Response.Status.OK.getStatusCode()); - Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("error").thenReturn("ok"); + Mockito.when(mockedClientResponse.readEntity(String.class)).thenReturn("error").thenReturn("ok"); OperationResult result = buildClient().get(TEST_URL, emptyMap, MediaType.APPLICATION_JSON_TYPE, 3); @@ -273,7 +269,7 @@ public class RestfulClientTest { @Test public void validateFailedGetWithRetriesCausedByResourceNotFound() throws Exception { setResponseStatus(Response.Status.NOT_FOUND); - Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("error").thenReturn("ok"); + Mockito.when(mockedClientResponse.readEntity(String.class)).thenReturn("error").thenReturn("ok"); OperationResult result = buildClient().get(TEST_URL, emptyMap, MediaType.APPLICATION_JSON_TYPE, 3); @@ -286,7 +282,7 @@ public class RestfulClientTest { @Test public void validateFailedGetAfterMaxRetries() throws Exception { setResponseStatus(Response.Status.INTERNAL_SERVER_ERROR); - Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("error"); + Mockito.when(mockedClientResponse.readEntity(String.class)).thenReturn("error"); OperationResult result = buildClient().get(TEST_URL, emptyMap, MediaType.APPLICATION_JSON_TYPE, 3); @@ -315,7 +311,6 @@ public class RestfulClientTest { assertNull(result.getFailureCause()); } - @Test public void validateSuccessfulHead() throws Exception { OperationResult result = buildClient().head(TEST_URL, emptyMap, MediaType.APPLICATION_JSON_TYPE); @@ -323,7 +318,6 @@ public class RestfulClientTest { assertEquals(Response.Status.OK.getStatusCode(), result.getResultCode()); assertNotNull(result.getResult()); assertNull(result.getFailureCause()); - } @Test @@ -335,12 +329,11 @@ public class RestfulClientTest { assertEquals(Response.Status.OK.getStatusCode(), result.getResultCode()); assertNotNull(result.getResult()); assertNull(result.getFailureCause()); - } @Test public void testGetClient() throws Exception { - RestClientBuilder restClientBuilder= new RestClientBuilder(); + RestClientBuilder restClientBuilder = new RestClientBuilder(); restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_BASIC); restClientBuilder.setTruststoreFilename("truststore"); assertTrue(restClientBuilder.getClient() instanceof Client); @@ -348,7 +341,7 @@ public class RestfulClientTest { /** * Specify the status code of the response object returned by the mocked client - * + * * @param status object storing the status code to mock in the ClientResponse */ private void setResponseStatus(Status status) { @@ -360,9 +353,9 @@ public class RestfulClientTest { */ private void setResponseToNoContent() { setResponseStatus(Response.Status.NO_CONTENT); - // The Jersey client throws an exception when getEntity() is called following a 204 response - UniformInterfaceException uniformInterfaceException = new UniformInterfaceException(mockedClientResponse); - Mockito.when(mockedClientResponse.getEntity(String.class)).thenThrow(uniformInterfaceException); + // The Jersey client throws an exception when readEntity() is called following a 204 response + ProcessingException processingException = new ProcessingException("No content"); + Mockito.when(mockedClientResponse.readEntity(String.class)).thenThrow(processingException); } /** diff --git a/src/test/java/org/onap/aai/restclient/rest/RestClientBuilderTest.java b/src/test/java/org/onap/aai/restclient/rest/RestClientBuilderTest.java index 7155f9a..3878813 100644 --- a/src/test/java/org/onap/aai/restclient/rest/RestClientBuilderTest.java +++ b/src/test/java/org/onap/aai/restclient/rest/RestClientBuilderTest.java @@ -26,243 +26,216 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import javax.ws.rs.client.Client;
import org.junit.Before;
import org.junit.Test;
import org.onap.aai.restclient.enums.RestAuthenticationMode;
-import org.onap.aai.restclient.rest.RestClientBuilder;
-
-import com.sun.jersey.api.client.Client;
-import com.sun.jersey.client.urlconnection.HTTPSProperties;
/**
- * This suite of tests is intended to exercise the functionality of the generice REST client
- * builder.
+ * This suite of tests is intended to exercise the functionality of the generice REST client builder.
*/
public class RestClientBuilderTest {
- /**
- * Test case initialization
- *
- * @throws Exception the exception
- */
- @Before
- public void init() throws Exception {
- }
-
- private String generateAuthorizationHeaderValue(String username, String password) {
- String usernameAndPassword = username + ":" + password;
- return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes());
- }
-
- @Test
- public void validateAccesors() {
-
- RestClientBuilder restClientBuilder = new RestClientBuilder();
-
- // test defaults
- assertEquals(restClientBuilder.isValidateServerHostname(), RestClientBuilder.DEFAULT_VALIDATE_SERVER_HOST);
- assertEquals(restClientBuilder.isValidateServerCertChain(), RestClientBuilder.DEFAULT_VALIDATE_CERT_CHAIN);
- assertEquals(restClientBuilder.getClientCertFileName(), RestClientBuilder.DEFAULT_CLIENT_CERT_FILENAME);
- assertEquals(restClientBuilder.getClientCertPassword(), RestClientBuilder.DEFAULT_CERT_PASSWORD);
- assertEquals(restClientBuilder.getTruststoreFilename(), RestClientBuilder.DEFAULT_TRUST_STORE_FILENAME);
- assertEquals(restClientBuilder.getConnectTimeoutInMs(), RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS);
- assertEquals(restClientBuilder.getReadTimeoutInMs(), RestClientBuilder.DEFAULT_READ_TIMEOUT_MS);
- assertEquals(restClientBuilder.getAuthenticationMode(), RestClientBuilder.DEFAULT_AUTH_MODE);
- assertEquals(restClientBuilder.getBasicAuthUsername(), RestClientBuilder.DEFAULT_BASIC_AUTH_USERNAME);
- assertEquals(restClientBuilder.getBasicAuthPassword(), RestClientBuilder.DEFAULT_BASIC_AUTH_PASSWORD);
-
- restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE);
- restClientBuilder.setBasicAuthPassword("password");
- restClientBuilder.setBasicAuthUsername("username");
- restClientBuilder.setClientCertFileName("filename");
- restClientBuilder.setClientCertPassword("password");
- restClientBuilder.setConnectTimeoutInMs(12345);
- restClientBuilder.setReadTimeoutInMs(54321);
- restClientBuilder.setTruststoreFilename("truststore");
- restClientBuilder.setValidateServerCertChain(true);
- restClientBuilder.setValidateServerHostname(true);
-
- assertEquals(restClientBuilder.isValidateServerHostname(), true);
- assertEquals(restClientBuilder.isValidateServerCertChain(), true);
- assertEquals(restClientBuilder.getClientCertFileName(), "filename");
- assertEquals(restClientBuilder.getClientCertPassword(), "password");
- assertEquals(restClientBuilder.getTruststoreFilename(), "truststore");
- assertEquals(restClientBuilder.getConnectTimeoutInMs(), 12345);
- assertEquals(restClientBuilder.getReadTimeoutInMs(), 54321);
- assertEquals(restClientBuilder.getAuthenticationMode(), RestAuthenticationMode.UNKNOWN_MODE);
- assertEquals(restClientBuilder.getBasicAuthUsername(), "username");
- assertEquals(restClientBuilder.getBasicAuthPassword(), "password");
-
- assertEquals(restClientBuilder.getBasicAuthenticationCredentials(),
- generateAuthorizationHeaderValue("username", "password"));
-
- assertTrue(restClientBuilder.toString().contains("RestClientBuilder"));
-
- }
-
- @Test
- public void validateNoAuthClientCreation() throws Exception {
-
- RestClientBuilder restClientBuilder = new RestClientBuilder();
-
- restClientBuilder.setAuthenticationMode(RestAuthenticationMode.HTTP_NOAUTH);
- restClientBuilder.setConnectTimeoutInMs(12345);
- restClientBuilder.setReadTimeoutInMs(54321);
-
- Client client = restClientBuilder.getClient();
- assertNotNull(client);
- assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
- }
-
-
- @Test
- public void validateUnknownModeCreateNoAuthClient() throws Exception {
-
- RestClientBuilder restClientBuilder = new RestClientBuilder();
-
- restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE);
- restClientBuilder.setConnectTimeoutInMs(12345);
- restClientBuilder.setReadTimeoutInMs(54321);
-
- Client client = restClientBuilder.getClient();
- assertNotNull(client);
- assertNull(client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES));
- }
-
- @Test
- public void validateBasicAuthSslClient() throws Exception {
-
- RestClientBuilder restClientBuilder = new RestClientBuilder();
-
- restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_BASIC);
- restClientBuilder.setConnectTimeoutInMs(12345);
- restClientBuilder.setReadTimeoutInMs(54321);
- restClientBuilder.setBasicAuthUsername("username");
- restClientBuilder.setBasicAuthPassword("password");
- restClientBuilder.setTruststoreFilename("truststore");
-
- Client client = restClientBuilder.getClient();
-
- Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);
- HTTPSProperties sslProps = null;
- if ( sslPropertiesObj instanceof HTTPSProperties ) {
- sslProps = (HTTPSProperties)sslPropertiesObj;
- assertNotNull(sslProps.getHostnameVerifier());
- } else {
- fail("Unexpected value for https properties object");
+ /**
+ * Test case initialization
+ *
+ * @throws Exception the exception
+ */
+ @Before
+ public void init() throws Exception {}
+
+ private String generateAuthorizationHeaderValue(String username, String password) {
+ String usernameAndPassword = username + ":" + password;
+ return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes());
}
-
- }
-
- @Test (expected=IllegalArgumentException.class)
- public void validateSslCertClient_noHostOrCertChainValidation() throws Exception {
-
- RestClientBuilder restClientBuilder = new RestClientBuilder();
-
- restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
- restClientBuilder.setConnectTimeoutInMs(12345);
- restClientBuilder.setReadTimeoutInMs(54321);
- restClientBuilder.setValidateServerCertChain(false);
- restClientBuilder.setValidateServerHostname(false);
-
- Client client = restClientBuilder.getClient();
- }
-
- @Test (expected=IllegalArgumentException.class)
- public void validateSslCertClient_hostOnlyValidation() throws Exception {
-
- RestClientBuilder restClientBuilder = new RestClientBuilder();
-
- restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
- restClientBuilder.setConnectTimeoutInMs(12345);
- restClientBuilder.setReadTimeoutInMs(54321);
- restClientBuilder.setValidateServerCertChain(false);
- restClientBuilder.setValidateServerHostname(true);
-
- Client client = restClientBuilder.getClient();
-
- }
-
- @Test
- public void validateSslCertClient_certChainOnlyValidation() throws Exception {
-
- RestClientBuilder restClientBuilder = new RestClientBuilder();
-
- restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
- restClientBuilder.setConnectTimeoutInMs(12345);
- restClientBuilder.setReadTimeoutInMs(54321);
- restClientBuilder.setValidateServerCertChain(true);
- restClientBuilder.setValidateServerHostname(false);
- restClientBuilder.setTruststoreFilename("truststore");
- restClientBuilder.setClientCertPassword(null);
-
- Client client = restClientBuilder.getClient();
-
- Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);
- HTTPSProperties sslProps = null;
- if ( sslPropertiesObj instanceof HTTPSProperties ) {
- sslProps = (HTTPSProperties)sslPropertiesObj;
- assertNotNull(sslProps.getHostnameVerifier());
- } else {
- fail("Unexpected value for https properties object");
+
+ @Test
+ public void validateAccesors() {
+
+ RestClientBuilder restClientBuilder = new RestClientBuilder();
+
+ // test defaults
+ assertEquals(restClientBuilder.isValidateServerHostname(), RestClientBuilder.DEFAULT_VALIDATE_SERVER_HOST);
+ assertEquals(restClientBuilder.isValidateServerCertChain(), RestClientBuilder.DEFAULT_VALIDATE_CERT_CHAIN);
+ assertEquals(restClientBuilder.getClientCertFileName(), RestClientBuilder.DEFAULT_CLIENT_CERT_FILENAME);
+ assertEquals(restClientBuilder.getClientCertPassword(), RestClientBuilder.DEFAULT_CERT_PASSWORD);
+ assertEquals(restClientBuilder.getTruststoreFilename(), RestClientBuilder.DEFAULT_TRUST_STORE_FILENAME);
+ assertEquals(restClientBuilder.getConnectTimeoutInMs(), RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS);
+ assertEquals(restClientBuilder.getReadTimeoutInMs(), RestClientBuilder.DEFAULT_READ_TIMEOUT_MS);
+ assertEquals(restClientBuilder.getAuthenticationMode(), RestClientBuilder.DEFAULT_AUTH_MODE);
+ assertEquals(restClientBuilder.getBasicAuthUsername(), RestClientBuilder.DEFAULT_BASIC_AUTH_USERNAME);
+ assertEquals(restClientBuilder.getBasicAuthPassword(), RestClientBuilder.DEFAULT_BASIC_AUTH_PASSWORD);
+
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE);
+ restClientBuilder.setBasicAuthPassword("password");
+ restClientBuilder.setBasicAuthUsername("username");
+ restClientBuilder.setClientCertFileName("filename");
+ restClientBuilder.setClientCertPassword("password");
+ restClientBuilder.setConnectTimeoutInMs(12345);
+ restClientBuilder.setReadTimeoutInMs(54321);
+ restClientBuilder.setTruststoreFilename("truststore");
+ restClientBuilder.setValidateServerCertChain(true);
+ restClientBuilder.setValidateServerHostname(true);
+
+ assertEquals(restClientBuilder.isValidateServerHostname(), true);
+ assertEquals(restClientBuilder.isValidateServerCertChain(), true);
+ assertEquals(restClientBuilder.getClientCertFileName(), "filename");
+ assertEquals(restClientBuilder.getClientCertPassword(), "password");
+ assertEquals(restClientBuilder.getTruststoreFilename(), "truststore");
+ assertEquals(restClientBuilder.getConnectTimeoutInMs(), 12345);
+ assertEquals(restClientBuilder.getReadTimeoutInMs(), 54321);
+ assertEquals(restClientBuilder.getAuthenticationMode(), RestAuthenticationMode.UNKNOWN_MODE);
+ assertEquals(restClientBuilder.getBasicAuthUsername(), "username");
+ assertEquals(restClientBuilder.getBasicAuthPassword(), "password");
+
+ assertEquals(restClientBuilder.getBasicAuthenticationCredentials(),
+ generateAuthorizationHeaderValue("username", "password"));
+
+ assertTrue(restClientBuilder.toString().contains("RestClientBuilder"));
+
}
- }
-
- @Test
- public void validateSslCertClient_withHostAndCertChainValidation() throws Exception {
-
- RestClientBuilder restClientBuilder = new RestClientBuilder();
-
- restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
- restClientBuilder.setConnectTimeoutInMs(12345);
- restClientBuilder.setReadTimeoutInMs(54321);
- restClientBuilder.setValidateServerCertChain(true);
- restClientBuilder.setValidateServerHostname(true);
- restClientBuilder.setClientCertPassword("password");
- restClientBuilder.setTruststoreFilename("truststore");
-
- Client client = restClientBuilder.getClient();
-
- Object sslPropertiesObj = client.getProperties().get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);
- HTTPSProperties sslProps = null;
- if ( sslPropertiesObj instanceof HTTPSProperties ) {
- sslProps = (HTTPSProperties)sslPropertiesObj;
- assertNull(sslProps.getHostnameVerifier());
- } else {
- fail("Unexpected value for https properties object");
- } }
-
- @Test (expected=IllegalArgumentException.class)
- public void validateSslCertClient_illegalArgumentExceptionWhenTruststoreIsNull() throws Exception {
-
- RestClientBuilder restClientBuilder = new RestClientBuilder();
-
- restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
- restClientBuilder.setConnectTimeoutInMs(12345);
- restClientBuilder.setReadTimeoutInMs(54321);
- restClientBuilder.setValidateServerCertChain(true);
- restClientBuilder.setValidateServerHostname(true);
- restClientBuilder.setTruststoreFilename(null);
-
- /*
- * Creating the client in this scenario will cause an IllegalArgumentException caused by the
- * truststore being null
- */
- Client client = restClientBuilder.getClient();
-
- }
-
- @Test
- public void validateSslProtocolConfiguration() throws Exception {
-
- RestClientBuilder restClientBuilder = new RestClientBuilder();
- assertEquals(RestClientBuilder.DEFAULT_SSL_PROTOCOL, restClientBuilder.getSslProtocol());
-
- restClientBuilder.setSslProtocol("TLSv1.2");
- assertEquals("TLSv1.2", restClientBuilder.getSslProtocol());
-
- }
-
+
+ @Test
+ public void validateNoAuthClientCreation() throws Exception {
+
+ RestClientBuilder restClientBuilder = new RestClientBuilder();
+
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.HTTP_NOAUTH);
+ restClientBuilder.setConnectTimeoutInMs(12345);
+ restClientBuilder.setReadTimeoutInMs(54321);
+
+ Client client = restClientBuilder.getClient();
+ assertNotNull(client);
+ }
+
+
+ @Test
+ public void validateUnknownModeCreateNoAuthClient() throws Exception {
+
+ RestClientBuilder restClientBuilder = new RestClientBuilder();
+
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.UNKNOWN_MODE);
+ restClientBuilder.setConnectTimeoutInMs(12345);
+ restClientBuilder.setReadTimeoutInMs(54321);
+
+ Client client = restClientBuilder.getClient();
+ assertNotNull(client);
+ }
+
+ @Test
+ public void validateBasicAuthSslClient() throws Exception {
+
+ RestClientBuilder restClientBuilder = new RestClientBuilder();
+
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_BASIC);
+ restClientBuilder.setConnectTimeoutInMs(12345);
+ restClientBuilder.setReadTimeoutInMs(54321);
+ restClientBuilder.setBasicAuthUsername("username");
+ restClientBuilder.setBasicAuthPassword("password");
+ restClientBuilder.setTruststoreFilename("truststore");
+
+ Client client = restClientBuilder.getClient();
+ assertNotNull(client.getHostnameVerifier());
+ assertEquals("truststore", System.getProperty("javax.net.ssl.trustStore"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void validateSslCertClient_noHostOrCertChainValidation() throws Exception {
+
+ RestClientBuilder restClientBuilder = new RestClientBuilder();
+
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
+ restClientBuilder.setConnectTimeoutInMs(12345);
+ restClientBuilder.setReadTimeoutInMs(54321);
+ restClientBuilder.setValidateServerCertChain(false);
+ restClientBuilder.setValidateServerHostname(false);
+
+ restClientBuilder.getClient();
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void validateSslCertClient_hostOnlyValidation() throws Exception {
+
+ RestClientBuilder restClientBuilder = new RestClientBuilder();
+
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
+ restClientBuilder.setConnectTimeoutInMs(12345);
+ restClientBuilder.setReadTimeoutInMs(54321);
+ restClientBuilder.setValidateServerCertChain(false);
+ restClientBuilder.setValidateServerHostname(true);
+
+ restClientBuilder.getClient();
+
+ }
+
+ @Test
+ public void validateSslCertClient_certChainOnlyValidation() throws Exception {
+
+ RestClientBuilder restClientBuilder = new RestClientBuilder();
+
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
+ restClientBuilder.setConnectTimeoutInMs(12345);
+ restClientBuilder.setReadTimeoutInMs(54321);
+ restClientBuilder.setValidateServerCertChain(true);
+ restClientBuilder.setValidateServerHostname(false);
+ restClientBuilder.setTruststoreFilename("truststore");
+ restClientBuilder.setClientCertPassword(null);
+
+ Client client = restClientBuilder.getClient();
+ // TODO
+ assertNotNull(client.getHostnameVerifier());
+ assertEquals("truststore", System.getProperty("javax.net.ssl.trustStore"));
+ }
+
+ @Test
+ public void validateSslCertClient_withHostAndCertChainValidation() throws Exception {
+
+ RestClientBuilder restClientBuilder = new RestClientBuilder();
+
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
+ restClientBuilder.setConnectTimeoutInMs(12345);
+ restClientBuilder.setReadTimeoutInMs(54321);
+ restClientBuilder.setValidateServerCertChain(true);
+ restClientBuilder.setValidateServerHostname(true);
+ restClientBuilder.setClientCertPassword("password");
+ restClientBuilder.setTruststoreFilename("truststore");
+
+ Client client = restClientBuilder.getClient();
+ // TODO
+ assertNull(client.getHostnameVerifier());
+ assertEquals("truststore", System.getProperty("javax.net.ssl.trustStore"));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void validateSslCertClient_illegalArgumentExceptionWhenTruststoreIsNull() throws Exception {
+
+ RestClientBuilder restClientBuilder = new RestClientBuilder();
+
+ restClientBuilder.setAuthenticationMode(RestAuthenticationMode.SSL_CERT);
+ restClientBuilder.setConnectTimeoutInMs(12345);
+ restClientBuilder.setReadTimeoutInMs(54321);
+ restClientBuilder.setValidateServerCertChain(true);
+ restClientBuilder.setValidateServerHostname(true);
+ restClientBuilder.setTruststoreFilename(null);
+
+ /*
+ * Creating the client in this scenario will cause an IllegalArgumentException caused by the truststore being
+ * null
+ */
+ restClientBuilder.getClient();
+
+ }
+
+ @Test
+ public void validateSslProtocolConfiguration() throws Exception {
+
+ RestClientBuilder restClientBuilder = new RestClientBuilder();
+ assertEquals(RestClientBuilder.DEFAULT_SSL_PROTOCOL, restClientBuilder.getSslProtocol());
+
+ restClientBuilder.setSslProtocol("TLSv1.2");
+ assertEquals("TLSv1.2", restClientBuilder.getSslProtocol());
+
+ }
+
}
|