Skip to content

Commit 8d673ba

Browse files
Changes for stop-replication action
Signed-off-by: aggarwalShivani <shivani.aggarwal@nokia.com>
1 parent 4d65546 commit 8d673ba

File tree

5 files changed

+61
-33
lines changed

5 files changed

+61
-33
lines changed

src/main/kotlin/org/opensearch/commons/replication/ReplicationPluginInterface.kt

100755100644
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
package org.opensearch.commons.replication
66

77
import org.opensearch.action.support.master.AcknowledgedResponse
8+
import org.opensearch.client.Client
89
import org.opensearch.client.node.NodeClient
9-
import org.opensearch.commons.replication.action.ReplicationActions.UNFOLLOW_REPLICATION_ACTION_TYPE
10+
import org.opensearch.commons.replication.action.ReplicationActions.INTERNAL_STOP_REPLICATION_ACTION_TYPE
1011
import org.opensearch.commons.replication.action.StopIndexReplicationRequest
1112
import org.opensearch.commons.utils.recreateObject
1213
import org.opensearch.core.action.ActionListener
@@ -16,7 +17,7 @@ import org.opensearch.core.common.io.stream.Writeable
1617
/**
1718
* Transport action plugin interfaces for the cross-cluster-replication plugin.
1819
*/
19-
object ReplicationPluginInterface {
20+
open class ReplicationPluginInterface {
2021

2122
/**
2223
* Stop replication.
@@ -25,13 +26,14 @@ object ReplicationPluginInterface {
2526
* @param listener The listener for getting response
2627
*/
2728

28-
fun stopReplication(
29-
client: NodeClient,
29+
open fun stopReplication(
30+
client: Client,
3031
request: StopIndexReplicationRequest,
3132
listener: ActionListener<AcknowledgedResponse>
3233
) {
33-
return client.execute(
34-
UNFOLLOW_REPLICATION_ACTION_TYPE,
34+
val nodeClient = client as NodeClient
35+
return nodeClient.execute(
36+
INTERNAL_STOP_REPLICATION_ACTION_TYPE,
3537
request,
3638
wrapActionListener(listener) { response ->
3739
recreateObject(response) {

src/main/kotlin/org/opensearch/commons/replication/action/ReplicationActions.kt

100755100644
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ object ReplicationActions {
1414

1515
/**
1616
* Action names for stopping replication
17-
* STOP_REPLICATION_ACTION_NAME: action used for _stop REST API
18-
* UNFOLLOW_REPLICATION_ACTION_NAME: internal action used for inter-plugin communication i.e. by ism to invoke stop
19-
* replication.
17+
* STOP_REPLICATION_ACTION_NAME: action used for _replication/_stop REST API
18+
* INTERNAL_STOP_REPLICATION_ACTION_NAME: Internal only - Used by Index Management plugin to invoke stop replication
2019
*/
2120
const val STOP_REPLICATION_ACTION_NAME = "indices:admin/plugins/replication/index/stop"
22-
const val UNFOLLOW_REPLICATION_ACTION_NAME = "indices:admin/plugins/replication/index/unfollow"
21+
const val INTERNAL_STOP_REPLICATION_ACTION_NAME = "indices:internal/plugins/replication/index/stop"
2322

2423
/**
2524
* Stop replication transport action types.
2625
*/
2726
val STOP_REPLICATION_ACTION_TYPE =
2827
ActionType(STOP_REPLICATION_ACTION_NAME, ::AcknowledgedResponse)
29-
val UNFOLLOW_REPLICATION_ACTION_TYPE =
30-
ActionType(UNFOLLOW_REPLICATION_ACTION_NAME, ::AcknowledgedResponse)
28+
val INTERNAL_STOP_REPLICATION_ACTION_TYPE =
29+
ActionType(INTERNAL_STOP_REPLICATION_ACTION_NAME, ::AcknowledgedResponse)
3130
}

src/main/kotlin/org/opensearch/commons/replication/action/StopIndexReplicationRequest.kt

100755100644
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
15
package org.opensearch.commons.replication.action
26

37
import org.opensearch.action.ActionRequestValidationException

src/test/kotlin/org/opensearch/commons/replication/ReplicationPluginInterfaceTests.kt

100755100644
Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,60 @@ package org.opensearch.commons.replication
88
import com.nhaarman.mockitokotlin2.whenever
99
import org.junit.jupiter.api.Test
1010
import 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
1514
import org.mockito.junit.jupiter.MockitoExtension
16-
import org.opensearch.action.ActionType
1715
import org.opensearch.action.support.master.AcknowledgedResponse
1816
import org.opensearch.client.node.NodeClient
1917
import org.opensearch.commons.replication.action.StopIndexReplicationRequest
2018
import org.opensearch.core.action.ActionListener
19+
import org.opensearch.core.action.ActionResponse
2120

22-
@Suppress("UNCHECKED_CAST")
2321
@ExtendWith(MockitoExtension::class)
2422
internal 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
}

src/test/kotlin/org/opensearch/commons/replication/action/StopIndexReplicationRequestTests.kt

100755100644
File mode changed.

0 commit comments

Comments
 (0)