|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.commons.replication |
| 7 | + |
| 8 | +import com.nhaarman.mockitokotlin2.whenever |
| 9 | +import org.junit.jupiter.api.Test |
| 10 | +import org.junit.jupiter.api.extension.ExtendWith |
| 11 | +import org.mockito.Mockito.any |
| 12 | +import org.mockito.Mockito.mock |
| 13 | +import org.mockito.Mockito.verify |
| 14 | +import org.mockito.junit.jupiter.MockitoExtension |
| 15 | +import org.opensearch.action.support.clustermanager.AcknowledgedResponse |
| 16 | +import org.opensearch.commons.replication.action.StopIndexReplicationRequest |
| 17 | +import org.opensearch.core.action.ActionListener |
| 18 | +import org.opensearch.core.action.ActionResponse |
| 19 | +import org.opensearch.transport.client.node.NodeClient |
| 20 | + |
| 21 | +@ExtendWith(MockitoExtension::class) |
| 22 | +internal class ReplicationPluginInterfaceTests { |
| 23 | + |
| 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 | + // Call method under test |
| 40 | + ReplicationPluginInterface.stopReplication(client, request, listener) |
| 41 | + // Verify that listener.onResponse is called with the correct response |
| 42 | + verify(listener).onResponse(acknowledgedResponse) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun `test stopReplication failure response`() { |
| 47 | + // Mock dependencies |
| 48 | + val client: NodeClient = mock() |
| 49 | + val request: StopIndexReplicationRequest = mock() |
| 50 | + val listener: ActionListener<AcknowledgedResponse> = mock() |
| 51 | + val exception = Exception("Test failure") |
| 52 | + |
| 53 | + // Mock the behavior of NodeClient.execute() |
| 54 | + whenever(client.execute(any(), any(), any<ActionListener<ActionResponse>>())) |
| 55 | + .thenAnswer { invocation -> |
| 56 | + val actionListener = invocation.getArgument<ActionListener<ActionResponse>>(2) |
| 57 | + actionListener.onFailure(exception) // Simulate failure |
| 58 | + } |
| 59 | + |
| 60 | + // Call method under test |
| 61 | + ReplicationPluginInterface.stopReplication(client, request, listener) |
| 62 | + // Verify that listener.onResponse is called with the correct response |
| 63 | + verify(listener).onFailure(exception) |
| 64 | + } |
| 65 | +} |
0 commit comments