Search in sources :

Example 1 with SubscribeRequest

use of com.aws.greengrass.mqttclient.SubscribeRequest in project aws-greengrass-nucleus by aws-greengrass.

the class MqttProxyIPCAgentTest method GIVEN_MqttProxyIPCAgent_WHEN_subscribe_to_topic_THEN_topic_subscribed.

@Test
void GIVEN_MqttProxyIPCAgent_WHEN_subscribe_to_topic_THEN_topic_subscribed() throws Exception {
    SubscribeToIoTCoreRequest subscribeToIoTCoreRequest = new SubscribeToIoTCoreRequest();
    subscribeToIoTCoreRequest.setTopicName(TEST_TOPIC);
    subscribeToIoTCoreRequest.setQos(QOS.AT_LEAST_ONCE);
    when(authorizationHandler.isAuthorized(any(), any(), any())).thenReturn(true);
    ArgumentCaptor<SubscribeRequest> subscribeRequestArgumentCaptor = ArgumentCaptor.forClass(SubscribeRequest.class);
    ArgumentCaptor<UnsubscribeRequest> unsubscribeRequestArgumentCaptor = ArgumentCaptor.forClass(UnsubscribeRequest.class);
    ArgumentCaptor<IoTCoreMessage> ioTCoreMessageArgumentCaptor = ArgumentCaptor.forClass(IoTCoreMessage.class);
    try (MqttProxyIPCAgent.SubscribeToIoTCoreOperationHandler subscribeToIoTCoreOperationHandler = spy(mqttProxyIPCAgent.getSubscribeToIoTCoreOperationHandler(mockContext))) {
        SubscribeToIoTCoreResponse subscribeToIoTCoreResponse = subscribeToIoTCoreOperationHandler.handleRequest(subscribeToIoTCoreRequest);
        assertNotNull(subscribeToIoTCoreResponse);
        verify(authorizationHandler).isAuthorized(MQTT_PROXY_SERVICE_NAME, Permission.builder().principal(TEST_SERVICE).operation(GreengrassCoreIPCService.SUBSCRIBE_TO_IOT_CORE).resource(TEST_TOPIC).build(), ResourceLookupPolicy.MQTT_STYLE);
        verify(mqttClient).subscribe(subscribeRequestArgumentCaptor.capture());
        SubscribeRequest capturedSubscribeRequest = subscribeRequestArgumentCaptor.getValue();
        assertThat(capturedSubscribeRequest.getTopic(), is(TEST_TOPIC));
        assertThat(capturedSubscribeRequest.getQos(), is(QualityOfService.AT_LEAST_ONCE));
        Consumer<MqttMessage> callback = capturedSubscribeRequest.getCallback();
        MqttMessage message = new MqttMessage(TEST_TOPIC, TEST_PAYLOAD);
        doReturn(new CompletableFuture<>()).when(subscribeToIoTCoreOperationHandler).sendStreamEvent(any());
        callback.accept(message);
        verify(subscribeToIoTCoreOperationHandler).sendStreamEvent(ioTCoreMessageArgumentCaptor.capture());
        MQTTMessage mqttMessage = ioTCoreMessageArgumentCaptor.getValue().getMessage();
        assertThat(mqttMessage.getPayload(), is(TEST_PAYLOAD));
        assertThat(mqttMessage.getTopicName(), is(TEST_TOPIC));
        subscribeToIoTCoreOperationHandler.onStreamClosed();
        verify(mqttClient).unsubscribe(unsubscribeRequestArgumentCaptor.capture());
        UnsubscribeRequest capturedUnsubscribedRequest = unsubscribeRequestArgumentCaptor.getValue();
        assertThat(capturedUnsubscribedRequest.getTopic(), is(TEST_TOPIC));
        assertThat(capturedUnsubscribedRequest.getCallback(), is(callback));
    }
}
Also used : MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) MQTTMessage(software.amazon.awssdk.aws.greengrass.model.MQTTMessage) SubscribeToIoTCoreResponse(software.amazon.awssdk.aws.greengrass.model.SubscribeToIoTCoreResponse) IoTCoreMessage(software.amazon.awssdk.aws.greengrass.model.IoTCoreMessage) UnsubscribeRequest(com.aws.greengrass.mqttclient.UnsubscribeRequest) SubscribeToIoTCoreRequest(software.amazon.awssdk.aws.greengrass.model.SubscribeToIoTCoreRequest) SubscribeRequest(com.aws.greengrass.mqttclient.SubscribeRequest) Test(org.junit.jupiter.api.Test)

Example 2 with SubscribeRequest

use of com.aws.greengrass.mqttclient.SubscribeRequest in project aws-greengrass-nucleus by aws-greengrass.

the class IPCMqttProxyTest method GIVEN_MqttProxyEventStreamClient_WHEN_called_subscribe_THEN_subscribed_and_message_received.

@Test
void GIVEN_MqttProxyEventStreamClient_WHEN_called_subscribe_THEN_subscribed_and_message_received() throws Exception {
    CountDownLatch messageLatch = new CountDownLatch(1);
    GreengrassCoreIPCClient greengrassCoreIPCClient = new GreengrassCoreIPCClient(clientConnection);
    SubscribeToIoTCoreRequest subscribeToIoTCoreRequest = new SubscribeToIoTCoreRequest();
    subscribeToIoTCoreRequest.setQos(QOS.AT_LEAST_ONCE);
    subscribeToIoTCoreRequest.setTopicName(TEST_SUBSCRIBE_TOPIC);
    StreamResponseHandler<IoTCoreMessage> streamResponseHandler = new StreamResponseHandler<IoTCoreMessage>() {

        @Override
        public void onStreamEvent(IoTCoreMessage streamEvent) {
            if (Arrays.equals(streamEvent.getMessage().getPayload(), TEST_PAYLOAD) && streamEvent.getMessage().getTopicName().equals(TEST_SUBSCRIBE_TOPIC)) {
                messageLatch.countDown();
            }
        }

        @Override
        public boolean onStreamError(Throwable error) {
            logger.atError().cause(error).log("Subscribe stream errored");
            return false;
        }

        @Override
        public void onStreamClosed() {
        }
    };
    SubscribeToIoTCoreResponseHandler responseHandler = greengrassCoreIPCClient.subscribeToIoTCore(subscribeToIoTCoreRequest, Optional.of(streamResponseHandler));
    responseHandler.getResponse().get(TIMEOUT_FOR_MQTTPROXY_SECONDS, TimeUnit.SECONDS);
    ArgumentCaptor<SubscribeRequest> subscribeRequestArgumentCaptor = ArgumentCaptor.forClass(SubscribeRequest.class);
    verify(mqttClient).subscribe(subscribeRequestArgumentCaptor.capture());
    SubscribeRequest capturedSubscribeRequest = subscribeRequestArgumentCaptor.getValue();
    assertThat(capturedSubscribeRequest.getTopic(), is(TEST_SUBSCRIBE_TOPIC));
    assertThat(capturedSubscribeRequest.getQos(), is(QualityOfService.AT_LEAST_ONCE));
    Consumer<MqttMessage> callback = capturedSubscribeRequest.getCallback();
    MqttMessage message = new MqttMessage(TEST_SUBSCRIBE_TOPIC, TEST_PAYLOAD);
    callback.accept(message);
    assertTrue(messageLatch.await(TIMEOUT_FOR_MQTTPROXY_SECONDS, TimeUnit.SECONDS));
    // close stream -> unsubscribe
    responseHandler.closeStream();
    Thread.sleep(500);
    ArgumentCaptor<UnsubscribeRequest> unsubscribeRequestArgumentCaptor = ArgumentCaptor.forClass(UnsubscribeRequest.class);
    verify(mqttClient).unsubscribe(unsubscribeRequestArgumentCaptor.capture());
    UnsubscribeRequest capturedUnsubscribeRequest = unsubscribeRequestArgumentCaptor.getValue();
    assertThat(capturedUnsubscribeRequest.getTopic(), is(TEST_SUBSCRIBE_TOPIC));
    assertThat(capturedUnsubscribeRequest.getCallback(), is(callback));
}
Also used : MqttMessage(software.amazon.awssdk.crt.mqtt.MqttMessage) IoTCoreMessage(software.amazon.awssdk.aws.greengrass.model.IoTCoreMessage) UnsubscribeRequest(com.aws.greengrass.mqttclient.UnsubscribeRequest) StreamResponseHandler(software.amazon.awssdk.eventstreamrpc.StreamResponseHandler) CountDownLatch(java.util.concurrent.CountDownLatch) SubscribeToIoTCoreRequest(software.amazon.awssdk.aws.greengrass.model.SubscribeToIoTCoreRequest) SubscribeToIoTCoreResponseHandler(software.amazon.awssdk.aws.greengrass.SubscribeToIoTCoreResponseHandler) SubscribeRequest(com.aws.greengrass.mqttclient.SubscribeRequest) GreengrassCoreIPCClient(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient) Test(org.junit.jupiter.api.Test)

Aggregations

SubscribeRequest (com.aws.greengrass.mqttclient.SubscribeRequest)2 UnsubscribeRequest (com.aws.greengrass.mqttclient.UnsubscribeRequest)2 Test (org.junit.jupiter.api.Test)2 IoTCoreMessage (software.amazon.awssdk.aws.greengrass.model.IoTCoreMessage)2 SubscribeToIoTCoreRequest (software.amazon.awssdk.aws.greengrass.model.SubscribeToIoTCoreRequest)2 MqttMessage (software.amazon.awssdk.crt.mqtt.MqttMessage)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 GreengrassCoreIPCClient (software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient)1 SubscribeToIoTCoreResponseHandler (software.amazon.awssdk.aws.greengrass.SubscribeToIoTCoreResponseHandler)1 MQTTMessage (software.amazon.awssdk.aws.greengrass.model.MQTTMessage)1 SubscribeToIoTCoreResponse (software.amazon.awssdk.aws.greengrass.model.SubscribeToIoTCoreResponse)1 StreamResponseHandler (software.amazon.awssdk.eventstreamrpc.StreamResponseHandler)1