Search in sources :

Example 1 with InvalidArgumentsError

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

the class PubSubIPCEventStreamAgent method handlePublishToTopicRequest.

@SuppressWarnings("PMD.PreserveStackTrace")
private PublishToTopicResponse handlePublishToTopicRequest(String topic, String serviceName, Optional<Map<String, Object>> jsonMessage, Optional<byte[]> binaryMessage) {
    if (topic == null) {
        throw new InvalidArgumentsError("Publish topic must not be null");
    }
    if (topic.contains(MQTT_SINGLELEVEL_WILDCARD) || topic.contains(MQTT_MULTILEVEL_WILDCARD) || topic.contains(GLOB_WILDCARD)) {
        throw new InvalidArgumentsError("Publish topic must not contain a wildcard.");
    }
    Set<Object> contexts = listeners.get(topic);
    if (contexts == null || contexts.isEmpty()) {
        log.atDebug().kv(COMPONENT_NAME, serviceName).log("No one subscribed to topic {}. Returning.", topic);
        // Still technically successful, just no one was subscribed
        return new PublishToTopicResponse();
    }
    SubscriptionResponseMessage message = new SubscriptionResponseMessage();
    PublishEvent publishedEvent = PublishEvent.builder().topic(topic).build();
    if (jsonMessage.isPresent()) {
        JsonMessage message1 = new JsonMessage();
        message1.setMessage(jsonMessage.get());
        message1.setEventTopic(topic);
        message.setJsonMessage(message1);
        try {
            publishedEvent.setPayload(SERIALIZER.writeValueAsBytes(jsonMessage.get()));
        } catch (JsonProcessingException e) {
            log.atError().cause(e).kv(COMPONENT_NAME, serviceName).log("Unable to serialize JSON message.");
            throw new InvalidArgumentsError("Unable to serialize payload as JSON");
        }
    }
    if (binaryMessage.isPresent()) {
        BinaryMessage binaryMessage1 = new BinaryMessage();
        binaryMessage1.setMessage(binaryMessage.get());
        binaryMessage1.setEventTopic(topic);
        message.setBinaryMessage(binaryMessage1);
        publishedEvent.setPayload(binaryMessage.get());
    }
    contexts.forEach(context -> {
        log.atDebug().kv(COMPONENT_NAME, serviceName).log("Sending publish event for topic {}", topic);
        if (context instanceof StreamEventPublisher) {
            StreamEventPublisher<SubscriptionResponseMessage> publisher = (StreamEventPublisher<SubscriptionResponseMessage>) context;
            orderedExecutorService.execute(() -> publisher.sendStreamEvent(message), publisher);
        } else if (context instanceof Consumer) {
            Consumer<PublishEvent> consumer = (Consumer<PublishEvent>) context;
            orderedExecutorService.execute(() -> consumer.accept(publishedEvent), consumer);
        }
    });
    return new PublishToTopicResponse();
}
Also used : InvalidArgumentsError(software.amazon.awssdk.aws.greengrass.model.InvalidArgumentsError) PublishToTopicResponse(software.amazon.awssdk.aws.greengrass.model.PublishToTopicResponse) SubscriptionResponseMessage(software.amazon.awssdk.aws.greengrass.model.SubscriptionResponseMessage) Consumer(java.util.function.Consumer) JsonMessage(software.amazon.awssdk.aws.greengrass.model.JsonMessage) EventStreamJsonMessage(software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) StreamEventPublisher(software.amazon.awssdk.eventstreamrpc.StreamEventPublisher) BinaryMessage(software.amazon.awssdk.aws.greengrass.model.BinaryMessage)

Example 2 with InvalidArgumentsError

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

the class ConfigStoreIPCEventStreamAgentTest method GIVEN_no_validation_event_is_tracked_WHEN_send_config_validity_report_request_THEN_fail.

@Test
void GIVEN_no_validation_event_is_tracked_WHEN_send_config_validity_report_request_THEN_fail() {
    when(mockAuthenticationData.getIdentityLabel()).thenReturn(TEST_COMPONENT_A);
    SendConfigurationValidityReportRequest reportRequest = new SendConfigurationValidityReportRequest();
    ConfigurationValidityReport validityReport = new ConfigurationValidityReport();
    validityReport.setStatus(ConfigurationValidityStatus.ACCEPTED);
    validityReport.setDeploymentId("abc");
    reportRequest.setConfigurationValidityReport(validityReport);
    InvalidArgumentsError error = assertThrows(InvalidArgumentsError.class, () -> agent.getSendConfigurationValidityReportHandler(mockContext).handleRequest(reportRequest));
    assertEquals("Validation request either timed out or was never made", error.getMessage());
}
Also used : InvalidArgumentsError(software.amazon.awssdk.aws.greengrass.model.InvalidArgumentsError) SendConfigurationValidityReportRequest(software.amazon.awssdk.aws.greengrass.model.SendConfigurationValidityReportRequest) ConfigurationValidityReport(software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport) Test(org.junit.jupiter.api.Test)

Example 3 with InvalidArgumentsError

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

the class ConfigStoreIPCEventStreamAgentTest method GIVEN_request_has_null_deployment_id_THEN_fail.

@Test
void GIVEN_request_has_null_deployment_id_THEN_fail() {
    SendConfigurationValidityReportRequest reportRequest = new SendConfigurationValidityReportRequest();
    ConfigurationValidityReport validityReport = new ConfigurationValidityReport();
    reportRequest.setConfigurationValidityReport(validityReport);
    InvalidArgumentsError error = assertThrows(InvalidArgumentsError.class, () -> agent.getSendConfigurationValidityReportHandler(mockContext).handleRequest(reportRequest));
    assertEquals("Cannot accept configuration validity report, the deployment ID provided was null", error.getMessage());
}
Also used : InvalidArgumentsError(software.amazon.awssdk.aws.greengrass.model.InvalidArgumentsError) SendConfigurationValidityReportRequest(software.amazon.awssdk.aws.greengrass.model.SendConfigurationValidityReportRequest) ConfigurationValidityReport(software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport) Test(org.junit.jupiter.api.Test)

Aggregations

InvalidArgumentsError (software.amazon.awssdk.aws.greengrass.model.InvalidArgumentsError)3 Test (org.junit.jupiter.api.Test)2 ConfigurationValidityReport (software.amazon.awssdk.aws.greengrass.model.ConfigurationValidityReport)2 SendConfigurationValidityReportRequest (software.amazon.awssdk.aws.greengrass.model.SendConfigurationValidityReportRequest)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Consumer (java.util.function.Consumer)1 BinaryMessage (software.amazon.awssdk.aws.greengrass.model.BinaryMessage)1 JsonMessage (software.amazon.awssdk.aws.greengrass.model.JsonMessage)1 PublishToTopicResponse (software.amazon.awssdk.aws.greengrass.model.PublishToTopicResponse)1 SubscriptionResponseMessage (software.amazon.awssdk.aws.greengrass.model.SubscriptionResponseMessage)1 StreamEventPublisher (software.amazon.awssdk.eventstreamrpc.StreamEventPublisher)1 EventStreamJsonMessage (software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage)1