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
17 changes: 17 additions & 0 deletions sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Comment thread
artursouza marked this conversation as resolved.
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<skipITs>true</skipITs>
</properties>
Expand Down
115 changes: 115 additions & 0 deletions sdk/src/test/java/io/dapr/client/DaprHttpTest.java
Original file line number Diff line number Diff line change
@@ -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());
}

}