@@ -7,6 +7,9 @@ package io.airbyte.commons.temporal
77import io.airbyte.metrics.MetricClient
88import io.grpc.Status
99import io.grpc.StatusRuntimeException
10+ import io.mockk.every
11+ import io.mockk.mockk
12+ import io.mockk.verify
1013import io.temporal.api.workflowservice.v1.DescribeWorkflowExecutionRequest
1114import io.temporal.api.workflowservice.v1.DescribeWorkflowExecutionResponse
1215import io.temporal.api.workflowservice.v1.WorkflowServiceGrpc.WorkflowServiceBlockingStub
@@ -17,53 +20,43 @@ import io.temporal.serviceclient.WorkflowServiceStubs
1720import org.junit.jupiter.api.Assertions
1821import org.junit.jupiter.api.BeforeEach
1922import org.junit.jupiter.api.Test
20- import org.mockito.ArgumentMatchers
21- import org.mockito.Mockito
22- import org.mockito.kotlin.anyOrNull
23- import org.mockito.kotlin.anyVararg
2423
2524internal class WorkflowClientWrappedTest {
2625 internal class MyWorkflow
2726
28- lateinit var metricClient: MetricClient
29- lateinit var temporalWorkflowServiceStubs: WorkflowServiceStubs
30- lateinit var temporalWorkflowServiceBlockingStub: WorkflowServiceBlockingStub
31- lateinit var temporalWorkflowClient: WorkflowClient
32- lateinit var workflowClient: WorkflowClientWrapped
27+ private lateinit var metricClient: MetricClient
28+ private lateinit var temporalWorkflowServiceStubs: WorkflowServiceStubs
29+ private lateinit var temporalWorkflowServiceBlockingStub: WorkflowServiceBlockingStub
30+ private lateinit var temporalWorkflowClient: WorkflowClient
31+ private lateinit var workflowClient: WorkflowClientWrapped
3332
3433 @BeforeEach
3534 fun beforeEach () {
36- metricClient = Mockito .mock( MetricClient :: class .java )
37- temporalWorkflowServiceBlockingStub = Mockito .mock( WorkflowServiceBlockingStub :: class .java )
38- temporalWorkflowServiceStubs = Mockito .mock( WorkflowServiceStubs :: class .java )
39- Mockito .` when `( temporalWorkflowServiceStubs.blockingStub()).thenReturn( temporalWorkflowServiceBlockingStub)
40- temporalWorkflowClient = Mockito .mock( WorkflowClient :: class .java )
41- Mockito .` when `( temporalWorkflowClient.getWorkflowServiceStubs()).thenReturn( temporalWorkflowServiceStubs)
35+ metricClient = mockk(relaxed = true )
36+ temporalWorkflowServiceBlockingStub = mockk( )
37+ temporalWorkflowServiceStubs = mockk( )
38+ every { temporalWorkflowServiceStubs.blockingStub() } returns temporalWorkflowServiceBlockingStub
39+ temporalWorkflowClient = mockk( )
40+ every { temporalWorkflowClient.workflowServiceStubs } returns temporalWorkflowServiceStubs
4241 workflowClient =
4342 WorkflowClientWrapped (temporalWorkflowClient, metricClient, MAX_ATTEMPT , BACKOFF_DELAY_IN_MILLIS , BACKOFF_MAX_DELAY_IN_MILLIS )
4443 }
4544
4645 @Test
4746 fun testRetryLogic () {
48- Mockito .`when `(temporalWorkflowClient.newWorkflowStub(ArgumentMatchers .any<Class <Any >>(), ArgumentMatchers .anyString())).thenAnswer {
49- throw
50- unavailable()
51- }
47+ every { temporalWorkflowClient.newWorkflowStub(any<Class <Any >>(), any<String >()) } throws unavailable()
5248
5349 Assertions .assertThrows(
5450 StatusRuntimeException ::class .java,
5551 ) { workflowClient.newWorkflowStub(MyWorkflow ::class .java, " fail" ) }
56- Mockito . verify(temporalWorkflowClient, Mockito .times( 3 )) .newWorkflowStub(ArgumentMatchers . any<Class <Any >>(), ArgumentMatchers .anyString ())
57- Mockito . verify(metricClient, Mockito .times( 2 )) .count(anyOrNull (), anyOrNull (), anyVararg())
52+ verify(exactly = 3 ) { temporalWorkflowClient .newWorkflowStub(any<Class <Any >>(), any< String > ()) }
53+ verify(exactly = 2 ) { metricClient .count(any (), any (), * anyVararg()) }
5854 }
5955
6056 @Test
6157 fun testNewWorkflowStub () {
6258 val expected = MyWorkflow ()
63- Mockito
64- .`when `(temporalWorkflowClient.newWorkflowStub(ArgumentMatchers .any<Class <Any >>(), ArgumentMatchers .anyString()))
65- .thenAnswer { throw unavailable() }
66- .thenReturn(expected)
59+ every { temporalWorkflowClient.newWorkflowStub(any<Class <Any >>(), any<String >()) } throws unavailable() andThen expected
6760
6861 val actual = workflowClient.newWorkflowStub(MyWorkflow ::class .java, " woot" )
6962 Assertions .assertEquals(expected, actual)
@@ -72,54 +65,43 @@ internal class WorkflowClientWrappedTest {
7265 @Test
7366 fun testNewWorkflowStubWithOptions () {
7467 val expected = MyWorkflow ()
75- Mockito
76- .`when `(temporalWorkflowClient.newWorkflowStub(anyOrNull<Class <Any >>(), anyOrNull<WorkflowOptions >()))
77- .thenAnswer { throw unavailable() }
78- .thenReturn(expected)
68+ every { temporalWorkflowClient.newWorkflowStub(any<Class <Any >>(), any<WorkflowOptions >()) } throws unavailable() andThen expected
7969
8070 val actual = workflowClient.newWorkflowStub(MyWorkflow ::class .java, WorkflowOptions .getDefaultInstance())
8171 Assertions .assertEquals(expected, actual)
8272 }
8373
8474 @Test
8575 fun testTerminateWorkflow () {
86- val workflowStub = Mockito .mock(WorkflowStub ::class .java)
87- Mockito
88- .`when `(temporalWorkflowClient.newUntypedWorkflowStub(ArgumentMatchers .anyString()))
89- .thenAnswer { throw unavailable() }
90- .thenReturn(workflowStub)
76+ val workflowStub = mockk<WorkflowStub >(relaxed = true )
77+ every { temporalWorkflowClient.newUntypedWorkflowStub(any<String >()) } throws unavailable() andThen workflowStub
9178
9279 workflowClient.terminateWorkflow(" workflow" , " test terminate" )
93- Mockito . verify(temporalWorkflowClient, Mockito .times( 2 )) .newUntypedWorkflowStub(" workflow" )
94- Mockito . verify( workflowStub) .terminate(" test terminate" )
80+ verify(exactly = 2 ) { temporalWorkflowClient .newUntypedWorkflowStub(" workflow" ) }
81+ verify { workflowStub.terminate(" test terminate" ) }
9582 }
9683
9784 @Test
9885 fun testBlockingDescribeWorkflowExecution () {
99- val expected = Mockito .mock(DescribeWorkflowExecutionResponse ::class .java)
100- Mockito
101- .`when `(temporalWorkflowServiceBlockingStub.describeWorkflowExecution(ArgumentMatchers .any()))
102- .thenAnswer { throw unavailable() }
103- .thenReturn(expected)
86+ val expected = mockk<DescribeWorkflowExecutionResponse >()
87+ every { temporalWorkflowServiceBlockingStub.describeWorkflowExecution(any()) } throws unavailable() andThen expected
10488
10589 val actual =
10690 workflowClient.blockingDescribeWorkflowExecution(
107- Mockito .mock(
108- DescribeWorkflowExecutionRequest ::class .java,
109- ),
91+ mockk<DescribeWorkflowExecutionRequest >(),
11092 )
11193 Assertions .assertEquals(expected, actual)
11294 }
11395
11496 @Test
11597 fun testSignalsAreNotRetried () {
116- Mockito .` when `( temporalWorkflowClient.signalWithStart(ArgumentMatchers . any())).thenAnswer { throw unavailable() }
117- Mockito .` when `( temporalWorkflowClient.newSignalWithStartRequest()).thenReturn( Mockito .mock() )
98+ every { temporalWorkflowClient.signalWithStart(any()) } throws unavailable()
99+ every { temporalWorkflowClient.newSignalWithStartRequest() } returns mockk( )
118100 Assertions .assertThrows(StatusRuntimeException ::class .java) {
119101 val request = workflowClient.newSignalWithStartRequest()
120102 workflowClient.signalWithStart(request)
121103 }
122- Mockito . verify(temporalWorkflowClient, Mockito .times( 1 )) .signalWithStart(ArgumentMatchers . any())
104+ verify(exactly = 1 ) { temporalWorkflowClient .signalWithStart(any()) }
123105 }
124106
125107 companion object {
0 commit comments