Search in sources :

Example 1 with PublishToIoTCoreRequest

use of software.amazon.awssdk.aws.greengrass.model.PublishToIoTCoreRequest in project aws-greengrass-nucleus by aws-greengrass.

the class MqttClientPublishTest method whenPublishGetSpoolerException.

private boolean whenPublishGetSpoolerException(Pair<String, byte[]> requestTopicPayload) {
    PublishToIoTCoreRequest publishToIoTCoreRequest = new PublishToIoTCoreRequest();
    publishToIoTCoreRequest.setTopicName(requestTopicPayload.getLeft());
    publishToIoTCoreRequest.setPayload(requestTopicPayload.getRight());
    publishToIoTCoreRequest.setQos(QOS.AT_LEAST_ONCE);
    try {
        greengrassCoreIPCClient.publishToIoTCore(publishToIoTCoreRequest, Optional.empty()).getResponse().get();
    } catch (ExecutionException | InterruptedException e) {
        return true;
    }
    return false;
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) PublishToIoTCoreRequest(software.amazon.awssdk.aws.greengrass.model.PublishToIoTCoreRequest)

Example 2 with PublishToIoTCoreRequest

use of software.amazon.awssdk.aws.greengrass.model.PublishToIoTCoreRequest in project aws-greengrass-nucleus by aws-greengrass.

the class MqttProxyIPCAgentTest method GIVEN_MqttProxyIPCAgent_WHEN_publish_on_topic_THEN_message_published.

@Test
void GIVEN_MqttProxyIPCAgent_WHEN_publish_on_topic_THEN_message_published() throws Exception {
    PublishToIoTCoreRequest publishToIoTCoreRequest = new PublishToIoTCoreRequest();
    publishToIoTCoreRequest.setPayload(TEST_PAYLOAD);
    publishToIoTCoreRequest.setTopicName(TEST_TOPIC);
    publishToIoTCoreRequest.setQos(QOS.AT_LEAST_ONCE);
    CompletableFuture<Integer> completableFuture = new CompletableFuture<>();
    completableFuture.complete(0);
    when(mqttClient.publish(any())).thenReturn(completableFuture);
    when(authorizationHandler.isAuthorized(any(), any(), any())).thenReturn(true);
    ArgumentCaptor<PublishRequest> publishRequestArgumentCaptor = ArgumentCaptor.forClass(PublishRequest.class);
    try (MqttProxyIPCAgent.PublishToIoTCoreOperationHandler publishToIoTCoreOperationHandler = mqttProxyIPCAgent.getPublishToIoTCoreOperationHandler(mockContext)) {
        PublishToIoTCoreResponse publishToIoTCoreResponse = publishToIoTCoreOperationHandler.handleRequest(publishToIoTCoreRequest);
        assertNotNull(publishToIoTCoreResponse);
        verify(authorizationHandler).isAuthorized(MQTT_PROXY_SERVICE_NAME, Permission.builder().principal(TEST_SERVICE).operation(GreengrassCoreIPCService.PUBLISH_TO_IOT_CORE).resource(TEST_TOPIC).build(), ResourceLookupPolicy.MQTT_STYLE);
        verify(mqttClient).publish(publishRequestArgumentCaptor.capture());
        PublishRequest capturedPublishRequest = publishRequestArgumentCaptor.getValue();
        assertThat(capturedPublishRequest.getPayload(), is(TEST_PAYLOAD));
        assertThat(capturedPublishRequest.getTopic(), is(TEST_TOPIC));
        assertThat(capturedPublishRequest.getQos(), is(QualityOfService.AT_LEAST_ONCE));
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) PublishToIoTCoreResponse(software.amazon.awssdk.aws.greengrass.model.PublishToIoTCoreResponse) PublishRequest(com.aws.greengrass.mqttclient.PublishRequest) PublishToIoTCoreRequest(software.amazon.awssdk.aws.greengrass.model.PublishToIoTCoreRequest) Test(org.junit.jupiter.api.Test)

Example 3 with PublishToIoTCoreRequest

use of software.amazon.awssdk.aws.greengrass.model.PublishToIoTCoreRequest in project aws-greengrass-nucleus by aws-greengrass.

the class MqttProxyIPCAgentTest method GIVEN_MqttProxyIPCAgent_WHEN_publish_with_no_qos_THEN_error_thrown.

@Test
void GIVEN_MqttProxyIPCAgent_WHEN_publish_with_no_qos_THEN_error_thrown() throws Exception {
    PublishToIoTCoreRequest publishToIoTCoreRequest = new PublishToIoTCoreRequest();
    publishToIoTCoreRequest.setPayload(TEST_PAYLOAD);
    publishToIoTCoreRequest.setTopicName(TEST_TOPIC);
    when(authorizationHandler.isAuthorized(any(), any(), any())).thenReturn(true);
    try (MqttProxyIPCAgent.PublishToIoTCoreOperationHandler publishToIoTCoreOperationHandler = mqttProxyIPCAgent.getPublishToIoTCoreOperationHandler(mockContext)) {
        assertThrows(InvalidArgumentsError.class, () -> {
            publishToIoTCoreOperationHandler.handleRequest(publishToIoTCoreRequest);
        });
    }
}
Also used : PublishToIoTCoreRequest(software.amazon.awssdk.aws.greengrass.model.PublishToIoTCoreRequest) Test(org.junit.jupiter.api.Test)

Example 4 with PublishToIoTCoreRequest

use of software.amazon.awssdk.aws.greengrass.model.PublishToIoTCoreRequest in project aws-greengrass-nucleus by aws-greengrass.

the class MqttProxyIPCAgentTest method GIVEN_MqttProxyIPCAgent_WHEN_publish_with_no_payload_THEN_error_thrown.

@Test
void GIVEN_MqttProxyIPCAgent_WHEN_publish_with_no_payload_THEN_error_thrown() throws Exception {
    PublishToIoTCoreRequest publishToIoTCoreRequest = new PublishToIoTCoreRequest();
    publishToIoTCoreRequest.setTopicName(TEST_TOPIC);
    publishToIoTCoreRequest.setQos(QOS.AT_LEAST_ONCE);
    when(authorizationHandler.isAuthorized(any(), any(), any())).thenReturn(true);
    try (MqttProxyIPCAgent.PublishToIoTCoreOperationHandler publishToIoTCoreOperationHandler = mqttProxyIPCAgent.getPublishToIoTCoreOperationHandler(mockContext)) {
        assertThrows(InvalidArgumentsError.class, () -> {
            publishToIoTCoreOperationHandler.handleRequest(publishToIoTCoreRequest);
        });
    }
}
Also used : PublishToIoTCoreRequest(software.amazon.awssdk.aws.greengrass.model.PublishToIoTCoreRequest) Test(org.junit.jupiter.api.Test)

Example 5 with PublishToIoTCoreRequest

use of software.amazon.awssdk.aws.greengrass.model.PublishToIoTCoreRequest in project aws-greengrass-nucleus by aws-greengrass.

the class MqttProxyIPCAgentTest method GIVEN_full_mqtt_spool_WHEN_publish_on_topic_THEN_service_error_thrown.

@Test
void GIVEN_full_mqtt_spool_WHEN_publish_on_topic_THEN_service_error_thrown() throws Exception {
    PublishToIoTCoreRequest publishToIoTCoreRequest = new PublishToIoTCoreRequest();
    publishToIoTCoreRequest.setPayload(TEST_PAYLOAD);
    publishToIoTCoreRequest.setTopicName(TEST_TOPIC);
    publishToIoTCoreRequest.setQos(QOS.AT_LEAST_ONCE);
    CompletableFuture<Integer> f = new CompletableFuture<>();
    f.completeExceptionally(new SpoolerStoreException("Spool full"));
    when(mqttClient.publish(any())).thenReturn(f);
    when(authorizationHandler.isAuthorized(any(), any(), any())).thenReturn(true);
    try (MqttProxyIPCAgent.PublishToIoTCoreOperationHandler publishToIoTCoreOperationHandler = mqttProxyIPCAgent.getPublishToIoTCoreOperationHandler(mockContext)) {
        assertThrows(ServiceError.class, () -> {
            publishToIoTCoreOperationHandler.handleRequest(publishToIoTCoreRequest);
        });
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SpoolerStoreException(com.aws.greengrass.mqttclient.spool.SpoolerStoreException) PublishToIoTCoreRequest(software.amazon.awssdk.aws.greengrass.model.PublishToIoTCoreRequest) Test(org.junit.jupiter.api.Test)

Aggregations

PublishToIoTCoreRequest (software.amazon.awssdk.aws.greengrass.model.PublishToIoTCoreRequest)10 Test (org.junit.jupiter.api.Test)9 PublishRequest (com.aws.greengrass.mqttclient.PublishRequest)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 SpoolerStoreException (com.aws.greengrass.mqttclient.spool.SpoolerStoreException)2 ExecutionException (java.util.concurrent.ExecutionException)2 GreengrassCoreIPCClient (software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient)2 Matchers.containsString (org.hamcrest.Matchers.containsString)1 PublishToIoTCoreResponse (software.amazon.awssdk.aws.greengrass.model.PublishToIoTCoreResponse)1