11package de .gdata .mobilelab .alertmanagercallback ;
22
3+ import org .graylog2 .plugin .Message ;
4+ import org .graylog2 .plugin .MessageSummary ;
35import org .graylog2 .plugin .alarms .AlertCondition ;
46import org .graylog2 .plugin .configuration .Configuration ;
57import org .graylog2 .plugin .streams .Stream ;
68import org .joda .time .DateTime ;
9+ import org .joda .time .DateTimeZone ;
710import org .junit .Test ;
811
912import java .lang .reflect .Field ;
13+ import java .util .Arrays ;
1014import java .util .HashMap ;
15+ import java .util .List ;
1116import java .util .Map ;
1217
13- import static org .junit .Assert .*;
18+ import static org .junit .Assert .assertEquals ;
19+ import static org .junit .Assert .assertFalse ;
20+ import static org .junit .Assert .assertNotEquals ;
21+ import static org .junit .Assert .assertNotNull ;
22+ import static org .junit .Assert .assertNull ;
23+ import static org .junit .Assert .assertTrue ;
1424import static org .mockito .Mockito .mock ;
1525import static org .mockito .Mockito .when ;
1626
@@ -482,6 +492,7 @@ public void buildWithCustomTemplateAnnotations() {
482492 + "myalertconditiontitle=${alertCondition.title};"
483493 + "myalertconditiondesc=${alertCondition.description};"
484494 + "mytriggeredcondition=${check_result.triggeredCondition};"
495+ + "myMessageBacklog=${if backlog}${foreach backlog message}${message.message} ${end}${else}<No Backlog>${end};"
485496 );
486497
487498 // and: stream mock
@@ -502,6 +513,74 @@ public void buildWithCustomTemplateAnnotations() {
502513 when (checkResult .getTriggeredCondition ()).thenReturn (alertCondition );
503514
504515
516+ // Message Mocks
517+ Message messageOne = new Message ("messageOne" , "source" , new DateTime (2015 , 1 , 1 , 0 , 0 , DateTimeZone .UTC ));
518+ Message messageTwo = new Message ("messageTwo" , "source" , new DateTime (2015 , 1 , 1 , 0 , 0 , DateTimeZone .UTC ));
519+ MessageSummary messageSummaryOne = mock (MessageSummary .class );
520+ MessageSummary messageSummaryTwo = mock (MessageSummary .class );
521+ List <MessageSummary > messageSummaryList = Arrays .asList (messageSummaryOne , messageSummaryTwo );
522+ when (messageSummaryOne .getRawMessage ()).thenReturn (messageOne );
523+ when (messageSummaryTwo .getRawMessage ()).thenReturn (messageTwo );
524+ when (checkResult .getMatchingMessages ()).thenReturn (messageSummaryList );
525+
526+
527+
528+ // and: instance with set mocks as values
529+ AlertManagerPayload alertManagerPayload = AlertManagerPayloadBuilder .newInstance ()
530+ .withConfiguration (configuration )
531+ .withStream (stream )
532+ .withCheckResult (checkResult )
533+ .build ();
534+
535+ // expect: correct values set
536+ // - annotations
537+ assertEquals ("StreamTitle" , alertManagerPayload .getAnnotations ().get ("mystreamtitle" ));
538+ assertEquals ("CheckResultResultDescription" , alertManagerPayload .getAnnotations ().get ("myresultdesc" ));
539+ assertNotNull (alertManagerPayload .getAnnotations ().get ("mytriggeredat" ));
540+ assertNotEquals ("" , alertManagerPayload .getAnnotations ().get ("mytriggeredat" ));
541+ assertEquals ("StreamId" , alertManagerPayload .getAnnotations ().get ("mystreamid" ));
542+ assertEquals ("StreamDescription" , alertManagerPayload .getAnnotations ().get ("mystreamdescription" ));
543+ assertEquals ("AlertTitle" , alertManagerPayload .getAnnotations ().get ("myalertconditiontitle" ));
544+ assertEquals ("AlertDescription" , alertManagerPayload .getAnnotations ().get ("myalertconditiondesc" ));
545+ assertEquals ("TriggeredConditionString" , alertManagerPayload .getAnnotations ().get ("mytriggeredcondition" ));
546+ assertEquals ("messageOne messageTwo " , alertManagerPayload .getAnnotations ().get ("myMessageBacklog" ));
547+ assertEquals (13 , alertManagerPayload .getAnnotations ().size ());
548+ }
549+
550+ @ Test
551+ public void buildWithCustomTemplateAnnotationsWithoutMessageBacklog () {
552+ // given: configuration mock
553+ Configuration configuration = mock (Configuration .class );
554+ when (configuration .getString ("alertmanager_custom_annotations" ))
555+ .thenReturn (
556+ "mystreamtitle=${stream.title};"
557+ + "myresultdesc=${check_result.resultDescription};"
558+ + "mytriggeredat=${check_result.triggeredAt};"
559+ + "mystreamid=${stream.id};"
560+ + "mystreamdescription=${stream.description};"
561+ + "myalertconditiontitle=${alertCondition.title};"
562+ + "myalertconditiondesc=${alertCondition.description};"
563+ + "mytriggeredcondition=${check_result.triggeredCondition};"
564+ + "myMessageBacklog=${if backlog}${foreach backlog message}${message.message} ${end}${else}<No Backlog>${end};"
565+ );
566+
567+ // and: stream mock
568+ Stream stream = mock (Stream .class );
569+ when (stream .getTitle ()).thenReturn ("StreamTitle" );
570+ when (stream .getId ()).thenReturn ("StreamId" );
571+ when (stream .getDescription ()).thenReturn ("StreamDescription" );
572+
573+ // and: checkResult mock
574+ AlertCondition .CheckResult checkResult = mock (AlertCondition .CheckResult .class );
575+ DateTime triggeredAt = new DateTime ();
576+ when (checkResult .getTriggeredAt ()).thenReturn (triggeredAt );
577+ when (checkResult .getResultDescription ()).thenReturn ("CheckResultResultDescription" );
578+ AlertCondition alertCondition = mock (AlertCondition .class );
579+ when (alertCondition .toString ()).thenReturn ("TriggeredConditionString" );
580+ when (alertCondition .getDescription ()).thenReturn ("AlertDescription" );
581+ when (alertCondition .getTitle ()).thenReturn ("AlertTitle" );
582+ when (checkResult .getTriggeredCondition ()).thenReturn (alertCondition );
583+
505584 // and: instance with set mocks as values
506585 AlertManagerPayload alertManagerPayload = AlertManagerPayloadBuilder .newInstance ()
507586 .withConfiguration (configuration )
@@ -520,6 +599,7 @@ public void buildWithCustomTemplateAnnotations() {
520599 assertEquals ("AlertTitle" , alertManagerPayload .getAnnotations ().get ("myalertconditiontitle" ));
521600 assertEquals ("AlertDescription" , alertManagerPayload .getAnnotations ().get ("myalertconditiondesc" ));
522601 assertEquals ("TriggeredConditionString" , alertManagerPayload .getAnnotations ().get ("mytriggeredcondition" ));
523- assertEquals (12 , alertManagerPayload .getAnnotations ().size ());
602+ assertEquals ("<No Backlog>" , alertManagerPayload .getAnnotations ().get ("myMessageBacklog" ));
603+ assertEquals (13 , alertManagerPayload .getAnnotations ().size ());
524604 }
525605}
0 commit comments