Search in sources :

Example 1 with PublishMessage

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

the class IPCTestUtils method publishToTopicOverIpcAsBinaryMessage.

public static void publishToTopicOverIpcAsBinaryMessage(GreengrassCoreIPCClient ipcClient, String topic, String message) throws InterruptedException, ExecutionException, TimeoutException {
    PublishToTopicRequest publishToTopicRequest = new PublishToTopicRequest();
    publishToTopicRequest.setTopic(topic);
    PublishMessage publishMessage = new PublishMessage();
    BinaryMessage binaryMessage = new BinaryMessage();
    binaryMessage.setMessage(message.getBytes(StandardCharsets.UTF_8));
    publishMessage.setBinaryMessage(binaryMessage);
    publishToTopicRequest.setPublishMessage(publishMessage);
    ipcClient.publishToTopic(publishToTopicRequest, Optional.empty()).getResponse().get(5, TimeUnit.SECONDS);
}
Also used : PublishMessage(software.amazon.awssdk.aws.greengrass.model.PublishMessage) PublishToTopicRequest(software.amazon.awssdk.aws.greengrass.model.PublishToTopicRequest) BinaryMessage(software.amazon.awssdk.aws.greengrass.model.BinaryMessage)

Example 2 with PublishMessage

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

the class PubSubIPCEventStreamAgentTest method GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_many_binary_message_THEN_publishes_message_inorder.

@Test
void GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_many_binary_message_THEN_publishes_message_inorder() throws InterruptedException, AuthorizationException {
    StreamEventPublisher publisher = mock(StreamEventPublisher.class);
    Set<Object> set = new HashSet<>();
    set.add(publisher);
    pubSubIPCEventStreamAgent.getListeners().add(TEST_TOPIC, set);
    when(publisher.sendStreamEvent(subscriptionResponseMessageCaptor.capture())).thenReturn(new CompletableFuture());
    List<PublishToTopicRequest> publishToTopicRequests = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        PublishToTopicRequest publishToTopicRequest = new PublishToTopicRequest();
        publishToTopicRequest.setTopic(TEST_TOPIC);
        PublishMessage publishMessage = new PublishMessage();
        BinaryMessage binaryMessage = new BinaryMessage();
        binaryMessage.setMessage(String.valueOf(i).getBytes());
        publishMessage.setBinaryMessage(binaryMessage);
        publishToTopicRequest.setPublishMessage(publishMessage);
        publishToTopicRequests.add(publishToTopicRequest);
    }
    try (PubSubIPCEventStreamAgent.PublishToTopicOperationHandler publishToTopicHandler = pubSubIPCEventStreamAgent.getPublishToTopicHandler(mockContext)) {
        for (PublishToTopicRequest publishToTopicRequest : publishToTopicRequests) {
            PublishToTopicResponse publishToTopicResponse = publishToTopicHandler.handleRequest(publishToTopicRequest);
            assertNotNull(publishToTopicResponse);
        }
        verify(authorizationHandler, times(10)).isAuthorized(eq(PUB_SUB_SERVICE_NAME), permissionArgumentCaptor.capture(), eq(ResourceLookupPolicy.MQTT_STYLE));
        Permission capturedPermission = permissionArgumentCaptor.getValue();
        assertThat(capturedPermission.getOperation(), is(GreengrassCoreIPCService.PUBLISH_TO_TOPIC));
        assertThat(capturedPermission.getPrincipal(), is(TEST_SERVICE));
        assertThat(capturedPermission.getResource(), is(TEST_TOPIC));
        TimeUnit.SECONDS.sleep(2);
        assertNotNull(subscriptionResponseMessageCaptor.getAllValues());
        assertEquals(10, subscriptionResponseMessageCaptor.getAllValues().size());
        int i = 0;
        for (SubscriptionResponseMessage responseMessage : subscriptionResponseMessageCaptor.getAllValues()) {
            assertNull(responseMessage.getJsonMessage());
            assertNotNull(responseMessage.getBinaryMessage());
            assertEquals(String.valueOf(i), new String(responseMessage.getBinaryMessage().getMessage()));
            assertEquals(TEST_TOPIC, responseMessage.getBinaryMessage().getEventTopic());
            i++;
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) BinaryMessage(software.amazon.awssdk.aws.greengrass.model.BinaryMessage) PublishMessage(software.amazon.awssdk.aws.greengrass.model.PublishMessage) CompletableFuture(java.util.concurrent.CompletableFuture) PublishToTopicResponse(software.amazon.awssdk.aws.greengrass.model.PublishToTopicResponse) SubscriptionResponseMessage(software.amazon.awssdk.aws.greengrass.model.SubscriptionResponseMessage) Permission(com.aws.greengrass.authorization.Permission) StreamEventPublisher(software.amazon.awssdk.eventstreamrpc.StreamEventPublisher) PublishToTopicRequest(software.amazon.awssdk.aws.greengrass.model.PublishToTopicRequest) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with PublishMessage

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

the class PubSubIPCEventStreamAgentTest method GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_many_json_message_THEN_publishes_message_inorder.

@Test
void GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_many_json_message_THEN_publishes_message_inorder() throws InterruptedException, AuthorizationException {
    StreamEventPublisher publisher = mock(StreamEventPublisher.class);
    Set<Object> set = new HashSet<>();
    set.add(publisher);
    pubSubIPCEventStreamAgent.getListeners().add(TEST_TOPIC, set);
    when(publisher.sendStreamEvent(subscriptionResponseMessageCaptor.capture())).thenReturn(new CompletableFuture());
    List<PublishToTopicRequest> publishToTopicRequests = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        PublishToTopicRequest publishToTopicRequest = new PublishToTopicRequest();
        publishToTopicRequest.setTopic(TEST_TOPIC);
        PublishMessage publishMessage = new PublishMessage();
        JsonMessage jsonMessage = new JsonMessage();
        Map<String, Object> message = new HashMap<>();
        message.putIfAbsent("SomeKey", i);
        jsonMessage.setMessage(message);
        publishMessage.setJsonMessage(jsonMessage);
        publishToTopicRequest.setPublishMessage(publishMessage);
        publishToTopicRequests.add(publishToTopicRequest);
    }
    try (PubSubIPCEventStreamAgent.PublishToTopicOperationHandler publishToTopicHandler = pubSubIPCEventStreamAgent.getPublishToTopicHandler(mockContext)) {
        for (PublishToTopicRequest publishToTopicRequest : publishToTopicRequests) {
            PublishToTopicResponse publishToTopicResponse = publishToTopicHandler.handleRequest(publishToTopicRequest);
            assertNotNull(publishToTopicResponse);
        }
        verify(authorizationHandler, times(10)).isAuthorized(eq(PUB_SUB_SERVICE_NAME), permissionArgumentCaptor.capture(), eq(ResourceLookupPolicy.MQTT_STYLE));
        Permission capturedPermission = permissionArgumentCaptor.getValue();
        assertThat(capturedPermission.getOperation(), is(GreengrassCoreIPCService.PUBLISH_TO_TOPIC));
        assertThat(capturedPermission.getPrincipal(), is(TEST_SERVICE));
        assertThat(capturedPermission.getResource(), is(TEST_TOPIC));
        TimeUnit.SECONDS.sleep(2);
        assertNotNull(subscriptionResponseMessageCaptor.getAllValues());
        assertEquals(10, subscriptionResponseMessageCaptor.getAllValues().size());
        int i = 0;
        for (SubscriptionResponseMessage responseMessage : subscriptionResponseMessageCaptor.getAllValues()) {
            assertNotNull(responseMessage.getJsonMessage());
            assertNull(responseMessage.getBinaryMessage());
            assertThat(responseMessage.getJsonMessage().getMessage(), IsMapContaining.hasEntry("SomeKey", i));
            assertEquals(TEST_TOPIC, responseMessage.getJsonMessage().getEventTopic());
            i++;
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) PublishMessage(software.amazon.awssdk.aws.greengrass.model.PublishMessage) CompletableFuture(java.util.concurrent.CompletableFuture) PublishToTopicResponse(software.amazon.awssdk.aws.greengrass.model.PublishToTopicResponse) SubscriptionResponseMessage(software.amazon.awssdk.aws.greengrass.model.SubscriptionResponseMessage) JsonMessage(software.amazon.awssdk.aws.greengrass.model.JsonMessage) Permission(com.aws.greengrass.authorization.Permission) StreamEventPublisher(software.amazon.awssdk.eventstreamrpc.StreamEventPublisher) PublishToTopicRequest(software.amazon.awssdk.aws.greengrass.model.PublishToTopicRequest) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with PublishMessage

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

the class PubSubIPCEventStreamAgentTest method GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_binary_message_THEN_publishes_message.

@Test
void GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_binary_message_THEN_publishes_message() throws InterruptedException, AuthorizationException {
    StreamEventPublisher publisher = mock(StreamEventPublisher.class);
    Set<Object> set = new HashSet<>();
    set.add(publisher);
    pubSubIPCEventStreamAgent.getListeners().add(TEST_TOPIC, set);
    when(publisher.sendStreamEvent(subscriptionResponseMessageCaptor.capture())).thenReturn(new CompletableFuture());
    PublishToTopicRequest publishToTopicRequest = new PublishToTopicRequest();
    publishToTopicRequest.setTopic(TEST_TOPIC);
    PublishMessage publishMessage = new PublishMessage();
    BinaryMessage binaryMessage = new BinaryMessage();
    binaryMessage.setMessage("ABCD".getBytes());
    publishMessage.setBinaryMessage(binaryMessage);
    publishToTopicRequest.setPublishMessage(publishMessage);
    try (PubSubIPCEventStreamAgent.PublishToTopicOperationHandler publishToTopicHandler = pubSubIPCEventStreamAgent.getPublishToTopicHandler(mockContext)) {
        PublishToTopicResponse publishToTopicResponse = publishToTopicHandler.handleRequest(publishToTopicRequest);
        assertNotNull(publishToTopicResponse);
        verify(authorizationHandler).isAuthorized(eq(PUB_SUB_SERVICE_NAME), permissionArgumentCaptor.capture(), eq(ResourceLookupPolicy.MQTT_STYLE));
        Permission capturedPermission = permissionArgumentCaptor.getValue();
        assertThat(capturedPermission.getOperation(), is(GreengrassCoreIPCService.PUBLISH_TO_TOPIC));
        assertThat(capturedPermission.getPrincipal(), is(TEST_SERVICE));
        assertThat(capturedPermission.getResource(), is(TEST_TOPIC));
        TimeUnit.SECONDS.sleep(2);
        assertNotNull(subscriptionResponseMessageCaptor.getValue());
        SubscriptionResponseMessage message = subscriptionResponseMessageCaptor.getValue();
        assertNull(message.getJsonMessage());
        assertNotNull(message.getBinaryMessage());
        assertEquals("ABCD", new String(message.getBinaryMessage().getMessage()));
        assertEquals(TEST_TOPIC, message.getBinaryMessage().getEventTopic());
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) BinaryMessage(software.amazon.awssdk.aws.greengrass.model.BinaryMessage) PublishMessage(software.amazon.awssdk.aws.greengrass.model.PublishMessage) CompletableFuture(java.util.concurrent.CompletableFuture) PublishToTopicResponse(software.amazon.awssdk.aws.greengrass.model.PublishToTopicResponse) SubscriptionResponseMessage(software.amazon.awssdk.aws.greengrass.model.SubscriptionResponseMessage) Permission(com.aws.greengrass.authorization.Permission) StreamEventPublisher(software.amazon.awssdk.eventstreamrpc.StreamEventPublisher) PublishToTopicRequest(software.amazon.awssdk.aws.greengrass.model.PublishToTopicRequest) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 5 with PublishMessage

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

the class PubSubIPCEventStreamAgentTest method GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_json_message_THEN_publishes_message.

@Test
void GIVEN_subscribed_to_topic_from_all_sources_WHEN_publish_json_message_THEN_publishes_message() throws InterruptedException, AuthorizationException {
    StreamEventPublisher publisher = mock(StreamEventPublisher.class);
    Set<Object> set = new HashSet<>();
    set.add(publisher);
    pubSubIPCEventStreamAgent.getListeners().add(TEST_TOPIC, set);
    when(publisher.sendStreamEvent(subscriptionResponseMessageCaptor.capture())).thenReturn(new CompletableFuture());
    PublishToTopicRequest publishToTopicRequest = new PublishToTopicRequest();
    publishToTopicRequest.setTopic(TEST_TOPIC);
    PublishMessage publishMessage = new PublishMessage();
    JsonMessage jsonMessage = new JsonMessage();
    Map<String, Object> message = new HashMap<>();
    message.putIfAbsent("SomeKey", "SomValue");
    jsonMessage.setMessage(message);
    publishMessage.setJsonMessage(jsonMessage);
    publishToTopicRequest.setPublishMessage(publishMessage);
    try (PubSubIPCEventStreamAgent.PublishToTopicOperationHandler publishToTopicHandler = pubSubIPCEventStreamAgent.getPublishToTopicHandler(mockContext)) {
        PublishToTopicResponse publishToTopicResponse = publishToTopicHandler.handleRequest(publishToTopicRequest);
        assertNotNull(publishToTopicResponse);
        verify(authorizationHandler).isAuthorized(eq(PUB_SUB_SERVICE_NAME), permissionArgumentCaptor.capture(), eq(ResourceLookupPolicy.MQTT_STYLE));
        Permission capturedPermission = permissionArgumentCaptor.getValue();
        assertThat(capturedPermission.getOperation(), is(GreengrassCoreIPCService.PUBLISH_TO_TOPIC));
        assertThat(capturedPermission.getPrincipal(), is(TEST_SERVICE));
        assertThat(capturedPermission.getResource(), is(TEST_TOPIC));
        TimeUnit.SECONDS.sleep(2);
        assertNotNull(subscriptionResponseMessageCaptor.getValue());
        SubscriptionResponseMessage responseMessage = subscriptionResponseMessageCaptor.getValue();
        assertNotNull(responseMessage.getJsonMessage());
        assertNull(responseMessage.getBinaryMessage());
        assertThat(responseMessage.getJsonMessage().getMessage(), IsMapContaining.hasEntry("SomeKey", "SomValue"));
        assertEquals(TEST_TOPIC, responseMessage.getJsonMessage().getEventTopic());
    }
}
Also used : HashMap(java.util.HashMap) Matchers.containsString(org.hamcrest.Matchers.containsString) PublishMessage(software.amazon.awssdk.aws.greengrass.model.PublishMessage) CompletableFuture(java.util.concurrent.CompletableFuture) PublishToTopicResponse(software.amazon.awssdk.aws.greengrass.model.PublishToTopicResponse) SubscriptionResponseMessage(software.amazon.awssdk.aws.greengrass.model.SubscriptionResponseMessage) JsonMessage(software.amazon.awssdk.aws.greengrass.model.JsonMessage) Permission(com.aws.greengrass.authorization.Permission) StreamEventPublisher(software.amazon.awssdk.eventstreamrpc.StreamEventPublisher) PublishToTopicRequest(software.amazon.awssdk.aws.greengrass.model.PublishToTopicRequest) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

PublishMessage (software.amazon.awssdk.aws.greengrass.model.PublishMessage)7 PublishToTopicRequest (software.amazon.awssdk.aws.greengrass.model.PublishToTopicRequest)7 Permission (com.aws.greengrass.authorization.Permission)6 HashSet (java.util.HashSet)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 Test (org.junit.jupiter.api.Test)6 PublishToTopicResponse (software.amazon.awssdk.aws.greengrass.model.PublishToTopicResponse)6 SubscriptionResponseMessage (software.amazon.awssdk.aws.greengrass.model.SubscriptionResponseMessage)6 StreamEventPublisher (software.amazon.awssdk.eventstreamrpc.StreamEventPublisher)6 BinaryMessage (software.amazon.awssdk.aws.greengrass.model.BinaryMessage)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)2 JsonMessage (software.amazon.awssdk.aws.greengrass.model.JsonMessage)2