Search in sources :

Example 1 with ConfigStoreIPCEventStreamAgent

use of com.aws.greengrass.builtin.services.configstore.ConfigStoreIPCEventStreamAgent in project aws-greengrass-nucleus by aws-greengrass.

the class IPCServicesTest method GIVEN_ConfigStoreEventStreamClient_WHEN_report_config_validation_status_THEN_inform_validation_requester.

@SuppressWarnings({ "PMD.CloseResource", "PMD.AvoidCatchingGenericException" })
@Test
void GIVEN_ConfigStoreEventStreamClient_WHEN_report_config_validation_status_THEN_inform_validation_requester() throws Exception {
    LogConfig.getRootLogConfig().setLevel(Level.DEBUG);
    CountDownLatch cdl = new CountDownLatch(1);
    String authToken = IPCTestUtils.getAuthTokeForService(kernel, TEST_SERVICE_NAME);
    try (EventStreamRPCConnection clientConnection = IPCTestUtils.connectToGGCOverEventStreamIPC(socketOptions, authToken, kernel)) {
        CountDownLatch subscriptionLatch = new CountDownLatch(1);
        Slf4jLogAdapter.addGlobalListener(m -> {
            if (m.getMessage().contains("Config IPC subscribe to config validation request")) {
                subscriptionLatch.countDown();
            }
        });
        GreengrassCoreIPCClient greengrassCoreIPCClient = new GreengrassCoreIPCClient(clientConnection);
        SubscribeToValidateConfigurationUpdatesRequest subscribe = new SubscribeToValidateConfigurationUpdatesRequest();
        CompletableFuture<SubscribeToValidateConfigurationUpdatesResponse> fut = 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("keyToValidate", "valueToValidate"));
                cdl.countDown();
                SendConfigurationValidityReportRequest reportRequest = new SendConfigurationValidityReportRequest();
                ConfigurationValidityReport report = new ConfigurationValidityReport();
                report.setStatus(ConfigurationValidityStatus.ACCEPTED);
                report.setDeploymentId(events.getValidateConfigurationUpdateEvent().getDeploymentId());
                reportRequest.setConfigurationValidityReport(report);
                greengrassCoreIPCClient.sendConfigurationValidityReport(reportRequest, Optional.empty());
            }

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

            @Override
            public void onStreamClosed() {
            }
        })).getResponse();
        try {
            fut.get(3, TimeUnit.SECONDS);
        } catch (Exception e) {
            logger.atError().setCause(e).log("Error when subscribing to component updates");
            fail("Caught exception when subscribing to component updates");
        }
        assertTrue(subscriptionLatch.await(20, TimeUnit.SECONDS));
        CompletableFuture<ConfigurationValidityReport> responseTracker = new CompletableFuture<>();
        ConfigStoreIPCEventStreamAgent agent = kernel.getContext().get(ConfigStoreIPCEventStreamAgent.class);
        agent.validateConfiguration("ServiceName", "A", Collections.singletonMap("keyToValidate", "valueToValidate"), responseTracker);
        assertTrue(cdl.await(20, TimeUnit.SECONDS));
        assertEquals(ConfigurationValidityStatus.ACCEPTED, responseTracker.get(20, TimeUnit.SECONDS).getStatus());
        SendConfigurationValidityReportRequest reportRequest = new SendConfigurationValidityReportRequest();
        ConfigurationValidityReport report = new ConfigurationValidityReport();
        report.setStatus(ConfigurationValidityStatus.ACCEPTED);
        reportRequest.setConfigurationValidityReport(report);
        ExecutionException ex = assertThrows(ExecutionException.class, () -> greengrassCoreIPCClient.sendConfigurationValidityReport(reportRequest, Optional.empty()).getResponse().get(5, TimeUnit.SECONDS));
        assertThat(ex.getCause().getMessage(), containsString("was null"));
    }
}
Also used : ValidateConfigurationUpdateEvents(software.amazon.awssdk.aws.greengrass.model.ValidateConfigurationUpdateEvents) SubscribeToValidateConfigurationUpdatesRequest(software.amazon.awssdk.aws.greengrass.model.SubscribeToValidateConfigurationUpdatesRequest) EventStreamRPCConnection(software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection) SendConfigurationValidityReportRequest(software.amazon.awssdk.aws.greengrass.model.SendConfigurationValidityReportRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SubscribeToValidateConfigurationUpdatesResponse(software.amazon.awssdk.aws.greengrass.model.SubscribeToValidateConfigurationUpdatesResponse) CompletableFuture(java.util.concurrent.CompletableFuture) GreengrassCoreIPCClient(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient) ConfigStoreIPCEventStreamAgent(com.aws.greengrass.builtin.services.configstore.ConfigStoreIPCEventStreamAgent) ExecutionException(java.util.concurrent.ExecutionException) ConfigurationValidityReport(software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigStoreIPCEventStreamAgent (com.aws.greengrass.builtin.services.configstore.ConfigStoreIPCEventStreamAgent)1 IOException (java.io.IOException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Test (org.junit.jupiter.api.Test)1 GreengrassCoreIPCClient (software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient)1 ConfigurationValidityReport (software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport)1 SendConfigurationValidityReportRequest (software.amazon.awssdk.aws.greengrass.model.SendConfigurationValidityReportRequest)1 SubscribeToValidateConfigurationUpdatesRequest (software.amazon.awssdk.aws.greengrass.model.SubscribeToValidateConfigurationUpdatesRequest)1 SubscribeToValidateConfigurationUpdatesResponse (software.amazon.awssdk.aws.greengrass.model.SubscribeToValidateConfigurationUpdatesResponse)1 ValidateConfigurationUpdateEvents (software.amazon.awssdk.aws.greengrass.model.ValidateConfigurationUpdateEvents)1 EventStreamRPCConnection (software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection)1