Java sdk wip (#77)

* Fixing issue regarding GET and DELETE method

* Adding unit test for DaprHttp.java

* Adding test scope

* Renaming a property and fixing conflict to merge.

Co-authored-by: Artur Souza <artursouza.ms@outlook.com>
This commit is contained in:
mestizoLopez 2020-01-10 16:52:21 -06:00 committed by Artur Souza
parent 9dd89da7e1
commit 44a2806659
2 changed files with 132 additions and 0 deletions

View File

@ -15,6 +15,16 @@
<version>0.2.0</version>
<name>dapr-sdk</name>
<description>SDK for Dapr</description>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>libs-release</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<dependencies>
<dependency>
@ -52,7 +62,14 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.gmazzo</groupId>
<artifactId>okhttp-mock</artifactId>
<version>1.3.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<skipITs>true</skipITs>
</properties>

View File

@ -0,0 +1,115 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*/
package io.dapr.client;
import okhttp3.*;
import okhttp3.mock.Behavior;
import okhttp3.mock.MockInterceptor;
import org.junit.Before;
import org.junit.Test;
import reactor.core.publisher.Mono;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
public class DaprHttpTest {
private OkHttpClient okHttpClient;
private MockInterceptor mockInterceptor;
private final String EXPECTED_RESULT = "{\"data\":\"ewoJCSJwcm9wZXJ0eUEiOiAidmFsdWVBIiwKCQkicHJvcGVydHlCIjogInZhbHVlQiIKCX0=\"}";
@Before
public void setUp() throws Exception {
mockInterceptor = new MockInterceptor(Behavior.UNORDERED);
okHttpClient = new OkHttpClient.Builder().addInterceptor(mockInterceptor).build();
}
@Test
public void invokePostMethod() throws IOException {
mockInterceptor.addRule()
.post("http://localhost:3500/v1.0/state")
.respond(EXPECTED_RESULT);
DaprHttp daprHttp = new DaprHttp("http://localhost",3500,okHttpClient);
Mono<String> mono = daprHttp.invokeAPI("POST","v1.0/state",null);
assertEquals(EXPECTED_RESULT,mono.block());
}
@Test
public void invokeDeleteMethod() throws IOException {
mockInterceptor.addRule()
.delete("http://localhost:3500/v1.0/state")
.respond(EXPECTED_RESULT);
DaprHttp daprHttp = new DaprHttp("http://localhost",3500,okHttpClient);
Mono<String> mono = daprHttp.invokeAPI("DELETE","v1.0/state",null);
assertEquals(EXPECTED_RESULT,mono.block());
}
@Test
public void invokeGetMethod() throws IOException {
mockInterceptor.addRule()
.get("http://localhost:3500/v1.0/get")
.respond(EXPECTED_RESULT);
DaprHttp daprHttp = new DaprHttp("http://localhost",3500,okHttpClient);
Mono<String> mono = daprHttp.invokeAPI("GET","v1.0/get",null);
assertEquals(EXPECTED_RESULT,mono.block());
}
@Test
public void invokeMethodWithHeaders() {
Map<String, String> headers = new HashMap<>();
headers.put("header","value");
headers.put("header1","value1");
mockInterceptor.addRule()
.get("http://localhost:3500/v1.0/get")
.respond(EXPECTED_RESULT);
DaprHttp daprHttp = new DaprHttp("http://localhost",3500,okHttpClient);
Mono<String> mono = daprHttp.invokeAPI("GET","v1.0/get",headers);
assertEquals(EXPECTED_RESULT,mono.block());
}
@Test(expected = RuntimeException.class)
public void invokeMethodRuntimeException(){
Map<String, String> headers = new HashMap<>();
headers.put("header","value");
headers.put("header1","value1");
mockInterceptor.addRule()
.get("http://localhost:3500/v1.0/get")
.respond(500, ResponseBody.create(MediaType.parse("application/json"),
"{\"errorCode\":\"500\",\"message\":\"Error\"}"));
DaprHttp daprHttp = new DaprHttp("http://localhost",3500,okHttpClient);
Mono<String> mono = daprHttp.invokeAPI("GET","v1.0/get",headers);
assertEquals(EXPECTED_RESULT,mono.block());
}
}