@@ -8,37 +8,60 @@ package org.opensearch.commons.replication
88import com.nhaarman.mockitokotlin2.whenever
99import org.junit.jupiter.api.Test
1010import org.junit.jupiter.api.extension.ExtendWith
11- import org.mockito.Answers
12- import org.mockito.ArgumentMatchers
13- import org.mockito.Mock
14- import org.mockito.Mockito
11+ import org.mockito.Mockito.any
12+ import org.mockito.Mockito.verify
13+ import org.mockito.Mockito.mock
1514import org.mockito.junit.jupiter.MockitoExtension
16- import org.opensearch.action.ActionType
1715import org.opensearch.action.support.master.AcknowledgedResponse
1816import org.opensearch.client.node.NodeClient
1917import org.opensearch.commons.replication.action.StopIndexReplicationRequest
2018import org.opensearch.core.action.ActionListener
19+ import org.opensearch.core.action.ActionResponse
2120
22- @Suppress(" UNCHECKED_CAST" )
2321@ExtendWith(MockitoExtension ::class )
2422internal class ReplicationPluginInterfaceTests {
2523
26- @Mock(answer = Answers .RETURNS_DEEP_STUBS )
27- private lateinit var client: NodeClient
24+ @Test
25+ fun `test stopReplication successful response` () {
26+ // Mock dependencies
27+ val client: NodeClient = mock()
28+ val request: StopIndexReplicationRequest = mock()
29+ val listener: ActionListener <AcknowledgedResponse > = mock()
30+ val acknowledgedResponse = AcknowledgedResponse (true ) // Successful response
31+
32+ // Mock the behavior of NodeClient.execute()
33+ whenever(client.execute(any(), any(), any<ActionListener <ActionResponse >>()))
34+ .thenAnswer { invocation ->
35+ val actionListener = invocation.getArgument<ActionListener <ActionResponse >>(2 )
36+ actionListener.onResponse(acknowledgedResponse) // Simulate success
37+ }
38+
39+ val replicationPluginInterface = ReplicationPluginInterface ()
40+ // Call method under test
41+ replicationPluginInterface.stopReplication(client, request, listener)
42+ // Verify that listener.onResponse is called with the correct response
43+ verify(listener).onResponse(acknowledgedResponse)
44+ }
2845
2946 @Test
30- fun stopReplication () {
31- val request = Mockito .mock(StopIndexReplicationRequest ::class .java)
32- val response = AcknowledgedResponse (true )
33- val listener: ActionListener <AcknowledgedResponse > =
34- Mockito .mock(ActionListener ::class .java) as ActionListener <AcknowledgedResponse >
35-
36- Mockito .doAnswer {
37- (it.getArgument(2 ) as ActionListener <AcknowledgedResponse >)
38- .onResponse(response)
39- }.whenever(client).execute(Mockito .any(ActionType ::class .java), Mockito .any(), Mockito .any())
40-
41- ReplicationPluginInterface .stopReplication(client, request, listener)
42- Mockito .verify(listener, Mockito .times(1 )).onResponse(ArgumentMatchers .eq(response))
47+ fun `test stopReplication failure response` () {
48+ // Mock dependencies
49+ val client: NodeClient = mock()
50+ val request: StopIndexReplicationRequest = mock()
51+ val listener: ActionListener <AcknowledgedResponse > = mock()
52+ val exception = Exception (" Test failure" )
53+
54+ // Mock the behavior of NodeClient.execute()
55+ whenever(client.execute(any(), any(), any<ActionListener <ActionResponse >>()))
56+ .thenAnswer { invocation ->
57+ val actionListener = invocation.getArgument<ActionListener <ActionResponse >>(2 )
58+ actionListener.onFailure(exception) // Simulate failure
59+ }
60+
61+ val replicationPluginInterface = ReplicationPluginInterface ()
62+ // Call method under test
63+ replicationPluginInterface.stopReplication(client, request, listener)
64+ // Verify that listener.onResponse is called with the correct response
65+ verify(listener).onFailure(exception)
4366 }
4467}
0 commit comments