Skip to content
Merged
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
111 changes: 89 additions & 22 deletions sdk/src/test/java/io/dapr/client/DaprHttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,42 @@ public class DaprHttpTest {
@Before
public void setUp() throws Exception {
mockInterceptor = new MockInterceptor(Behavior.UNORDERED);
okHttpClient = new OkHttpClient.Builder().addInterceptor(mockInterceptor).build();
okHttpClient = new OkHttpClient.Builder().addInterceptor(mockInterceptor).build();
}

@Test
public void invokePostMethod() throws IOException {
public void invokeMethod() throws IOException {

Map<String, String> headers = new HashMap<>();
headers.put("content-type", "text/html");
headers.put("header1", "value1");

mockInterceptor.addRule()
.post("http://localhost:3500/v1.0/state")
.respond(EXPECTED_RESULT);

DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST", "v1.0/state", null, (byte[])null, headers);

Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST","v1.0/state",null, null);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT,body);
assertEquals(EXPECTED_RESULT, body);
}

@Test
public void invokePostMethod() throws IOException {

mockInterceptor.addRule()
.post("http://localhost:3500/v1.0/state")
.respond(EXPECTED_RESULT)
.addHeader("Header","Value");

DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);

Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST","v1.0/state",null,"", null);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT, body);

}

Expand All @@ -60,10 +80,10 @@ public void invokeDeleteMethod() throws IOException {

DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);

Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("DELETE","v1.0/state", null, null);
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("DELETE","v1.0/state", null, (String) null,null);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT,body);
assertEquals(EXPECTED_RESULT, body);

}

Expand All @@ -79,47 +99,94 @@ public void invokeGetMethod() throws IOException {
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET","v1.0/get",null, null);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT,body);
assertEquals(EXPECTED_RESULT, body);

}

@Test
public void invokeMethodWithHeaders() throws IOException {

Map<String, String> headers = new HashMap<>();
headers.put("header","value");
headers.put("header1","value1");
headers.put("header", "value");
headers.put("header1", "value1");

Map<String, String> urlParameters = new HashMap<>();
urlParameters.put("orderId", "41");

mockInterceptor.addRule()
.get("http://localhost:3500/v1.0/get?header1=value1&header=value")

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.

Do we have any other test for params?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added another one for parameters. When i fix the styling issue, i will push that test as well.

.get("http://localhost:3500/v1.0/state/order?orderId=41")
.respond(EXPECTED_RESULT);
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);

Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET","v1.0/get", headers, null);
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET","v1.0/state/order", urlParameters, headers);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT,body);
assertEquals(EXPECTED_RESULT, body);

}

@Test(expected = RuntimeException.class)
public void invokeMethodRuntimeException() throws IOException {
public void invokePostMethodRuntime() throws IOException {

Map<String, String> headers = new HashMap<>();
headers.put("header","value");
headers.put("header1","value1");
mockInterceptor.addRule()
.post("http://localhost:3500/v1.0/state")
.respond(500);

DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);

Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST","v1.0/state",null, null);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT, body);

}

@Test(expected = RuntimeException.class)
public void invokePostDaprError() throws IOException {

mockInterceptor.addRule()
.get("http://localhost:3500/v1.0/get")
.respond(500, ResponseBody.create(MediaType.parse("application/json"),
"{\"errorCode\":\"500\",\"message\":\"Error\"}"));
.post("http://localhost:3500/v1.0/state")
.respond(500, ResponseBody.create(MediaType.parse("text"),
"{\"errorCode\":null,\"message\":null}"));

DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);

Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("GET","v1.0/get", headers, null);
Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST","v1.0/state",null, null);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT,body);
assertEquals(EXPECTED_RESULT, body);

}

@Test(expected = RuntimeException.class)
public void invokePostMethodUnknownError() throws IOException {

mockInterceptor.addRule()
.post("http://localhost:3500/v1.0/state")
.respond(500, ResponseBody.create(MediaType.parse("application/json"),
"{\"errorCode\":\"null\",\"message\":\"null\"}"));

DaprHttp daprHttp = new DaprHttp(3500,okHttpClient);

Mono<DaprHttp.Response> mono = daprHttp.invokeAPI("POST","v1.0/state",null, null);
DaprHttp.Response response = mono.block();
String body = serializer.deserialize(response.getBody(), String.class);
assertEquals(EXPECTED_RESULT, body);

}

@Test
public void getHeadersAndStatus(){

mockInterceptor.addRule()
.post("http://localhost:3500/v1.0/state")
.respond(500, ResponseBody.create(MediaType.parse("application/json"),
"{\"errorCode\":\"null\",\"message\":\"null\"}"));

DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);

System.out.println(daprHttp);

}

}
}