|
25 | 25 | import org.opensearch.common.util.concurrent.ThreadContext;
|
26 | 26 | import org.opensearch.core.action.ActionListener;
|
27 | 27 | import org.opensearch.core.common.bytes.BytesArray;
|
| 28 | +import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException; |
28 | 29 | import org.opensearch.core.index.shard.ShardId;
|
29 | 30 | import org.opensearch.index.engine.DocumentMissingException;
|
30 | 31 | import org.opensearch.index.query.QueryBuilder;
|
@@ -109,6 +110,7 @@ public void setUp() throws Exception {
|
109 | 110 | queryBuilder = mock(QueryBuilder.class);
|
110 | 111 | when(queryBuilder.filter(any())).thenReturn(queryBuilder);
|
111 | 112 | when(ruleQueryMapper.from(any(GetRuleRequest.class))).thenReturn(queryBuilder);
|
| 113 | + when(ruleQueryMapper.getCardinalityQuery()).thenReturn(mock(QueryBuilder.class)); |
112 | 114 | when(ruleEntityParser.parse(anyString())).thenReturn(rule);
|
113 | 115 |
|
114 | 116 | rulePersistenceService = new IndexStoredRulePersistenceService(
|
@@ -144,6 +146,25 @@ public void testCreateRuleOnExistingIndex() throws Exception {
|
144 | 146 | assertNotNull(responseCaptor.getValue().getRule());
|
145 | 147 | }
|
146 | 148 |
|
| 149 | + public void testCardinalityCheckBasedFailure() throws Exception { |
| 150 | + CreateRuleRequest createRuleRequest = mock(CreateRuleRequest.class); |
| 151 | + when(createRuleRequest.getRule()).thenReturn(rule); |
| 152 | + when(rule.toXContent(any(), any())).thenAnswer(invocation -> invocation.getArgument(0)); |
| 153 | + |
| 154 | + SearchResponse searchResponse = mock(SearchResponse.class); |
| 155 | + when(searchResponse.getHits()).thenReturn( |
| 156 | + new SearchHits(new SearchHit[] {}, new TotalHits(10000, TotalHits.Relation.EQUAL_TO), 1.0f) |
| 157 | + ); |
| 158 | + when(searchRequestBuilder.get()).thenReturn(searchResponse); |
| 159 | + |
| 160 | + ActionListener<CreateRuleResponse> listener = mock(ActionListener.class); |
| 161 | + rulePersistenceService.createRule(createRuleRequest, listener); |
| 162 | + |
| 163 | + ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(OpenSearchRejectedExecutionException.class); |
| 164 | + verify(listener).onFailure(exceptionCaptor.capture()); |
| 165 | + assertNotNull(exceptionCaptor.getValue()); |
| 166 | + } |
| 167 | + |
147 | 168 | public void testConcurrentCreateDuplicateRules() throws InterruptedException {
|
148 | 169 | ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
|
149 | 170 | int threadCount = 10;
|
|
0 commit comments