use of com.aws.greengrass.iot.IotCloudHelper in project aws-greengrass-nucleus by aws-greengrass.
the class IotCloudHelperTest method GIVEN_error_code_once_WHEN_send_request_called_THEN_retry_and_success.
@Test
void GIVEN_error_code_once_WHEN_send_request_called_THEN_retry_and_success() throws Exception {
when(mockConnectionManager.getClient()).thenReturn(mockClient);
when(mockConnectionManager.getURI()).thenReturn(HOST);
ExecutableHttpRequest requestMock = mock(ExecutableHttpRequest.class);
when(requestMock.call()).thenThrow(IOException.class).thenReturn(HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(STATUS_CODE).build()).responseBody(AbortableInputStream.create(new ByteArrayInputStream(CLOUD_RESPONSE))).build());
doReturn(requestMock).when(mockClient).prepareRequest(any());
IotCloudHelper cloudHelper = new IotCloudHelper();
final IotCloudResponse response = cloudHelper.sendHttpRequest(mockConnectionManager, null, IOT_CREDENTIALS_PATH, CredentialRequestHandler.IOT_CREDENTIALS_HTTP_VERB, null);
assertArrayEquals(CLOUD_RESPONSE, response.getResponseBody());
assertEquals(STATUS_CODE, response.getStatusCode());
}
use of com.aws.greengrass.iot.IotCloudHelper in project aws-greengrass-nucleus by aws-greengrass.
the class IotCloudHelperTest method GIVEN_error_code_once_WHEN_client_and_endpoint_null_THEN_unsuccessful.
@Test
void GIVEN_error_code_once_WHEN_client_and_endpoint_null_THEN_unsuccessful() throws Exception {
when(mockConnectionManager.getURI()).thenThrow(new DeviceConfigurationException("Credentials endpoint not " + "configured"));
IotCloudHelper cloudHelper = new IotCloudHelper();
assertThrows(AWSIotException.class, () -> cloudHelper.sendHttpRequest(mockConnectionManager, null, IOT_CREDENTIALS_PATH, CredentialRequestHandler.IOT_CREDENTIALS_HTTP_VERB, null));
}
use of com.aws.greengrass.iot.IotCloudHelper in project aws-greengrass-nucleus by aws-greengrass.
the class IotCloudHelperTest method GIVEN_valid_creds_WHEN_send_request_called_THEN_success.
@Test
void GIVEN_valid_creds_WHEN_send_request_called_THEN_success() throws Exception {
when(mockConnectionManager.getClient()).thenReturn(mockClient);
when(mockConnectionManager.getURI()).thenReturn(HOST);
ExecutableHttpRequest requestMock = mock(ExecutableHttpRequest.class);
when(requestMock.call()).thenReturn(HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(STATUS_CODE).build()).responseBody(AbortableInputStream.create(new ByteArrayInputStream(CLOUD_RESPONSE))).build());
doReturn(requestMock).when(mockClient).prepareRequest(any());
IotCloudHelper cloudHelper = new IotCloudHelper();
final IotCloudResponse response = cloudHelper.sendHttpRequest(mockConnectionManager, null, IOT_CREDENTIALS_PATH, CredentialRequestHandler.IOT_CREDENTIALS_HTTP_VERB, null);
assertArrayEquals(CLOUD_RESPONSE, response.getResponseBody());
assertEquals(STATUS_CODE, response.getStatusCode());
}
use of com.aws.greengrass.iot.IotCloudHelper in project aws-greengrass-nucleus by aws-greengrass.
the class IotCloudHelperTest method GIVEN_valid_creds_WHEN_send_request_called_with_body_THEN_success.
@Test
void GIVEN_valid_creds_WHEN_send_request_called_with_body_THEN_success() throws Exception {
byte[] body = "hello".getBytes(StandardCharsets.UTF_8);
when(mockConnectionManager.getClient()).thenReturn(mockClient);
when(mockConnectionManager.getURI()).thenReturn(HOST);
ExecutableHttpRequest requestMock = mock(ExecutableHttpRequest.class);
when(requestMock.call()).thenReturn(HttpExecuteResponse.builder().response(SdkHttpResponse.builder().statusCode(STATUS_CODE).build()).responseBody(AbortableInputStream.create(new ByteArrayInputStream(CLOUD_RESPONSE))).build());
doReturn(requestMock).when(mockClient).prepareRequest(any());
IotCloudHelper cloudHelper = new IotCloudHelper();
final byte[] creds = cloudHelper.sendHttpRequest(mockConnectionManager, null, IOT_CREDENTIALS_PATH, CredentialRequestHandler.IOT_CREDENTIALS_HTTP_VERB, body).getResponseBody();
assertArrayEquals(CLOUD_RESPONSE, creds);
}
Aggregations