Search in sources :

Example 1 with IotCloudHelper

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());
}
Also used : IotCloudHelper(com.aws.greengrass.iot.IotCloudHelper) ByteArrayInputStream(java.io.ByteArrayInputStream) IotCloudResponse(com.aws.greengrass.iot.model.IotCloudResponse) IOException(java.io.IOException) ExecutableHttpRequest(software.amazon.awssdk.http.ExecutableHttpRequest) Test(org.junit.jupiter.api.Test)

Example 2 with IotCloudHelper

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));
}
Also used : IotCloudHelper(com.aws.greengrass.iot.IotCloudHelper) DeviceConfigurationException(com.aws.greengrass.deployment.exceptions.DeviceConfigurationException) Test(org.junit.jupiter.api.Test)

Example 3 with IotCloudHelper

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());
}
Also used : IotCloudHelper(com.aws.greengrass.iot.IotCloudHelper) ByteArrayInputStream(java.io.ByteArrayInputStream) IotCloudResponse(com.aws.greengrass.iot.model.IotCloudResponse) ExecutableHttpRequest(software.amazon.awssdk.http.ExecutableHttpRequest) Test(org.junit.jupiter.api.Test)

Example 4 with IotCloudHelper

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);
}
Also used : IotCloudHelper(com.aws.greengrass.iot.IotCloudHelper) ByteArrayInputStream(java.io.ByteArrayInputStream) ExecutableHttpRequest(software.amazon.awssdk.http.ExecutableHttpRequest) Test(org.junit.jupiter.api.Test)

Aggregations

IotCloudHelper (com.aws.greengrass.iot.IotCloudHelper)4 Test (org.junit.jupiter.api.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ExecutableHttpRequest (software.amazon.awssdk.http.ExecutableHttpRequest)3 IotCloudResponse (com.aws.greengrass.iot.model.IotCloudResponse)2 DeviceConfigurationException (com.aws.greengrass.deployment.exceptions.DeviceConfigurationException)1 IOException (java.io.IOException)1