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);
}
}
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
}
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);
}
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);
}
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);
}
Aggregations