|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.ml.common.utils; |
| 7 | + |
| 8 | +import static org.mockito.ArgumentMatchers.any; |
| 9 | +import static org.mockito.Mockito.doAnswer; |
| 10 | +import static org.mockito.Mockito.mock; |
| 11 | +import static org.mockito.Mockito.verify; |
| 12 | +import static org.mockito.Mockito.when; |
| 13 | +import static org.opensearch.ml.common.CommonValue.ML_TASK_INDEX; |
| 14 | + |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | +import org.mockito.junit.MockitoJUnitRunner; |
| 22 | +import org.opensearch.action.DocWriteResponse; |
| 23 | +import org.opensearch.action.update.UpdateRequest; |
| 24 | +import org.opensearch.action.update.UpdateResponse; |
| 25 | +import org.opensearch.common.settings.Settings; |
| 26 | +import org.opensearch.common.util.concurrent.ThreadContext; |
| 27 | +import org.opensearch.core.action.ActionListener; |
| 28 | +import org.opensearch.core.index.Index; |
| 29 | +import org.opensearch.core.index.shard.ShardId; |
| 30 | +import org.opensearch.ml.common.MLTaskState; |
| 31 | +import org.opensearch.threadpool.ThreadPool; |
| 32 | +import org.opensearch.transport.client.Client; |
| 33 | + |
| 34 | +@RunWith(MockitoJUnitRunner.class) |
| 35 | +public class MLTaskUtilsTests { |
| 36 | + private Client client; |
| 37 | + private ThreadPool threadPool; |
| 38 | + private ThreadContext threadContext; |
| 39 | + |
| 40 | + @Before |
| 41 | + public void setup() { |
| 42 | + this.client = mock(Client.class); |
| 43 | + this.threadPool = mock(ThreadPool.class); |
| 44 | + Settings settings = Settings.builder().build(); |
| 45 | + this.threadContext = new ThreadContext(settings); |
| 46 | + when(client.threadPool()).thenReturn(threadPool); |
| 47 | + when(threadPool.getThreadContext()).thenReturn(threadContext); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testUpdateMLTaskDirectly_NullFields() { |
| 52 | + ActionListener<UpdateResponse> listener = mock(ActionListener.class); |
| 53 | + MLTaskUtils.updateMLTaskDirectly("task_id", null, client, listener); |
| 54 | + verify(listener).onFailure(any(IllegalArgumentException.class)); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testUpdateMLTaskDirectly_EmptyFields() { |
| 59 | + ActionListener<UpdateResponse> listener = mock(ActionListener.class); |
| 60 | + MLTaskUtils.updateMLTaskDirectly("task_id", new HashMap<>(), client, listener); |
| 61 | + verify(listener).onFailure(any(IllegalArgumentException.class)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testUpdateMLTaskDirectly_NullTaskId() { |
| 66 | + ActionListener<UpdateResponse> listener = mock(ActionListener.class); |
| 67 | + MLTaskUtils.updateMLTaskDirectly(null, new HashMap<>(), client, listener); |
| 68 | + verify(listener).onFailure(any(IllegalArgumentException.class)); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testUpdateMLTaskDirectly_EmptyTaskId() { |
| 73 | + ActionListener<UpdateResponse> listener = mock(ActionListener.class); |
| 74 | + MLTaskUtils.updateMLTaskDirectly("", new HashMap<>(), client, listener); |
| 75 | + verify(listener).onFailure(any(IllegalArgumentException.class)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testUpdateMLTaskDirectly_Success() { |
| 80 | + Map<String, Object> updatedFields = new HashMap<>(); |
| 81 | + updatedFields.put("field1", "value1"); |
| 82 | + |
| 83 | + doAnswer(invocation -> { |
| 84 | + ActionListener<UpdateResponse> actionListener = invocation.getArgument(1); |
| 85 | + ShardId shardId = new ShardId(new Index(ML_TASK_INDEX, "_na_"), 0); |
| 86 | + UpdateResponse response = new UpdateResponse(shardId, "task_id", 1, 1, 1, DocWriteResponse.Result.CREATED); |
| 87 | + actionListener.onResponse(response); |
| 88 | + return null; |
| 89 | + }).when(client).update(any(UpdateRequest.class), any()); |
| 90 | + |
| 91 | + ActionListener<UpdateResponse> listener = mock(ActionListener.class); |
| 92 | + MLTaskUtils.updateMLTaskDirectly("task_id", updatedFields, client, listener); |
| 93 | + verify(listener).onResponse(any(UpdateResponse.class)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testUpdateMLTaskDirectly_InvalidStateType() { |
| 98 | + Map<String, Object> updatedFields = new HashMap<>(); |
| 99 | + updatedFields.put("state", "INVALID_STATE"); |
| 100 | + |
| 101 | + ActionListener<UpdateResponse> listener = mock(ActionListener.class); |
| 102 | + MLTaskUtils.updateMLTaskDirectly("task_id", updatedFields, client, listener); |
| 103 | + verify(listener).onFailure(any(IllegalArgumentException.class)); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testUpdateMLTaskDirectly_TaskDoneState() { |
| 108 | + Map<String, Object> updatedFields = new HashMap<>(); |
| 109 | + updatedFields.put("state", MLTaskState.COMPLETED); |
| 110 | + |
| 111 | + doAnswer(invocation -> { |
| 112 | + ActionListener<UpdateResponse> actionListener = invocation.getArgument(1); |
| 113 | + UpdateRequest request = invocation.getArgument(0); |
| 114 | + // Verify retry policy is set for task done state |
| 115 | + assert request.retryOnConflict() == 3; |
| 116 | + |
| 117 | + ShardId shardId = new ShardId(new Index(ML_TASK_INDEX, "_na_"), 0); |
| 118 | + UpdateResponse response = new UpdateResponse(shardId, "task_id", 1, 1, 1, DocWriteResponse.Result.CREATED); |
| 119 | + actionListener.onResponse(response); |
| 120 | + return null; |
| 121 | + }).when(client).update(any(UpdateRequest.class), any()); |
| 122 | + |
| 123 | + ActionListener<UpdateResponse> listener = mock(ActionListener.class); |
| 124 | + MLTaskUtils.updateMLTaskDirectly("task_id", updatedFields, client, listener); |
| 125 | + verify(listener).onResponse(any(UpdateResponse.class)); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testUpdateMLTaskDirectly_ClientException() { |
| 130 | + Map<String, Object> updatedFields = new HashMap<>(); |
| 131 | + updatedFields.put("field1", "value1"); |
| 132 | + |
| 133 | + doAnswer(invocation -> { |
| 134 | + ActionListener<UpdateResponse> actionListener = invocation.getArgument(1); |
| 135 | + actionListener.onFailure(new RuntimeException("Test exception")); |
| 136 | + return null; |
| 137 | + }).when(client).update(any(UpdateRequest.class), any()); |
| 138 | + |
| 139 | + ActionListener<UpdateResponse> listener = mock(ActionListener.class); |
| 140 | + MLTaskUtils.updateMLTaskDirectly("task_id", updatedFields, client, listener); |
| 141 | + verify(listener).onFailure(any(RuntimeException.class)); |
| 142 | + } |
| 143 | +} |
0 commit comments