|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.ml.rest; |
| 7 | + |
| 8 | +import static org.mockito.ArgumentMatchers.any; |
| 9 | +import static org.mockito.ArgumentMatchers.eq; |
| 10 | +import static org.mockito.Mockito.doAnswer; |
| 11 | +import static org.mockito.Mockito.spy; |
| 12 | +import static org.mockito.Mockito.times; |
| 13 | +import static org.mockito.Mockito.verify; |
| 14 | +import static org.opensearch.ml.utils.TestHelper.getLocalSampleCalculatorRestRequest; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import org.junit.Before; |
| 20 | +import org.mockito.ArgumentCaptor; |
| 21 | +import org.mockito.Mock; |
| 22 | +import org.mockito.MockitoAnnotations; |
| 23 | +import org.opensearch.action.ActionListener; |
| 24 | +import org.opensearch.client.node.NodeClient; |
| 25 | +import org.opensearch.common.Strings; |
| 26 | +import org.opensearch.common.settings.Settings; |
| 27 | +import org.opensearch.ml.common.FunctionName; |
| 28 | +import org.opensearch.ml.common.input.Input; |
| 29 | +import org.opensearch.ml.common.transport.execute.MLExecuteTaskAction; |
| 30 | +import org.opensearch.ml.common.transport.execute.MLExecuteTaskRequest; |
| 31 | +import org.opensearch.ml.common.transport.execute.MLExecuteTaskResponse; |
| 32 | +import org.opensearch.rest.RestChannel; |
| 33 | +import org.opensearch.rest.RestHandler; |
| 34 | +import org.opensearch.rest.RestRequest; |
| 35 | +import org.opensearch.test.OpenSearchTestCase; |
| 36 | +import org.opensearch.threadpool.TestThreadPool; |
| 37 | +import org.opensearch.threadpool.ThreadPool; |
| 38 | + |
| 39 | +public class RestMLExecuteActionTests extends OpenSearchTestCase { |
| 40 | + |
| 41 | + private RestMLExecuteAction restMLExecuteAction; |
| 42 | + |
| 43 | + NodeClient client; |
| 44 | + private ThreadPool threadPool; |
| 45 | + |
| 46 | + @Mock |
| 47 | + RestChannel channel; |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setup() { |
| 51 | + MockitoAnnotations.openMocks(this); |
| 52 | + restMLExecuteAction = new RestMLExecuteAction(); |
| 53 | + |
| 54 | + threadPool = new TestThreadPool(this.getClass().getSimpleName() + "ThreadPool"); |
| 55 | + client = spy(new NodeClient(Settings.EMPTY, threadPool)); |
| 56 | + |
| 57 | + doAnswer(invocation -> { |
| 58 | + ActionListener<MLExecuteTaskResponse> actionListener = invocation.getArgument(2); |
| 59 | + return null; |
| 60 | + }).when(client).execute(eq(MLExecuteTaskAction.INSTANCE), any(), any()); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void tearDown() throws Exception { |
| 65 | + super.tearDown(); |
| 66 | + threadPool.shutdown(); |
| 67 | + client.close(); |
| 68 | + } |
| 69 | + |
| 70 | + public void testConstructor() { |
| 71 | + RestMLExecuteAction restMLExecuteAction = new RestMLExecuteAction(); |
| 72 | + assertNotNull(restMLExecuteAction); |
| 73 | + } |
| 74 | + |
| 75 | + public void testGetName() { |
| 76 | + String actionName = restMLExecuteAction.getName(); |
| 77 | + assertFalse(Strings.isNullOrEmpty(actionName)); |
| 78 | + assertEquals("ml_execute_action", actionName); |
| 79 | + } |
| 80 | + |
| 81 | + public void testRoutes() { |
| 82 | + List<RestHandler.Route> routes = restMLExecuteAction.routes(); |
| 83 | + assertNotNull(routes); |
| 84 | + assertFalse(routes.isEmpty()); |
| 85 | + RestHandler.Route route = routes.get(0); |
| 86 | + assertEquals(RestRequest.Method.POST, route.getMethod()); |
| 87 | + assertEquals("/_plugins/_ml/_execute/{algorithm}", route.getPath()); |
| 88 | + } |
| 89 | + |
| 90 | + public void testGetRequest() throws IOException { |
| 91 | + RestRequest request = getLocalSampleCalculatorRestRequest(); |
| 92 | + MLExecuteTaskRequest executeTaskRequest = restMLExecuteAction.getRequest(request); |
| 93 | + |
| 94 | + Input input = executeTaskRequest.getInput(); |
| 95 | + assertNotNull(input); |
| 96 | + assertEquals(FunctionName.LOCAL_SAMPLE_CALCULATOR, input.getFunctionName()); |
| 97 | + } |
| 98 | + |
| 99 | + public void testPrepareRequest() throws Exception { |
| 100 | + RestRequest request = getLocalSampleCalculatorRestRequest(); |
| 101 | + restMLExecuteAction.handleRequest(request, channel, client); |
| 102 | + |
| 103 | + ArgumentCaptor<MLExecuteTaskRequest> argumentCaptor = ArgumentCaptor.forClass(MLExecuteTaskRequest.class); |
| 104 | + verify(client, times(1)).execute(eq(MLExecuteTaskAction.INSTANCE), argumentCaptor.capture(), any()); |
| 105 | + Input input = argumentCaptor.getValue().getInput(); |
| 106 | + assertEquals(FunctionName.LOCAL_SAMPLE_CALCULATOR, input.getFunctionName()); |
| 107 | + } |
| 108 | +} |
0 commit comments