Search in sources :

Example 1 with StreamResponseHandler

use of software.amazon.awssdk.eventstreamrpc.StreamResponseHandler in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class VideoUploadRequestHandlerTest method subscribeToMqttTopic_receivesInvalidMessageWithoutHyphen_onStartNotCalled.

@Test
public void subscribeToMqttTopic_receivesInvalidMessageWithoutHyphen_onStartNotCalled() throws ExecutionException, InterruptedException {
    MQTTMessage mqttMessage = new MQTTMessage();
    mqttMessage.setTopicName(MQTT_TOPIC_NAME);
    mqttMessage.setPayload(MQTT_PAYLOAD_INVALID_VALUE_WITHOUT_HYPHEN.getBytes(StandardCharsets.US_ASCII));
    IoTCoreMessage invalidIoTCoreMessage = new IoTCoreMessage();
    invalidIoTCoreMessage.setMessage(mqttMessage);
    SubscribeToIoTCoreResponse response = new SubscribeToIoTCoreResponse();
    when(responseCompletableFuture.get()).thenReturn(response);
    when(subscribeToIoTCoreResponseHandler.getResponse()).thenReturn(responseCompletableFuture);
    doAnswer(invocationOnMock -> {
        Optional<StreamResponseHandler<IoTCoreMessage>> streamResponseHandler = invocationOnMock.getArgument(1);
        streamResponseHandler.get().onStreamEvent(invalidIoTCoreMessage);
        return subscribeToIoTCoreResponseHandler;
    }).when(greengrassCoreIPCClient).subscribeToIoTCore(any(), any());
    videoUploadRequestHandler.subscribeToMqttTopic(MQTT_TOPIC_NAME, videoUploadRequestEvent);
    verify(videoUploadRequestEvent, never()).onStart(IS_LIVE_TRUE, EVENT_TIMESTAMP, START_TIME, END_TIME);
}
Also used : MQTTMessage(software.amazon.awssdk.aws.greengrass.model.MQTTMessage) SubscribeToIoTCoreResponse(software.amazon.awssdk.aws.greengrass.model.SubscribeToIoTCoreResponse) IoTCoreMessage(software.amazon.awssdk.aws.greengrass.model.IoTCoreMessage) StreamResponseHandler(software.amazon.awssdk.eventstreamrpc.StreamResponseHandler) Test(org.junit.jupiter.api.Test)

Example 2 with StreamResponseHandler

use of software.amazon.awssdk.eventstreamrpc.StreamResponseHandler in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class VideoUploadRequestHandlerTest method subscribeToMqttTopic_receivesOnLiveEvent_onStart.

@Test
public void subscribeToMqttTopic_receivesOnLiveEvent_onStart() throws ExecutionException, InterruptedException {
    MQTTMessage mqttMessage = new MQTTMessage();
    mqttMessage.setTopicName(MQTT_TOPIC_NAME);
    mqttMessage.setPayload(MQTT_PAYLOAD_LIVE.getBytes(StandardCharsets.US_ASCII));
    IoTCoreMessage ioTCoreMessage = new IoTCoreMessage();
    ioTCoreMessage.setMessage(mqttMessage);
    SubscribeToIoTCoreResponse response = new SubscribeToIoTCoreResponse();
    when(responseCompletableFuture.get()).thenReturn(response);
    when(subscribeToIoTCoreResponseHandler.getResponse()).thenReturn(responseCompletableFuture);
    doAnswer(invocationOnMock -> {
        Optional<StreamResponseHandler<IoTCoreMessage>> streamResponseHandler = invocationOnMock.getArgument(1);
        streamResponseHandler.get().onStreamEvent(ioTCoreMessage);
        return subscribeToIoTCoreResponseHandler;
    }).when(greengrassCoreIPCClient).subscribeToIoTCore(any(), any());
    videoUploadRequestHandler.subscribeToMqttTopic(MQTT_TOPIC_NAME, videoUploadRequestEvent);
    verify(videoUploadRequestEvent).onStart(IS_LIVE_TRUE, EVENT_TIMESTAMP, 0, 0);
}
Also used : MQTTMessage(software.amazon.awssdk.aws.greengrass.model.MQTTMessage) SubscribeToIoTCoreResponse(software.amazon.awssdk.aws.greengrass.model.SubscribeToIoTCoreResponse) IoTCoreMessage(software.amazon.awssdk.aws.greengrass.model.IoTCoreMessage) StreamResponseHandler(software.amazon.awssdk.eventstreamrpc.StreamResponseHandler) Test(org.junit.jupiter.api.Test)

Example 3 with StreamResponseHandler

use of software.amazon.awssdk.eventstreamrpc.StreamResponseHandler in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class VideoUploadRequestHandlerTest method subscribeToMqttTopic_receivesMultipleValuesMessage_onStartNotCalled.

@Test
public void subscribeToMqttTopic_receivesMultipleValuesMessage_onStartNotCalled() throws ExecutionException, InterruptedException {
    MQTTMessage mqttMessage = new MQTTMessage();
    mqttMessage.setTopicName(MQTT_TOPIC_NAME);
    mqttMessage.setPayload(MQTT_PAYLOAD_MULTIPLE_VALUES.getBytes(StandardCharsets.US_ASCII));
    IoTCoreMessage multipleValuesIoTCoreMessage = new IoTCoreMessage();
    multipleValuesIoTCoreMessage.setMessage(mqttMessage);
    SubscribeToIoTCoreResponse response = new SubscribeToIoTCoreResponse();
    when(responseCompletableFuture.get()).thenReturn(response);
    when(subscribeToIoTCoreResponseHandler.getResponse()).thenReturn(responseCompletableFuture);
    doAnswer(invocationOnMock -> {
        Optional<StreamResponseHandler<IoTCoreMessage>> streamResponseHandler = invocationOnMock.getArgument(1);
        streamResponseHandler.get().onStreamEvent(multipleValuesIoTCoreMessage);
        return subscribeToIoTCoreResponseHandler;
    }).when(greengrassCoreIPCClient).subscribeToIoTCore(any(), any());
    videoUploadRequestHandler.subscribeToMqttTopic(MQTT_TOPIC_NAME, videoUploadRequestEvent);
    verify(videoUploadRequestEvent, never()).onStart(IS_LIVE_TRUE, EVENT_TIMESTAMP, START_TIME, END_TIME);
}
Also used : MQTTMessage(software.amazon.awssdk.aws.greengrass.model.MQTTMessage) SubscribeToIoTCoreResponse(software.amazon.awssdk.aws.greengrass.model.SubscribeToIoTCoreResponse) IoTCoreMessage(software.amazon.awssdk.aws.greengrass.model.IoTCoreMessage) StreamResponseHandler(software.amazon.awssdk.eventstreamrpc.StreamResponseHandler) Test(org.junit.jupiter.api.Test)

Example 4 with StreamResponseHandler

use of software.amazon.awssdk.eventstreamrpc.StreamResponseHandler in project aws-iot-greengrass-edge-connector-for-kinesis-video-stream by awslabs.

the class VideoUploadRequestHandlerTest method subscribeToMqttTopic_receivesEvent_onError.

@Test
public void subscribeToMqttTopic_receivesEvent_onError() throws ExecutionException, InterruptedException {
    SubscribeToIoTCoreResponse response = new SubscribeToIoTCoreResponse();
    when(responseCompletableFuture.get()).thenReturn(response);
    when(subscribeToIoTCoreResponseHandler.getResponse()).thenReturn(responseCompletableFuture);
    doAnswer(invocationOnMock -> {
        Optional<StreamResponseHandler<IoTCoreMessage>> streamResponseHandler = invocationOnMock.getArgument(1);
        streamResponseHandler.get().onStreamError(new Exception(ERR_MSG));
        return subscribeToIoTCoreResponseHandler;
    }).when(greengrassCoreIPCClient).subscribeToIoTCore(any(), any());
    videoUploadRequestHandler.subscribeToMqttTopic(MQTT_TOPIC_NAME, videoUploadRequestEvent);
    verify(videoUploadRequestEvent).onError(ERR_MSG);
}
Also used : SubscribeToIoTCoreResponse(software.amazon.awssdk.aws.greengrass.model.SubscribeToIoTCoreResponse) StreamResponseHandler(software.amazon.awssdk.eventstreamrpc.StreamResponseHandler) EdgeConnectorForKVSException(com.aws.iot.edgeconnectorforkvs.model.exceptions.EdgeConnectorForKVSException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 5 with StreamResponseHandler

use of software.amazon.awssdk.eventstreamrpc.StreamResponseHandler in project aws-greengrass-nucleus by aws-greengrass.

the class DynamicComponentConfigurationValidationTest method GIVEN_deployment_changes_component_config_WHEN_component_validates_config_THEN_deployment_is_successful.

@Test
void GIVEN_deployment_changes_component_config_WHEN_component_validates_config_THEN_deployment_is_successful() throws Throwable {
    LogConfig.getRootLogConfig().setLevel(Level.DEBUG);
    // Subscribe to config validation on behalf of the running service
    CountDownLatch eventReceivedByClient = new CountDownLatch(1);
    Topics servicePrivateConfig = kernel.getConfig().findTopics(SERVICES_NAMESPACE_TOPIC, "OldService", PRIVATE_STORE_NAMESPACE_TOPIC);
    String authToken = Coerce.toString(servicePrivateConfig.find(SERVICE_UNIQUE_ID_KEY));
    CountDownLatch subscriptionLatch = new CountDownLatch(1);
    try (EventStreamRPCConnection clientConnection = IPCTestUtils.connectToGGCOverEventStreamIPC(socketOptions, authToken, kernel);
        AutoCloseable l = TestUtils.createCloseableLogListener(m -> {
            if (m.getMessage().contains("Config IPC subscribe to config validation request")) {
                subscriptionLatch.countDown();
            }
        })) {
        GreengrassCoreIPCClient greengrassCoreIPCClient = new GreengrassCoreIPCClient(clientConnection);
        SubscribeToValidateConfigurationUpdatesRequest subscribe = new SubscribeToValidateConfigurationUpdatesRequest();
        greengrassCoreIPCClient.subscribeToValidateConfigurationUpdates(subscribe, Optional.of(new StreamResponseHandler<ValidateConfigurationUpdateEvents>() {

            @Override
            public void onStreamEvent(ValidateConfigurationUpdateEvents events) {
                assertNotNull(events);
                assertNotNull(events.getValidateConfigurationUpdateEvent());
                assertNotNull(events.getValidateConfigurationUpdateEvent().getConfiguration());
                assertThat(events.getValidateConfigurationUpdateEvent().getConfiguration(), IsMapContaining.hasEntry("ConfigKey1", "ConfigValue2"));
                eventReceivedByClient.countDown();
                SendConfigurationValidityReportRequest reportRequest = new SendConfigurationValidityReportRequest();
                ConfigurationValidityReport report = new ConfigurationValidityReport();
                report.setStatus(ConfigurationValidityStatus.ACCEPTED);
                report.setDeploymentId(events.getValidateConfigurationUpdateEvent().getDeploymentId());
                reportRequest.setConfigurationValidityReport(report);
                try {
                    greengrassCoreIPCClient.sendConfigurationValidityReport(reportRequest, Optional.empty()).getResponse().get(10, TimeUnit.SECONDS);
                } catch (InterruptedException | ExecutionException | TimeoutException e) {
                    fail("received invalid update validate configuration event", e);
                }
            }

            @Override
            public boolean onStreamError(Throwable error) {
                log.atError().log("Received stream error.", error);
                return false;
            }

            @Override
            public void onStreamClosed() {
            }
        }));
        assertTrue(subscriptionLatch.await(20, TimeUnit.SECONDS));
        // Attempt changing the configuration for the running service
        Map<String, Object> newConfig = new HashMap<String, Object>() {

            {
                put(SERVICES_NAMESPACE_TOPIC, new HashMap<String, Object>() {

                    {
                        put("main", kernel.getMain().getServiceConfig().toPOJO());
                        put("OldService", new HashMap<String, Object>() {

                            {
                                put(CONFIGURATION_CONFIG_KEY, new HashMap<String, Object>() {

                                    {
                                        put("ConfigKey1", "ConfigValue2");
                                    }
                                });
                                put(SERVICE_LIFECYCLE_NAMESPACE_TOPIC, new HashMap<String, Object>() {

                                    {
                                        put(LIFECYCLE_RUN_NAMESPACE_TOPIC, "echo Running OldService");
                                    }
                                });
                                put(VERSION_CONFIG_KEY, DEFAULT_EXISTING_SERVICE_VERSION);
                            }
                        });
                        put(DEFAULT_NUCLEUS_COMPONENT_NAME, getNucleusConfig(kernel));
                    }
                });
            }
        };
        DeploymentResult result = deploymentConfigMerger.mergeInNewConfig(createTestDeployment(), newConfig).get(60, TimeUnit.SECONDS);
        assertEquals(DeploymentResult.DeploymentStatus.SUCCESSFUL, result.getDeploymentStatus());
        assertTrue(eventReceivedByClient.await(20, TimeUnit.SECONDS));
    }
}
Also used : Topics(com.aws.greengrass.config.Topics) ValidateConfigurationUpdateEvents(software.amazon.awssdk.aws.greengrass.model.ValidateConfigurationUpdateEvents) SubscribeToValidateConfigurationUpdatesRequest(software.amazon.awssdk.aws.greengrass.model.SubscribeToValidateConfigurationUpdatesRequest) HashMap(java.util.HashMap) EventStreamRPCConnection(software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection) StreamResponseHandler(software.amazon.awssdk.eventstreamrpc.StreamResponseHandler) SendConfigurationValidityReportRequest(software.amazon.awssdk.aws.greengrass.model.SendConfigurationValidityReportRequest) DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) CountDownLatch(java.util.concurrent.CountDownLatch) GreengrassCoreIPCClient(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient) ExecutionException(java.util.concurrent.ExecutionException) ConfigurationValidityReport(software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.jupiter.api.Test)

Aggregations

StreamResponseHandler (software.amazon.awssdk.eventstreamrpc.StreamResponseHandler)17 Test (org.junit.jupiter.api.Test)16 SubscribeToIoTCoreResponse (software.amazon.awssdk.aws.greengrass.model.SubscribeToIoTCoreResponse)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 GreengrassCoreIPCClient (software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient)7 IoTCoreMessage (software.amazon.awssdk.aws.greengrass.model.IoTCoreMessage)7 ComponentUpdatePolicyEvents (software.amazon.awssdk.aws.greengrass.model.ComponentUpdatePolicyEvents)5 DeferComponentUpdateRequest (software.amazon.awssdk.aws.greengrass.model.DeferComponentUpdateRequest)5 SubscribeToComponentUpdatesRequest (software.amazon.awssdk.aws.greengrass.model.SubscribeToComponentUpdatesRequest)5 GreengrassLogMessage (com.aws.greengrass.logging.impl.GreengrassLogMessage)4 EventStreamRPCConnection (software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection)4 ExecutionException (java.util.concurrent.ExecutionException)3 MQTTMessage (software.amazon.awssdk.aws.greengrass.model.MQTTMessage)3 KernelConfigResolver (com.aws.greengrass.componentmanager.KernelConfigResolver)2 PackageDownloadException (com.aws.greengrass.componentmanager.exceptions.PackageDownloadException)2 State (com.aws.greengrass.dependency.State)2 DeploymentDocumentDownloader (com.aws.greengrass.deployment.DeploymentDocumentDownloader)2 Deployment (com.aws.greengrass.deployment.model.Deployment)2 DeploymentDocument (com.aws.greengrass.deployment.model.DeploymentDocument)2 PreloadComponentStoreHelper (com.aws.greengrass.helper.PreloadComponentStoreHelper)2