Skip to content

Commit

Permalink
using mockbean
Browse files Browse the repository at this point in the history
  • Loading branch information
apanicker-nflx committed Aug 8, 2022
1 parent 4be61d0 commit e1cb986
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.test.context.ContextConfiguration;
Expand Down Expand Up @@ -49,6 +50,7 @@
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

@SpringBootTest
@ContextConfiguration(
classes = {
TestObjectMapperConfiguration.class,
Expand All @@ -57,29 +59,15 @@
@RunWith(SpringRunner.class)
public class TestSimpleActionProcessor {

private WorkflowExecutor workflowExecutor;
private ExternalPayloadStorageUtils externalPayloadStorageUtils;
private SimpleActionProcessor actionProcessor;
@MockBean private WorkflowExecutor workflowExecutor;
@MockBean private ExternalPayloadStorageUtils externalPayloadStorageUtils;

@Autowired private ObjectMapper objectMapper;

@EnableRetry
@Configuration
public static class TestConfiguration {}

@Before
public void setup() {
externalPayloadStorageUtils = mock(ExternalPayloadStorageUtils.class);

workflowExecutor = mock(WorkflowExecutor.class);

actionProcessor =
new SimpleActionProcessor(
workflowExecutor,
new ParametersUtils(objectMapper),
new JsonUtils(objectMapper));
}

@SuppressWarnings({"unchecked", "rawtypes"})
@Test
public void testStartWorkflow_correlationId() throws Exception {
Expand Down Expand Up @@ -114,6 +102,11 @@ public void testStartWorkflow_correlationId() throws Exception {
anyMap()))
.thenReturn("workflow_1");

SimpleActionProcessor actionProcessor =
new SimpleActionProcessor(
workflowExecutor,
new ParametersUtils(objectMapper),
new JsonUtils(objectMapper));
Map<String, Object> output =
actionProcessor.execute(action, payload, "testEvent", "testMessage");

Expand Down Expand Up @@ -170,6 +163,11 @@ public void testStartWorkflow() throws Exception {
anyMap()))
.thenReturn("workflow_1");

SimpleActionProcessor actionProcessor =
new SimpleActionProcessor(
workflowExecutor,
new ParametersUtils(objectMapper),
new JsonUtils(objectMapper));
Map<String, Object> output =
actionProcessor.execute(action, payload, "testEvent", "testMessage");

Expand Down Expand Up @@ -220,6 +218,11 @@ public void testCompleteTask() throws Exception {
when(workflowExecutor.getWorkflow(eq("workflow_1"), anyBoolean())).thenReturn(workflow);
doNothing().when(externalPayloadStorageUtils).verifyAndUpload(any(), any());

SimpleActionProcessor actionProcessor =
new SimpleActionProcessor(
workflowExecutor,
new ParametersUtils(objectMapper),
new JsonUtils(objectMapper));
actionProcessor.execute(action, payload, "testEvent", "testMessage");

ArgumentCaptor<TaskResult> argumentCaptor = ArgumentCaptor.forClass(TaskResult.class);
Expand Down Expand Up @@ -267,6 +270,11 @@ public void testCompleteLoopOverTask() throws Exception {
when(workflowExecutor.getWorkflow(eq("workflow_1"), anyBoolean())).thenReturn(workflow);
doNothing().when(externalPayloadStorageUtils).verifyAndUpload(any(), any());

SimpleActionProcessor actionProcessor =
new SimpleActionProcessor(
workflowExecutor,
new ParametersUtils(objectMapper),
new JsonUtils(objectMapper));
actionProcessor.execute(action, payload, "testEvent", "testMessage");

ArgumentCaptor<TaskResult> argumentCaptor = ArgumentCaptor.forClass(TaskResult.class);
Expand Down Expand Up @@ -309,6 +317,11 @@ public void testCompleteTaskByTaskId() throws Exception {
when(workflowExecutor.getTask(eq("task_1"))).thenReturn(task);
doNothing().when(externalPayloadStorageUtils).verifyAndUpload(any(), any());

SimpleActionProcessor actionProcessor =
new SimpleActionProcessor(
workflowExecutor,
new ParametersUtils(objectMapper),
new JsonUtils(objectMapper));
actionProcessor.execute(action, payload, "testEvent", "testMessage");

ArgumentCaptor<TaskResult> argumentCaptor = ArgumentCaptor.forClass(TaskResult.class);
Expand All @@ -326,15 +339,14 @@ public void testCompleteTaskByTaskId() throws Exception {
@Test
public void testExecuteTransientException() {

WorkflowExecutor workflowExec = mock(WorkflowExecutor.class);
AtomicInteger executeInvoked = new AtomicInteger(0);
doAnswer(
(Answer<String>)
invocation -> {
executeInvoked.incrementAndGet();
throw new TransientException("some retriable error");
})
.when(workflowExec)
.when(workflowExecutor)
.startWorkflow(
anyString(),
anyInt(),
Expand All @@ -344,9 +356,9 @@ public void testExecuteTransientException() {
anyString(),
anyMap());

SimpleActionProcessor actionProc =
SimpleActionProcessor actionProcessor =
new SimpleActionProcessor(
workflowExec,
workflowExecutor,
new ParametersUtils(objectMapper),
new JsonUtils(objectMapper));

Expand All @@ -358,7 +370,7 @@ public void testExecuteTransientException() {
action.setAction(Type.start_workflow);
action.setStart_workflow(startWorkflow);

actionProc.execute(action, "{\"key\": \"value\"}", "testEvent", "testMessage");
actionProcessor.execute(action, "{\"key\": \"value\"}", "testEvent", "testMessage");
assertEquals(3, executeInvoked.get());
}
}
2 changes: 2 additions & 0 deletions server/src/main/java/com/netflix/conductor/Conductor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.retry.annotation.EnableRetry;

// Prevents from the datasource beans to be loaded, AS they are needed only for specific databases.
// In case that SQL database is selected this class will be imported back in the appropriate
// database persistence module.
@EnableRetry
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class Conductor {

Expand Down

0 comments on commit e1cb986

Please sign in to comment.