use of software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentResponse 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.model.CreateLocalDeploymentResponse in project aws-iot-device-sdk-java-v2 by aws.
the class GreengrassV2ClientTest method before.
@BeforeEach
public void before() throws IOException {
port = randomPort();
try (final EventLoopGroup elGroup = new EventLoopGroup(1);
SocketOptions socketOptions = new SocketOptions()) {
socketOptions.connectTimeoutMs = 3000;
socketOptions.domain = SocketOptions.SocketDomain.IPv4;
socketOptions.type = SocketOptions.SocketType.STREAM;
GreengrassCoreIPCService service = new GreengrassCoreIPCService();
service.setCreateLocalDeploymentHandler((c) -> new GeneratedAbstractCreateLocalDeploymentOperationHandler(c) {
@Override
protected void onStreamClosed() {
}
@Override
public CreateLocalDeploymentResponse handleRequest(CreateLocalDeploymentRequest request) {
return new CreateLocalDeploymentResponse().withDeploymentId("deployment");
}
@Override
public void handleStreamEvent(EventStreamJsonMessage streamRequestEvent) {
}
});
service.setSubscribeToTopicHandler((c) -> new GeneratedAbstractSubscribeToTopicOperationHandler(c) {
@Override
protected void onStreamClosed() {
subscriptionClosed.complete(null);
}
@Override
public SubscribeToTopicResponse handleRequest(SubscribeToTopicRequest request) {
new Thread(() -> {
sendStreamEvent(new SubscriptionResponseMessage().withBinaryMessage(new BinaryMessage().withMessage("message".getBytes(StandardCharsets.UTF_8))));
}).start();
return new SubscribeToTopicResponse().withTopicName(request.getTopic());
}
@Override
public void handleStreamEvent(EventStreamJsonMessage streamRequestEvent) {
}
});
service.setAuthenticationHandler((headers, bytes) -> {
authenticationRequest = new Gson().fromJson(new String(bytes), GreengrassEventStreamConnectMessage.class);
return () -> "connected";
});
service.setAuthorizationHandler(authenticationData -> Authorization.ACCEPT);
ipcServer = new RpcServer(elGroup, socketOptions, null, "127.0.0.1", port, service);
ipcServer.runServer();
client = GreengrassCoreIPCClientV2.builder().withPort(port).withSocketPath("127.0.0.1").withSocketDomain(SocketOptions.SocketDomain.IPv4).withAuthToken("myAuthToken").build();
}
}
use of software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentResponse in project aws-greengrass-cli by aws-greengrass.
the class IPCCliTest method GIVEN_kernel_running_WHEN_create_deployment_after_recipe_update_THEN_kernel_runs_latest_recipe.
@Test
@Order(7)
void GIVEN_kernel_running_WHEN_create_deployment_after_recipe_update_THEN_kernel_runs_latest_recipe(ExtensionContext context) throws Exception {
// updated recipes
Path recipesPath = Paths.get(this.getClass().getResource("recipes").toURI());
// Deployment to add a component
CreateLocalDeploymentRequest createLocalDeploymentRequest = new CreateLocalDeploymentRequest();
createLocalDeploymentRequest.setRootComponentVersionsToAdd(Collections.singletonMap(TEST_SERVICE_NAME, "1.0.1"));
createLocalDeploymentRequest.setRecipeDirectoryPath(recipesPath.toAbsolutePath().toString());
CountDownLatch serviceLatch = waitForServiceToComeInState(TEST_SERVICE_NAME, State.RUNNING, kernel);
CreateLocalDeploymentResponse addComponentDeploymentResponse = clientConnection.createLocalDeployment(createLocalDeploymentRequest, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
String deploymentId1 = addComponentDeploymentResponse.getDeploymentId();
CountDownLatch deploymentLatch = waitForDeploymentToBeSuccessful(deploymentId1, kernel);
assertTrue(serviceLatch.await(SERVICE_STATE_CHECK_TIMEOUT_SECONDS, TimeUnit.SECONDS));
assertTrue(deploymentLatch.await(LOCAL_DEPLOYMENT_TIMEOUT_SECONDS, TimeUnit.SECONDS));
GetComponentDetailsRequest getComponentDetailsRequest = new GetComponentDetailsRequest();
getComponentDetailsRequest.setComponentName(TEST_SERVICE_NAME);
ComponentDetails componentDetails = clientConnection.getComponentDetails(getComponentDetailsRequest, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS).getComponentDetails();
assertEquals("1.0.1", componentDetails.getVersion());
// Deployment to remove a component
createLocalDeploymentRequest = new CreateLocalDeploymentRequest();
createLocalDeploymentRequest.setRootComponentsToRemove(Arrays.asList(TEST_SERVICE_NAME));
serviceLatch = waitForServiceToComeInState(TEST_SERVICE_NAME, State.FINISHED, kernel);
CreateLocalDeploymentResponse deploymentToRemoveComponentResponse = clientConnection.createLocalDeployment(createLocalDeploymentRequest, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
String deploymentId2 = deploymentToRemoveComponentResponse.getDeploymentId();
deploymentLatch = waitForDeploymentToBeSuccessful(deploymentId2, kernel);
assertTrue(serviceLatch.await(SERVICE_STATE_CHECK_TIMEOUT_SECONDS, TimeUnit.SECONDS));
assertTrue(deploymentLatch.await(LOCAL_DEPLOYMENT_TIMEOUT_SECONDS, TimeUnit.SECONDS));
ignoreExceptionOfType(context, ServiceLoadException.class);
GetComponentDetailsRequest getRemovedComponent = new GetComponentDetailsRequest();
getRemovedComponent.setComponentName(TEST_SERVICE_NAME);
ExecutionException executionException = assertThrows(ExecutionException.class, () -> clientConnection.getComponentDetails(getRemovedComponent, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS).getComponentDetails());
assertEquals(ResourceNotFoundError.class, executionException.getCause().getClass());
ListLocalDeploymentsRequest listLocalDeploymentsRequest = new ListLocalDeploymentsRequest();
ListLocalDeploymentsResponse listLocalDeploymentsResponse = clientConnection.listLocalDeployments(listLocalDeploymentsRequest, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
List<String> localDeploymentIds = listLocalDeploymentsResponse.getLocalDeployments().stream().map(ld -> ld.getDeploymentId()).collect(Collectors.toList());
assertThat(localDeploymentIds, containsInAnyOrder(deploymentId1, deploymentId2));
}
use of software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentResponse in project aws-greengrass-cli by aws-greengrass.
the class IPCCliTest method GIVEN_kernel_running_WHEN_change_configuration_and_deployment_THEN_kernel_copies_artifacts_correctly.
@Test
@Order(8)
void GIVEN_kernel_running_WHEN_change_configuration_and_deployment_THEN_kernel_copies_artifacts_correctly(ExtensionContext context) throws Exception {
ignoreExceptionOfType(context, SdkClientException.class);
ignoreExceptionOfType(context, PackageDownloadException.class);
ignoreExceptionOfType(context, ComponentVersionNegotiationException.class);
// updated recipes
Path recipesPath = Paths.get(this.getClass().getResource("recipes").toURI());
Path artifactsPath = Paths.get(this.getClass().getResource("artifacts").toURI());
Map<String, Map<String, Object>> componentToConfiguration;
String update = "{\"Component1\":{\"MERGE\":{\"Message\":\"NewWorld\"}}}";
componentToConfiguration = OBJECT_MAPPER.readValue(update, Map.class);
Map<String, RunWithInfo> componentToRunWithInfo = new HashMap<>();
RunWithInfo runWithInfo = new RunWithInfo();
runWithInfo.setPosixUser("nobody");
componentToRunWithInfo.put("Component1", runWithInfo);
CreateLocalDeploymentRequest createLocalDeploymentRequest = new CreateLocalDeploymentRequest();
createLocalDeploymentRequest.setGroupName("NewGroup");
createLocalDeploymentRequest.setRootComponentVersionsToAdd(Collections.singletonMap("Component1", "1.0.0"));
createLocalDeploymentRequest.setComponentToConfiguration(componentToConfiguration);
createLocalDeploymentRequest.setComponentToRunWithInfo(componentToRunWithInfo);
createLocalDeploymentRequest.setRecipeDirectoryPath(recipesPath.toAbsolutePath().toString());
createLocalDeploymentRequest.setArtifactsDirectoryPath(artifactsPath.toAbsolutePath().toString());
CountDownLatch waitForComponent1ToRun = waitForServiceToComeInState("Component1", State.RUNNING, kernel);
CreateLocalDeploymentResponse addComponentDeploymentResponse = clientConnection.createLocalDeployment(createLocalDeploymentRequest, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
String deploymentId1 = addComponentDeploymentResponse.getDeploymentId();
CountDownLatch waitFordeploymentId1 = waitForDeploymentToBeSuccessful(deploymentId1, kernel);
assertTrue(waitForComponent1ToRun.await(SERVICE_STATE_CHECK_TIMEOUT_SECONDS, TimeUnit.SECONDS));
assertTrue(waitFordeploymentId1.await(LOCAL_DEPLOYMENT_TIMEOUT_SECONDS, TimeUnit.SECONDS));
GetComponentDetailsRequest getComponentDetailsRequest = new GetComponentDetailsRequest();
getComponentDetailsRequest.setComponentName("Component1");
ComponentDetails componentDetails = clientConnection.getComponentDetails(getComponentDetailsRequest, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS).getComponentDetails();
assertEquals("NewWorld", componentDetails.getConfiguration().get("Message"));
Topic posixUser = kernel.getConfig().find(SERVICES_NAMESPACE_TOPIC, "Component1", RUN_WITH_NAMESPACE_TOPIC, "posixUser");
assertEquals("nobody", posixUser.getOnce());
assertTrue(Files.exists(kernel.getNucleusPaths().componentStorePath().resolve(ComponentStore.ARTIFACT_DIRECTORY).resolve("Component1").resolve("1.0.0").resolve("run.sh")));
}
use of software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentResponse in project aws-greengrass-cli by aws-greengrass.
the class IPCCliTest method GIVEN_kernel_running_WHEN_multiple_deployments_scheduled_THEN_all_deployments_succeed.
@Test
@Order(9)
void GIVEN_kernel_running_WHEN_multiple_deployments_scheduled_THEN_all_deployments_succeed(ExtensionContext context) throws Exception {
ignoreExceptionOfType(context, SdkClientException.class);
ignoreExceptionOfType(context, PackageDownloadException.class);
ignoreExceptionOfType(context, ComponentVersionNegotiationException.class);
Path recipesPath = Paths.get(this.getClass().getResource("recipes").toURI());
Path artifactsPath = Paths.get(this.getClass().getResource("artifacts").toURI());
CreateLocalDeploymentRequest createLocalDeploymentRequest = new CreateLocalDeploymentRequest();
createLocalDeploymentRequest.setGroupName("NewGroup");
createLocalDeploymentRequest.setRootComponentVersionsToAdd(Collections.singletonMap("Component1", "1.0.0"));
createLocalDeploymentRequest.setRecipeDirectoryPath(recipesPath.toAbsolutePath().toString());
createLocalDeploymentRequest.setArtifactsDirectoryPath(artifactsPath.toAbsolutePath().toString());
CreateLocalDeploymentResponse firstDeploymentResponse = clientConnection.createLocalDeployment(createLocalDeploymentRequest, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
CountDownLatch waitForFirstDeployment = waitForDeploymentToBeSuccessful(firstDeploymentResponse.getDeploymentId(), kernel);
createLocalDeploymentRequest = new CreateLocalDeploymentRequest();
createLocalDeploymentRequest.setGroupName("NewGroup");
createLocalDeploymentRequest.setRootComponentVersionsToAdd(Collections.singletonMap("ServiceName", "1.0.1"));
createLocalDeploymentRequest.setRecipeDirectoryPath(recipesPath.toAbsolutePath().toString());
createLocalDeploymentRequest.setArtifactsDirectoryPath(artifactsPath.toAbsolutePath().toString());
CreateLocalDeploymentResponse secondDeploymentResponse = clientConnection.createLocalDeployment(createLocalDeploymentRequest, Optional.empty()).getResponse().get(DEFAULT_TIMEOUT_IN_SEC, TimeUnit.SECONDS);
CountDownLatch waitForSecondDeployment = waitForDeploymentToBeSuccessful(secondDeploymentResponse.getDeploymentId(), kernel);
assertTrue(waitForFirstDeployment.await(60, TimeUnit.SECONDS));
assertTrue(waitForSecondDeployment.await(60, TimeUnit.SECONDS));
}
Aggregations