|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package org.opensearch.neuralsearch.rest; |
| 6 | + |
| 7 | +import org.junit.Before; |
| 8 | +import org.mockito.ArgumentCaptor; |
| 9 | +import org.mockito.Mock; |
| 10 | +import org.mockito.MockitoAnnotations; |
| 11 | +import org.opensearch.common.settings.Settings; |
| 12 | +import org.opensearch.core.action.ActionListener; |
| 13 | +import org.opensearch.core.rest.RestStatus; |
| 14 | +import org.opensearch.core.xcontent.NamedXContentRegistry; |
| 15 | +import org.opensearch.neuralsearch.processor.InferenceProcessorTestCase; |
| 16 | +import org.opensearch.neuralsearch.settings.NeuralSearchSettingsAccessor; |
| 17 | +import org.opensearch.neuralsearch.stats.NeuralStatsInput; |
| 18 | +import org.opensearch.neuralsearch.stats.events.EventStatName; |
| 19 | +import org.opensearch.neuralsearch.stats.info.InfoStatName; |
| 20 | +import org.opensearch.neuralsearch.transport.NeuralStatsAction; |
| 21 | +import org.opensearch.neuralsearch.transport.NeuralStatsRequest; |
| 22 | +import org.opensearch.neuralsearch.transport.NeuralStatsResponse; |
| 23 | +import org.opensearch.rest.BytesRestResponse; |
| 24 | +import org.opensearch.rest.RestChannel; |
| 25 | +import org.opensearch.rest.RestRequest; |
| 26 | +import org.opensearch.test.rest.FakeRestRequest; |
| 27 | +import org.opensearch.threadpool.TestThreadPool; |
| 28 | +import org.opensearch.threadpool.ThreadPool; |
| 29 | +import org.opensearch.transport.client.node.NodeClient; |
| 30 | + |
| 31 | +import java.util.EnumSet; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +import static org.mockito.ArgumentMatchers.any; |
| 36 | +import static org.mockito.ArgumentMatchers.eq; |
| 37 | +import static org.mockito.Mockito.doAnswer; |
| 38 | +import static org.mockito.Mockito.never; |
| 39 | +import static org.mockito.Mockito.spy; |
| 40 | +import static org.mockito.Mockito.times; |
| 41 | +import static org.mockito.Mockito.verify; |
| 42 | +import static org.mockito.Mockito.when; |
| 43 | + |
| 44 | +public class RestNeuralStatsActionTests extends InferenceProcessorTestCase { |
| 45 | + private NodeClient client; |
| 46 | + private ThreadPool threadPool; |
| 47 | + |
| 48 | + @Mock |
| 49 | + RestChannel channel; |
| 50 | + |
| 51 | + @Mock |
| 52 | + private NeuralSearchSettingsAccessor settingsAccessor; |
| 53 | + |
| 54 | + @Before |
| 55 | + public void setup() { |
| 56 | + MockitoAnnotations.openMocks(this); |
| 57 | + |
| 58 | + threadPool = new TestThreadPool(this.getClass().getSimpleName() + "ThreadPool"); |
| 59 | + client = spy(new NodeClient(Settings.EMPTY, threadPool)); |
| 60 | + |
| 61 | + doAnswer(invocation -> { |
| 62 | + ActionListener<NeuralStatsResponse> actionListener = invocation.getArgument(2); |
| 63 | + return null; |
| 64 | + }).when(client).execute(eq(NeuralStatsAction.INSTANCE), any(), any()); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void tearDown() throws Exception { |
| 69 | + super.tearDown(); |
| 70 | + threadPool.shutdown(); |
| 71 | + client.close(); |
| 72 | + } |
| 73 | + |
| 74 | + public void test_execute() throws Exception { |
| 75 | + when(settingsAccessor.isStatsEnabled()).thenReturn(true); |
| 76 | + RestNeuralStatsAction restNeuralStatsAction = new RestNeuralStatsAction(settingsAccessor); |
| 77 | + |
| 78 | + RestRequest request = getRestRequest(); |
| 79 | + restNeuralStatsAction.handleRequest(request, channel, client); |
| 80 | + |
| 81 | + ArgumentCaptor<NeuralStatsRequest> argumentCaptor = ArgumentCaptor.forClass(NeuralStatsRequest.class); |
| 82 | + verify(client, times(1)).execute(eq(NeuralStatsAction.INSTANCE), argumentCaptor.capture(), any()); |
| 83 | + |
| 84 | + NeuralStatsInput capturedInput = argumentCaptor.getValue().getNeuralStatsInput(); |
| 85 | + assertEquals(capturedInput.getEventStatNames(), EnumSet.allOf(EventStatName.class)); |
| 86 | + assertEquals(capturedInput.getInfoStatNames(), EnumSet.allOf(InfoStatName.class)); |
| 87 | + } |
| 88 | + |
| 89 | + public void test_handleRequest_disabledForbidden() throws Exception { |
| 90 | + when(settingsAccessor.isStatsEnabled()).thenReturn(false); |
| 91 | + RestNeuralStatsAction restNeuralStatsAction = new RestNeuralStatsAction(settingsAccessor); |
| 92 | + |
| 93 | + RestRequest request = getRestRequest(); |
| 94 | + restNeuralStatsAction.handleRequest(request, channel, client); |
| 95 | + |
| 96 | + verify(client, never()).execute(eq(NeuralStatsAction.INSTANCE), any(), any()); |
| 97 | + |
| 98 | + ArgumentCaptor<BytesRestResponse> responseCaptor = ArgumentCaptor.forClass(BytesRestResponse.class); |
| 99 | + verify(channel).sendResponse(responseCaptor.capture()); |
| 100 | + |
| 101 | + BytesRestResponse response = responseCaptor.getValue(); |
| 102 | + assertEquals(RestStatus.FORBIDDEN, response.status()); |
| 103 | + } |
| 104 | + |
| 105 | + public void test_handleRequest_invalidStatParameter() throws Exception { |
| 106 | + when(settingsAccessor.isStatsEnabled()).thenReturn(true); |
| 107 | + RestNeuralStatsAction restNeuralStatsAction = new RestNeuralStatsAction(settingsAccessor); |
| 108 | + |
| 109 | + // Create request with invalid stat parameter |
| 110 | + Map<String, String> params = new HashMap<>(); |
| 111 | + params.put("stat", "INVALID_STAT"); |
| 112 | + RestRequest request = new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY) |
| 113 | + .withParams(params) |
| 114 | + .build(); |
| 115 | + |
| 116 | + assertThrows( |
| 117 | + IllegalArgumentException.class, |
| 118 | + () -> restNeuralStatsAction.handleRequest(request, channel, client) |
| 119 | + ); |
| 120 | + |
| 121 | + verify(client, never()).execute(eq(NeuralStatsAction.INSTANCE), any(), any()); |
| 122 | + } |
| 123 | + |
| 124 | + private RestRequest getRestRequest() { |
| 125 | + Map<String, String> params = new HashMap<>(); |
| 126 | + return new FakeRestRequest.Builder(NamedXContentRegistry.EMPTY).withParams(params).build(); |
| 127 | + } |
| 128 | +} |
0 commit comments