|
31 | 31 | import org.mockito.Mockito;
|
32 | 32 | import org.mockito.MockitoAnnotations;
|
33 | 33 | import org.opensearch.action.search.SearchResponse;
|
| 34 | +import org.opensearch.action.support.PlainActionFuture; |
34 | 35 | import org.opensearch.action.support.WriteRequest;
|
35 | 36 | import org.opensearch.ad.indices.ADIndex;
|
36 | 37 | import org.opensearch.ad.indices.ADIndexManagement;
|
|
54 | 55 | import org.opensearch.timeseries.AbstractTimeSeriesTest;
|
55 | 56 | import org.opensearch.timeseries.NodeStateManager;
|
56 | 57 | import org.opensearch.timeseries.TestHelpers;
|
| 58 | +import org.opensearch.timeseries.common.exception.TimeSeriesException; |
57 | 59 | import org.opensearch.timeseries.common.exception.ValidationException;
|
58 | 60 | import org.opensearch.timeseries.feature.SearchFeatureDao;
|
59 | 61 | import org.opensearch.timeseries.model.ValidationAspect;
|
@@ -150,7 +152,7 @@ public void testValidateMoreThanThousandSingleEntityDetectorLimit() throws IOExc
|
150 | 152 | // extend NodeClient since its execute method is final and mockito does not allow to mock final methods
|
151 | 153 | // we can also use spy to overstep the final methods
|
152 | 154 | NodeClient client = IndexAnomalyDetectorActionHandlerTests
|
153 |
| - .getCustomNodeClient(detectorResponse, userIndexResponse, singleEntityDetector, threadPool); |
| 155 | + .getCustomNodeClient(detectorResponse, userIndexResponse, null, false, singleEntityDetector, threadPool); |
154 | 156 |
|
155 | 157 | NodeClient clientSpy = spy(client);
|
156 | 158 | NodeStateManager nodeStateManager = mock(NodeStateManager.class);
|
@@ -208,7 +210,7 @@ public void testValidateMoreThanTenMultiEntityDetectorsLimit() throws IOExceptio
|
208 | 210 | // extend NodeClient since its execute method is final and mockito does not allow to mock final methods
|
209 | 211 | // we can also use spy to overstep the final methods
|
210 | 212 | NodeClient client = IndexAnomalyDetectorActionHandlerTests
|
211 |
| - .getCustomNodeClient(detectorResponse, userIndexResponse, detector, threadPool); |
| 213 | + .getCustomNodeClient(detectorResponse, userIndexResponse, null, false, detector, threadPool); |
212 | 214 | NodeClient clientSpy = spy(client);
|
213 | 215 | NodeStateManager nodeStateManager = mock(NodeStateManager.class);
|
214 | 216 | SecurityClientUtil clientUtil = new SecurityClientUtil(nodeStateManager, settings);
|
@@ -250,4 +252,60 @@ public void testValidateMoreThanTenMultiEntityDetectorsLimit() throws IOExceptio
|
250 | 252 | assertTrue(inProgressLatch.await(100, TimeUnit.SECONDS));
|
251 | 253 | verify(clientSpy, never()).execute(eq(GetMappingsAction.INSTANCE), any(), any());
|
252 | 254 | }
|
| 255 | + |
| 256 | + // This test also validates that if we get a non timeseries exception or not an invalid query that we will not completely block |
| 257 | + // detector creation, this is applicable like things when we get timeout not cause of AD configuration errors but because cluster |
| 258 | + // is momentarily under utilized. |
| 259 | + public void testValidateMoreThanTenMultiEntityDetectorsLimitDuplicateNameFailure() throws IOException, InterruptedException { |
| 260 | + SearchResponse mockResponse = mock(SearchResponse.class); |
| 261 | + int totalHits = 1; |
| 262 | + when(mockResponse.getHits()).thenReturn(TestHelpers.createSearchHits(totalHits)); |
| 263 | + SearchResponse detectorResponse = mock(SearchResponse.class); |
| 264 | + when(detectorResponse.getHits()).thenReturn(TestHelpers.createSearchHits(totalHits)); |
| 265 | + SearchResponse userIndexResponse = mock(SearchResponse.class); |
| 266 | + when(userIndexResponse.getHits()).thenReturn(TestHelpers.createSearchHits(0)); |
| 267 | + AnomalyDetector singleEntityDetector = TestHelpers.randomAnomalyDetector(TestHelpers.randomUiMetadata(), null, true); |
| 268 | + |
| 269 | + SearchResponse configInputIndicesResponse = mock(SearchResponse.class); |
| 270 | + when(configInputIndicesResponse.getHits()).thenReturn(TestHelpers.createSearchHits(2)); |
| 271 | + |
| 272 | + // extend NodeClient since its execute method is final and mockito does not allow to mock final methods |
| 273 | + // we can also use spy to overstep the final methods |
| 274 | + NodeClient client = IndexAnomalyDetectorActionHandlerTests |
| 275 | + .getCustomNodeClient(detectorResponse, userIndexResponse, configInputIndicesResponse, true, singleEntityDetector, threadPool); |
| 276 | + |
| 277 | + NodeClient clientSpy = spy(client); |
| 278 | + NodeStateManager nodeStateManager = mock(NodeStateManager.class); |
| 279 | + SecurityClientUtil clientUtil = new SecurityClientUtil(nodeStateManager, settings); |
| 280 | + |
| 281 | + handler = new ValidateAnomalyDetectorActionHandler( |
| 282 | + clusterService, |
| 283 | + clientSpy, |
| 284 | + clientUtil, |
| 285 | + anomalyDetectionIndices, |
| 286 | + singleEntityDetector, |
| 287 | + requestTimeout, |
| 288 | + maxSingleEntityAnomalyDetectors, |
| 289 | + maxMultiEntityAnomalyDetectors, |
| 290 | + maxAnomalyFeatures, |
| 291 | + maxCategoricalFields, |
| 292 | + method, |
| 293 | + xContentRegistry(), |
| 294 | + null, |
| 295 | + searchFeatureDao, |
| 296 | + ValidationAspect.DETECTOR.getName(), |
| 297 | + clock, |
| 298 | + settings |
| 299 | + ); |
| 300 | + PlainActionFuture<ValidateConfigResponse> future = PlainActionFuture.newFuture(); |
| 301 | + handler.start(future); |
| 302 | + try { |
| 303 | + future.actionGet(100, TimeUnit.SECONDS); |
| 304 | + fail("should not reach here"); |
| 305 | + } catch (Exception e) { |
| 306 | + assertTrue(e instanceof TimeSeriesException); |
| 307 | + assertTrue(e.getMessage().contains("Cannot create anomaly detector with name")); |
| 308 | + } |
| 309 | + verify(clientSpy, never()).execute(eq(GetMappingsAction.INSTANCE), any(), any()); |
| 310 | + } |
253 | 311 | }
|
0 commit comments