From 57b4aedf1740df2fc368c8d1c6521d471bfd1567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Herrera=20de=20la=20Garza?= Date: Wed, 8 Jan 2020 23:27:32 -0600 Subject: [PATCH 1/8] #26 Add Hello World Integration Testing working on Windows, need work to work on MAC and Linux --- sdk/pom.xml | 11 +++ .../dapr/it/DaprIntegrationTestingRunner.java | 87 +++++++++++++++++++ .../services/HelloWorldGrpcStateService.java | 44 ++++++++++ .../io/dapr/it/state/HelloWorldClientIT.java | 86 ++++++++++++++++++ 4 files changed, 228 insertions(+) create mode 100644 sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java create mode 100644 sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java create mode 100644 sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java diff --git a/sdk/pom.xml b/sdk/pom.xml index 43259eae50..beecc2a06f 100644 --- a/sdk/pom.xml +++ b/sdk/pom.xml @@ -112,11 +112,22 @@ + verify + verify integration-test verify + + integration-test + integration-test + + integration-test + verify + + + diff --git a/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java b/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java new file mode 100644 index 0000000000..9df8abca95 --- /dev/null +++ b/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java @@ -0,0 +1,87 @@ +package io.dapr.it; + +import org.junit.Assert; + +import java.io.*; +import java.util.UUID; +import java.util.concurrent.*; + + +public class DaprIntegrationTestingRunner { + + private Runtime rt = Runtime.getRuntime(); + private Process proc; + + private String successMessage; + private Class serviceClass; + private Boolean isGrpc; + private int port; + private int sleepTime; + private String appName; + + private DaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean isGrpc, int port, int sleepTime) { + this.successMessage = successMessage; + this.serviceClass = serviceClass; + this.isGrpc = isGrpc; + this.port = port; + this.sleepTime = sleepTime; + this.generateAppName(); + } + + public static DaprIntegrationTestingRunner createDaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean isGrpc, int port, int sleepTime) { + return new DaprIntegrationTestingRunner(successMessage, serviceClass, isGrpc, port, sleepTime); + } + + + public void initializeDapr() throws IOException, InterruptedException, TimeoutException, ExecutionException { + + File file= new File("../"); + proc= rt.exec(this.buildDaprCommand(), null,file); + InputStream stdin = proc.getInputStream(); + InputStreamReader isr = new InputStreamReader(stdin); + BufferedReader br = new BufferedReader(isr); + + final Runnable stuffToDo = new Thread(() -> { + try { + String line ; + while ((line = br.readLine()) != null) { + System.out.println(line); + if (line.contains(successMessage)) { + break; + } + } + }catch(IOException ex){ + Assert.fail(ex.getMessage()); + } + }); + + final ExecutorService executor = Executors.newSingleThreadExecutor(); + final Future future = executor.submit(stuffToDo); + executor.shutdown(); // This does not cancel the already-scheduled task. + future.get(2, TimeUnit.MINUTES); + Thread.sleep(sleepTime); + + } + + private static final String DAPR_RUN = "dapr run --app-id %s "; + private static final String DAPR_COMMAND = " -- mvn exec:java -pl=sdk -D exec.mainClass=%s -D exec.classpathScope=\"test\""; + + private String buildDaprCommand(){ + StringBuilder stringBuilder= new StringBuilder(String.format(DAPR_RUN,this.appName)) + .append(this.isGrpc ? " --grpc-port ": " --app-port ") + .append(this.port) + .append(String.format(DAPR_COMMAND, this.serviceClass.getCanonicalName())); + return stringBuilder.toString(); + } + + private void generateAppName(){ + + this.appName=UUID.randomUUID().toString(); + } + + public void destroyDapr() throws IOException { + rt.exec("dapr stop --app-id " + this.appName); + proc.destroy(); + + } +} diff --git a/sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java b/sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java new file mode 100644 index 0000000000..36f2d5efa2 --- /dev/null +++ b/sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java @@ -0,0 +1,44 @@ +package io.dapr.it.services; + +import com.google.protobuf.Any; +import com.google.protobuf.ByteString; +import io.dapr.DaprGrpc; +import io.dapr.DaprGrpc.DaprBlockingStub; +import io.dapr.DaprProtos.SaveStateEnvelope; +import io.dapr.DaprProtos.StateRequest; +import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; + +/** + * Simple example, to run: + * mvn clean install + * dapr run --grpc-port 50001 -- mvn exec:java -pl=examples -Dexec.mainClass=io.dapr.examples.Example + */ +public class HelloWorldGrpcStateService { + + public static void main(String[] args) { + ManagedChannel channel = + ManagedChannelBuilder.forAddress("localhost", 50001).usePlaintext().build(); + DaprBlockingStub client = DaprGrpc.newBlockingStub(channel); + + String key = "mykey"; + // First, write key-value pair. + + String value = "Hello World"; + StateRequest req = StateRequest + .newBuilder() + .setKey(key) + .setValue(Any.newBuilder().setValue(ByteString.copyFromUtf8(value)).build()) + .build(); + SaveStateEnvelope state = SaveStateEnvelope.newBuilder() + .addRequests(req) + .build(); + client.saveState(state); + System.out.println("Saved!"); + channel.shutdown(); + + + + + } +} diff --git a/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java b/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java new file mode 100644 index 0000000000..71c0705a59 --- /dev/null +++ b/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java @@ -0,0 +1,86 @@ +package io.dapr.it.state; + +import io.dapr.DaprGrpc; +import io.dapr.DaprProtos; +import io.dapr.it.DaprIntegrationTestingRunner; +import io.dapr.it.services.HelloWorldGrpcStateService; +import io.grpc.ManagedChannel; +import io.grpc.ManagedChannelBuilder; +import org.junit.*; + + +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; + + +public class HelloWorldClientIT { + + + private static DaprIntegrationTestingRunner daprIntegrationTestingRunner; + + @BeforeClass + public static void init() throws IOException, InterruptedException, TimeoutException, ExecutionException { + daprIntegrationTestingRunner = + DaprIntegrationTestingRunner.createDaprIntegrationTestingRunner( + "BUILD SUCCESS", + HelloWorldGrpcStateService.class, + true, + 50001, + 2000 + ); + daprIntegrationTestingRunner.initializeDapr(); + } + + @Test + public void testHelloWorldState(){ + ManagedChannel channel = + ManagedChannelBuilder.forAddress("localhost", 50001).usePlaintext().build(); + DaprGrpc.DaprBlockingStub client = DaprGrpc.newBlockingStub(channel); + + String key = "mykey"; + { + DaprProtos.GetStateEnvelope req = DaprProtos.GetStateEnvelope + .newBuilder() + .setKey(key) + .build(); + DaprProtos.GetStateResponseEnvelope response = client.getState(req); + String value = response.getData().getValue().toStringUtf8(); + System.out.println("Got: " + value); + Assert.assertEquals("Hello World",value); + } + + // Then, delete it. + { + DaprProtos.DeleteStateEnvelope req = DaprProtos.DeleteStateEnvelope + .newBuilder() + .setKey(key) + .build(); + client.deleteState(req); + System.out.println("Deleted!"); + } + + { + DaprProtos.GetStateEnvelope req = DaprProtos.GetStateEnvelope + .newBuilder() + .setKey(key) + .build(); + DaprProtos.GetStateResponseEnvelope response = client.getState(req); + String value = response.getData().getValue().toStringUtf8(); + System.out.println("Got: " + value); + Assert.assertEquals("",value); + } + } + + @Test + public void test2(){ + + } + + @AfterClass + public static void cleanUp() throws IOException { + daprIntegrationTestingRunner.destroyDapr(); + + } + +} From e1e18b0127b1eae6d69eb620d394e0b2ed61d69b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Herrera=20de=20la=20Garza?= Date: Fri, 10 Jan 2020 00:01:42 -0600 Subject: [PATCH 2/8] #26 Add new Integration Test to test DAPR state functionality --- .../actors/client/ActorProxyImplTest.java | 8 +- sdk-springboot/pom.xml | 1 + .../dapr/it/DaprIntegrationTestingRunner.java | 35 +++--- .../io/dapr/it/services/EmptyService.java | 16 +++ .../services/HelloWorldGrpcStateService.java | 50 ++++---- .../io/dapr/it/state/HelloWorldClientIT.java | 10 +- .../io/dapr/it/state/HttpStateClientIT.java | 113 ++++++++++++++++++ 7 files changed, 186 insertions(+), 47 deletions(-) create mode 100644 sdk/src/test/java/io/dapr/it/services/EmptyService.java create mode 100644 sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java diff --git a/sdk-actors/src/test/java/io/dapr/actors/client/ActorProxyImplTest.java b/sdk-actors/src/test/java/io/dapr/actors/client/ActorProxyImplTest.java index 6cc7c74e88..b81d2f5e60 100644 --- a/sdk-actors/src/test/java/io/dapr/actors/client/ActorProxyImplTest.java +++ b/sdk-actors/src/test/java/io/dapr/actors/client/ActorProxyImplTest.java @@ -27,8 +27,7 @@ public void constructorActorProxyTest() { Assert.assertEquals(actorProxy.getActorType(), "myActorType"); } - //@Test() - // TODO: review this test. + @Test() public void invokeActorMethodWithoutDataWithReturnType() { final DaprClient daprClient = mock(DaprClient.class); when(daprClient.invokeActorMethod(anyString(), anyString(), anyString(), Mockito.isNull())) @@ -88,8 +87,7 @@ public void invokeActorMethodWithIncorrectReturnType() { } - //@Test() - // TODO: review this test. + @Test() public void invokeActorMethodSavingDataWithReturnType() { final DaprClient daprClient = mock(DaprClient.class); when(daprClient.invokeActorMethod(anyString(), anyString(), anyString(), Mockito.isNotNull())) @@ -262,7 +260,7 @@ public String getPropertyB() { return propertyB; } - public void setPropertyB(String propActorProxyBuilderTestertyB) { + public void setPropertyB(String propertyB) { this.propertyB = propertyB; } diff --git a/sdk-springboot/pom.xml b/sdk-springboot/pom.xml index 75f03f2824..af3aaab7e5 100644 --- a/sdk-springboot/pom.xml +++ b/sdk-springboot/pom.xml @@ -63,6 +63,7 @@ org.springframework.boot spring-boot-maven-plugin + 2.2.2.RELEASE diff --git a/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java b/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java index 9df8abca95..46df9fa78a 100644 --- a/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java +++ b/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java @@ -12,7 +12,7 @@ public class DaprIntegrationTestingRunner { private Runtime rt = Runtime.getRuntime(); private Process proc; - private String successMessage; + private String successMessage; private Class serviceClass; private Boolean isGrpc; private int port; @@ -35,22 +35,28 @@ public static DaprIntegrationTestingRunner createDaprIntegrationTestingRunner(St public void initializeDapr() throws IOException, InterruptedException, TimeoutException, ExecutionException { - File file= new File("../"); - proc= rt.exec(this.buildDaprCommand(), null,file); - InputStream stdin = proc.getInputStream(); - InputStreamReader isr = new InputStreamReader(stdin); - BufferedReader br = new BufferedReader(isr); + String daprCommand=this.buildDaprCommand(); + System.out.println(daprCommand); + proc= rt.exec(daprCommand); + final Runnable stuffToDo = new Thread(() -> { try { - String line ; - while ((line = br.readLine()) != null) { - System.out.println(line); - if (line.contains(successMessage)) { - break; + try (InputStream stdin = proc.getInputStream()) { + try(InputStreamReader isr = new InputStreamReader(stdin)) { + try (BufferedReader br = new BufferedReader(isr)){ + String line; + while ((line = br.readLine()) != null) { + System.out.println(line); + if (line.contains(successMessage)) { + break; + } + } + } } + } - }catch(IOException ex){ + } catch (IOException ex) { Assert.fail(ex.getMessage()); } }); @@ -64,11 +70,11 @@ public void initializeDapr() throws IOException, InterruptedException, TimeoutEx } private static final String DAPR_RUN = "dapr run --app-id %s "; - private static final String DAPR_COMMAND = " -- mvn exec:java -pl=sdk -D exec.mainClass=%s -D exec.classpathScope=\"test\""; + private static final String DAPR_COMMAND = " -- mvn exec:java -D exec.mainClass=%s -D exec.classpathScope=\"test\""; private String buildDaprCommand(){ StringBuilder stringBuilder= new StringBuilder(String.format(DAPR_RUN,this.appName)) - .append(this.isGrpc ? " --grpc-port ": " --app-port ") + .append(this.isGrpc ? " --grpc-port ": " --port ") .append(this.port) .append(String.format(DAPR_COMMAND, this.serviceClass.getCanonicalName())); return stringBuilder.toString(); @@ -82,6 +88,5 @@ private void generateAppName(){ public void destroyDapr() throws IOException { rt.exec("dapr stop --app-id " + this.appName); proc.destroy(); - } } diff --git a/sdk/src/test/java/io/dapr/it/services/EmptyService.java b/sdk/src/test/java/io/dapr/it/services/EmptyService.java new file mode 100644 index 0000000000..d43c45d891 --- /dev/null +++ b/sdk/src/test/java/io/dapr/it/services/EmptyService.java @@ -0,0 +1,16 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + +package io.dapr.it.services; + + +/** + * Use this class in order to run DAPR with any needed services, like states. + */ +public class EmptyService { + public static void main(String[] args) { + + } +} diff --git a/sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java b/sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java index 36f2d5efa2..ffd312c198 100644 --- a/sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java +++ b/sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java @@ -1,3 +1,8 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + package io.dapr.it.services; import com.google.protobuf.Any; @@ -16,29 +21,24 @@ */ public class HelloWorldGrpcStateService { - public static void main(String[] args) { - ManagedChannel channel = - ManagedChannelBuilder.forAddress("localhost", 50001).usePlaintext().build(); - DaprBlockingStub client = DaprGrpc.newBlockingStub(channel); - - String key = "mykey"; - // First, write key-value pair. - - String value = "Hello World"; - StateRequest req = StateRequest - .newBuilder() - .setKey(key) - .setValue(Any.newBuilder().setValue(ByteString.copyFromUtf8(value)).build()) - .build(); - SaveStateEnvelope state = SaveStateEnvelope.newBuilder() - .addRequests(req) - .build(); - client.saveState(state); - System.out.println("Saved!"); - channel.shutdown(); - - - - - } + public static void main(String[] args) { + ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 50002).usePlaintext().build(); + DaprBlockingStub client = DaprGrpc.newBlockingStub(channel); + + String key = "mykey"; + // First, write key-value pair. + + String value = "Hello World"; + StateRequest req = StateRequest + .newBuilder() + .setKey(key) + .setValue(Any.newBuilder().setValue(ByteString.copyFromUtf8(value)).build()) + .build(); + SaveStateEnvelope state = SaveStateEnvelope.newBuilder() + .addRequests(req) + .build(); + client.saveState(state); + System.out.println("Saved!"); + channel.shutdown(); + } } diff --git a/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java b/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java index 71c0705a59..81aa266496 100644 --- a/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java +++ b/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java @@ -1,8 +1,14 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + package io.dapr.it.state; import io.dapr.DaprGrpc; import io.dapr.DaprProtos; import io.dapr.it.DaprIntegrationTestingRunner; +import io.dapr.it.services.EmptyService; import io.dapr.it.services.HelloWorldGrpcStateService; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; @@ -26,7 +32,7 @@ public static void init() throws IOException, InterruptedException, TimeoutExcep "BUILD SUCCESS", HelloWorldGrpcStateService.class, true, - 50001, + 50002, 2000 ); daprIntegrationTestingRunner.initializeDapr(); @@ -35,7 +41,7 @@ public static void init() throws IOException, InterruptedException, TimeoutExcep @Test public void testHelloWorldState(){ ManagedChannel channel = - ManagedChannelBuilder.forAddress("localhost", 50001).usePlaintext().build(); + ManagedChannelBuilder.forAddress("localhost", 50002).usePlaintext().build(); DaprGrpc.DaprBlockingStub client = DaprGrpc.newBlockingStub(channel); String key = "mykey"; diff --git a/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java b/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java new file mode 100644 index 0000000000..f03dd2fb77 --- /dev/null +++ b/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + +package io.dapr.it.state; + +import io.dapr.client.DaprClient; +import io.dapr.client.DaprClientBuilder; +import io.dapr.client.domain.StateKeyValue; +import io.dapr.it.DaprIntegrationTestingRunner; +import io.dapr.it.services.EmptyService; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import reactor.core.publisher.Mono; + +import java.io.IOException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; + +/** + * Test State HTTP DAPR capabilities using a DAPR instance with an empty service running + */ +public class HttpStateClientIT { + + + private static DaprIntegrationTestingRunner daprIntegrationTestingRunner; + + @BeforeClass + public static void init() throws IOException, InterruptedException, TimeoutException, ExecutionException { + daprIntegrationTestingRunner = + DaprIntegrationTestingRunner.createDaprIntegrationTestingRunner( + "BUILD SUCCESS", + EmptyService.class, + false, + 3500, + 0 + ); + daprIntegrationTestingRunner.initializeDapr(); + } + + @Test + public void saveAndGetState() { + final String stateKey= "myKey"; + + DaprClient daprClient= new DaprClientBuilder().build(); + MyData data= new MyData(); + data.setPropertyA("data in property A"); + data.setPropertyB("data in property B"); + //todo if I set the etag as "eTag" the save fails, but is nor reported. + Mono saveResponse= daprClient.saveState(stateKey,"",data, null); + saveResponse.block(); + + Mono response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); + MyData myDataResponse=response.block(); + + Assert.assertEquals("data in property A",myDataResponse.getPropertyA()); + Assert.assertEquals("data in property B",myDataResponse.getPropertyB()); + } + + + @AfterClass + public static void cleanUp() throws IOException { + daprIntegrationTestingRunner.destroyDapr(); + } + + + static class MyData { + + /// Gets or sets the value for PropertyA. + private String propertyA; + + /// Gets or sets the value for PropertyB. + private String propertyB; + + private MyData myData; + + + public String getPropertyB() { + return propertyB; + } + + public void setPropertyB(String propertyB) { + this.propertyB = propertyB; + } + + public String getPropertyA() { + return propertyA; + } + + public void setPropertyA(String propertyA) { + this.propertyA = propertyA; + } + + @Override + public String toString() { + return "MyData{" + + "propertyA='" + propertyA + '\'' + + ", propertyB='" + propertyB + '\'' + + '}'; + } + + public MyData getMyData() { + return myData; + } + + public void setMyData(MyData myData) { + this.myData = myData; + } + } +} From 5ffcf7ae8479bb1d4eb825fb1bfec63b029c8aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Herrera=20de=20la=20Garza?= Date: Wed, 8 Jan 2020 23:27:32 -0600 Subject: [PATCH 3/8] #26 Add Hello World Integration Testing working on Windows, need work to work on MAC and Linux --- .../dapr/it/DaprIntegrationTestingRunner.java | 35 ++++++------- .../services/HelloWorldGrpcStateService.java | 50 +++++++++---------- .../io/dapr/it/state/HelloWorldClientIT.java | 10 +--- 3 files changed, 42 insertions(+), 53 deletions(-) diff --git a/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java b/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java index 46df9fa78a..9df8abca95 100644 --- a/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java +++ b/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java @@ -12,7 +12,7 @@ public class DaprIntegrationTestingRunner { private Runtime rt = Runtime.getRuntime(); private Process proc; - private String successMessage; + private String successMessage; private Class serviceClass; private Boolean isGrpc; private int port; @@ -35,28 +35,22 @@ public static DaprIntegrationTestingRunner createDaprIntegrationTestingRunner(St public void initializeDapr() throws IOException, InterruptedException, TimeoutException, ExecutionException { - String daprCommand=this.buildDaprCommand(); - System.out.println(daprCommand); - proc= rt.exec(daprCommand); - + File file= new File("../"); + proc= rt.exec(this.buildDaprCommand(), null,file); + InputStream stdin = proc.getInputStream(); + InputStreamReader isr = new InputStreamReader(stdin); + BufferedReader br = new BufferedReader(isr); final Runnable stuffToDo = new Thread(() -> { try { - try (InputStream stdin = proc.getInputStream()) { - try(InputStreamReader isr = new InputStreamReader(stdin)) { - try (BufferedReader br = new BufferedReader(isr)){ - String line; - while ((line = br.readLine()) != null) { - System.out.println(line); - if (line.contains(successMessage)) { - break; - } - } - } + String line ; + while ((line = br.readLine()) != null) { + System.out.println(line); + if (line.contains(successMessage)) { + break; } - } - } catch (IOException ex) { + }catch(IOException ex){ Assert.fail(ex.getMessage()); } }); @@ -70,11 +64,11 @@ public void initializeDapr() throws IOException, InterruptedException, TimeoutEx } private static final String DAPR_RUN = "dapr run --app-id %s "; - private static final String DAPR_COMMAND = " -- mvn exec:java -D exec.mainClass=%s -D exec.classpathScope=\"test\""; + private static final String DAPR_COMMAND = " -- mvn exec:java -pl=sdk -D exec.mainClass=%s -D exec.classpathScope=\"test\""; private String buildDaprCommand(){ StringBuilder stringBuilder= new StringBuilder(String.format(DAPR_RUN,this.appName)) - .append(this.isGrpc ? " --grpc-port ": " --port ") + .append(this.isGrpc ? " --grpc-port ": " --app-port ") .append(this.port) .append(String.format(DAPR_COMMAND, this.serviceClass.getCanonicalName())); return stringBuilder.toString(); @@ -88,5 +82,6 @@ private void generateAppName(){ public void destroyDapr() throws IOException { rt.exec("dapr stop --app-id " + this.appName); proc.destroy(); + } } diff --git a/sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java b/sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java index ffd312c198..36f2d5efa2 100644 --- a/sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java +++ b/sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java @@ -1,8 +1,3 @@ -/* - * Copyright (c) Microsoft Corporation. - * Licensed under the MIT License. - */ - package io.dapr.it.services; import com.google.protobuf.Any; @@ -21,24 +16,29 @@ */ public class HelloWorldGrpcStateService { - public static void main(String[] args) { - ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 50002).usePlaintext().build(); - DaprBlockingStub client = DaprGrpc.newBlockingStub(channel); - - String key = "mykey"; - // First, write key-value pair. - - String value = "Hello World"; - StateRequest req = StateRequest - .newBuilder() - .setKey(key) - .setValue(Any.newBuilder().setValue(ByteString.copyFromUtf8(value)).build()) - .build(); - SaveStateEnvelope state = SaveStateEnvelope.newBuilder() - .addRequests(req) - .build(); - client.saveState(state); - System.out.println("Saved!"); - channel.shutdown(); - } + public static void main(String[] args) { + ManagedChannel channel = + ManagedChannelBuilder.forAddress("localhost", 50001).usePlaintext().build(); + DaprBlockingStub client = DaprGrpc.newBlockingStub(channel); + + String key = "mykey"; + // First, write key-value pair. + + String value = "Hello World"; + StateRequest req = StateRequest + .newBuilder() + .setKey(key) + .setValue(Any.newBuilder().setValue(ByteString.copyFromUtf8(value)).build()) + .build(); + SaveStateEnvelope state = SaveStateEnvelope.newBuilder() + .addRequests(req) + .build(); + client.saveState(state); + System.out.println("Saved!"); + channel.shutdown(); + + + + + } } diff --git a/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java b/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java index 81aa266496..71c0705a59 100644 --- a/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java +++ b/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java @@ -1,14 +1,8 @@ -/* - * Copyright (c) Microsoft Corporation. - * Licensed under the MIT License. - */ - package io.dapr.it.state; import io.dapr.DaprGrpc; import io.dapr.DaprProtos; import io.dapr.it.DaprIntegrationTestingRunner; -import io.dapr.it.services.EmptyService; import io.dapr.it.services.HelloWorldGrpcStateService; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; @@ -32,7 +26,7 @@ public static void init() throws IOException, InterruptedException, TimeoutExcep "BUILD SUCCESS", HelloWorldGrpcStateService.class, true, - 50002, + 50001, 2000 ); daprIntegrationTestingRunner.initializeDapr(); @@ -41,7 +35,7 @@ public static void init() throws IOException, InterruptedException, TimeoutExcep @Test public void testHelloWorldState(){ ManagedChannel channel = - ManagedChannelBuilder.forAddress("localhost", 50002).usePlaintext().build(); + ManagedChannelBuilder.forAddress("localhost", 50001).usePlaintext().build(); DaprGrpc.DaprBlockingStub client = DaprGrpc.newBlockingStub(channel); String key = "mykey"; From 89887663d3799b7f81bbfe85d439ba2068760515 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Herrera=20de=20la=20Garza?= Date: Mon, 13 Jan 2020 11:53:53 -0600 Subject: [PATCH 4/8] Update Integration Testing getting free ports automatically --- sdk/pom.xml | 7 +- .../io/dapr/client/DaprClientBuilder.java | 8 +- .../io/dapr/client/DaprClientHttpAdapter.java | 2 +- .../java/io/dapr/utils/ObjectSerializer.java | 3 + .../dapr/it/DaprIntegrationTestingRunner.java | 104 +++++++++++++----- .../services/HelloWorldGrpcStateService.java | 62 ++++++----- .../io/dapr/it/state/HelloWorldClientIT.java | 28 ++--- .../io/dapr/it/state/HttpStateClientIT.java | 72 ++++++++++-- 8 files changed, 204 insertions(+), 82 deletions(-) diff --git a/sdk/pom.xml b/sdk/pom.xml index beecc2a06f..eb2b4ad018 100644 --- a/sdk/pom.xml +++ b/sdk/pom.xml @@ -62,6 +62,12 @@ mockito-core test + + commons-cli + commons-cli + 1.4 + test + com.github.gmazzo okhttp-mock @@ -69,7 +75,6 @@ test - true diff --git a/sdk/src/main/java/io/dapr/client/DaprClientBuilder.java b/sdk/src/main/java/io/dapr/client/DaprClientBuilder.java index 5a1256adec..9572f0efd1 100644 --- a/sdk/src/main/java/io/dapr/client/DaprClientBuilder.java +++ b/sdk/src/main/java/io/dapr/client/DaprClientBuilder.java @@ -53,7 +53,11 @@ private static int getEnvPortOrDefault() { * @throws java.lang.IllegalStateException if any required field is missing */ public DaprClient build() { - return buildDaprClientHttp(); + return buildDaprClientHttp(this.port); + } + + public DaprClient build(int port) { + return buildDaprClientHttp(port); } /** @@ -76,7 +80,7 @@ private DaprClient buildDaprClientGrpc() { * * @return */ - private DaprClient buildDaprClientHttp() { + private DaprClient buildDaprClientHttp(int port) { if (port <= 0) { throw new IllegalStateException("Invalid port."); } diff --git a/sdk/src/main/java/io/dapr/client/DaprClientHttpAdapter.java b/sdk/src/main/java/io/dapr/client/DaprClientHttpAdapter.java index 08cb3ad5d5..5fe47ff8d8 100644 --- a/sdk/src/main/java/io/dapr/client/DaprClientHttpAdapter.java +++ b/sdk/src/main/java/io/dapr/client/DaprClientHttpAdapter.java @@ -188,7 +188,7 @@ public Mono> getState(StateKeyValue state, StateOptions .flatMap(s -> { try { return Mono.just(buildStateKeyValue(s, state.getKey(), clazz)); - } catch (Exception ex) { + }catch (Exception ex){ return Mono.error(ex); } }); diff --git a/sdk/src/main/java/io/dapr/utils/ObjectSerializer.java b/sdk/src/main/java/io/dapr/utils/ObjectSerializer.java index 6446c392af..477b9209e6 100644 --- a/sdk/src/main/java/io/dapr/utils/ObjectSerializer.java +++ b/sdk/src/main/java/io/dapr/utils/ObjectSerializer.java @@ -131,6 +131,9 @@ public T deserialize(Object value, Class clazz) throws IOException { // Not string, not primitive, not byte[], so it is a complex type: we use JSON for that. if (value instanceof byte[]) { + if (((byte[]) value).length==0) { + return null; + } return OBJECT_MAPPER.readValue((byte[]) value, clazz); } diff --git a/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java b/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java index 9df8abca95..082123ad59 100644 --- a/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java +++ b/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java @@ -3,54 +3,61 @@ import org.junit.Assert; import java.io.*; +import java.net.ServerSocket; import java.util.UUID; import java.util.concurrent.*; public class DaprIntegrationTestingRunner { + private DaprFreePorts daprFreePorts= new DaprFreePorts(); private Runtime rt = Runtime.getRuntime(); private Process proc; - private String successMessage; + private String successMessage; private Class serviceClass; - private Boolean isGrpc; - private int port; + private Boolean useAppPort; private int sleepTime; private String appName; - private DaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean isGrpc, int port, int sleepTime) { + private DaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean useAppPort, int sleepTime) { this.successMessage = successMessage; this.serviceClass = serviceClass; - this.isGrpc = isGrpc; - this.port = port; + this.useAppPort = useAppPort; this.sleepTime = sleepTime; this.generateAppName(); } - public static DaprIntegrationTestingRunner createDaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean isGrpc, int port, int sleepTime) { - return new DaprIntegrationTestingRunner(successMessage, serviceClass, isGrpc, port, sleepTime); + public static DaprIntegrationTestingRunner createDaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean useAppPort, int sleepTime) { + return new DaprIntegrationTestingRunner(successMessage, serviceClass, useAppPort, sleepTime); } - public void initializeDapr() throws IOException, InterruptedException, TimeoutException, ExecutionException { + public DaprFreePorts initializeDapr() throws Exception { + daprFreePorts.initPorts(); + + String daprCommand=this.buildDaprCommand(); + System.out.println(daprCommand); + proc= rt.exec(daprCommand); - File file= new File("../"); - proc= rt.exec(this.buildDaprCommand(), null,file); - InputStream stdin = proc.getInputStream(); - InputStreamReader isr = new InputStreamReader(stdin); - BufferedReader br = new BufferedReader(isr); final Runnable stuffToDo = new Thread(() -> { try { - String line ; - while ((line = br.readLine()) != null) { - System.out.println(line); - if (line.contains(successMessage)) { - break; + try (InputStream stdin = proc.getInputStream()) { + try(InputStreamReader isr = new InputStreamReader(stdin)) { + try (BufferedReader br = new BufferedReader(isr)){ + String line; + while ((line = br.readLine()) != null) { + System.out.println(line); + if (line.contains(successMessage)) { + break; + } + } + } } + } - }catch(IOException ex){ + } catch (IOException ex) { Assert.fail(ex.getMessage()); } }); @@ -58,19 +65,22 @@ public void initializeDapr() throws IOException, InterruptedException, TimeoutEx final ExecutorService executor = Executors.newSingleThreadExecutor(); final Future future = executor.submit(stuffToDo); executor.shutdown(); // This does not cancel the already-scheduled task. - future.get(2, TimeUnit.MINUTES); + future.get(1, TimeUnit.MINUTES); Thread.sleep(sleepTime); - + return daprFreePorts; } private static final String DAPR_RUN = "dapr run --app-id %s "; - private static final String DAPR_COMMAND = " -- mvn exec:java -pl=sdk -D exec.mainClass=%s -D exec.classpathScope=\"test\""; + private static final String DAPR_COMMAND = " -- mvn exec:java -D exec.mainClass=%s -D exec.classpathScope=\"test\" -Dexec.args=\"-p %d -grpcPort %d -httpPort %d\""; private String buildDaprCommand(){ - StringBuilder stringBuilder= new StringBuilder(String.format(DAPR_RUN,this.appName)) - .append(this.isGrpc ? " --grpc-port ": " --app-port ") - .append(this.port) - .append(String.format(DAPR_COMMAND, this.serviceClass.getCanonicalName())); + StringBuilder stringBuilder= new StringBuilder(String.format(DAPR_RUN, this.appName)) + .append(this.useAppPort ? "--app-port " + this.daprFreePorts.appPort : "") + .append(" --grpc-port ") + .append(this.daprFreePorts.grpcPort) + .append(" --port ") + .append(this.daprFreePorts.httpPort) + .append(String.format(DAPR_COMMAND, this.serviceClass.getCanonicalName(),this.daprFreePorts.appPort, this.daprFreePorts.grpcPort, this.daprFreePorts.httpPort)); return stringBuilder.toString(); } @@ -82,6 +92,46 @@ private void generateAppName(){ public void destroyDapr() throws IOException { rt.exec("dapr stop --app-id " + this.appName); proc.destroy(); + } + + private static Integer findRandomOpenPortOnAllLocalInterfaces() throws Exception { + try ( + ServerSocket socket = new ServerSocket(0) + ) { + return socket.getLocalPort(); + + } + } + + public static class DaprFreePorts + { + public void initPorts() throws Exception { + this.appPort= findRandomOpenPortOnAllLocalInterfaces(); + this.grpcPort= findRandomOpenPortOnAllLocalInterfaces(); + this.httpPort= findRandomOpenPortOnAllLocalInterfaces(); + } + + private int grpcPort; + + public int getGrpcPort() { + return grpcPort; + } + + + + public int getHttpPort() { + return httpPort; + } + + + + public int getAppPort() { + return appPort; + } + + + private int httpPort; + private int appPort; } } diff --git a/sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java b/sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java index 36f2d5efa2..cc8cf63a01 100644 --- a/sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java +++ b/sdk/src/test/java/io/dapr/it/services/HelloWorldGrpcStateService.java @@ -1,3 +1,8 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + package io.dapr.it.services; import com.google.protobuf.Any; @@ -8,6 +13,8 @@ import io.dapr.DaprProtos.StateRequest; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; +import org.apache.commons.cli.*; + /** * Simple example, to run: @@ -16,29 +23,34 @@ */ public class HelloWorldGrpcStateService { - public static void main(String[] args) { - ManagedChannel channel = - ManagedChannelBuilder.forAddress("localhost", 50001).usePlaintext().build(); - DaprBlockingStub client = DaprGrpc.newBlockingStub(channel); - - String key = "mykey"; - // First, write key-value pair. - - String value = "Hello World"; - StateRequest req = StateRequest - .newBuilder() - .setKey(key) - .setValue(Any.newBuilder().setValue(ByteString.copyFromUtf8(value)).build()) - .build(); - SaveStateEnvelope state = SaveStateEnvelope.newBuilder() - .addRequests(req) - .build(); - client.saveState(state); - System.out.println("Saved!"); - channel.shutdown(); - - - - - } + public static void main(String[] args) throws ParseException { + Options options = new Options(); + options.addRequiredOption("grpcPort", "grpcPort", true, "Dapr GRPC."); + options.addRequiredOption("httpPort", "httpPort", true, "Dapr HTTP port."); + options.addRequiredOption("p", "port", true, "Port Dapr will listen to."); + + CommandLineParser parser = new DefaultParser(); + CommandLine cmd = parser.parse(options, args); + + // If port string is not valid, it will throw an exception. + int port = Integer.parseInt(cmd.getOptionValue("grpcPort")); + ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", port).usePlaintext().build(); + DaprBlockingStub client = DaprGrpc.newBlockingStub(channel); + + String key = "mykey"; + // First, write key-value pair. + + String value = "Hello World"; + StateRequest req = StateRequest + .newBuilder() + .setKey(key) + .setValue(Any.newBuilder().setValue(ByteString.copyFromUtf8(value)).build()) + .build(); + SaveStateEnvelope state = SaveStateEnvelope.newBuilder() + .addRequests(req) + .build(); + client.saveState(state); + System.out.println("Saved!"); + channel.shutdown(); + } } diff --git a/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java b/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java index 71c0705a59..c7e5d7ee2f 100644 --- a/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java +++ b/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java @@ -1,3 +1,8 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + package io.dapr.it.state; import io.dapr.DaprGrpc; @@ -6,36 +11,36 @@ import io.dapr.it.services.HelloWorldGrpcStateService; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; -import org.junit.*; - +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; import java.io.IOException; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeoutException; public class HelloWorldClientIT { private static DaprIntegrationTestingRunner daprIntegrationTestingRunner; + private static DaprIntegrationTestingRunner.DaprFreePorts daprFreePorts; @BeforeClass - public static void init() throws IOException, InterruptedException, TimeoutException, ExecutionException { + public static void init() throws Exception { daprIntegrationTestingRunner = DaprIntegrationTestingRunner.createDaprIntegrationTestingRunner( "BUILD SUCCESS", HelloWorldGrpcStateService.class, - true, - 50001, + false, 2000 ); - daprIntegrationTestingRunner.initializeDapr(); + daprFreePorts = daprIntegrationTestingRunner.initializeDapr(); } @Test public void testHelloWorldState(){ ManagedChannel channel = - ManagedChannelBuilder.forAddress("localhost", 50001).usePlaintext().build(); + ManagedChannelBuilder.forAddress("localhost", daprFreePorts.getGrpcPort()).usePlaintext().build(); DaprGrpc.DaprBlockingStub client = DaprGrpc.newBlockingStub(channel); String key = "mykey"; @@ -72,11 +77,6 @@ public void testHelloWorldState(){ } } - @Test - public void test2(){ - - } - @AfterClass public static void cleanUp() throws IOException { daprIntegrationTestingRunner.destroyDapr(); diff --git a/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java b/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java index f03dd2fb77..6ee30fc5c7 100644 --- a/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java +++ b/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java @@ -17,8 +17,6 @@ import reactor.core.publisher.Mono; import java.io.IOException; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeoutException; /** * Test State HTTP DAPR capabilities using a DAPR instance with an empty service running @@ -27,37 +25,87 @@ public class HttpStateClientIT { private static DaprIntegrationTestingRunner daprIntegrationTestingRunner; + private static DaprIntegrationTestingRunner.DaprFreePorts daprFreePorts; @BeforeClass - public static void init() throws IOException, InterruptedException, TimeoutException, ExecutionException { + public static void init() throws Exception { daprIntegrationTestingRunner = DaprIntegrationTestingRunner.createDaprIntegrationTestingRunner( "BUILD SUCCESS", EmptyService.class, false, - 3500, 0 ); - daprIntegrationTestingRunner.initializeDapr(); + daprFreePorts = daprIntegrationTestingRunner.initializeDapr(); } @Test public void saveAndGetState() { final String stateKey= "myKey"; - DaprClient daprClient= new DaprClientBuilder().build(); + DaprClient daprClient= new DaprClientBuilder().build(daprFreePorts.getHttpPort()); MyData data= new MyData(); data.setPropertyA("data in property A"); data.setPropertyB("data in property B"); - //todo if I set the etag as "eTag" the save fails, but is nor reported. - Mono saveResponse= daprClient.saveState(stateKey,"",data, null); + Mono saveResponse= daprClient.saveState(stateKey,null,data, null); saveResponse.block(); - Mono response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); - MyData myDataResponse=response.block(); + Mono> response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); + StateKeyValue myDataResponse=response.block(); + + Assert.assertEquals("data in property A",myDataResponse.getValue().getPropertyA()); + Assert.assertEquals("data in property B",myDataResponse.getValue().getPropertyB()); + } + + @Test + public void saveUpdateAndGetState() { + final String stateKey= "keyToBeUpdated"; + + DaprClient daprClient= new DaprClientBuilder().build(daprFreePorts.getHttpPort()); + MyData data= new MyData(); + data.setPropertyA("data in property A"); + data.setPropertyB("data in property B"); + Mono saveResponse= daprClient.saveState(stateKey,null,data, null); + saveResponse.block(); + + + data.setPropertyA("data in property A"); + data.setPropertyB("data in property B2"); + saveResponse= daprClient.saveState(stateKey,null,data, null); + saveResponse.block(); + + Mono> response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); + StateKeyValue myDataResponse=response.block(); + + Assert.assertEquals("data in property A",myDataResponse.getValue().getPropertyA()); + Assert.assertEquals("data in property B2",myDataResponse.getValue().getPropertyB()); + } + + @Test + public void saveAndDeleteState() { + final String stateKey= "myeKeyToBeDeleted"; + + DaprClient daprClient= new DaprClientBuilder().build(daprFreePorts.getHttpPort()); + MyData data= new MyData(); + data.setPropertyA("data in property A"); + data.setPropertyB("data in property B"); + Mono saveResponse= daprClient.saveState(stateKey,null,data, null); + saveResponse.block(); + + Mono> response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); + StateKeyValue myDataResponse=response.block(); + + Assert.assertEquals("data in property A",myDataResponse.getValue().getPropertyA()); + Assert.assertEquals("data in property B",myDataResponse.getValue().getPropertyB()); + + Mono deleteResponse= daprClient.deleteState( new StateKeyValue(null,stateKey,null),null); + deleteResponse.block(); + + response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); + myDataResponse=response.block(); + + Assert.assertNull(myDataResponse.getValue()); - Assert.assertEquals("data in property A",myDataResponse.getPropertyA()); - Assert.assertEquals("data in property B",myDataResponse.getPropertyB()); } From d82821d56c8efdacc00af2ba7ae7352d9874b451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Herrera=20de=20la=20Garza?= Date: Mon, 13 Jan 2020 18:45:11 -0600 Subject: [PATCH 5/8] #26 Refractor to use a base class for all the integration tests --- sdk/pom.xml | 6 ++ .../io/dapr/client/DaprClientBuilder.java | 9 +- sdk/src/test/java/io/dapr/it/BaseIT.java | 84 +++++++++++++++++++ .../dapr/it/DaprIntegrationTestingRunner.java | 52 ++++++++---- .../actor}/DaprHttpAsyncClientIT.java | 7 +- .../io/dapr/it/state/HelloWorldClientIT.java | 20 ++--- .../io/dapr/it/state/HttpStateClientIT.java | 74 +++------------- 7 files changed, 150 insertions(+), 102 deletions(-) create mode 100644 sdk/src/test/java/io/dapr/it/BaseIT.java rename sdk/src/test/java/io/dapr/{client => it/actor}/DaprHttpAsyncClientIT.java (87%) diff --git a/sdk/pom.xml b/sdk/pom.xml index eb2b4ad018..84828e2847 100644 --- a/sdk/pom.xml +++ b/sdk/pom.xml @@ -74,6 +74,12 @@ 1.3.2 test + + com.github.stefanbirkner + system-rules + 1.19.0 + test + true diff --git a/sdk/src/main/java/io/dapr/client/DaprClientBuilder.java b/sdk/src/main/java/io/dapr/client/DaprClientBuilder.java index 9572f0efd1..c4c0f9d9f4 100644 --- a/sdk/src/main/java/io/dapr/client/DaprClientBuilder.java +++ b/sdk/src/main/java/io/dapr/client/DaprClientBuilder.java @@ -53,11 +53,7 @@ private static int getEnvPortOrDefault() { * @throws java.lang.IllegalStateException if any required field is missing */ public DaprClient build() { - return buildDaprClientHttp(this.port); - } - - public DaprClient build(int port) { - return buildDaprClientHttp(port); + return buildDaprClientHttp(); } /** @@ -80,7 +76,8 @@ private DaprClient buildDaprClientGrpc() { * * @return */ - private DaprClient buildDaprClientHttp(int port) { + private DaprClient buildDaprClientHttp() { + int port=DaprClientBuilder.getEnvPortOrDefault(); if (port <= 0) { throw new IllegalStateException("Invalid port."); } diff --git a/sdk/src/test/java/io/dapr/it/BaseIT.java b/sdk/src/test/java/io/dapr/it/BaseIT.java new file mode 100644 index 0000000000..b3d2d7afc4 --- /dev/null +++ b/sdk/src/test/java/io/dapr/it/BaseIT.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + +package io.dapr.it; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.contrib.java.lang.system.EnvironmentVariables; + +import java.util.Optional; + +import static io.dapr.it.DaprIntegrationTestingRunner.DAPR_FREEPORTS; + +public class BaseIT { + + protected static DaprIntegrationTestingRunner daprIntegrationTestingRunner; + + + @ClassRule + public static final EnvironmentVariables environmentVariables = new EnvironmentVariables(); + + + + @BeforeClass + public static void setEnvironmentVariables(){ + environmentVariables.set("DAPR_HTTP_PORT", String.valueOf(DAPR_FREEPORTS.getHttpPort())); + } + + public static DaprIntegrationTestingRunner createDaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean useAppPort, int sleepTime) { + return new DaprIntegrationTestingRunner(successMessage, serviceClass, useAppPort, sleepTime); + } + + @AfterClass + public static void cleanUp() { + Optional.ofNullable(daprIntegrationTestingRunner).ifPresent(daprRunner -> daprRunner.destroyDapr()); + } + + public static class MyData { + + /// Gets or sets the value for PropertyA. + private String propertyA; + + /// Gets or sets the value for PropertyB. + private String propertyB; + + private MyData myData; + + + public String getPropertyB() { + return propertyB; + } + + public void setPropertyB(String propertyB) { + this.propertyB = propertyB; + } + + public String getPropertyA() { + return propertyA; + } + + public void setPropertyA(String propertyA) { + this.propertyA = propertyA; + } + + @Override + public String toString() { + return "MyData{" + + "propertyA='" + propertyA + '\'' + + ", propertyB='" + propertyB + '\'' + + '}'; + } + + public MyData getMyData() { + return myData; + } + + public void setMyData(MyData myData) { + this.myData = myData; + } + } +} diff --git a/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java b/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java index 082123ad59..0273f9c9f9 100644 --- a/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java +++ b/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java @@ -4,13 +4,23 @@ import java.io.*; import java.net.ServerSocket; +import java.util.Optional; import java.util.UUID; import java.util.concurrent.*; public class DaprIntegrationTestingRunner { - private DaprFreePorts daprFreePorts= new DaprFreePorts(); + public static DaprIntegrationTestingRunner.DaprFreePorts DAPR_FREEPORTS; + + static { + try { + DAPR_FREEPORTS = new DaprIntegrationTestingRunner.DaprFreePorts().initPorts(); + } catch (Exception e) { + e.printStackTrace(); + } + } + private Runtime rt = Runtime.getRuntime(); private Process proc; @@ -20,7 +30,7 @@ public class DaprIntegrationTestingRunner { private int sleepTime; private String appName; - private DaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean useAppPort, int sleepTime) { + DaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean useAppPort, int sleepTime) { this.successMessage = successMessage; this.serviceClass = serviceClass; this.useAppPort = useAppPort; @@ -28,13 +38,10 @@ private DaprIntegrationTestingRunner(String successMessage, Class serviceClass, this.generateAppName(); } - public static DaprIntegrationTestingRunner createDaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean useAppPort, int sleepTime) { - return new DaprIntegrationTestingRunner(successMessage, serviceClass, useAppPort, sleepTime); - } public DaprFreePorts initializeDapr() throws Exception { - daprFreePorts.initPorts(); + String daprCommand=this.buildDaprCommand(); System.out.println(daprCommand); @@ -67,7 +74,7 @@ public DaprFreePorts initializeDapr() throws Exception { executor.shutdown(); // This does not cancel the already-scheduled task. future.get(1, TimeUnit.MINUTES); Thread.sleep(sleepTime); - return daprFreePorts; + return DAPR_FREEPORTS; } private static final String DAPR_RUN = "dapr run --app-id %s "; @@ -75,12 +82,12 @@ public DaprFreePorts initializeDapr() throws Exception { private String buildDaprCommand(){ StringBuilder stringBuilder= new StringBuilder(String.format(DAPR_RUN, this.appName)) - .append(this.useAppPort ? "--app-port " + this.daprFreePorts.appPort : "") + .append(this.useAppPort ? "--app-port " + this.DAPR_FREEPORTS.appPort : "") .append(" --grpc-port ") - .append(this.daprFreePorts.grpcPort) + .append(this.DAPR_FREEPORTS.grpcPort) .append(" --port ") - .append(this.daprFreePorts.httpPort) - .append(String.format(DAPR_COMMAND, this.serviceClass.getCanonicalName(),this.daprFreePorts.appPort, this.daprFreePorts.grpcPort, this.daprFreePorts.httpPort)); + .append(this.DAPR_FREEPORTS.httpPort) + .append(String.format(DAPR_COMMAND, this.serviceClass.getCanonicalName(),this.DAPR_FREEPORTS.appPort, this.DAPR_FREEPORTS.grpcPort, this.DAPR_FREEPORTS.httpPort)); return stringBuilder.toString(); } @@ -89,11 +96,6 @@ private void generateAppName(){ this.appName=UUID.randomUUID().toString(); } - public void destroyDapr() throws IOException { - rt.exec("dapr stop --app-id " + this.appName); - proc.destroy(); - } - private static Integer findRandomOpenPortOnAllLocalInterfaces() throws Exception { try ( ServerSocket socket = new ServerSocket(0) @@ -103,12 +105,28 @@ private static Integer findRandomOpenPortOnAllLocalInterfaces() throws Exception } } + public void destroyDapr() { + Optional.ofNullable(rt).ifPresent( runtime -> { + try { + runtime.exec("dapr stop --app-id " + this.appName); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + Optional.ofNullable(proc).ifPresent(process -> process.destroy()); + } + + + + + public static class DaprFreePorts { - public void initPorts() throws Exception { + public DaprFreePorts initPorts() throws Exception { this.appPort= findRandomOpenPortOnAllLocalInterfaces(); this.grpcPort= findRandomOpenPortOnAllLocalInterfaces(); this.httpPort= findRandomOpenPortOnAllLocalInterfaces(); + return this; } private int grpcPort; diff --git a/sdk/src/test/java/io/dapr/client/DaprHttpAsyncClientIT.java b/sdk/src/test/java/io/dapr/it/actor/DaprHttpAsyncClientIT.java similarity index 87% rename from sdk/src/test/java/io/dapr/client/DaprHttpAsyncClientIT.java rename to sdk/src/test/java/io/dapr/it/actor/DaprHttpAsyncClientIT.java index 9b2ead69e6..d2ac154d0c 100644 --- a/sdk/src/test/java/io/dapr/client/DaprHttpAsyncClientIT.java +++ b/sdk/src/test/java/io/dapr/it/actor/DaprHttpAsyncClientIT.java @@ -2,9 +2,12 @@ * Copyright (c) Microsoft Corporation. * Licensed under the MIT License. */ -package io.dapr.client; +package io.dapr.it.actor; +import io.dapr.client.DaprClient; +import io.dapr.client.DaprClientBuilder; import io.dapr.exceptions.DaprException; +import io.dapr.it.BaseIT; import org.junit.Assert; import org.junit.Test; @@ -13,7 +16,7 @@ *

* Requires Dapr running. */ -public class DaprHttpAsyncClientIT { +public class DaprHttpAsyncClientIT extends BaseIT { /** * Checks if the error is correctly parsed when trying to invoke a function on diff --git a/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java b/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java index c7e5d7ee2f..8801d43e38 100644 --- a/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java +++ b/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java @@ -7,40 +7,40 @@ import io.dapr.DaprGrpc; import io.dapr.DaprProtos; +import io.dapr.it.BaseIT; import io.dapr.it.DaprIntegrationTestingRunner; import io.dapr.it.services.HelloWorldGrpcStateService; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -import java.io.IOException; +import static io.dapr.it.DaprIntegrationTestingRunner.DAPR_FREEPORTS; -public class HelloWorldClientIT { +public class HelloWorldClientIT extends BaseIT { private static DaprIntegrationTestingRunner daprIntegrationTestingRunner; - private static DaprIntegrationTestingRunner.DaprFreePorts daprFreePorts; + @BeforeClass public static void init() throws Exception { daprIntegrationTestingRunner = - DaprIntegrationTestingRunner.createDaprIntegrationTestingRunner( + createDaprIntegrationTestingRunner( "BUILD SUCCESS", HelloWorldGrpcStateService.class, false, 2000 ); - daprFreePorts = daprIntegrationTestingRunner.initializeDapr(); + daprIntegrationTestingRunner.initializeDapr(); } @Test public void testHelloWorldState(){ ManagedChannel channel = - ManagedChannelBuilder.forAddress("localhost", daprFreePorts.getGrpcPort()).usePlaintext().build(); + ManagedChannelBuilder.forAddress("localhost", DAPR_FREEPORTS.getGrpcPort()).usePlaintext().build(); DaprGrpc.DaprBlockingStub client = DaprGrpc.newBlockingStub(channel); String key = "mykey"; @@ -77,10 +77,4 @@ public void testHelloWorldState(){ } } - @AfterClass - public static void cleanUp() throws IOException { - daprIntegrationTestingRunner.destroyDapr(); - - } - } diff --git a/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java b/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java index 6ee30fc5c7..d281c97ff5 100644 --- a/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java +++ b/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java @@ -8,42 +8,38 @@ import io.dapr.client.DaprClient; import io.dapr.client.DaprClientBuilder; import io.dapr.client.domain.StateKeyValue; -import io.dapr.it.DaprIntegrationTestingRunner; +import io.dapr.it.BaseIT; import io.dapr.it.services.EmptyService; -import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import reactor.core.publisher.Mono; -import java.io.IOException; - /** * Test State HTTP DAPR capabilities using a DAPR instance with an empty service running */ -public class HttpStateClientIT { - - - private static DaprIntegrationTestingRunner daprIntegrationTestingRunner; - private static DaprIntegrationTestingRunner.DaprFreePorts daprFreePorts; +public class HttpStateClientIT extends BaseIT { @BeforeClass public static void init() throws Exception { daprIntegrationTestingRunner = - DaprIntegrationTestingRunner.createDaprIntegrationTestingRunner( + createDaprIntegrationTestingRunner( "BUILD SUCCESS", EmptyService.class, false, 0 ); - daprFreePorts = daprIntegrationTestingRunner.initializeDapr(); + daprIntegrationTestingRunner.initializeDapr(); } + + @Test public void saveAndGetState() { + final String stateKey= "myKey"; - DaprClient daprClient= new DaprClientBuilder().build(daprFreePorts.getHttpPort()); + DaprClient daprClient= new DaprClientBuilder().build(); MyData data= new MyData(); data.setPropertyA("data in property A"); data.setPropertyB("data in property B"); @@ -61,7 +57,7 @@ public void saveAndGetState() { public void saveUpdateAndGetState() { final String stateKey= "keyToBeUpdated"; - DaprClient daprClient= new DaprClientBuilder().build(daprFreePorts.getHttpPort()); + DaprClient daprClient= new DaprClientBuilder().build(); MyData data= new MyData(); data.setPropertyA("data in property A"); data.setPropertyB("data in property B"); @@ -85,7 +81,7 @@ public void saveUpdateAndGetState() { public void saveAndDeleteState() { final String stateKey= "myeKeyToBeDeleted"; - DaprClient daprClient= new DaprClientBuilder().build(daprFreePorts.getHttpPort()); + DaprClient daprClient= new DaprClientBuilder().build(); MyData data= new MyData(); data.setPropertyA("data in property A"); data.setPropertyB("data in property B"); @@ -108,54 +104,4 @@ public void saveAndDeleteState() { } - - @AfterClass - public static void cleanUp() throws IOException { - daprIntegrationTestingRunner.destroyDapr(); - } - - - static class MyData { - - /// Gets or sets the value for PropertyA. - private String propertyA; - - /// Gets or sets the value for PropertyB. - private String propertyB; - - private MyData myData; - - - public String getPropertyB() { - return propertyB; - } - - public void setPropertyB(String propertyB) { - this.propertyB = propertyB; - } - - public String getPropertyA() { - return propertyA; - } - - public void setPropertyA(String propertyA) { - this.propertyA = propertyA; - } - - @Override - public String toString() { - return "MyData{" + - "propertyA='" + propertyA + '\'' + - ", propertyB='" + propertyB + '\'' + - '}'; - } - - public MyData getMyData() { - return myData; - } - - public void setMyData(MyData myData) { - this.myData = myData; - } - } } From b6d4afd898c7db48dd56e8e0f9537384ba94c133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Herrera=20de=20la=20Garza?= Date: Mon, 13 Jan 2020 19:16:17 -0600 Subject: [PATCH 6/8] #26 Make StateOptions as optional in order to not throw a null pointer exception --- .../io/dapr/client/DaprClientHttpAdapter.java | 17 +++++++---------- .../io/dapr/it/state/HttpStateClientIT.java | 2 +- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/sdk/src/main/java/io/dapr/client/DaprClientHttpAdapter.java b/sdk/src/main/java/io/dapr/client/DaprClientHttpAdapter.java index 951a0d5a06..504a5ce4c7 100644 --- a/sdk/src/main/java/io/dapr/client/DaprClientHttpAdapter.java +++ b/sdk/src/main/java/io/dapr/client/DaprClientHttpAdapter.java @@ -13,10 +13,7 @@ import java.io.IOException; import java.lang.reflect.Field; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * An adapter for the HTTP Client. @@ -183,7 +180,7 @@ public Mono> getState(StateKeyValue state, StateOptions StringBuilder url = new StringBuilder(Constants.STATE_PATH) .append("/") .append(state.getKey()); - Map urlParameters = stateOptions.getStateOptionsAsMap(); + Map urlParameters = Optional.ofNullable(stateOptions).map(options -> options.getStateOptionsAsMap() ).orElse( new HashMap<>());; return this.client .invokeAPI(DaprHttp.HttpMethods.GET.name(), url.toString(), urlParameters, headers) .flatMap(s -> { @@ -207,14 +204,14 @@ public Mono saveStates(List> states, StateOptions opt if (states == null || states.isEmpty()) { return Mono.empty(); } - Map headers = new HashMap<>(); - String etag = states.stream().filter(state -> null != state.getEtag() && !state.getEtag().trim().isEmpty()) + final Map headers = new HashMap<>(); + final String etag = states.stream().filter(state -> null != state.getEtag() && !state.getEtag().trim().isEmpty()) .findFirst().orElse(new StateKeyValue<>(null, null, null)).getEtag(); if (etag != null && !etag.trim().isEmpty()) { headers.put(Constants.HEADER_HTTP_ETAG_ID, etag); } - String url = Constants.STATE_PATH; - Map urlParameter = options.getStateOptionsAsMap(); + final String url = Constants.STATE_PATH; + Map urlParameter = Optional.ofNullable(options).map(stateOptions -> stateOptions.getStateOptionsAsMap() ).orElse( new HashMap<>()); byte[] serializedStateBody = objectSerializer.serialize(states); return this.client.invokeAPI( DaprHttp.HttpMethods.POST.name(), url, urlParameter, serializedStateBody, headers).then(); @@ -249,7 +246,7 @@ public Mono deleteState(StateKeyValue state, StateOptions options) headers.put(Constants.HEADER_HTTP_ETAG_ID, state.getEtag()); } String url = Constants.STATE_PATH + "/" + state.getKey(); - Map urlParameters = options.getStateOptionsAsMap(); + Map urlParameters = Optional.ofNullable(options).map(stateOptions -> stateOptions.getStateOptionsAsMap() ).orElse( new HashMap<>());; return this.client.invokeAPI(DaprHttp.HttpMethods.DELETE.name(), url, urlParameters, headers).then(); } catch (Exception ex) { return Mono.error(ex); diff --git a/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java b/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java index d281c97ff5..2cdb7fad77 100644 --- a/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java +++ b/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java @@ -46,7 +46,7 @@ public void saveAndGetState() { Mono saveResponse= daprClient.saveState(stateKey,null,data, null); saveResponse.block(); - Mono> response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); + Mono> response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); StateKeyValue myDataResponse=response.block(); Assert.assertEquals("data in property A",myDataResponse.getValue().getPropertyA()); From ae66c4e3588f0f7891f31e4636bff2ea50f47d76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Herrera=20de=20la=20Garza?= Date: Mon, 13 Jan 2020 19:41:28 -0600 Subject: [PATCH 7/8] #26 Remove empty lines and correct the ident --- sdk/src/test/java/io/dapr/it/BaseIT.java | 93 ++++--- .../dapr/it/DaprIntegrationTestingRunner.java | 238 +++++++++--------- .../io/dapr/it/services/EmptyService.java | 1 - .../io/dapr/it/state/HelloWorldClientIT.java | 96 ++++--- .../io/dapr/it/state/HttpStateClientIT.java | 162 ++++++------ 5 files changed, 282 insertions(+), 308 deletions(-) diff --git a/sdk/src/test/java/io/dapr/it/BaseIT.java b/sdk/src/test/java/io/dapr/it/BaseIT.java index b3d2d7afc4..bea550d901 100644 --- a/sdk/src/test/java/io/dapr/it/BaseIT.java +++ b/sdk/src/test/java/io/dapr/it/BaseIT.java @@ -16,69 +16,66 @@ public class BaseIT { - protected static DaprIntegrationTestingRunner daprIntegrationTestingRunner; + protected static DaprIntegrationTestingRunner daprIntegrationTestingRunner; - @ClassRule - public static final EnvironmentVariables environmentVariables = new EnvironmentVariables(); + @ClassRule + public static final EnvironmentVariables environmentVariables = new EnvironmentVariables(); + @BeforeClass + public static void setEnvironmentVariables(){ + environmentVariables.set("DAPR_HTTP_PORT", String.valueOf(DAPR_FREEPORTS.getHttpPort())); + } + public static DaprIntegrationTestingRunner createDaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean useAppPort, int sleepTime) { + return new DaprIntegrationTestingRunner(successMessage, serviceClass, useAppPort, sleepTime); + } - @BeforeClass - public static void setEnvironmentVariables(){ - environmentVariables.set("DAPR_HTTP_PORT", String.valueOf(DAPR_FREEPORTS.getHttpPort())); - } - - public static DaprIntegrationTestingRunner createDaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean useAppPort, int sleepTime) { - return new DaprIntegrationTestingRunner(successMessage, serviceClass, useAppPort, sleepTime); - } - - @AfterClass - public static void cleanUp() { - Optional.ofNullable(daprIntegrationTestingRunner).ifPresent(daprRunner -> daprRunner.destroyDapr()); - } - - public static class MyData { + @AfterClass + public static void cleanUp() { + Optional.ofNullable(daprIntegrationTestingRunner).ifPresent(daprRunner -> daprRunner.destroyDapr()); + } - /// Gets or sets the value for PropertyA. - private String propertyA; + public static class MyData { - /// Gets or sets the value for PropertyB. - private String propertyB; + /// Gets or sets the value for PropertyA. + private String propertyA; - private MyData myData; + /// Gets or sets the value for PropertyB. + private String propertyB; + private MyData myData; - public String getPropertyB() { - return propertyB; - } + public String getPropertyB() { + return propertyB; + } - public void setPropertyB(String propertyB) { - this.propertyB = propertyB; - } + public void setPropertyB(String propertyB) { + this.propertyB = propertyB; + } - public String getPropertyA() { - return propertyA; - } + public String getPropertyA() { + return propertyA; + } - public void setPropertyA(String propertyA) { - this.propertyA = propertyA; - } + public void setPropertyA(String propertyA) { + this.propertyA = propertyA; + } - @Override - public String toString() { - return "MyData{" + - "propertyA='" + propertyA + '\'' + - ", propertyB='" + propertyB + '\'' + - '}'; - } + @Override + public String toString() { + return "MyData{" + + "propertyA='" + propertyA + '\'' + + ", propertyB='" + propertyB + '\'' + + '}'; + } - public MyData getMyData() { - return myData; - } + public MyData getMyData() { + return myData; + } - public void setMyData(MyData myData) { - this.myData = myData; - } + public void setMyData(MyData myData) { + this.myData = myData; } + } } diff --git a/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java b/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java index 0273f9c9f9..b38b6c639c 100644 --- a/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java +++ b/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java @@ -11,145 +11,131 @@ public class DaprIntegrationTestingRunner { - public static DaprIntegrationTestingRunner.DaprFreePorts DAPR_FREEPORTS; + public static DaprIntegrationTestingRunner.DaprFreePorts DAPR_FREEPORTS; - static { - try { - DAPR_FREEPORTS = new DaprIntegrationTestingRunner.DaprFreePorts().initPorts(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - private Runtime rt = Runtime.getRuntime(); - private Process proc; - - private String successMessage; - private Class serviceClass; - private Boolean useAppPort; - private int sleepTime; - private String appName; - - DaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean useAppPort, int sleepTime) { - this.successMessage = successMessage; - this.serviceClass = serviceClass; - this.useAppPort = useAppPort; - this.sleepTime = sleepTime; - this.generateAppName(); + static { + try { + DAPR_FREEPORTS = new DaprIntegrationTestingRunner.DaprFreePorts().initPorts(); + } catch (Exception e) { + e.printStackTrace(); } - - - - public DaprFreePorts initializeDapr() throws Exception { - - - String daprCommand=this.buildDaprCommand(); - System.out.println(daprCommand); - proc= rt.exec(daprCommand); - - - final Runnable stuffToDo = new Thread(() -> { - try { - try (InputStream stdin = proc.getInputStream()) { - try(InputStreamReader isr = new InputStreamReader(stdin)) { - try (BufferedReader br = new BufferedReader(isr)){ - String line; - while ((line = br.readLine()) != null) { - System.out.println(line); - if (line.contains(successMessage)) { - break; - } - } - } - } - + } + + private Runtime rt = Runtime.getRuntime(); + private Process proc; + + private String successMessage; + private Class serviceClass; + private Boolean useAppPort; + private int sleepTime; + private String appName; + + DaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean useAppPort, int sleepTime) { + this.successMessage = successMessage; + this.serviceClass = serviceClass; + this.useAppPort = useAppPort; + this.sleepTime = sleepTime; + this.generateAppName(); + } + + public DaprFreePorts initializeDapr() throws Exception { + String daprCommand=this.buildDaprCommand(); + System.out.println(daprCommand); + proc= rt.exec(daprCommand); + + final Runnable stuffToDo = new Thread(() -> { + try { + try (InputStream stdin = proc.getInputStream()) { + try(InputStreamReader isr = new InputStreamReader(stdin)) { + try (BufferedReader br = new BufferedReader(isr)){ + String line; + while ((line = br.readLine()) != null) { + System.out.println(line); + if (line.contains(successMessage)) { + break; } - } catch (IOException ex) { - Assert.fail(ex.getMessage()); + } } - }); - - final ExecutorService executor = Executors.newSingleThreadExecutor(); - final Future future = executor.submit(stuffToDo); - executor.shutdown(); // This does not cancel the already-scheduled task. - future.get(1, TimeUnit.MINUTES); - Thread.sleep(sleepTime); - return DAPR_FREEPORTS; - } + } - private static final String DAPR_RUN = "dapr run --app-id %s "; - private static final String DAPR_COMMAND = " -- mvn exec:java -D exec.mainClass=%s -D exec.classpathScope=\"test\" -Dexec.args=\"-p %d -grpcPort %d -httpPort %d\""; - - private String buildDaprCommand(){ - StringBuilder stringBuilder= new StringBuilder(String.format(DAPR_RUN, this.appName)) - .append(this.useAppPort ? "--app-port " + this.DAPR_FREEPORTS.appPort : "") - .append(" --grpc-port ") - .append(this.DAPR_FREEPORTS.grpcPort) - .append(" --port ") - .append(this.DAPR_FREEPORTS.httpPort) - .append(String.format(DAPR_COMMAND, this.serviceClass.getCanonicalName(),this.DAPR_FREEPORTS.appPort, this.DAPR_FREEPORTS.grpcPort, this.DAPR_FREEPORTS.httpPort)); - return stringBuilder.toString(); - } - - private void generateAppName(){ + } + } catch (IOException ex) { + Assert.fail(ex.getMessage()); + } + }); + + final ExecutorService executor = Executors.newSingleThreadExecutor(); + final Future future = executor.submit(stuffToDo); + executor.shutdown(); // This does not cancel the already-scheduled task. + future.get(1, TimeUnit.MINUTES); + Thread.sleep(sleepTime); + return DAPR_FREEPORTS; + } + + private static final String DAPR_RUN = "dapr run --app-id %s "; + private static final String DAPR_COMMAND = " -- mvn exec:java -D exec.mainClass=%s -D exec.classpathScope=\"test\" -Dexec.args=\"-p %d -grpcPort %d -httpPort %d\""; + + private String buildDaprCommand(){ + StringBuilder stringBuilder= new StringBuilder(String.format(DAPR_RUN, this.appName)) + .append(this.useAppPort ? "--app-port " + this.DAPR_FREEPORTS.appPort : "") + .append(" --grpc-port ") + .append(this.DAPR_FREEPORTS.grpcPort) + .append(" --port ") + .append(this.DAPR_FREEPORTS.httpPort) + .append(String.format(DAPR_COMMAND, this.serviceClass.getCanonicalName(),this.DAPR_FREEPORTS.appPort, this.DAPR_FREEPORTS.grpcPort, this.DAPR_FREEPORTS.httpPort)); + return stringBuilder.toString(); + } + + private void generateAppName(){ + + this.appName=UUID.randomUUID().toString(); + } + + private static Integer findRandomOpenPortOnAllLocalInterfaces() throws Exception { + try ( + ServerSocket socket = new ServerSocket(0) + ) { + return socket.getLocalPort(); - this.appName=UUID.randomUUID().toString(); + } + } + + public void destroyDapr() { + Optional.ofNullable(rt).ifPresent( runtime -> { + try { + runtime.exec("dapr stop --app-id " + this.appName); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + Optional.ofNullable(proc).ifPresent(process -> process.destroy()); + } + + public static class DaprFreePorts + { + public DaprFreePorts initPorts() throws Exception { + this.appPort= findRandomOpenPortOnAllLocalInterfaces(); + this.grpcPort= findRandomOpenPortOnAllLocalInterfaces(); + this.httpPort= findRandomOpenPortOnAllLocalInterfaces(); + return this; } - private static Integer findRandomOpenPortOnAllLocalInterfaces() throws Exception { - try ( - ServerSocket socket = new ServerSocket(0) - ) { - return socket.getLocalPort(); + private int grpcPort; - } + public int getGrpcPort() { + return grpcPort; } - public void destroyDapr() { - Optional.ofNullable(rt).ifPresent( runtime -> { - try { - runtime.exec("dapr stop --app-id " + this.appName); - } catch (IOException e) { - throw new RuntimeException(e); - } - }); - Optional.ofNullable(proc).ifPresent(process -> process.destroy()); + public int getHttpPort() { + return httpPort; } + public int getAppPort() { + return appPort; + } + private int httpPort; - - - public static class DaprFreePorts - { - public DaprFreePorts initPorts() throws Exception { - this.appPort= findRandomOpenPortOnAllLocalInterfaces(); - this.grpcPort= findRandomOpenPortOnAllLocalInterfaces(); - this.httpPort= findRandomOpenPortOnAllLocalInterfaces(); - return this; - } - - private int grpcPort; - - public int getGrpcPort() { - return grpcPort; - } - - - - public int getHttpPort() { - return httpPort; - } - - - - public int getAppPort() { - return appPort; - } - - - private int httpPort; - - private int appPort; - } + private int appPort; + } } diff --git a/sdk/src/test/java/io/dapr/it/services/EmptyService.java b/sdk/src/test/java/io/dapr/it/services/EmptyService.java index d43c45d891..9f6f6ecee0 100644 --- a/sdk/src/test/java/io/dapr/it/services/EmptyService.java +++ b/sdk/src/test/java/io/dapr/it/services/EmptyService.java @@ -5,7 +5,6 @@ package io.dapr.it.services; - /** * Use this class in order to run DAPR with any needed services, like states. */ diff --git a/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java b/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java index 8801d43e38..637e692c4a 100644 --- a/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java +++ b/sdk/src/test/java/io/dapr/it/state/HelloWorldClientIT.java @@ -18,63 +18,59 @@ import static io.dapr.it.DaprIntegrationTestingRunner.DAPR_FREEPORTS; - public class HelloWorldClientIT extends BaseIT { + private static DaprIntegrationTestingRunner daprIntegrationTestingRunner; - private static DaprIntegrationTestingRunner daprIntegrationTestingRunner; + @BeforeClass + public static void init() throws Exception { + daprIntegrationTestingRunner = + createDaprIntegrationTestingRunner( + "BUILD SUCCESS", + HelloWorldGrpcStateService.class, + false, + 2000 + ); + daprIntegrationTestingRunner.initializeDapr(); + } + @Test + public void testHelloWorldState(){ + ManagedChannel channel = + ManagedChannelBuilder.forAddress("localhost", DAPR_FREEPORTS.getGrpcPort()).usePlaintext().build(); + DaprGrpc.DaprBlockingStub client = DaprGrpc.newBlockingStub(channel); - @BeforeClass - public static void init() throws Exception { - daprIntegrationTestingRunner = - createDaprIntegrationTestingRunner( - "BUILD SUCCESS", - HelloWorldGrpcStateService.class, - false, - 2000 - ); - daprIntegrationTestingRunner.initializeDapr(); + String key = "mykey"; + { + DaprProtos.GetStateEnvelope req = DaprProtos.GetStateEnvelope + .newBuilder() + .setKey(key) + .build(); + DaprProtos.GetStateResponseEnvelope response = client.getState(req); + String value = response.getData().getValue().toStringUtf8(); + System.out.println("Got: " + value); + Assert.assertEquals("Hello World",value); } - @Test - public void testHelloWorldState(){ - ManagedChannel channel = - ManagedChannelBuilder.forAddress("localhost", DAPR_FREEPORTS.getGrpcPort()).usePlaintext().build(); - DaprGrpc.DaprBlockingStub client = DaprGrpc.newBlockingStub(channel); - - String key = "mykey"; - { - DaprProtos.GetStateEnvelope req = DaprProtos.GetStateEnvelope - .newBuilder() - .setKey(key) - .build(); - DaprProtos.GetStateResponseEnvelope response = client.getState(req); - String value = response.getData().getValue().toStringUtf8(); - System.out.println("Got: " + value); - Assert.assertEquals("Hello World",value); - } - - // Then, delete it. - { - DaprProtos.DeleteStateEnvelope req = DaprProtos.DeleteStateEnvelope - .newBuilder() - .setKey(key) - .build(); - client.deleteState(req); - System.out.println("Deleted!"); - } - - { - DaprProtos.GetStateEnvelope req = DaprProtos.GetStateEnvelope - .newBuilder() - .setKey(key) - .build(); - DaprProtos.GetStateResponseEnvelope response = client.getState(req); - String value = response.getData().getValue().toStringUtf8(); - System.out.println("Got: " + value); - Assert.assertEquals("",value); - } + // Then, delete it. + { + DaprProtos.DeleteStateEnvelope req = DaprProtos.DeleteStateEnvelope + .newBuilder() + .setKey(key) + .build(); + client.deleteState(req); + System.out.println("Deleted!"); } + { + DaprProtos.GetStateEnvelope req = DaprProtos.GetStateEnvelope + .newBuilder() + .setKey(key) + .build(); + DaprProtos.GetStateResponseEnvelope response = client.getState(req); + String value = response.getData().getValue().toStringUtf8(); + System.out.println("Got: " + value); + Assert.assertEquals("",value); + } + } } diff --git a/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java b/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java index 2cdb7fad77..454c01164a 100644 --- a/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java +++ b/sdk/src/test/java/io/dapr/it/state/HttpStateClientIT.java @@ -20,88 +20,84 @@ */ public class HttpStateClientIT extends BaseIT { - @BeforeClass - public static void init() throws Exception { - daprIntegrationTestingRunner = - createDaprIntegrationTestingRunner( - "BUILD SUCCESS", - EmptyService.class, - false, - 0 - ); - daprIntegrationTestingRunner.initializeDapr(); - } - - - - @Test - public void saveAndGetState() { - - final String stateKey= "myKey"; - - DaprClient daprClient= new DaprClientBuilder().build(); - MyData data= new MyData(); - data.setPropertyA("data in property A"); - data.setPropertyB("data in property B"); - Mono saveResponse= daprClient.saveState(stateKey,null,data, null); - saveResponse.block(); - - Mono> response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); - StateKeyValue myDataResponse=response.block(); - - Assert.assertEquals("data in property A",myDataResponse.getValue().getPropertyA()); - Assert.assertEquals("data in property B",myDataResponse.getValue().getPropertyB()); - } - - @Test - public void saveUpdateAndGetState() { - final String stateKey= "keyToBeUpdated"; - - DaprClient daprClient= new DaprClientBuilder().build(); - MyData data= new MyData(); - data.setPropertyA("data in property A"); - data.setPropertyB("data in property B"); - Mono saveResponse= daprClient.saveState(stateKey,null,data, null); - saveResponse.block(); - - - data.setPropertyA("data in property A"); - data.setPropertyB("data in property B2"); - saveResponse= daprClient.saveState(stateKey,null,data, null); - saveResponse.block(); - - Mono> response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); - StateKeyValue myDataResponse=response.block(); - - Assert.assertEquals("data in property A",myDataResponse.getValue().getPropertyA()); - Assert.assertEquals("data in property B2",myDataResponse.getValue().getPropertyB()); - } - - @Test - public void saveAndDeleteState() { - final String stateKey= "myeKeyToBeDeleted"; - - DaprClient daprClient= new DaprClientBuilder().build(); - MyData data= new MyData(); - data.setPropertyA("data in property A"); - data.setPropertyB("data in property B"); - Mono saveResponse= daprClient.saveState(stateKey,null,data, null); - saveResponse.block(); - - Mono> response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); - StateKeyValue myDataResponse=response.block(); - - Assert.assertEquals("data in property A",myDataResponse.getValue().getPropertyA()); - Assert.assertEquals("data in property B",myDataResponse.getValue().getPropertyB()); - - Mono deleteResponse= daprClient.deleteState( new StateKeyValue(null,stateKey,null),null); - deleteResponse.block(); - - response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); - myDataResponse=response.block(); - - Assert.assertNull(myDataResponse.getValue()); - - } + @BeforeClass + public static void init() throws Exception { + daprIntegrationTestingRunner = + createDaprIntegrationTestingRunner( + "BUILD SUCCESS", + EmptyService.class, + false, + 0 + ); + daprIntegrationTestingRunner.initializeDapr(); + } + + @Test + public void saveAndGetState() { + + final String stateKey= "myKey"; + + DaprClient daprClient= new DaprClientBuilder().build(); + MyData data= new MyData(); + data.setPropertyA("data in property A"); + data.setPropertyB("data in property B"); + Mono saveResponse= daprClient.saveState(stateKey,null,data, null); + saveResponse.block(); + + Mono> response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); + StateKeyValue myDataResponse=response.block(); + + Assert.assertEquals("data in property A",myDataResponse.getValue().getPropertyA()); + Assert.assertEquals("data in property B",myDataResponse.getValue().getPropertyB()); + } + + @Test + public void saveUpdateAndGetState() { + final String stateKey= "keyToBeUpdated"; + + DaprClient daprClient= new DaprClientBuilder().build(); + MyData data= new MyData(); + data.setPropertyA("data in property A"); + data.setPropertyB("data in property B"); + Mono saveResponse= daprClient.saveState(stateKey,null,data, null); + saveResponse.block(); + + data.setPropertyA("data in property A"); + data.setPropertyB("data in property B2"); + saveResponse= daprClient.saveState(stateKey,null,data, null); + saveResponse.block(); + + Mono> response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); + StateKeyValue myDataResponse=response.block(); + + Assert.assertEquals("data in property A",myDataResponse.getValue().getPropertyA()); + Assert.assertEquals("data in property B2",myDataResponse.getValue().getPropertyB()); + } + + @Test + public void saveAndDeleteState() { + final String stateKey= "myeKeyToBeDeleted"; + + DaprClient daprClient= new DaprClientBuilder().build(); + MyData data= new MyData(); + data.setPropertyA("data in property A"); + data.setPropertyB("data in property B"); + Mono saveResponse= daprClient.saveState(stateKey,null,data, null); + saveResponse.block(); + + Mono> response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); + StateKeyValue myDataResponse=response.block(); + + Assert.assertEquals("data in property A",myDataResponse.getValue().getPropertyA()); + Assert.assertEquals("data in property B",myDataResponse.getValue().getPropertyB()); + + Mono deleteResponse= daprClient.deleteState( new StateKeyValue(null,stateKey,null),null); + deleteResponse.block(); + + response= daprClient.getState( new StateKeyValue(null,stateKey,null),null,MyData.class); + myDataResponse=response.block(); + + Assert.assertNull(myDataResponse.getValue()); + } } From cfcca1df0cb263bf02fbde5b177b273395f73193 Mon Sep 17 00:00:00 2001 From: Artur Souza Date: Mon, 13 Jan 2020 17:56:16 -0800 Subject: [PATCH 8/8] Adding license to DaprIntegrationTestingRunner --- .../test/java/io/dapr/it/DaprIntegrationTestingRunner.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java b/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java index b38b6c639c..504b24722f 100644 --- a/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java +++ b/sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java @@ -1,3 +1,8 @@ +/* + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + */ + package io.dapr.it; import org.junit.Assert;