Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -262,7 +260,7 @@ public String getPropertyB() {
return propertyB;
}

public void setPropertyB(String propActorProxyBuilderTestertyB) {
public void setPropertyB(String propertyB) {
this.propertyB = propertyB;
}

Expand Down
1 change: 1 addition & 0 deletions sdk-springboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.2.RELEASE</version>
</plugin>
</plugins>
</build>
Expand Down
24 changes: 23 additions & 1 deletion sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,25 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.gmazzo</groupId>
<artifactId>okhttp-mock</artifactId>
<version>1.3.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.19.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<skipITs>true</skipITs>
</properties>
Expand Down Expand Up @@ -112,11 +123,22 @@
</configuration>
<executions>
<execution>
<id>verify</id>
<phase>verify</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>

</executions>
</plugin>
<plugin>
Expand Down
1 change: 1 addition & 0 deletions sdk/src/main/java/io/dapr/client/DaprClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ private DaprClient buildDaprClientGrpc() {
* @return
*/
private DaprClient buildDaprClientHttp() {
int port=DaprClientBuilder.getEnvPortOrDefault();
if (port <= 0) {
throw new IllegalStateException("Invalid port.");
}
Expand Down
19 changes: 8 additions & 11 deletions sdk/src/main/java/io/dapr/client/DaprClientHttpAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -183,13 +180,13 @@ public <T> Mono<StateKeyValue<T>> getState(StateKeyValue<T> state, StateOptions
StringBuilder url = new StringBuilder(Constants.STATE_PATH)
.append("/")
.append(state.getKey());
Map<String, String> urlParameters = stateOptions.getStateOptionsAsMap();
Map<String, String> 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 -> {
try {
return Mono.just(buildStateKeyValue(s, state.getKey(), clazz));
} catch (Exception ex) {
}catch (Exception ex){
return Mono.error(ex);
}
});
Expand All @@ -207,14 +204,14 @@ public <T> Mono<Void> saveStates(List<StateKeyValue<T>> states, StateOptions opt
if (states == null || states.isEmpty()) {
return Mono.empty();
}
Map<String, String> headers = new HashMap<>();
String etag = states.stream().filter(state -> null != state.getEtag() && !state.getEtag().trim().isEmpty())
final Map<String, String> 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<String, String> urlParameter = options.getStateOptionsAsMap();
final String url = Constants.STATE_PATH;
Map<String, String> 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();
Expand Down Expand Up @@ -249,7 +246,7 @@ public <T> Mono<Void> deleteState(StateKeyValue<T> state, StateOptions options)
headers.put(Constants.HEADER_HTTP_ETAG_ID, state.getEtag());
}
String url = Constants.STATE_PATH + "/" + state.getKey();
Map<String, String> urlParameters = options.getStateOptionsAsMap();
Map<String, String> 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);
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/main/java/io/dapr/utils/ObjectSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ public <T> T deserialize(Object value, Class<T> 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);
}

Expand Down
81 changes: 81 additions & 0 deletions sdk/src/test/java/io/dapr/it/BaseIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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;
}
}
}
146 changes: 146 additions & 0 deletions sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*/

package io.dapr.it;

import org.junit.Assert;

import java.io.*;
import java.net.ServerSocket;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.*;


public class DaprIntegrationTestingRunner {

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

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(){

this.appName=UUID.randomUUID().toString();
}

private static Integer findRandomOpenPortOnAllLocalInterfaces() throws Exception {
try (
ServerSocket socket = new ServerSocket(0)
) {
return socket.getLocalPort();

}
}

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 int grpcPort;

public int getGrpcPort() {
return grpcPort;
}

public int getHttpPort() {
return httpPort;
}

public int getAppPort() {
return appPort;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep single spaces.

private int httpPort;

private int appPort;
}
}
Loading