diff options
Diffstat (limited to 'src/test')
6 files changed, 1110 insertions, 529 deletions
diff --git a/src/test/java/org/openecomp/restclient/client/OperationResultTest.java b/src/test/java/org/openecomp/restclient/client/OperationResultTest.java new file mode 100644 index 0000000..df85877 --- /dev/null +++ b/src/test/java/org/openecomp/restclient/client/OperationResultTest.java @@ -0,0 +1,83 @@ +package org.openecomp.restclient.client; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import javax.ws.rs.core.MultivaluedMap; + +import org.junit.Before; +import org.junit.Test; + +import com.sun.jersey.core.util.MultivaluedMapImpl; + +public class OperationResultTest { + + /** + * Test case initialization + * + * @throws Exception the exception + */ + @Before + public void init() throws Exception { + } + + @Test + public void validateConstruction() { + + OperationResult opResult = new OperationResult(); + assertEquals(opResult.getNumRetries(),0); + assertFalse(opResult.isFromCache()); + assertFalse(opResult.wasSuccessful()); + opResult.setResultCode(612); + assertFalse(opResult.wasSuccessful()); + assertNull(opResult.getHeaders()); + + opResult = new OperationResult(204,"no content found"); + assertEquals(opResult.getResultCode(),204); + assertEquals(opResult.getResult(),"no content found"); + assertTrue(opResult.wasSuccessful()); + + MultivaluedMap<String,String> multiMap = new MultivaluedMapImpl(); + multiMap.add("p1","v1"); + multiMap.add("p2","v2"); + opResult.setHeaders(multiMap); + assertNotNull(opResult.getHeaders()); + assertEquals(opResult.getHeaders().size(), 2); + + } + + @Test + public void validateAccesors() { + + OperationResult opResult = new OperationResult(); + + opResult.setFailureCause("failure"); + opResult.setFromCache(false); + opResult.setNumRetries(101); + opResult.setRequestedLink("http://localhost:1234"); + opResult.setResult("result"); + opResult.setResultCode(555); + + assertEquals(opResult.getFailureCause(), "failure"); + assertFalse(opResult.isFromCache()); + assertEquals(opResult.getNumRetries(),101); + assertEquals(opResult.getRequestedLink(),"http://localhost:1234"); + assertEquals(opResult.getResult(), "result"); + assertEquals(opResult.getResultCode(),555); + + opResult.setResult(212, "mostly successful"); + assertEquals(opResult.getResultCode(),212); + assertEquals(opResult.getResult(), "mostly successful"); + + assertTrue(opResult.toString().contains("OperationResult")); + + opResult.setFailureCause(511, "things melting"); + assertEquals(opResult.getResultCode(),511); + assertEquals(opResult.getFailureCause(), "things melting"); + + } + +} diff --git a/src/test/java/org/openecomp/restclient/client/RESTClientTest.java b/src/test/java/org/openecomp/restclient/client/RESTClientTest.java deleted file mode 100644 index b049c38..0000000 --- a/src/test/java/org/openecomp/restclient/client/RESTClientTest.java +++ /dev/null @@ -1,391 +0,0 @@ -package org.openecomp.restclient.client;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.openecomp.restclient.client.OperationResult;
-import org.openecomp.restclient.client.RestClient;
-import org.openecomp.restclient.rest.RestClientBuilder;
-
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertEquals;
-
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.core.MediaType;
-
-import com.sun.jersey.test.framework.AppDescriptor;
-import com.sun.jersey.test.framework.JerseyTest;
-import com.sun.jersey.test.framework.WebAppDescriptor;
-
-import com.sun.jersey.api.client.Client;
-
-
-/**
- * This suite of tests is intended to exercise the behaviour of the {@link RestClient}.
- */
-public class RESTClientTest extends JerseyTest {
-
- private static final String GOOD_AAI_ENDPOINT = "testaai/good";
- private static final String FAIL_ALWAYS_AAI_ENDPOINT = "testaai/failalways";
- private static final String FAIL_THEN_SUCCEED_ENDPOINT = "testaai/failthensucceed";
- private static final String INVALID_AAI_ENDPOINT = "testaai/bad";
-
- private static final String AAI_GET_REPLY_PAYLOAD = "Reply from AAI";
-
- private static final int SUCCESS_RESULT_CODE = 200;
- private static final int INVALID_END_POINT_RESULT_CODE = 404;
- private static final int INTERNAL_ERR_RESULT_CODE = 500;
- private static final int TIMEOUT_RESULT_CODE = 504;
-
-
- /**
- * Creates a new instance of the {@link RESTClientTest} test suite.
- */
- public RESTClientTest() throws Exception {
-
- // Tell our in memory container to look here for resource endpoints.
- super("org.openecomp.restclient.client");
- }
-
-
- @Override
- protected AppDescriptor configure() {
- return new WebAppDescriptor.Builder().build();
- }
-
-
- /**
- * Perform common initialization actions that need to run before every unit test.
- */
- @Before
- public void setup() {
-
- // Initialize our test endpoints to make sure that all of their
- // counters have valid starting values
- AAI_FailAlways_Stub.initialize();
- AAI_FailThenSucceed_Stub.initialize();
- }
-
-
- /**
- * This test validates that all of the {@link RestClient}'s configurable parameters can be set via
- * its fluent interface and that those values are successfully passed down to the underlying
- * {@link RestClientBuilder} instance.
- */
- @Test
- public void configureAAIClientTest() {
-
- final boolean VALIDATE_SERVER = true;
- final boolean VALIDATE_CERT_CHAIN = true;
- final String CLIENT_CERT_FILE = "myCertFile";
- final String CLIENT_CERT_PASSWORD = "My voice is my password";
- final String TRUST_STORE = "myTrustStore";
- final int CONNECT_TIMEOUT = 5000;
- final int READ_TIMEOUT = 5000;
-
- // Create an instance of our test version of the REST client builder.
- TestRestClientBuilder clientBuilder = new TestRestClientBuilder();
-
- // Now, create a new instance of the {@link AAIClient} and configure
- // its parameters.
- RestClient testClient =
- new RestClient(clientBuilder).validateServerHostname(true).validateServerCertChain(true)
- .clientCertFile("myCertFile").clientCertPassword("My voice is my password")
- .trustStore("myTrustStore").connectTimeoutMs(5000).readTimeoutMs(5000);
-
- // Validate that the parameters of the test REST client builder that
- // we passed to the AAI client have been set according to what we
- // passed in when we instantiated the AAI client.
- assertEquals("Unexpected 'validate server host name' value", VALIDATE_SERVER,
- clientBuilder.isValidateServerHostname());
- assertEquals("Unexpected 'validate certificat chain' value", VALIDATE_CERT_CHAIN,
- clientBuilder.isValidateServerCertChain());
- assertTrue("Unexpected client certificate filename",
- CLIENT_CERT_FILE.equals(clientBuilder.getClientCertFileName()));
- assertTrue("Unexpected client certificate password",
- CLIENT_CERT_PASSWORD.equals(clientBuilder.getClientCertPassword()));
- assertTrue("Unexpected trust store filename",
- TRUST_STORE.equals(clientBuilder.getTruststoreFilename()));
- assertEquals("Unexpected connection timeout value", CONNECT_TIMEOUT,
- clientBuilder.getConnectTimeoutInMs());
- assertEquals("Unexpected read timeout value", READ_TIMEOUT, clientBuilder.getReadTimeoutInMs());
- }
-
-
- /**
- * This test validates that the {@link RestClient} can submit a GET request to a valid REST
- * endpoint and receive a valid response.
- */
- @Test
- public void queryAAI_SuccessTest() {
-
- // Create an instance of the AAIClient that uses our test version of
- // the REST client builder.
- RestClient testClient = new RestClient(new TestRestClientBuilder());
-
- // Query our stubbed out AAI with a URL that we expecte to get a successful
- // reply from.
- OperationResult or =
- testClient.get(getBaseURI() + GOOD_AAI_ENDPOINT, null, MediaType.APPLICATION_JSON_TYPE);
-
- // Validate that a successful query returns a result code of 200.
- assertEquals("Unexpected result code", SUCCESS_RESULT_CODE, or.getResultCode());
-
- // Validate that no error cause gets set on a successful query.
- assertNull("Operation result failure code should not be set for successful GET",
- or.getFailureCause());
-
- // Validate that our query returned the expected payload from our dummy
- // AAI.
- assertTrue("Incorrect payload returned from AAI query",
- AAI_GET_REPLY_PAYLOAD.equals(or.getResult()));
- }
-
-
- /**
- * This test validates that the {@link RestClient} behaves as expected when query requests are
- * unsuccessful.
- * <p>
- * Specifically, the following scenarios are covered:<br>
- * 1) Submitting a GET request to an invalid REST endpoint 2) Submitting a GET request to a valid
- * endpoint which throws an error rather than replying successfully.
- * <p>
- * Note that this test exercises the 'single attempt' variant of the query method.
- */
- @Test
- public void queryAAI_FailureTest() {
-
- // Create an instance of the AAIClient that uses our test version of
- // the REST client builder.
- RestClient testClient = new RestClient(new TestRestClientBuilder());
-
- // Query our stubbed out AAI with a URL that we expecte to get a successful
- // reply from.
- OperationResult or =
- testClient.get(getBaseURI() + INVALID_AAI_ENDPOINT, null, MediaType.APPLICATION_JSON_TYPE);
-
- // Validate that an attempt to query a non-existing endpoint results in
- // a 404 error.
- assertEquals("Unexpected result code", INVALID_END_POINT_RESULT_CODE, or.getResultCode());
-
- // Validate that no payload was set since the query failed.
- assertNull("Payload should not be set on 404 error", or.getResult());
-
- // Now, submit a query request to the stubbed AAI.
- or = testClient.get(getBaseURI() + FAIL_ALWAYS_AAI_ENDPOINT, null,
- MediaType.APPLICATION_JSON_TYPE);
-
- // Validate that a query to a avalid returns a result code of 500.
- assertEquals("Unexpected result code", INTERNAL_ERR_RESULT_CODE, or.getResultCode());
- }
-
-
- /**
- * This test validates the behaviour of querying the AAI with a number of retries requested in the
- * case where we never get a successful reply.
- */
- @Test
- public void queryAAIWithRetries_TimeoutTest() {
-
- int NUM_RETRIES = 3;
-
-
- // Create an instance of the AAIClient that uses our test version of
- // the REST client builder.
- RestClient testClient = new RestClient(new TestRestClientBuilder());
-
- // Initialize our test endpoint to make sure that all of its
- // counters have valid starting values
- // AAI_FailAlways_Stub.initialize();
-
- // Perform a query against the stubbed AAI, specifying a number of times
- // to retry in the event of an error.
- OperationResult or = testClient.get(getBaseURI() + FAIL_ALWAYS_AAI_ENDPOINT, null,
- MediaType.APPLICATION_JSON_TYPE, NUM_RETRIES);
-
- // Validate that failing for all of our retry attempts results in a
- // 504 error.
- assertEquals("Unexpected result code", TIMEOUT_RESULT_CODE, or.getResultCode());
-
- // Validate that our stubbed AAI actually received the expected number
- // of retried requests.
- assertEquals("Unexpected number of retries", NUM_RETRIES, AAI_FailAlways_Stub.getCount);
- }
-
-
- /**
- * This test validates the behaviour of querying the AAI with a number of retries requested in the
- * case where our query initially fails but then succeeds on one of the subsequent retries.
- */
- @Test
- public void queryAAIWithRetries_FailThenSucceedTest() {
-
- int num_retries = AAI_FailThenSucceed_Stub.MAX_FAILURES + 2;
-
- // Create an instance of the AAIClient that uses our test version of
- // the REST client builder.
- RestClient testClient = new RestClient(new TestRestClientBuilder());
-
- // Initialize our test endpoint to make sure that all of its
- // counters have valid starting values.
- // AAI_FailThenSucceed_Stub.initialize();
-
- // Perform a query against the stubbed AAI, specifying a number of times
- // to retry in the event of an error.
- OperationResult or = testClient.get(getBaseURI() + FAIL_THEN_SUCCEED_ENDPOINT, null,
- MediaType.APPLICATION_JSON_TYPE, num_retries);
-
- // Validate that after failing a few attempts we finally got back a
- // success code.
- assertEquals("Unexpected result code", SUCCESS_RESULT_CODE, or.getResultCode());
-
- // Validate that our stubbed AAI actually received the expected number
- // of retried requests.
- assertEquals("Unexpected number of retries", AAI_FailThenSucceed_Stub.MAX_FAILURES + 1,
- AAI_FailThenSucceed_Stub.getCount);
- }
-
-
- /**
- * This class provides a simple in-memory REST end point to stand in for a real AAI.
- * <p>
- * This endpoint always returns a valid reply to a GET request and is used for success path
- * testing.
- */
- @Path(GOOD_AAI_ENDPOINT)
- public static class AAI_Success_Stub {
-
- /**
- * This is the end point for GET requests. It just returns a simple, pre-canned response
- * payload.
- *
- * @return - A pre-canned response.
- */
- @GET
- public String getEndpoint() {
- return AAI_GET_REPLY_PAYLOAD;
- }
- }
-
-
- /**
- * This class provides a simple in-memory REST end point to stand in for a real AAI.
- * <p>
- * This endpoint always returns throws an error instead of responding successfully and is used for
- * certain failure path tests.
- */
- @Path(FAIL_ALWAYS_AAI_ENDPOINT)
- public static class AAI_FailAlways_Stub {
-
- /**
- * Maintains a running count of the number of GET requests that have been received.
- */
- public static int getCount;
-
-
- /**
- * Resets all of the endpoints counters.
- */
- public static void initialize() {
- getCount = 0;
- }
-
-
- /**
- * This is the end point for GET requests. It just throws an error instead of returning a valid
- * response.
- *
- * @return - NONE. We actually throw an exception intentionally instead of returning.
- */
- @GET
- public String getEndpoint() {
-
- // Keep track of the number of get requests that we have received
- // so that this value can be used for validation purposes later.
- getCount++;
-
- // Always just throw an error instead of replying successfully.
- throw new UnsupportedOperationException("Intentional Failure");
- }
- }
-
-
- /**
- * This class provides a simple in-memory REST end point to stand in for a real AAI.
- * <p>
- * This end point will throw errors instead of responding for a certain number of requests, after
- * which it will return a valid, pre-canned response.
- *
- * @return - A pre-canned response.
- */
- @Path(FAIL_THEN_SUCCEED_ENDPOINT)
- public static class AAI_FailThenSucceed_Stub {
-
- /**
- * The number of requests for which we should throw errors before responding successfully.
- */
- public static int MAX_FAILURES = 2;
-
- /**
- * Maintains a running count of the number of GET requests that have been received.
- */
- public static int getCount;
-
- /**
- * Maintains a running count of the number of requests which we have failed, so that we will
- * know when to stop failing and return a valid response.
- */
- private static int failCount;
-
-
- /**
- * Resets all of the endpoints counters.
- */
- public static void initialize() {
- getCount = 0;
- failCount = 0;
- }
-
-
- /**
- * This is the end point for GET requests. It will throw errors for a certain number of requests
- * and then return a valid response.
- *
- * @return - A pre-canned response string.
- */
- @GET
- public String getEndpoint() {
-
- // Keep track of the number of get requests that we have received
- // so that this value can be used for validation purposes later.
- getCount++;
-
- // We only want to fail a set number of times, so check now to
- // see what we should do.
- if (failCount < MAX_FAILURES) {
- failCount++;
- throw new UnsupportedOperationException("Intentional Failure");
-
- } else {
- // We've failed as often as we need to. Time to reply
- // successfully.
- failCount = 0;
- return AAI_GET_REPLY_PAYLOAD;
- }
- }
- }
-
-
- /**
- * This class overrides the behaviour of the {@link RestClientBuilder} used by the
- * {@link RestClient} to just return the in memory client provided by the JerseyTest framework.
- */
- private class TestRestClientBuilder extends RestClientBuilder {
-
- @Override
- public Client getClient() throws Exception {
- return client();
- }
- }
-}
diff --git a/src/test/java/org/openecomp/restclient/client/RestfulClientTest.java b/src/test/java/org/openecomp/restclient/client/RestfulClientTest.java new file mode 100644 index 0000000..c116482 --- /dev/null +++ b/src/test/java/org/openecomp/restclient/client/RestfulClientTest.java @@ -0,0 +1,721 @@ +package org.openecomp.restclient.client; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.openecomp.restclient.enums.RestAuthenticationMode; +import org.openecomp.restclient.rest.RestClientBuilder; + +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +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 RestClientBuilder mockClientBuilder; + private Client mockedClient; + + /** + * Test case initialization + * + * @throws Exception the exception + */ + @Before + public void init() throws Exception { + mockClientBuilder = Mockito.mock( RestClientBuilder.class ); + mockedClient = Mockito.mock( Client.class ); + } + + @Test + public void validateConstructors() { + + RestClient restClient = new RestClient(); + assertNotNull(restClient); + + restClient = null; + restClient = new RestClient( mockClientBuilder ); + assertNotNull(restClient); + + } + + @Test + public void validateBasicClientConstruction() throws Exception { + + Mockito.when( mockClientBuilder.getClient() ).thenReturn(mockedClient); + + RestClient restClient = new RestClient( mockClientBuilder ); + assertNotNull(restClient); + + Client client = restClient.authenticationMode(RestAuthenticationMode.HTTP_NOAUTH) + .connectTimeoutMs(1000).readTimeoutMs(500).getClient(); + + assertNotNull(client); + + } + + @Test + public void validateClientWithSslBasicAuthConstruction() throws Exception { + + Mockito.when( mockClientBuilder.getClient() ).thenReturn(mockedClient); + + RestClient restClient = new RestClient( mockClientBuilder ); + assertNotNull(restClient); + + Client client = restClient.authenticationMode(RestAuthenticationMode.SSL_BASIC) + .connectTimeoutMs(1000).readTimeoutMs(500).basicAuthPassword("password") + .basicAuthUsername("username").getClient(); + + assertNotNull(client); + + } + + @Test + public void validateClientWithSslCertConstruction() throws Exception { + + Mockito.when( mockClientBuilder.getClient() ).thenReturn(mockedClient); + + RestClient restClient = new RestClient( mockClientBuilder ); + assertNotNull(restClient); + + Client client = + restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password").getClient(); + + assertNotNull(client); + + client = null; + client = restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password") + .validateServerCertChain(true).validateServerHostname(true).getClient(); + + assertNotNull(client); + + client = null; + client = restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password") + .trustStore("truststore").getClient(); + + assertNotNull(client); + + } + + @Test + public void validateSuccessfulPut() throws Exception { + + Mockito.when( mockClientBuilder.getClient() ).thenReturn(mockedClient); + + WebResource mockedWebResource = Mockito.mock(WebResource.class); + Builder mockedBuilder = Mockito.mock(Builder.class); + ClientResponse mockedClientResponse = Mockito.mock(ClientResponse.class); + + Mockito.when( mockedClient.resource(Mockito.anyString())).thenReturn( mockedWebResource ); + Mockito.when(mockedWebResource.accept(Mockito.<MediaType>anyVararg())).thenReturn( mockedBuilder ); + Mockito.when(mockedBuilder.put(Mockito.any(Class.class))).thenReturn(mockedClientResponse); + + /* + * Finally the elements we want to validate + */ + + Mockito.when(mockedClientResponse.getStatus()).thenReturn(200); + Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("hello"); + Mockito.when(mockedClientResponse.getHeaders()).thenReturn(new MultivaluedMapImpl()); + + RestClient restClient = new RestClient( mockClientBuilder ); + + assertNotNull(restClient); + + restClient = + restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password"); + + assertNotNull(restClient); + + MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); + + OperationResult result = restClient.put("http://localhost:9000/aai/v7", "", headers, MediaType.APPLICATION_JSON_TYPE, + MediaType.APPLICATION_JSON_TYPE); + + assertEquals(200, result.getResultCode()); + assertNotNull(result.getResult()); + assertNull(result.getFailureCause()); + + } + + @Test + public void validateSuccessfulPost() throws Exception { + + Mockito.when( mockClientBuilder.getClient() ).thenReturn(mockedClient); + + WebResource mockedWebResource = Mockito.mock(WebResource.class); + Builder mockedBuilder = Mockito.mock(Builder.class); + ClientResponse mockedClientResponse = Mockito.mock(ClientResponse.class); + + Mockito.when( mockedClient.resource(Mockito.anyString())).thenReturn( mockedWebResource ); + Mockito.when(mockedWebResource.accept(Mockito.<MediaType>anyVararg())).thenReturn( mockedBuilder ); + Mockito.when(mockedBuilder.post(Mockito.any(Class.class))).thenReturn(mockedClientResponse); + + /* + * Finally the elements we want to validate + */ + + Mockito.when(mockedClientResponse.getStatus()).thenReturn(200); + Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("hello"); + Mockito.when(mockedClientResponse.getHeaders()).thenReturn(new MultivaluedMapImpl()); + + RestClient restClient = new RestClient( mockClientBuilder ); + + assertNotNull(restClient); + + restClient = + restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password"); + + assertNotNull(restClient); + + MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); + + OperationResult result = restClient.post("http://localhost:9000/aai/v7", "", headers, MediaType.APPLICATION_JSON_TYPE, + MediaType.APPLICATION_JSON_TYPE); + + assertEquals(200, result.getResultCode()); + assertNotNull(result.getResult()); + assertNull(result.getFailureCause()); + + } + + @Test + public void validateSuccessfulGet() throws Exception { + + Mockito.when( mockClientBuilder.getClient() ).thenReturn(mockedClient); + + WebResource mockedWebResource = Mockito.mock(WebResource.class); + Builder mockedBuilder = Mockito.mock(Builder.class); + ClientResponse mockedClientResponse = Mockito.mock(ClientResponse.class); + + Mockito.when( mockedClient.resource(Mockito.anyString())).thenReturn( mockedWebResource ); + Mockito.when(mockedWebResource.accept(Mockito.<MediaType>anyVararg())).thenReturn( mockedBuilder ); + Mockito.when(mockedBuilder.get(Mockito.any(Class.class))).thenReturn(mockedClientResponse); + + /* + * Finally the elements we want to validate + */ + + Mockito.when(mockedClientResponse.getStatus()).thenReturn(200); + Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("hello"); + Mockito.when(mockedClientResponse.getHeaders()).thenReturn(new MultivaluedMapImpl()); + + RestClient restClient = new RestClient( mockClientBuilder ); + + assertNotNull(restClient); + + restClient = + restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password"); + + assertNotNull(restClient); + + MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); + + OperationResult result = + restClient.get("http://localhost:9000/aai/v7", headers, MediaType.APPLICATION_JSON_TYPE); + + assertEquals(200, result.getResultCode()); + assertNotNull(result.getResult()); + assertNull(result.getFailureCause()); + + } + + @Test + public void validateSuccessfulGetWithBasicAuth() throws Exception { + + Mockito.when( mockClientBuilder.getClient() ).thenReturn(mockedClient); + + WebResource mockedWebResource = Mockito.mock(WebResource.class); + Builder mockedBuilder = Mockito.mock(Builder.class); + ClientResponse mockedClientResponse = Mockito.mock(ClientResponse.class); + + Mockito.when( mockedClient.resource(Mockito.anyString())).thenReturn( mockedWebResource ); + Mockito.when(mockedWebResource.accept(Mockito.<MediaType>anyVararg())).thenReturn( mockedBuilder ); + Mockito.when(mockedBuilder.get(Mockito.any(Class.class))).thenReturn(mockedClientResponse); + + /* + * Finally the elements we want to validate + */ + + Mockito.when(mockedClientResponse.getStatus()).thenReturn(200); + Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("hello"); + Mockito.when(mockedClientResponse.getHeaders()).thenReturn(new MultivaluedMapImpl()); + + RestClient restClient = new RestClient( mockClientBuilder ); + + assertNotNull(restClient); + + restClient = + restClient.authenticationMode(RestAuthenticationMode.SSL_BASIC).connectTimeoutMs(1000) + .readTimeoutMs(500).basicAuthUsername("username").basicAuthUsername("password"); + + assertNotNull(restClient); + + MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); + + OperationResult result = + restClient.get("http://localhost:9000/aai/v7", headers, MediaType.APPLICATION_JSON_TYPE); + + assertEquals(200, result.getResultCode()); + assertNotNull(result.getResult()); + assertNull(result.getFailureCause()); + + } + + @Test + public void validateResourceNotFoundGet() throws Exception { + + Mockito.when( mockClientBuilder.getClient() ).thenReturn(mockedClient); + + WebResource mockedWebResource = Mockito.mock(WebResource.class); + Builder mockedBuilder = Mockito.mock(Builder.class); + ClientResponse mockedClientResponse = Mockito.mock(ClientResponse.class); + + Mockito.when( mockedClient.resource(Mockito.anyString())).thenReturn( mockedWebResource ); + Mockito.when(mockedWebResource.accept(Mockito.<MediaType>anyVararg())).thenReturn( mockedBuilder ); + Mockito.when(mockedBuilder.get(Mockito.any(Class.class))).thenReturn(mockedClientResponse); + + /* + * Finally the elements we want to validate + */ + + Mockito.when(mockedClientResponse.getStatus()).thenReturn(404); + Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("RNF"); + Mockito.when(mockedClientResponse.getHeaders()).thenReturn(new MultivaluedMapImpl()); + + RestClient restClient = new RestClient( mockClientBuilder ); + + assertNotNull(restClient); + + restClient = + restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password"); + + assertNotNull(restClient); + + MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); + + OperationResult result = + restClient.get("http://localhost:9000/aai/v7", headers, MediaType.APPLICATION_JSON_TYPE); + + assertEquals(404, result.getResultCode()); + assertNull(result.getResult()); + assertNotNull(result.getFailureCause()); + + } + + @Test + public void validateHealthCheck() throws Exception { + + Mockito.when( mockClientBuilder.getClient() ).thenReturn(mockedClient); + + WebResource mockedWebResource = Mockito.mock(WebResource.class); + Builder mockedBuilder = Mockito.mock(Builder.class); + ClientResponse mockedClientResponse = Mockito.mock(ClientResponse.class); + + Mockito.when( mockedClient.resource(Mockito.anyString())).thenReturn( mockedWebResource ); + Mockito.when(mockedWebResource.accept(Mockito.<MediaType>anyVararg())).thenReturn( mockedBuilder ); + Mockito.when(mockedBuilder.get(Mockito.any(Class.class))).thenReturn(mockedClientResponse); + + /* + * Finally the elements we want to validate + */ + + Mockito.when(mockedClientResponse.getStatus()).thenReturn(200); + Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("hello"); + Mockito.when(mockedClientResponse.getHeaders()).thenReturn(new MultivaluedMapImpl()); + + RestClient restClient = new RestClient( mockClientBuilder ); + + assertNotNull(restClient); + + restClient = + restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password"); + + assertNotNull(restClient); + + MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); + + boolean targetServiceHealthy = + restClient.healthCheck("http://localhost:9000/aai/util/echo", "startSerice", "targetService"); + + assertEquals(true, targetServiceHealthy); + + } + + @Test + public void validateHealthCheckFailureWith403() throws Exception { + + Mockito.when( mockClientBuilder.getClient() ).thenReturn(mockedClient); + + WebResource mockedWebResource = Mockito.mock(WebResource.class); + Builder mockedBuilder = Mockito.mock(Builder.class); + ClientResponse mockedClientResponse = Mockito.mock(ClientResponse.class); + + Mockito.when( mockedClient.resource(Mockito.anyString())).thenReturn( mockedWebResource ); + Mockito.when(mockedWebResource.accept(Mockito.<MediaType>anyVararg())).thenReturn( mockedBuilder ); + Mockito.when(mockedBuilder.get(Mockito.any(Class.class))).thenReturn(mockedClientResponse); + + /* + * Finally the elements we want to validate + */ + + Mockito.when(mockedClientResponse.getStatus()).thenReturn(403); + Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("hello"); + Mockito.when(mockedClientResponse.getHeaders()).thenReturn(new MultivaluedMapImpl()); + + RestClient restClient = new RestClient( mockClientBuilder ); + + assertNotNull(restClient); + + restClient = + restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password"); + + assertNotNull(restClient); + + MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); + + boolean targetServiceHealthy = + restClient.healthCheck("http://localhost:9000/aai/util/echo", "startSerice", "targetService"); + + assertEquals(false, targetServiceHealthy); + + } + + @Test + public void validateHealthCheckFailureWithThrownException() throws Exception { + + Mockito.when( mockClientBuilder.getClient() ).thenReturn(mockedClient); + + WebResource mockedWebResource = Mockito.mock(WebResource.class); + Builder mockedBuilder = Mockito.mock(Builder.class); + ClientResponse mockedClientResponse = Mockito.mock(ClientResponse.class); + + Mockito.when( mockedClient.resource(Mockito.anyString())).thenReturn( mockedWebResource ); + Mockito.when(mockedWebResource.accept(Mockito.<MediaType>anyVararg())).thenReturn( mockedBuilder ); + Mockito.when(mockedBuilder.get(Mockito.any(Class.class))).thenThrow(new IllegalArgumentException("error")); + + /* + * Finally the elements we want to validate + */ + +/* Mockito.when(mockedClientResponse.getStatus()).thenReturn(403); + Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("hello"); + Mockito.when(mockedClientResponse.getHeaders()).thenReturn(new MultivaluedMapImpl());*/ + + RestClient restClient = new RestClient( mockClientBuilder ); + + assertNotNull(restClient); + + restClient = + restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password"); + + assertNotNull(restClient); + + MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); + + boolean targetServiceHealthy = + restClient.healthCheck("http://localhost:9000/aai/util/echo", "startSerice", "targetService"); + + assertEquals(false, targetServiceHealthy); + + } + @Test + public void validateSuccessfulGetWithRetries() throws Exception { + + RestClientBuilder myClientBuilder = Mockito.mock(RestClientBuilder.class); + Client myClient = Mockito.mock(Client.class); + + Mockito.when(myClientBuilder.getClient()).thenReturn(myClient).thenReturn(myClient); + + WebResource mockedWebResource = Mockito.mock(WebResource.class); + Builder mockedBuilder = Mockito.mock(Builder.class); + ClientResponse mockedClientResponse = Mockito.mock(ClientResponse.class); + + Mockito.when( myClient.resource(Mockito.anyString())).thenReturn( mockedWebResource ); + Mockito.when(mockedWebResource.accept(Mockito.<MediaType>anyVararg())).thenReturn( mockedBuilder ); + Mockito.when(mockedBuilder.get(Mockito.any(Class.class))).thenReturn(mockedClientResponse); + + + /* + * Finally the elements we want to validate + */ + + Mockito.when(mockedClientResponse.getStatus()).thenReturn(408).thenReturn(200); + + Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("error").thenReturn("ok"); + + MultivaluedMap<String, String> emptyHeaderMap = new MultivaluedMapImpl(); + + // Mockito is smart, the last recorded thenReturn is repeated successively + // for all subsequent calls to the method. + Mockito.when(mockedClientResponse.getHeaders()).thenReturn(emptyHeaderMap); + + RestClient restClient = new RestClient( myClientBuilder ); + + assertNotNull(restClient); + + restClient = + restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password"); + + assertNotNull(restClient); + + MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); + + OperationResult result = + restClient.get("http://localhost:9000/aai/v7", headers, MediaType.APPLICATION_JSON_TYPE, 3); + + assertEquals(200, result.getResultCode()); + assertNotNull(result.getResult()); + assertNull(result.getFailureCause()); + + } + + + @Test + public void validateFailedGetWithRetriesCausedByResourceNotFound() throws Exception { + + RestClientBuilder myClientBuilder = Mockito.mock(RestClientBuilder.class); + Client myClient = Mockito.mock(Client.class); + + Mockito.when(myClientBuilder.getClient()).thenReturn(myClient).thenReturn(myClient); + + WebResource mockedWebResource = Mockito.mock(WebResource.class); + Builder mockedBuilder = Mockito.mock(Builder.class); + ClientResponse mockedClientResponse = Mockito.mock(ClientResponse.class); + + Mockito.when( myClient.resource(Mockito.anyString())).thenReturn( mockedWebResource ); + Mockito.when(mockedWebResource.accept(Mockito.<MediaType>anyVararg())).thenReturn( mockedBuilder ); + Mockito.when(mockedBuilder.get(Mockito.any(Class.class))).thenReturn(mockedClientResponse); + + + /* + * Finally the elements we want to validate + */ + + Mockito.when(mockedClientResponse.getStatus()).thenReturn(404); + + Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("error").thenReturn("ok"); + + MultivaluedMap<String, String> emptyHeaderMap = new MultivaluedMapImpl(); + + // Mockito is smart, the last recorded thenReturn is repeated successively + // for all subsequent calls to the method. + Mockito.when(mockedClientResponse.getHeaders()).thenReturn(emptyHeaderMap); + + RestClient restClient = new RestClient( myClientBuilder ); + + assertNotNull(restClient); + + restClient = + restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password"); + + assertNotNull(restClient); + + MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); + + OperationResult result = + restClient.get("http://localhost:9000/aai/v7", headers, MediaType.APPLICATION_JSON_TYPE, 3); + + assertEquals(404, result.getResultCode()); + assertNull(result.getResult()); + assertNotNull(result.getFailureCause()); + + } + + @Test + public void validateFailedGetAfterMaxRetries() throws Exception { + + Mockito.when( mockClientBuilder.getClient() ).thenReturn(mockedClient); + + WebResource mockedWebResource = Mockito.mock(WebResource.class); + Builder mockedBuilder = Mockito.mock(Builder.class); + ClientResponse mockedClientResponse = Mockito.mock(ClientResponse.class); + + Mockito.when( mockedClient.resource(Mockito.anyString())).thenReturn( mockedWebResource ); + Mockito.when(mockedWebResource.accept(Mockito.<MediaType>anyVararg())).thenReturn( mockedBuilder ); + + Mockito.when(mockedBuilder.get(Mockito.any(Class.class))).thenReturn(null); + + /* + * Finally the elements we want to validate + */ + + Mockito.when(mockedClientResponse.getStatus()).thenReturn(500).thenReturn(500).thenReturn(500); + + Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("error") + .thenReturn("error").thenReturn("error"); + + MultivaluedMap<String, String> emptyHeaderMap = new MultivaluedMapImpl(); + + Mockito.when(mockedClientResponse.getHeaders()).thenReturn(emptyHeaderMap) + .thenReturn(emptyHeaderMap).thenReturn(emptyHeaderMap); + + RestClient restClient = new RestClient( mockClientBuilder ); + + assertNotNull(restClient); + + restClient = + restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password"); + + assertNotNull(restClient); + + MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); + + OperationResult result = + restClient.get("http://localhost:9000/aai/v7", headers, MediaType.APPLICATION_JSON_TYPE, 3); + + + assertEquals(504, result.getResultCode()); + assertNull(result.getResult()); + assertNotNull(result.getFailureCause()); + + } + + @Test + public void validateSuccessfulDelete() throws Exception { + + Mockito.when( mockClientBuilder.getClient() ).thenReturn(mockedClient); + + WebResource mockedWebResource = Mockito.mock(WebResource.class); + Builder mockedBuilder = Mockito.mock(Builder.class); + ClientResponse mockedClientResponse = Mockito.mock(ClientResponse.class); + + Mockito.when( mockedClient.resource(Mockito.anyString())).thenReturn( mockedWebResource ); + Mockito.when(mockedWebResource.accept(Mockito.<MediaType>anyVararg())).thenReturn( mockedBuilder ); + Mockito.when(mockedBuilder.delete(Mockito.any(Class.class))).thenReturn(mockedClientResponse); + + /* + * Finally the elements we want to validate + */ + + Mockito.when(mockedClientResponse.getStatus()).thenReturn(200); + Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("hello"); + Mockito.when(mockedClientResponse.getHeaders()).thenReturn(new MultivaluedMapImpl()); + + RestClient restClient = new RestClient( mockClientBuilder ); + + assertNotNull(restClient); + + restClient = + restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password"); + + assertNotNull(restClient); + + MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); + + OperationResult result = restClient.delete("http://localhost:9000/aai/v7", headers, + MediaType.APPLICATION_JSON_TYPE); + + assertEquals(200, result.getResultCode()); + assertNotNull(result.getResult()); + assertNull(result.getFailureCause()); + + } + + @Test + public void validateSuccessfulHead() throws Exception { + + Mockito.when( mockClientBuilder.getClient() ).thenReturn(mockedClient); + + WebResource mockedWebResource = Mockito.mock(WebResource.class); + Builder mockedBuilder = Mockito.mock(Builder.class); + ClientResponse mockedClientResponse = Mockito.mock(ClientResponse.class); + + Mockito.when( mockedClient.resource(Mockito.anyString())).thenReturn( mockedWebResource ); + Mockito.when(mockedWebResource.accept(Mockito.<MediaType>anyVararg())).thenReturn( mockedBuilder ); + Mockito.when(mockedBuilder.head()).thenReturn(mockedClientResponse); + + /* + * Finally the elements we want to validate + */ + + Mockito.when(mockedClientResponse.getStatus()).thenReturn(200); + Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("hello"); + Mockito.when(mockedClientResponse.getHeaders()).thenReturn(new MultivaluedMapImpl()); + + RestClient restClient = new RestClient( mockClientBuilder ); + + assertNotNull(restClient); + + restClient = + restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password"); + + assertNotNull(restClient); + + MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); + + OperationResult result = + restClient.head("http://localhost:9000/aai/v7", headers, MediaType.APPLICATION_JSON_TYPE); + + assertEquals(200, result.getResultCode()); + assertNotNull(result.getResult()); + assertNull(result.getFailureCause()); + + } + + @Test + public void validateSuccessfulPatch() throws Exception { + + Mockito.when( mockClientBuilder.getClient() ).thenReturn(mockedClient); + + WebResource mockedWebResource = Mockito.mock(WebResource.class); + Builder mockedBuilder = Mockito.mock(Builder.class); + ClientResponse mockedClientResponse = Mockito.mock(ClientResponse.class); + + Mockito.when( mockedClient.resource(Mockito.anyString())).thenReturn( mockedWebResource ); + Mockito.when(mockedWebResource.accept(Mockito.<MediaType>anyVararg())).thenReturn( mockedBuilder ); + Mockito.when(mockedBuilder.post(Mockito.any(Class.class))).thenReturn(mockedClientResponse); + Mockito.when(mockedBuilder.header("X-HTTP-Method-Override", "PATCH")).thenReturn(mockedBuilder); + /* + * Finally the elements we want to validate + */ + + Mockito.when(mockedClientResponse.getStatus()).thenReturn(200); + Mockito.when(mockedClientResponse.getEntity(String.class)).thenReturn("hello"); + Mockito.when(mockedClientResponse.getHeaders()).thenReturn(new MultivaluedMapImpl()); + + RestClient restClient = new RestClient( mockClientBuilder ); + + assertNotNull(restClient); + + restClient = + restClient.authenticationMode(RestAuthenticationMode.SSL_CERT).connectTimeoutMs(1000) + .readTimeoutMs(500).clientCertFile("cert").clientCertPassword("password"); + + assertNotNull(restClient); + + MultivaluedMap<String, String> headers = new MultivaluedMapImpl(); + + OperationResult result = restClient.patch("http://localhost:9000/aai/v7", "", headers, MediaType.APPLICATION_JSON_TYPE, + MediaType.APPLICATION_JSON_TYPE); + + assertEquals(200, result.getResultCode()); + assertNotNull(result.getResult()); + assertNull(result.getFailureCause()); + + } + +} diff --git a/src/test/java/org/openecomp/restclient/enums/RestAuthenticationModeTest.java b/src/test/java/org/openecomp/restclient/enums/RestAuthenticationModeTest.java new file mode 100644 index 0000000..f4b280b --- /dev/null +++ b/src/test/java/org/openecomp/restclient/enums/RestAuthenticationModeTest.java @@ -0,0 +1,33 @@ +package org.openecomp.restclient.enums; + +import static org.junit.Assert.assertEquals; + +import org.junit.Before; +import org.junit.Test; + +public class RestAuthenticationModeTest { + + /** + * Test case initialization + * + * @throws Exception the exception + */ + @Before + public void init() throws Exception { + } + + @Test + public void validateEnumMappings() { + + assertEquals(RestAuthenticationMode.getRestAuthenticationMode(null), RestAuthenticationMode.UNKNOWN_MODE); + assertEquals(RestAuthenticationMode.getRestAuthenticationMode("OAuth"), RestAuthenticationMode.UNKNOWN_MODE); + assertEquals(RestAuthenticationMode.getRestAuthenticationMode("SSL_BASIC"), RestAuthenticationMode.SSL_BASIC); + assertEquals(RestAuthenticationMode.getRestAuthenticationMode("SSL_CERT"), RestAuthenticationMode.SSL_CERT); + assertEquals(RestAuthenticationMode.getRestAuthenticationMode("HTTP_NOAUTH"), RestAuthenticationMode.HTTP_NOAUTH); + + assertEquals(RestAuthenticationMode.SSL_BASIC.getAuthenticationModeLabel(),"SSL_BASIC"); + + + } + +} diff --git a/src/test/java/org/openecomp/restclient/rest/HttpUtilTest.java b/src/test/java/org/openecomp/restclient/rest/HttpUtilTest.java new file mode 100644 index 0000000..d1d88ea --- /dev/null +++ b/src/test/java/org/openecomp/restclient/rest/HttpUtilTest.java @@ -0,0 +1,51 @@ +package org.openecomp.restclient.rest; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; + +public class HttpUtilTest { + + /** + * Test case initialization + * + * @throws Exception the exception + */ + @Before + public void init() throws Exception { + } + + @Test + public void validateAccesors() { + + assertFalse(HttpUtil.isHttpResponseClassInformational(-1)); + assertFalse(HttpUtil.isHttpResponseClassInformational(99)); + assertTrue(HttpUtil.isHttpResponseClassInformational(183)); + assertFalse(HttpUtil.isHttpResponseClassInformational(200)); + + assertFalse(HttpUtil.isHttpResponseClassSuccess(199)); + assertTrue(HttpUtil.isHttpResponseClassSuccess(202)); + assertFalse(HttpUtil.isHttpResponseClassSuccess(300)); + + assertFalse(HttpUtil.isHttpResponseClassRedirection(299)); + assertTrue(HttpUtil.isHttpResponseClassRedirection(307)); + assertFalse(HttpUtil.isHttpResponseClassRedirection(401)); + + assertFalse(HttpUtil.isHttpResponseClassClientError(399)); + assertTrue(HttpUtil.isHttpResponseClassClientError(404)); + assertFalse(HttpUtil.isHttpResponseClassClientError(555)); + + assertFalse(HttpUtil.isHttpResponseClassServerError(499)); + assertTrue(HttpUtil.isHttpResponseClassServerError(504)); + assertFalse(HttpUtil.isHttpResponseClassServerError(662)); + + int[] successCodes = { 201, 202, 205, 299 }; + + assertTrue(HttpUtil.isHttpResponseInList(201, successCodes)); + assertFalse(HttpUtil.isHttpResponseInList(301, successCodes)); + + } + +} diff --git a/src/test/java/org/openecomp/restclient/rest/RestClientBuilderTest.java b/src/test/java/org/openecomp/restclient/rest/RestClientBuilderTest.java index 93e5520..e299e36 100644 --- a/src/test/java/org/openecomp/restclient/rest/RestClientBuilderTest.java +++ b/src/test/java/org/openecomp/restclient/rest/RestClientBuilderTest.java @@ -1,18 +1,18 @@ package org.openecomp.restclient.rest;
-import java.util.Map;
+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 org.junit.Before;
import org.junit.Test;
-import org.openecomp.restclient.rest.RestClientBuilder;
-
-import static org.junit.Assert.*;
+import org.openecomp.restclient.enums.RestAuthenticationMode;
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.
@@ -20,147 +20,231 @@ import com.sun.jersey.client.urlconnection.HTTPSProperties; public class RestClientBuilderTest {
/**
- * This test validates that we can enable and disable certificate chain verification and that the
- * associated parameters are correctly set.
+ * 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 certificateChainVerificationTest() throws Exception {
-
- final String TRUST_STORE_FILENAME = "myTrustStore";
-
-
- // Instantiate a RestClientBuilder with default parameters and
- // get a client instance.
- RestClientBuilder builder = new RestClientBuilder();
- Client client = builder.getClient();
-
- // Validate that, by default, no trust store has been set.
- assertNull("Trust store filename should not be set for default builder",
- System.getProperty("javax.net.ssl.trustStore"));
-
- // Now, enable certificate chain verification, but don't specify
- // a trust store filename.
- builder.setValidateServerCertChain(true);
-
- // Now, get a new client instance. We expect the builder to complain
- // because there is no trust store filename.
- try {
- Client client2 = builder.getClient();
- fail("Expected exception due to no trust store filename.");
-
- } catch (IllegalArgumentException e) {
- assertTrue(e.getMessage().contains("Trust store filename must be set"));
- }
+ 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"));
- // Now, set a value for the trust store filename and try again to
- // get a client instance. This time it should succeed and we should
- // see that our trust name filename was set.
- builder.setTruststoreFilename(TRUST_STORE_FILENAME);
- Client client3 = builder.getClient();
-
- // Validate that the trust store filename was set.
- assertNotNull("Expected trust store filename to be set",
- System.getProperty("javax.net.ssl.trustStore"));
-
- // Validate that the filename is set to the value we specified.
- assertTrue(
- "Unexpected trust store filename value " + System.getProperty("javax.net.ssl.trustStore"),
- System.getProperty("javax.net.ssl.trustStore").equals(TRUST_STORE_FILENAME));
}
-
-
- /**
- * This test validates that we can set timeout values in our client builder and that those values
- * are reflected in the client produced by the builder.
- */
+
@Test
- public void timeoutValuesTest() throws Exception {
-
- // Instantiate a RestClientBuilder with default parameters.
- RestClientBuilder builder = new RestClientBuilder();
-
- // Now, get a client instance and retrieve the client properties.
- Client client = builder.getClient();
-
- Map<String, Object> props = client.getProperties();
-
- // Validate that the connection and read timeouts are set to the
- // default values.
- assertEquals("Unexpected connect timeout parameter",
- props.get("com.sun.jersey.client.property.connectTimeout"),
- RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS);
- assertEquals("Unexpected read timeout parameter",
- props.get("com.sun.jersey.client.property.readTimeout"),
- RestClientBuilder.DEFAULT_READ_TIMEOUT_MS);
-
- // Now, change the timeouts in the builder to non-default values.
- builder.setConnectTimeoutInMs(RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS + 100);
- builder.setReadTimeoutInMs(RestClientBuilder.DEFAULT_READ_TIMEOUT_MS + 100);
-
- // Retrieve a new client instance and get the client properties.
- Client client2 = builder.getClient();
- props = client2.getProperties();
-
- // Validate that the connection and read timeouts are set to the
- // new values.
- assertEquals("Unexpected connect timeout parameter",
- props.get("com.sun.jersey.client.property.connectTimeout"),
- RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MS + 100);
- assertEquals("Unexpected read timeout parameter",
- props.get("com.sun.jersey.client.property.readTimeout"),
- RestClientBuilder.DEFAULT_READ_TIMEOUT_MS + 100);
+ 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));
}
-
-
- /**
- * This test validates that we can enable and disable host name verification in the clients
- * produced by our builder.
- */
+
+
@Test
- public void hostNameVerifierTest() throws Exception {
-
- // Instantiate a RestClientBuilder with default parameters.
- RestClientBuilder builder = new RestClientBuilder();
-
- // Now, get a client instance.
- Client client1 = builder.getClient();
-
- // Retrieve the client's HTTPS properties.
- HTTPSProperties httpProps = getHTTPSProperties(client1);
-
- // By default, hostname verification should be disabled, which means
- // that our builder will have injected its own {@link HostnameVerifier}
- // which just always returns true.
- assertNotNull(httpProps.getHostnameVerifier());
-
- // Verify that the host name verifier returns true regardless of what
- // hostname we pass in.
- assertTrue("Default hostname verifier should always return true",
- httpProps.getHostnameVerifier().verify("not_a_valid_hostname", null));
-
-
- // Now, enable hostname verification for our client builder, and
- // get a new client.
- builder.setValidateServerHostname(true);
- Client client2 = builder.getClient();
-
- // Retrieve the client's HTTPS properties.
- httpProps = getHTTPSProperties(client2);
-
- // Verify that with hostname verification enabled, our builder did not
- // insert its own stubbed verifier.
- assertNull(httpProps.getHostnameVerifier());
+ 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");
+
+ 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");
+ }
+
+ }
- /**
- * This is a convenience method which extracts the HTTPS properties from a supplied client.
- *
- * @parameter aClient - The client to retrieve the HTTPS properties from.
- */
- private HTTPSProperties getHTTPSProperties(Client aClient) {
-
- Map<String, Object> props = aClient.getProperties();
- return (HTTPSProperties) props.get(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES);
+ @Test
+ 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();
+
+ 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 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();
+
+ 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
+ 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 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();
+
}
+
+
}
|