Skip to content

Commit

Permalink
Add actor testcontainer tests
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Kaps <ck-github@mohiva.com>
  • Loading branch information
akkie committed Jan 20, 2025
1 parent be0e56b commit bec4bd2
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.dapr.it.testcontainers;

import io.dapr.actors.ActorId;
import io.dapr.actors.client.ActorClient;
import io.dapr.actors.client.ActorProxyBuilder;
import io.dapr.testcontainers.Component;
import io.dapr.testcontainers.DaprContainer;
import io.dapr.testcontainers.DaprLogLevel;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.Network;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.util.Map;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest(
webEnvironment = WebEnvironment.RANDOM_PORT,
classes = {
TestDaprActorsConfiguration.class,
TestActorsApplication.class
}
)
@Testcontainers
@Tag("testcontainers")
public class DaprActorsIT {
private static final Network DAPR_NETWORK = Network.newNetwork();

@Container
private static final DaprContainer DAPR_CONTAINER = new DaprContainer("daprio/daprd:1.13.2")
.withAppName("actor-dapr-app")
.withNetwork(DAPR_NETWORK)
.withComponent(new Component("kvstore", "state.in-memory", "v1",
Map.of("actorStateStore", "true")))
.withDaprLogLevel(DaprLogLevel.DEBUG)
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
.withAppChannelAddress("host.testcontainers.internal");

/**
* Expose the Dapr ports to the host.
*
* @param registry the dynamic property registry
*/
@DynamicPropertySource
static void daprProperties(DynamicPropertyRegistry registry) {
registry.add("dapr.http.endpoint", DAPR_CONTAINER::getHttpEndpoint);
registry.add("dapr.grpc.endpoint", DAPR_CONTAINER::getGrpcEndpoint);
}

@Autowired
private ActorClient actorClient;

@Test
public void testWorkflows() throws Exception {
ActorProxyBuilder<TestActor> builder = new ActorProxyBuilder(TestActor.class, actorClient);
ActorId actorId = ActorId.createRandom();
TestActor actor = builder.build(actorId);

String message = UUID.randomUUID().toString();

String echoedMessage = actor.echo(message);

assertEquals(echoedMessage, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.dapr.it.testcontainers;
import io.dapr.actors.ActorMethod;
import io.dapr.actors.ActorType;

@ActorType(name = "TestActor")
public interface TestActor {
@ActorMethod(name = "echo_message")
String echo(String message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.dapr.it.testcontainers;

import io.dapr.actors.ActorId;
import io.dapr.actors.runtime.AbstractActor;
import io.dapr.actors.runtime.ActorRuntimeContext;

public class TestActorImpl extends AbstractActor implements TestActor {
public TestActorImpl(ActorRuntimeContext runtimeContext, ActorId id) {
super(runtimeContext, id);
}

@Override
public String echo(String message) {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2024 The Dapr Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
limitations under the License.
*/

package io.dapr.it.testcontainers;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TestActorsApplication {

public static void main(String[] args) {
SpringApplication.run(TestActorsApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.dapr.it.testcontainers;

import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.dapr.actors.client.ActorClient;
import io.dapr.config.Properties;

@Configuration
public class TestDaprActorsConfiguration {
@Bean
public ActorClient daprActorClient(
@Value("${dapr.http.endpoint}") String daprHttpEndpoint,
@Value("${dapr.grpc.endpoint}") String daprGrpcEndpoint
){
Map<String, String> overrides = Map.of(
"dapr.http.endpoint", daprHttpEndpoint,
"dapr.grpc.endpoint", daprGrpcEndpoint
);

return new ActorClient(new Properties(overrides));
}
}

0 comments on commit bec4bd2

Please sign in to comment.