@@ -18,7 +18,10 @@ import { mockedStore } from '../../utils/testUtils';
18
18
import reducer , {
19
19
getDetectorResults ,
20
20
initialDetectorsState ,
21
+ searchResults ,
21
22
} from '../anomalyResults' ;
23
+ import { ALL_CUSTOM_AD_RESULT_INDICES } from '../../../pages/utils/constants'
24
+ import { getAnomalySummaryQuery } from '../../../pages/utils/anomalyResultUtils'
22
25
23
26
jest . mock ( '../../../services' , ( ) => ( {
24
27
...jest . requireActual ( '../../../services' ) ,
@@ -78,7 +81,7 @@ describe('anomaly results reducer actions', () => {
78
81
expect ( httpMockedClient . get ) . toHaveBeenCalledWith (
79
82
`..${
80
83
AD_NODE_API . DETECTOR
81
- } /${ tempDetectorId } /results/${ false } /${ resultIndex } /true`,
84
+ } /${ tempDetectorId } /results/${ false } /${ resultIndex } * /true`,
82
85
{ query : queryParams }
83
86
) ;
84
87
} ) ;
@@ -117,5 +120,161 @@ describe('anomaly results reducer actions', () => {
117
120
) ;
118
121
}
119
122
} ) ;
123
+ test ( 'result index pattern will not result in appended wildcard star' , async ( ) => {
124
+ const response = {
125
+ totalAnomalies : 1 ,
126
+ results : [ { anomalyGrade : 0 , confidence : 1 , starTime : 1 , endTime : 2 } ] ,
127
+ } ;
128
+ httpMockedClient . get = jest
129
+ . fn ( )
130
+ . mockResolvedValue ( { ok : true , response } ) ;
131
+ const tempDetectorId = '123' ;
132
+ let queryParams : DetectorResultsQueryParams = {
133
+ from : 0 ,
134
+ size : 20 ,
135
+ sortDirection : SORT_DIRECTION . ASC ,
136
+ sortField : 'startTime' ,
137
+ } ;
138
+ await store . dispatch (
139
+ getDetectorResults (
140
+ tempDetectorId ,
141
+ '' ,
142
+ queryParams ,
143
+ false ,
144
+ ALL_CUSTOM_AD_RESULT_INDICES ,
145
+ true
146
+ )
147
+ ) ;
148
+ const actions = store . getActions ( ) ;
149
+
150
+ expect ( actions [ 0 ] . type ) . toBe ( 'ad/DETECTOR_RESULTS_REQUEST' ) ;
151
+ expect ( reducer ( initialDetectorsState , actions [ 0 ] ) ) . toEqual ( {
152
+ ...initialDetectorsState ,
153
+ requesting : true ,
154
+ } ) ;
155
+ expect ( actions [ 1 ] . type ) . toBe ( 'ad/DETECTOR_RESULTS_SUCCESS' ) ;
156
+ expect ( reducer ( initialDetectorsState , actions [ 1 ] ) ) . toEqual ( {
157
+ ...initialDetectorsState ,
158
+ requesting : false ,
159
+ total : response . totalAnomalies ,
160
+ anomalies : response . results ,
161
+ featureData : undefined ,
162
+ } ) ;
163
+ expect ( httpMockedClient . get ) . toHaveBeenCalledWith (
164
+ `..${
165
+ AD_NODE_API . DETECTOR
166
+ } /${ tempDetectorId } /results/${ false } /${ ALL_CUSTOM_AD_RESULT_INDICES } /true`,
167
+ { query : queryParams }
168
+ ) ;
169
+ } ) ;
170
+ } ) ;
171
+ test ( 'searchResults should append wildcard star at the end of custom result index' , async ( ) => {
172
+ const response = {
173
+ aggregations : {
174
+ top_entities : {
175
+ doc_count : 0 ,
176
+ top_entity_aggs : {
177
+ doc_count_error_upper_bound : 0 ,
178
+ sum_other_doc_count : 0 ,
179
+ buckets : [ ]
180
+ }
181
+ }
182
+ }
183
+ } ;
184
+
185
+ httpMockedClient . post = jest
186
+ . fn ( )
187
+ . mockResolvedValue ( { ok : true , response } ) ;
188
+ const tempDetectorId = '123' ;
189
+ const resultIndex = 'opensearch-ad-plugin-result-test' ;
190
+ const requestBody = getAnomalySummaryQuery ( 1717529636479 , 1717529736479 , tempDetectorId , undefined , false , undefined , undefined )
191
+ await store . dispatch (
192
+ searchResults (
193
+ requestBody ,
194
+ resultIndex ,
195
+ '' ,
196
+ true
197
+ )
198
+ ) ;
199
+ const actions = store . getActions ( ) ;
200
+
201
+ expect ( actions [ 0 ] . type ) . toBe ( 'ad/SEARCH_ANOMALY_RESULTS_REQUEST' ) ;
202
+ expect ( reducer ( initialDetectorsState , actions [ 0 ] ) ) . toEqual ( {
203
+ ...initialDetectorsState ,
204
+ requesting : true ,
205
+ } ) ;
206
+ expect ( actions [ 1 ] . type ) . toBe ( 'ad/SEARCH_ANOMALY_RESULTS_SUCCESS' ) ;
207
+ expect ( reducer ( initialDetectorsState , actions [ 1 ] ) ) . toEqual ( {
208
+ ...initialDetectorsState ,
209
+ requesting : false ,
210
+ } ) ;
211
+ expect ( httpMockedClient . post ) . toHaveBeenCalledWith (
212
+ `..${
213
+ AD_NODE_API . DETECTOR
214
+ } /results/_search/${ resultIndex } */true`,
215
+ { body : JSON . stringify ( requestBody ) }
216
+ ) ;
217
+ } ) ;
218
+ test ( 'searchResults should not append wildcard star at the end of custom result index' , async ( ) => {
219
+ const response = {
220
+ took : 1 ,
221
+ timed_out : false ,
222
+ _shards : {
223
+ total : 2 ,
224
+ successful : 2 ,
225
+ skipped : 0 ,
226
+ failed : 0
227
+ } ,
228
+ hits : {
229
+ total : {
230
+ value : 0 ,
231
+ relation : "eq"
232
+ } ,
233
+ max_score : null ,
234
+ hits : [ ]
235
+ } ,
236
+ aggregations : {
237
+ top_entities : {
238
+ doc_count : 0 ,
239
+ top_entity_aggs : {
240
+ doc_count_error_upper_bound : 0 ,
241
+ sum_other_doc_count : 0 ,
242
+ buckets : [ ]
243
+ }
244
+ }
245
+ }
246
+ } ;
247
+
248
+ httpMockedClient . post = jest
249
+ . fn ( )
250
+ . mockResolvedValue ( { ok : true , response } ) ;
251
+ const tempDetectorId = '123' ;
252
+ const requestBody = getAnomalySummaryQuery ( 1717529636479 , 1717529736479 , tempDetectorId , undefined , false , undefined , undefined )
253
+ await store . dispatch (
254
+ searchResults (
255
+ requestBody ,
256
+ ALL_CUSTOM_AD_RESULT_INDICES ,
257
+ '' ,
258
+ true
259
+ )
260
+ ) ;
261
+ const actions = store . getActions ( ) ;
262
+
263
+ expect ( actions [ 0 ] . type ) . toBe ( 'ad/SEARCH_ANOMALY_RESULTS_REQUEST' ) ;
264
+ expect ( reducer ( initialDetectorsState , actions [ 0 ] ) ) . toEqual ( {
265
+ ...initialDetectorsState ,
266
+ requesting : true ,
267
+ } ) ;
268
+ expect ( actions [ 1 ] . type ) . toBe ( 'ad/SEARCH_ANOMALY_RESULTS_SUCCESS' ) ;
269
+ expect ( reducer ( initialDetectorsState , actions [ 1 ] ) ) . toEqual ( {
270
+ ...initialDetectorsState ,
271
+ requesting : false ,
272
+ } ) ;
273
+ expect ( httpMockedClient . post ) . toHaveBeenCalledWith (
274
+ `..${
275
+ AD_NODE_API . DETECTOR
276
+ } /results/_search/${ ALL_CUSTOM_AD_RESULT_INDICES } /true`,
277
+ { body : JSON . stringify ( requestBody ) }
278
+ ) ;
120
279
} ) ;
121
280
} ) ;
0 commit comments