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);
}
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());
}
}
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()));
}
}
Aggregations