36
36
import org .elasticsearch .xpack .core .ml .action .PutJobAction ;
37
37
import org .elasticsearch .xpack .core .ml .annotations .Annotation ;
38
38
import org .elasticsearch .xpack .core .ml .annotations .AnnotationIndex ;
39
+ import org .elasticsearch .xpack .core .ml .annotations .AnnotationTests ;
39
40
import org .elasticsearch .xpack .core .ml .job .config .AnalysisConfig ;
40
41
import org .elasticsearch .xpack .core .ml .job .config .DataDescription ;
41
42
import org .elasticsearch .xpack .core .ml .job .config .Detector ;
89
90
import java .util .concurrent .CountDownLatch ;
90
91
import java .util .concurrent .atomic .AtomicReference ;
91
92
93
+ import static java .util .stream .Collectors .toList ;
92
94
import static org .elasticsearch .common .xcontent .json .JsonXContent .jsonXContent ;
93
95
import static org .hamcrest .Matchers .closeTo ;
94
96
import static org .hamcrest .Matchers .contains ;
97
+ import static org .hamcrest .Matchers .containsInAnyOrder ;
95
98
import static org .hamcrest .Matchers .empty ;
96
99
import static org .hamcrest .Matchers .equalTo ;
100
+ import static org .hamcrest .Matchers .everyItem ;
97
101
import static org .hamcrest .Matchers .hasSize ;
98
102
import static org .hamcrest .Matchers .is ;
103
+ import static org .hamcrest .Matchers .not ;
104
+ import static org .hamcrest .Matchers .startsWith ;
99
105
import static org .mockito .Mockito .mock ;
100
106
import static org .mockito .Mockito .never ;
101
107
import static org .mockito .Mockito .verify ;
@@ -168,7 +174,9 @@ public void deleteJob() throws Exception {
168
174
AcknowledgedResponse response = client ().execute (DeleteJobAction .INSTANCE , request ).actionGet ();
169
175
assertTrue (response .isAcknowledged ());
170
176
// Verify that deleting job also deletes associated model snapshots annotations
171
- assertThat (getAnnotations (), empty ());
177
+ assertThat (
178
+ getAnnotations ().stream ().map (Annotation ::getAnnotation ).collect (toList ()),
179
+ everyItem (not (startsWith ("Job model snapshot" ))));
172
180
}
173
181
174
182
public void testProcessResults () throws Exception {
@@ -183,6 +191,8 @@ public void testProcessResults() throws Exception {
183
191
resultsBuilder .addCategoryDefinition (categoryDefinition );
184
192
ModelPlot modelPlot = createModelPlot ();
185
193
resultsBuilder .addModelPlot (modelPlot );
194
+ Annotation annotation = createAnnotation ();
195
+ resultsBuilder .addAnnotation (annotation );
186
196
ModelSizeStats modelSizeStats = createModelSizeStats ();
187
197
resultsBuilder .addModelSizeStats (modelSizeStats );
188
198
ModelSnapshot modelSnapshot = createModelSnapshot ();
@@ -224,17 +234,20 @@ public void testProcessResults() throws Exception {
224
234
assertEquals (modelSnapshot , persistedModelSnapshot .results ().get (0 ));
225
235
assertEquals (Collections .singletonList (modelSnapshot ), capturedUpdateModelSnapshotOnJobRequests );
226
236
227
- // Verify that creating model snapshot also creates associated annotation
228
- List <Annotation > annotations = getAnnotations ();
229
- assertThat (annotations , hasSize (1 ));
230
- assertThat (
231
- annotations .get (0 ).getAnnotation (),
232
- is (equalTo (
233
- new ParameterizedMessage ("Job model snapshot with id [{}] stored" , modelSnapshot .getSnapshotId ()).getFormattedMessage ())));
234
-
235
237
Optional <Quantiles > persistedQuantiles = getQuantiles ();
236
238
assertTrue (persistedQuantiles .isPresent ());
237
239
assertEquals (quantiles , persistedQuantiles .get ());
240
+
241
+ // Verify that there are two annotations:
242
+ // 1. one related to creating model snapshot
243
+ // 2. one for {@link Annotation} result
244
+ List <Annotation > annotations = getAnnotations ();
245
+ assertThat ("Annotations were: " + annotations .toString (), annotations , hasSize (2 ));
246
+ assertThat (
247
+ annotations .stream ().map (Annotation ::getAnnotation ).collect (toList ()),
248
+ containsInAnyOrder (
249
+ new ParameterizedMessage ("Job model snapshot with id [{}] stored" , modelSnapshot .getSnapshotId ()).getFormattedMessage (),
250
+ annotation .getAnnotation ()));
238
251
}
239
252
240
253
public void testProcessResults_ModelSnapshot () throws Exception {
@@ -466,6 +479,10 @@ private static ModelPlot createModelPlot() {
466
479
return new ModelPlotTests ().createTestInstance (JOB_ID );
467
480
}
468
481
482
+ private static Annotation createAnnotation () {
483
+ return AnnotationTests .randomAnnotation (JOB_ID );
484
+ }
485
+
469
486
private static ModelSizeStats createModelSizeStats () {
470
487
ModelSizeStats .Builder builder = new ModelSizeStats .Builder (JOB_ID );
471
488
builder .setTimestamp (randomDate ());
@@ -500,47 +517,53 @@ private static class ResultsBuilder {
500
517
private final List <AutodetectResult > results = new ArrayList <>();
501
518
502
519
ResultsBuilder addBucket (Bucket bucket ) {
503
- results .add (new AutodetectResult (Objects .requireNonNull (bucket ), null , null , null , null , null , null , null , null , null , null ));
520
+ results .add (
521
+ new AutodetectResult (Objects .requireNonNull (bucket ), null , null , null , null , null , null , null , null , null , null , null ));
504
522
return this ;
505
523
}
506
524
507
525
ResultsBuilder addRecords (List <AnomalyRecord > records ) {
508
- results .add (new AutodetectResult (null , records , null , null , null , null , null , null , null , null , null ));
526
+ results .add (new AutodetectResult (null , records , null , null , null , null , null , null , null , null , null , null ));
509
527
return this ;
510
528
}
511
529
512
530
ResultsBuilder addInfluencers (List <Influencer > influencers ) {
513
- results .add (new AutodetectResult (null , null , influencers , null , null , null , null , null , null , null , null ));
531
+ results .add (new AutodetectResult (null , null , influencers , null , null , null , null , null , null , null , null , null ));
514
532
return this ;
515
533
}
516
534
517
535
ResultsBuilder addCategoryDefinition (CategoryDefinition categoryDefinition ) {
518
- results .add (new AutodetectResult (null , null , null , null , null , null , null , null , null , categoryDefinition , null ));
536
+ results .add (new AutodetectResult (null , null , null , null , null , null , null , null , null , null , categoryDefinition , null ));
519
537
return this ;
520
538
}
521
539
522
540
ResultsBuilder addModelPlot (ModelPlot modelPlot ) {
523
- results .add (new AutodetectResult (null , null , null , null , null , null , modelPlot , null , null , null , null ));
541
+ results .add (new AutodetectResult (null , null , null , null , null , null , modelPlot , null , null , null , null , null ));
542
+ return this ;
543
+ }
544
+
545
+ ResultsBuilder addAnnotation (Annotation annotation ) {
546
+ results .add (new AutodetectResult (null , null , null , null , null , null , null , annotation , null , null , null , null ));
524
547
return this ;
525
548
}
526
549
527
550
ResultsBuilder addModelSizeStats (ModelSizeStats modelSizeStats ) {
528
- results .add (new AutodetectResult (null , null , null , null , null , modelSizeStats , null , null , null , null , null ));
551
+ results .add (new AutodetectResult (null , null , null , null , null , modelSizeStats , null , null , null , null , null , null ));
529
552
return this ;
530
553
}
531
554
532
555
ResultsBuilder addModelSnapshot (ModelSnapshot modelSnapshot ) {
533
- results .add (new AutodetectResult (null , null , null , null , modelSnapshot , null , null , null , null , null , null ));
556
+ results .add (new AutodetectResult (null , null , null , null , modelSnapshot , null , null , null , null , null , null , null ));
534
557
return this ;
535
558
}
536
559
537
560
ResultsBuilder addQuantiles (Quantiles quantiles ) {
538
- results .add (new AutodetectResult (null , null , null , quantiles , null , null , null , null , null , null , null ));
561
+ results .add (new AutodetectResult (null , null , null , quantiles , null , null , null , null , null , null , null , null ));
539
562
return this ;
540
563
}
541
564
542
565
ResultsBuilder addFlushAcknowledgement (FlushAcknowledgement flushAcknowledgement ) {
543
- results .add (new AutodetectResult (null , null , null , null , null , null , null , null , null , null , flushAcknowledgement ));
566
+ results .add (new AutodetectResult (null , null , null , null , null , null , null , null , null , null , null , flushAcknowledgement ));
544
567
return this ;
545
568
}
546
569
0 commit comments