Search in sources :

Example 1 with CreateLocalDeploymentRequest

use of software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentRequest 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 CreateLocalDeploymentRequest

use of software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentRequest 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();
    }
}
Also used : SocketOptions(software.amazon.awssdk.crt.io.SocketOptions) Gson(com.google.gson.Gson) EventStreamJsonMessage(software.amazon.awssdk.eventstreamrpc.model.EventStreamJsonMessage) BinaryMessage(software.amazon.awssdk.aws.greengrass.model.BinaryMessage) CreateLocalDeploymentRequest(software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentRequest) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) CreateLocalDeploymentResponse(software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentResponse) SubscriptionResponseMessage(software.amazon.awssdk.aws.greengrass.model.SubscriptionResponseMessage) GeneratedAbstractCreateLocalDeploymentOperationHandler(greengrass.GeneratedAbstractCreateLocalDeploymentOperationHandler) RpcServer(software.amazon.awssdk.eventstreamrpc.RpcServer) GreengrassCoreIPCService(greengrass.GreengrassCoreIPCService) GeneratedAbstractSubscribeToTopicOperationHandler(greengrass.GeneratedAbstractSubscribeToTopicOperationHandler) SubscribeToTopicResponse(software.amazon.awssdk.aws.greengrass.model.SubscribeToTopicResponse) GreengrassEventStreamConnectMessage(software.amazon.awssdk.eventstreamrpc.GreengrassEventStreamConnectMessage) SubscribeToTopicRequest(software.amazon.awssdk.aws.greengrass.model.SubscribeToTopicRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with CreateLocalDeploymentRequest

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

the class DeploymentCommandTest method GIVEN_WHEN_recipe_dir_is_provided_THEN_request_contains_provided_recipe_dir.

@Test
void GIVEN_WHEN_recipe_dir_is_provided_THEN_request_contains_provided_recipe_dir() {
    int exitCode = runCommandLine("deployment", "create", "--recipeDir", RECIPE_FOLDER_PATH_STR);
    CreateLocalDeploymentRequest request = new CreateLocalDeploymentRequest();
    request.setRecipeDirectoryPath(deTilde(RECIPE_FOLDER_PATH_STR).get().toString());
    verify(nucleusAdapteripc).createLocalDeployment(request);
    assertThat(exitCode, is(0));
}
Also used : CreateLocalDeploymentRequest(software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentRequest) Test(org.junit.jupiter.api.Test)

Example 4 with CreateLocalDeploymentRequest

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

the class DeploymentCommandTest method GIVEN_WHEN_configs_are_provided_THEN_request_contain_all_config.

@Test
void GIVEN_WHEN_configs_are_provided_THEN_request_contain_all_config() throws JsonProcessingException {
    String updateConfigString = "{ \"Component1\": { \"MERGE\": { \"Company\": { \"Office\": { \"temperature\": 22 } }, \"path1\": { \"Object2\": { \"key2\": \"val2\" } } } }, \"Component2\": { \"RESET\": [ \"/secret/first\" ] } }";
    int exitCode = runCommandLine("deployment", "create", "--update-config", updateConfigString);
    Map<String, Map<String, Object>> componentNameToConfig = mapper.readValue(updateConfigString, Map.class);
    CreateLocalDeploymentRequest request = new CreateLocalDeploymentRequest();
    request.setComponentToConfiguration(componentNameToConfig);
    verify(nucleusAdapteripc).createLocalDeployment(request);
    assertThat(exitCode, is(0));
}
Also used : CreateLocalDeploymentRequest(software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentRequest) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.jupiter.api.Test)

Example 5 with CreateLocalDeploymentRequest

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

the class DeploymentCommandTest method GIVEN_WHEN_components_runwith_provided_THEN_request_contains_the_info.

@Test
void GIVEN_WHEN_components_runwith_provided_THEN_request_contains_the_info() throws Exception {
    int exitCode = runCommandLine("deployment", "create", "--runWith", "Component1:windowsUser=foobar", "--runWith", "Component2:windowsUser=foobar", "--runWith", "Component2:posixUser=1234", "--systemLimits", Paths.get(this.getClass().getResource("resource_limits.json").toURI()).toString());
    Map<String, RunWithInfo> componentToRunWithInfo = new HashMap<>();
    RunWithInfo runWithInfo = new RunWithInfo();
    runWithInfo.setWindowsUser("foobar");
    MapType mapType = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, SystemResourceLimits.class);
    Map<String, SystemResourceLimits> systemResourceLimits = mapper.readValue(getClass().getResource("resource_limits.json"), mapType);
    runWithInfo.setSystemResourceLimits(systemResourceLimits.get("Component1"));
    componentToRunWithInfo.put("Component1", runWithInfo);
    runWithInfo = new RunWithInfo();
    runWithInfo.setPosixUser("1234");
    runWithInfo.setWindowsUser("foobar");
    componentToRunWithInfo.put("Component2", runWithInfo);
    CreateLocalDeploymentRequest request = new CreateLocalDeploymentRequest();
    request.setComponentToRunWithInfo(componentToRunWithInfo);
    verify(nucleusAdapteripc).createLocalDeployment(request);
    assertThat(exitCode, is(0));
}
Also used : CreateLocalDeploymentRequest(software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentRequest) RunWithInfo(software.amazon.awssdk.aws.greengrass.model.RunWithInfo) HashMap(java.util.HashMap) SystemResourceLimits(software.amazon.awssdk.aws.greengrass.model.SystemResourceLimits) MapType(com.fasterxml.jackson.databind.type.MapType) Test(org.junit.jupiter.api.Test)

Aggregations

CreateLocalDeploymentRequest (software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentRequest)16 Test (org.junit.jupiter.api.Test)14 HashMap (java.util.HashMap)6 Map (java.util.Map)5 CreateLocalDeploymentResponse (software.amazon.awssdk.aws.greengrass.model.CreateLocalDeploymentResponse)5 Path (java.nio.file.Path)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 Order (org.junit.jupiter.api.Order)3 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)3 RunWithInfo (software.amazon.awssdk.aws.greengrass.model.RunWithInfo)3 Topic (com.aws.greengrass.config.Topic)2 Topics (com.aws.greengrass.config.Topics)2 MapType (com.fasterxml.jackson.databind.type.MapType)2 Gson (com.google.gson.Gson)2 GeneratedAbstractCreateLocalDeploymentOperationHandler (greengrass.GeneratedAbstractCreateLocalDeploymentOperationHandler)2 GeneratedAbstractSubscribeToTopicOperationHandler (greengrass.GeneratedAbstractSubscribeToTopicOperationHandler)2 GreengrassCoreIPCService (greengrass.GreengrassCoreIPCService)2 IOException (java.io.IOException)2