Search in sources :

Example 6 with RestClientException

use of com.hazelcast.spi.exception.RestClientException in project hazelcast by hazelcast.

the class RestClient method buildSslSocketFactory.

/**
 * Builds SSL Socket Factory with the public CA Certificate from Kubernetes Master.
 */
private SSLSocketFactory buildSslSocketFactory() {
    try {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        keyStore.load(null, null);
        int i = 0;
        for (Certificate certificate : generateCertificates()) {
            String alias = String.format("ca-%d", i++);
            keyStore.setCertificateEntry(alias, certificate);
        }
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(keyStore);
        SSLContext context = SSLContext.getInstance("TLSv1.2");
        context.init(null, tmf.getTrustManagers(), null);
        return context.getSocketFactory();
    } catch (Exception e) {
        throw new RestClientException("Failure in generating SSLSocketFactory", e);
    }
}
Also used : TrustManagerFactory(javax.net.ssl.TrustManagerFactory) RestClientException(com.hazelcast.spi.exception.RestClientException) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) RestClientException(com.hazelcast.spi.exception.RestClientException) Certificate(java.security.cert.Certificate)

Example 7 with RestClientException

use of com.hazelcast.spi.exception.RestClientException in project hazelcast by hazelcast.

the class GcpClientTest method getAddressesUnknownException.

@Test(expected = Exception.class)
public void getAddressesUnknownException() {
    // given
    Label label = null;
    RestClientException exception = new RestClientException("unknown", 500);
    given(gcpComputeApi.instances(CURRENT_PROJECT, CURRENT_ZONE, label, ACCESS_TOKEN)).willThrow(exception);
    GcpConfig gcpConfig = GcpConfig.builder().setLabel(label).build();
    GcpClient gcpClient = new GcpClient(gcpMetadataApi, gcpComputeApi, gcpAuthenticator, gcpConfig);
    // when
    gcpClient.getAddresses();
// then
// throws exception
}
Also used : RestClientException(com.hazelcast.spi.exception.RestClientException) Test(org.junit.Test)

Example 8 with RestClientException

use of com.hazelcast.spi.exception.RestClientException in project hazelcast by hazelcast.

the class GcpClientTest method getAddressesNotFound.

@Test
public void getAddressesNotFound() {
    // given
    Label label = null;
    String forbiddenMessage = "Service account not enabled on this instance";
    RestClientException exception = new RestClientException(forbiddenMessage, 404);
    given(gcpComputeApi.instances(CURRENT_PROJECT, CURRENT_ZONE, label, ACCESS_TOKEN)).willThrow(exception);
    GcpConfig gcpConfig = GcpConfig.builder().setLabel(label).build();
    GcpClient gcpClient = new GcpClient(gcpMetadataApi, gcpComputeApi, gcpAuthenticator, gcpConfig);
    // when
    List<GcpAddress> result = gcpClient.getAddresses();
    // then
    assertEquals(emptyList(), result);
}
Also used : RestClientException(com.hazelcast.spi.exception.RestClientException) Test(org.junit.Test)

Example 9 with RestClientException

use of com.hazelcast.spi.exception.RestClientException in project hazelcast by hazelcast.

the class RestClientTest method expectHttpOkByDefault.

@Test
public void expectHttpOkByDefault() {
    // given
    int responseCode = 201;
    stubFor(post(urlEqualTo(API_ENDPOINT)).withRequestBody(equalTo(BODY_REQUEST)).willReturn(aResponse().withStatus(responseCode).withBody(BODY_RESPONSE)));
    // when
    RestClientException exception = assertThrows(RestClientException.class, () -> RestClient.create(String.format("%s%s", address, API_ENDPOINT)).withBody(BODY_REQUEST).get());
    // then
    assertEquals(exception.getHttpErrorCode(), responseCode);
}
Also used : RestClientException(com.hazelcast.spi.exception.RestClientException) Test(org.junit.Test)

Example 10 with RestClientException

use of com.hazelcast.spi.exception.RestClientException in project hazelcast by hazelcast.

the class RestClientTest method unexpectedResponseCode.

@Test
public void unexpectedResponseCode() {
    // given
    int expectedCode = 201;
    int unexpectedCode = 202;
    stubFor(post(urlEqualTo(API_ENDPOINT)).withRequestBody(equalTo(BODY_REQUEST)).willReturn(aResponse().withStatus(unexpectedCode).withBody(BODY_RESPONSE)));
    // when
    RestClientException exception = assertThrows(RestClientException.class, () -> RestClient.create(String.format("%s%s", address, API_ENDPOINT)).withBody(BODY_REQUEST).expectResponseCodes(expectedCode).post());
    // then
    assertEquals(exception.getHttpErrorCode(), unexpectedCode);
}
Also used : RestClientException(com.hazelcast.spi.exception.RestClientException) Test(org.junit.Test)

Aggregations

RestClientException (com.hazelcast.spi.exception.RestClientException)10 Test (org.junit.Test)7 IOException (java.io.IOException)2 Address (com.hazelcast.cluster.Address)1 InvalidConfigurationException (com.hazelcast.config.InvalidConfigurationException)1 DiscoveryNode (com.hazelcast.spi.discovery.DiscoveryNode)1 SimpleDiscoveryNode (com.hazelcast.spi.discovery.SimpleDiscoveryNode)1 NoCredentialsException (com.hazelcast.spi.exception.NoCredentialsException)1 DataOutputStream (java.io.DataOutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 KeyStore (java.security.KeyStore)1 Certificate (java.security.cert.Certificate)1 CertificateException (java.security.cert.CertificateException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 SSLContext (javax.net.ssl.SSLContext)1 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)1