Skip to content

Commit

Permalink
validating managed beans for workflows and activities
Browse files Browse the repository at this point in the history
Signed-off-by: salaboy <[email protected]>
  • Loading branch information
salaboy committed Dec 10, 2024
1 parent f7b8f3a commit a98ef07
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ DaprClient daprClient(DaprClientBuilder daprClientBuilder) {
@Bean
@ConditionalOnMissingBean
DaprWorkflowClient daprWorkflowClient(DaprConnectionDetails daprConnectionDetails) {
final Properties properties = createPropertiesFromConnectionDetails(daprConnectionDetails);
Properties properties = createPropertiesFromConnectionDetails(daprConnectionDetails);
return new DaprWorkflowClient(properties);
}

@Bean
@ConditionalOnMissingBean
WorkflowRuntimeBuilder daprWorkflowRuntimeBuilder(DaprConnectionDetails daprConnectionDetails) {
final Properties properties = createPropertiesFromConnectionDetails(daprConnectionDetails);
Properties properties = createPropertiesFromConnectionDetails(daprConnectionDetails);
return new WorkflowRuntimeBuilder(properties);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.dapr.spring.boot.autoconfigure.client.workflows.TestActivity;
import io.dapr.spring.boot.autoconfigure.client.workflows.TestWorkflow;
import io.dapr.workflows.client.DaprWorkflowClient;
import io.dapr.workflows.runtime.WorkflowRuntimeBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -15,9 +16,29 @@ public class DaprWorkflowsRegistrationTests {
@Autowired
private DaprWorkflowClient daprWorkflowClient;

@Autowired
private WorkflowRuntimeBuilder workflowRuntimeBuilder;

@Autowired
private TestActivity testActivity;

@Autowired
private TestWorkflow testWorkflow;

@Test
public void doThat(){
public void testWorkflowInjection(){

//I cannot test here if the client works, as it needs the runtime
assertNotNull(daprWorkflowClient);


//@TODO: there is no way to assert the runtime and its registered workflows and activities
assertNotNull(workflowRuntimeBuilder);

//Check that both Activities and Workflows are managed beans
assertNotNull(testActivity);
assertNotNull(testWorkflow);
assertNotNull(testActivity.getRestTemplate());
assertNotNull(testWorkflow.getRestTemplate());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
import io.dapr.spring.workflows.config.EnableDaprWorkflows;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDaprWorkflows
public class WorkflowTestApplication {
public static void main(String[] args) {
SpringApplication.run(WorkflowTestApplication.class, args);
}

@Configuration
static class Config {
@Bean
RestTemplate restTemplate(){
return new RestTemplate();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

import io.dapr.workflows.runtime.WorkflowActivity;
import io.dapr.workflows.runtime.WorkflowActivityContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

public class TestActivity implements WorkflowActivity {

@Autowired
private RestTemplate restTemplate;

@Override
public Object run(WorkflowActivityContext ctx) {
return null;
return "OK";
}

public RestTemplate getRestTemplate() {
return restTemplate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@

import io.dapr.workflows.Workflow;
import io.dapr.workflows.WorkflowStub;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

public class TestWorkflow extends Workflow {

@Autowired
private RestTemplate restTemplate;

@Override
public WorkflowStub create() {
return null;
return ctx -> {
ctx.callActivity(TestActivity.class.getName(), null).await();
};
}

public RestTemplate getRestTemplate() {
return restTemplate;
}
}
2 changes: 1 addition & 1 deletion dapr-spring/dapr-spring-workflows/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>dapr-spring-workflows</artifactId>
<name>dapr-spring-workflow</name>
<name>dapr-spring-workflows</name>
<description>Dapr Spring Workflows</description>
<packaging>jar</packaging>

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,32 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;

import java.util.Map;

@Configuration
@ComponentScan("io.dapr.spring.workflows.config")
public class DaprWorkflowsConfig implements ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(DaprWorkflowsConfig.class);
@Autowired
public class DaprWorkflowsConfiguration implements ApplicationContextAware {
private static final Logger LOGGER = LoggerFactory.getLogger(DaprWorkflowsConfiguration.class);

private WorkflowRuntimeBuilder workflowRuntimeBuilder;

private static ApplicationContext context;
public DaprWorkflowsConfiguration(WorkflowRuntimeBuilder workflowRuntimeBuilder) {
this.workflowRuntimeBuilder = workflowRuntimeBuilder;
}

/**
* Register workflows and activities to the workflowRuntimeBuilder.
*/
public void registerWorkflowsAndActivities() {
public void registerWorkflowsAndActivities(ApplicationContext applicationContext) {
LOGGER.info("Registering Dapr Workflows and Activities");
Map<String, Workflow> workflowBeans = context.getBeansOfType(Workflow.class);
Map<String, Workflow> workflowBeans = applicationContext.getBeansOfType(Workflow.class);
for (Workflow w : workflowBeans.values()) {
LOGGER.info("Dapr Workflow: '{}' registered", w.getClass().getName());
workflowRuntimeBuilder.registerWorkflow(w.getClass());
}

Map<String, WorkflowActivity> workflowActivitiesBeans = context.getBeansOfType(WorkflowActivity.class);
Map<String, WorkflowActivity> workflowActivitiesBeans = applicationContext.getBeansOfType(WorkflowActivity.class);
for (WorkflowActivity a : workflowActivitiesBeans.values()) {
LOGGER.info("Dapr Workflow Activity: '{}' registered", a.getClass().getName());
workflowRuntimeBuilder.registerActivity(a.getClass());
Expand All @@ -51,7 +49,6 @@ public void registerWorkflowsAndActivities() {

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
registerWorkflowsAndActivities();
registerWorkflowsAndActivities(applicationContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

@Retention(RUNTIME)
@Target(TYPE)
@Import(DaprWorkflowsConfig.class)
@Import(DaprWorkflowsConfiguration.class)
public @interface EnableDaprWorkflows {
}

0 comments on commit a98ef07

Please sign in to comment.