forked from opensearch-project/common-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplicationPluginInterfaceTests.kt
More file actions
67 lines (58 loc) · 2.82 KB
/
ReplicationPluginInterfaceTests.kt
File metadata and controls
67 lines (58 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.commons.replication
import com.nhaarman.mockitokotlin2.whenever
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.Mockito.any
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify
import org.mockito.junit.jupiter.MockitoExtension
import org.opensearch.action.support.clustermanager.AcknowledgedResponse
import org.opensearch.transport.client.node.NodeClient
import org.opensearch.commons.replication.action.StopIndexReplicationRequest
import org.opensearch.core.action.ActionListener
import org.opensearch.core.action.ActionResponse
@ExtendWith(MockitoExtension::class)
internal class ReplicationPluginInterfaceTests {
@Test
fun `test stopReplication successful response`() {
// Mock dependencies
val client: NodeClient = mock()
val request: StopIndexReplicationRequest = mock()
val listener: ActionListener<AcknowledgedResponse> = mock()
val acknowledgedResponse = AcknowledgedResponse(true) // Successful response
// Mock the behavior of NodeClient.execute()
whenever(client.execute(any(), any(), any<ActionListener<ActionResponse>>()))
.thenAnswer { invocation ->
val actionListener = invocation.getArgument<ActionListener<ActionResponse>>(2)
actionListener.onResponse(acknowledgedResponse) // Simulate success
}
val replicationPluginInterface = ReplicationPluginInterface()
// Call method under test
replicationPluginInterface.stopReplication(client, request, listener)
// Verify that listener.onResponse is called with the correct response
verify(listener).onResponse(acknowledgedResponse)
}
@Test
fun `test stopReplication failure response`() {
// Mock dependencies
val client: NodeClient = mock()
val request: StopIndexReplicationRequest = mock()
val listener: ActionListener<AcknowledgedResponse> = mock()
val exception = Exception("Test failure")
// Mock the behavior of NodeClient.execute()
whenever(client.execute(any(), any(), any<ActionListener<ActionResponse>>()))
.thenAnswer { invocation ->
val actionListener = invocation.getArgument<ActionListener<ActionResponse>>(2)
actionListener.onFailure(exception) // Simulate failure
}
val replicationPluginInterface = ReplicationPluginInterface()
// Call method under test
replicationPluginInterface.stopReplication(client, request, listener)
// Verify that listener.onResponse is called with the correct response
verify(listener).onFailure(exception)
}
}