use of com.aws.greengrass.mqttclient.UnsubscribeRequest 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));
}
}
use of com.aws.greengrass.mqttclient.UnsubscribeRequest 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));
}
Aggregations