-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathRestMLPredictionActionTests.java
181 lines (154 loc) · 7.74 KB
/
RestMLPredictionActionTests.java
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
package org.opensearch.ml.rest;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import static org.opensearch.ml.utils.MLExceptionUtils.LOCAL_MODEL_DISABLED_ERR_MSG;
import static org.opensearch.ml.utils.MLExceptionUtils.REMOTE_INFERENCE_DISABLED_ERR_MSG;
import static org.opensearch.ml.utils.RestActionUtils.PARAMETER_MODEL_ID;
import static org.opensearch.ml.utils.TestHelper.getKMeansRestRequest;
import static org.opensearch.ml.utils.TestHelper.verifyParsedKMeansMLInput;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.opensearch.client.node.NodeClient;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.common.Strings;
import org.opensearch.ml.common.FunctionName;
import org.opensearch.ml.common.MLModel;
import org.opensearch.ml.common.input.MLInput;
import org.opensearch.ml.common.transport.MLTaskResponse;
import org.opensearch.ml.common.transport.model.MLModelGetAction;
import org.opensearch.ml.common.transport.model.MLModelGetResponse;
import org.opensearch.ml.common.transport.prediction.MLPredictionTaskAction;
import org.opensearch.ml.common.transport.prediction.MLPredictionTaskRequest;
import org.opensearch.ml.model.MLModelManager;
import org.opensearch.ml.settings.MLFeatureEnabledSetting;
import org.opensearch.rest.RestChannel;
import org.opensearch.rest.RestHandler;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.action.RestToXContentListener;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.threadpool.TestThreadPool;
import org.opensearch.threadpool.ThreadPool;
public class RestMLPredictionActionTests extends OpenSearchTestCase {
@Rule
public ExpectedException thrown = ExpectedException.none();
private RestMLPredictionAction restMLPredictionAction;
NodeClient client;
private ThreadPool threadPool;
@Mock
RestChannel channel;
@Mock
MLModelManager modelManager;
@Mock
MLFeatureEnabledSetting mlFeatureEnabledSetting;
@Before
public void setup() {
MockitoAnnotations.openMocks(this);
when(modelManager.getOptionalModelFunctionName(anyString())).thenReturn(Optional.of(FunctionName.REMOTE));
when(mlFeatureEnabledSetting.isRemoteInferenceEnabled()).thenReturn(true);
when(mlFeatureEnabledSetting.isLocalModelEnabled()).thenReturn(true);
restMLPredictionAction = new RestMLPredictionAction(modelManager, mlFeatureEnabledSetting);
threadPool = new TestThreadPool(this.getClass().getSimpleName() + "ThreadPool");
client = spy(new NodeClient(Settings.EMPTY, threadPool));
doAnswer(invocation -> {
ActionListener<MLTaskResponse> actionListener = invocation.getArgument(2);
return null;
}).when(client).execute(eq(MLPredictionTaskAction.INSTANCE), any(), any());
}
@Override
public void tearDown() throws Exception {
super.tearDown();
threadPool.shutdown();
client.close();
}
public void testConstructor() {
RestMLPredictionAction mlPredictionAction = new RestMLPredictionAction(modelManager, mlFeatureEnabledSetting);
assertNotNull(mlPredictionAction);
}
public void testGetName() {
String actionName = restMLPredictionAction.getName();
assertFalse(Strings.isNullOrEmpty(actionName));
assertEquals("ml_prediction_action", actionName);
}
public void testRoutes() {
List<RestHandler.Route> routes = restMLPredictionAction.routes();
assertNotNull(routes);
assertFalse(routes.isEmpty());
RestHandler.Route route = routes.get(0);
assertEquals(RestRequest.Method.POST, route.getMethod());
assertEquals("/_plugins/_ml/_predict/{algorithm}/{model_id}", route.getPath());
}
public void testGetRequest() throws IOException {
RestRequest request = getRestRequest_PredictModel();
MLPredictionTaskRequest mlPredictionTaskRequest = restMLPredictionAction
.getRequest("modelId", FunctionName.KMEANS.name(), FunctionName.KMEANS.name(), request);
MLInput mlInput = mlPredictionTaskRequest.getMlInput();
verifyParsedKMeansMLInput(mlInput);
}
public void testGetRequest_RemoteInferenceDisabled() throws IOException {
thrown.expect(IllegalStateException.class);
thrown.expectMessage(REMOTE_INFERENCE_DISABLED_ERR_MSG);
when(mlFeatureEnabledSetting.isRemoteInferenceEnabled()).thenReturn(false);
RestRequest request = getRestRequest_PredictModel();
MLPredictionTaskRequest mlPredictionTaskRequest = restMLPredictionAction
.getRequest("modelId", FunctionName.REMOTE.name(), "text_embedding", request);
}
public void testGetRequest_LocalModelInferenceDisabled() throws IOException {
thrown.expect(IllegalStateException.class);
thrown.expectMessage(LOCAL_MODEL_DISABLED_ERR_MSG);
when(mlFeatureEnabledSetting.isLocalModelEnabled()).thenReturn(false);
RestRequest request = getRestRequest_PredictModel();
MLPredictionTaskRequest mlPredictionTaskRequest = restMLPredictionAction
.getRequest("modelId", FunctionName.TEXT_EMBEDDING.name(), "text_embedding", request);
}
public void testPrepareRequest() throws Exception {
RestRequest request = getRestRequest_PredictModel();
restMLPredictionAction.handleRequest(request, channel, client);
ArgumentCaptor<MLPredictionTaskRequest> argumentCaptor = ArgumentCaptor.forClass(MLPredictionTaskRequest.class);
verify(client, times(1)).execute(eq(MLPredictionTaskAction.INSTANCE), argumentCaptor.capture(), any());
MLInput mlInput = argumentCaptor.getValue().getMlInput();
verifyParsedKMeansMLInput(mlInput);
}
@Ignore
public void testPrepareRequest_EmptyAlgorithm() throws Exception {
MLModel model = MLModel.builder().algorithm(FunctionName.BATCH_RCF).build();
doAnswer(invocation -> {
ActionListener<MLModelGetResponse> actionListener = invocation.getArgument(2);
MLModelGetResponse response = new MLModelGetResponse(model);
actionListener.onResponse(response);
return null;
}).when(client).execute(eq(MLModelGetAction.INSTANCE), any(), any());
doAnswer(invocation -> {
ActionListener<RestToXContentListener> actionListener = invocation.getArgument(2);
RestToXContentListener<MLModelGetResponse> listener = new RestToXContentListener<>(channel);
actionListener.onResponse(listener);
return null;
}).when(client).execute(eq(MLPredictionTaskAction.INSTANCE), any(), any());
RestRequest request = getKMeansRestRequest();
request.params().clear();
request.params().put(PARAMETER_MODEL_ID, "model_id");
restMLPredictionAction.handleRequest(request, channel, client);
ArgumentCaptor<MLPredictionTaskRequest> argumentCaptor = ArgumentCaptor.forClass(MLPredictionTaskRequest.class);
verify(client, times(1)).execute(eq(MLPredictionTaskAction.INSTANCE), argumentCaptor.capture(), any());
MLInput mlInput = argumentCaptor.getValue().getMlInput();
verifyParsedKMeansMLInput(mlInput);
}
private RestRequest getRestRequest_PredictModel() {
RestRequest request = getKMeansRestRequest();
request.params().put(PARAMETER_MODEL_ID, "model_id");
return request;
}
}