Search in sources :

Example 1 with GreengrassCoreIPCClientV2

use of software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClientV2 in project aws-iot-device-sdk-java-v2 by aws.

the class GreengrassV2ClientTest method testV2Client.

@Test
public void testV2Client() throws InterruptedException, ExecutionException, TimeoutException {
    assertEquals(authenticationRequest.getAuthToken(), "myAuthToken");
    CreateLocalDeploymentResponse depResp = client.createLocalDeployment(new CreateLocalDeploymentRequest());
    assertEquals("deployment", depResp.getDeploymentId());
    CompletableFuture<CreateLocalDeploymentResponse> asyncDepResp = client.createLocalDeploymentAsync(new CreateLocalDeploymentRequest());
    assertEquals("deployment", asyncDepResp.get().getDeploymentId());
    CompletableFuture<String> receivedMessage = new CompletableFuture<>();
    CompletableFuture<String> finalReceivedMessage = receivedMessage;
    GreengrassCoreIPCClientV2.StreamingResponse<SubscribeToTopicResponse, SubscribeToTopicResponseHandler> subResp = client.subscribeToTopic(new SubscribeToTopicRequest().withTopic("abc"), (x) -> {
        if (!Thread.currentThread().getName().contains("pool")) {
            System.out.println(Thread.currentThread().getName());
            finalReceivedMessage.completeExceptionally(new RuntimeException("Ran on event loop instead of executor"));
        }
        finalReceivedMessage.complete(new String(x.getBinaryMessage().getMessage()));
    }, Optional.empty(), Optional.empty());
    assertEquals("message", receivedMessage.get());
    subResp.getHandler().closeStream().get();
    subscriptionClosed.get(1, TimeUnit.SECONDS);
    subscriptionClosed = new CompletableFuture<>();
    receivedMessage = new CompletableFuture<>();
    CompletableFuture<String> finalReceivedMessage1 = receivedMessage;
    subResp = client.subscribeToTopic(new SubscribeToTopicRequest().withTopic("abc"), new StreamResponseHandler<SubscriptionResponseMessage>() {

        @Override
        public void onStreamEvent(SubscriptionResponseMessage streamEvent) {
            if (!Thread.currentThread().getName().contains("pool")) {
                finalReceivedMessage1.completeExceptionally(new RuntimeException("Ran on event loop instead of executor"));
            }
            finalReceivedMessage1.complete(new String(streamEvent.getBinaryMessage().getMessage()));
        }

        @Override
        public boolean onStreamError(Throwable error) {
            return false;
        }

        @Override
        public void onStreamClosed() {
        }
    });
    assertEquals("message", receivedMessage.get());
    subResp.getHandler().closeStream().get();
    subscriptionClosed.get(1, TimeUnit.SECONDS);
    subscriptionClosed = new CompletableFuture<>();
    receivedMessage = new CompletableFuture<>();
    CompletableFuture<String> finalReceivedMessage2 = receivedMessage;
    GreengrassCoreIPCClientV2.StreamingResponse<CompletableFuture<SubscribeToTopicResponse>, SubscribeToTopicResponseHandler> subRespAsync = client.subscribeToTopicAsync(new SubscribeToTopicRequest().withTopic("abc"), new StreamResponseHandler<SubscriptionResponseMessage>() {

        @Override
        public void onStreamEvent(SubscriptionResponseMessage streamEvent) {
            if (!Thread.currentThread().getName().contains("pool")) {
                finalReceivedMessage2.completeExceptionally(new RuntimeException("Ran on event loop instead of executor"));
            }
            finalReceivedMessage2.complete(new String(streamEvent.getBinaryMessage().getMessage()));
        }

        @Override
        public boolean onStreamError(Throwable error) {
            return false;
        }

        @Override
        public void onStreamClosed() {
        }
    });
    assertEquals("message", receivedMessage.get());
    subRespAsync.getHandler().closeStream().get();
    subscriptionClosed.get(1, TimeUnit.SECONDS);
    subscriptionClosed = new CompletableFuture<>();
    receivedMessage = new CompletableFuture<>();
    CompletableFuture<String> finalReceivedMessage3 = receivedMessage;
    subRespAsync = client.subscribeToTopicAsync(new SubscribeToTopicRequest().withTopic("abc"), (x) -> {
        if (!Thread.currentThread().getName().contains("pool")) {
            finalReceivedMessage3.completeExceptionally(new RuntimeException("Ran on event loop instead of executor"));
        }
        finalReceivedMessage3.complete(new String(x.getBinaryMessage().getMessage()));
    }, Optional.empty(), Optional.empty());
    assertEquals("message", receivedMessage.get());
    subRespAsync.getHandler().closeStream().get();
    subscriptionClosed.get(1, TimeUnit.SECONDS);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) GreengrassCoreIPCClientV2(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClientV2) SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) TimeoutException(java.util.concurrent.TimeoutException) BinaryMessage(software.amazon.awssdk.aws.greengrass.model.BinaryMessage) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) GreengrassCoreIPCService(greengrass.GreengrassCoreIPCService) CrtResource(software.amazon.awssdk.crt.CrtResource) Gson(com.google.gson.Gson) GeneratedAbstractCreateLocalDeploymentOperationHandler(greengrass.GeneratedAbstractCreateLocalDeploymentOperationHandler) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) SubscribeToTopicResponseHandler(software.amazon.awssdk.aws.greengrass.SubscribeToTopicResponseHandler) Authorization(software.amazon.awssdk.eventstreamrpc.Authorization) StreamResponseHandler(software.amazon.awssdk.eventstreamrpc.StreamResponseHandler) RpcServer(software.amazon.awssdk.eventstreamrpc.RpcServer) IOException(java.io.IOException) EventStreamJsonMessage(software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) AfterEach(org.junit.jupiter.api.AfterEach) SubscriptionResponseMessage(software.amazon.awssdk.aws.greengrass.model.SubscriptionResponseMessage) CreateLocalDeploymentRequest(software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentRequest) GeneratedAbstractSubscribeToTopicOperationHandler(greengrass.GeneratedAbstractSubscribeToTopicOperationHandler) CreateLocalDeploymentResponse(software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentResponse) Optional(java.util.Optional) SubscribeToTopicRequest(software.amazon.awssdk.aws.greengrass.model.SubscribeToTopicRequest) SubscribeToTopicResponse(software.amazon.awssdk.aws.greengrass.model.SubscribeToTopicResponse) GreengrassEventStreamConnectMessage(software.amazon.awssdk.eventstreamrpc.GreengrassEventStreamConnectMessage) SubscribeToTopicResponseHandler(software.amazon.awssdk.aws.greengrass.SubscribeToTopicResponseHandler) GreengrassCoreIPCClientV2(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClientV2) StreamResponseHandler(software.amazon.awssdk.eventstreamrpc.StreamResponseHandler) CreateLocalDeploymentRequest(software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentRequest) CompletableFuture(java.util.concurrent.CompletableFuture) CreateLocalDeploymentResponse(software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentResponse) SubscriptionResponseMessage(software.amazon.awssdk.aws.greengrass.model.SubscriptionResponseMessage) SubscribeToTopicResponse(software.amazon.awssdk.aws.greengrass.model.SubscribeToTopicResponse) SubscribeToTopicRequest(software.amazon.awssdk.aws.greengrass.model.SubscribeToTopicRequest) Test(org.junit.jupiter.api.Test)

Example 2 with GreengrassCoreIPCClientV2

use of software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClientV2 in project aws-greengrass-cli by aws-greengrass.

the class IPCCliTest method GIVEN_component_running_WHEN_get_component_request_made_THEN_service_details_sent.

@Test
@Order(1)
void GIVEN_component_running_WHEN_get_component_request_made_THEN_service_details_sent() throws Exception {
    GetComponentDetailsRequest request = new GetComponentDetailsRequest();
    request.setComponentName("mqtt");
    ComponentDetails componentDetails = clientConnection.getComponentDetails(request, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS).getComponentDetails();
    assertNotNull(componentDetails);
    assertEquals("1.0.0", componentDetails.getVersion());
    try (EventStreamRPCConnection connection = IPCTestUtils.getEventStreamRpcConnection(kernel, "ServiceName")) {
        GreengrassCoreIPCClientV2 client = GreengrassCoreIPCClientV2.builder().withClient(new GreengrassCoreIPCClient(connection)).build();
        assertEquals(componentDetails, client.getComponentDetails(request).getComponentDetails());
    }
}
Also used : GreengrassCoreIPCClientV2(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClientV2) GreengrassCoreIPCClient(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient) EventStreamRPCConnection(software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection) ComponentDetails(software.amazon.awssdk.aws.greengrass.model.ComponentDetails) GetComponentDetailsRequest(software.amazon.awssdk.aws.greengrass.model.GetComponentDetailsRequest) Order(org.junit.jupiter.api.Order) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Example 3 with GreengrassCoreIPCClientV2

use of software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClientV2 in project aws-greengrass-cli by aws-greengrass.

the class IPCCliTest method GIVEN_kernel_running_WHEN_list_component_request_made_THEN_components_details_sent.

@Test
@Order(4)
void GIVEN_kernel_running_WHEN_list_component_request_made_THEN_components_details_sent() throws Exception {
    ListComponentsRequest request = new ListComponentsRequest();
    ListComponentsResponse listComponentsResponse = clientConnection.listComponents(request, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
    List<String> components = listComponentsResponse.getComponents().stream().map(ComponentDetails::getComponentName).collect(Collectors.toList());
    assertTrue(components.contains("mqtt"));
    assertTrue(components.contains(TEST_SERVICE_NAME));
    assertFalse(components.contains("main"));
    try (EventStreamRPCConnection connection = IPCTestUtils.getEventStreamRpcConnection(kernel, "ServiceName")) {
        GreengrassCoreIPCClientV2 client = GreengrassCoreIPCClientV2.builder().withClient(new GreengrassCoreIPCClient(connection)).build();
        assertEquals(components, client.listComponents(request).getComponents().stream().map(ComponentDetails::getComponentName).collect(Collectors.toList()));
    }
}
Also used : GreengrassCoreIPCClientV2(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClientV2) GreengrassCoreIPCClient(software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient) EventStreamRPCConnection(software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection) ListComponentsResponse(software.amazon.awssdk.aws.greengrass.model.ListComponentsResponse) ComponentDetails(software.amazon.awssdk.aws.greengrass.model.ComponentDetails) ListComponentsRequest(software.amazon.awssdk.aws.greengrass.model.ListComponentsRequest) Order(org.junit.jupiter.api.Order) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)3 GreengrassCoreIPCClientV2 (software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClientV2)3 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)2 Order (org.junit.jupiter.api.Order)2 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)2 GreengrassCoreIPCClient (software.amazon.awssdk.aws.greengrass.GreengrassCoreIPCClient)2 ComponentDetails (software.amazon.awssdk.aws.greengrass.model.ComponentDetails)2 EventStreamRPCConnection (software.amazon.awssdk.eventstreamrpc.EventStreamRPCConnection)2 Gson (com.google.gson.Gson)1 GeneratedAbstractCreateLocalDeploymentOperationHandler (greengrass.GeneratedAbstractCreateLocalDeploymentOperationHandler)1 GeneratedAbstractSubscribeToTopicOperationHandler (greengrass.GeneratedAbstractSubscribeToTopicOperationHandler)1 GreengrassCoreIPCService (greengrass.GreengrassCoreIPCService)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Optional (java.util.Optional)1 Random (java.util.Random)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1