1+ package de .gdata .mobilelab .alertmanagercallback ;
2+
3+ import org .graylog2 .plugin .alarms .AlertCondition ;
4+ import org .graylog2 .plugin .configuration .Configuration ;
5+ import org .graylog2 .plugin .streams .Stream ;
6+ import org .joda .time .DateTime ;
7+ import org .junit .Test ;
8+
9+ import java .lang .reflect .Field ;
10+ import java .util .HashMap ;
11+ import java .util .Map ;
12+
13+ import static org .junit .Assert .assertEquals ;
14+ import static org .junit .Assert .assertFalse ;
15+ import static org .junit .Assert .assertNotEquals ;
16+ import static org .junit .Assert .assertNotNull ;
17+ import static org .junit .Assert .assertNull ;
18+ import static org .junit .Assert .assertTrue ;
19+ import static org .mockito .Mockito .mock ;
20+ import static org .mockito .Mockito .when ;
21+
22+ public class AlertManagerPayloadBuilderTest {
23+
24+ @ Test
25+ public void newInstance () {
26+ assertNotNull (AlertManagerPayloadBuilder .newInstance ());
27+ assertEquals (AlertManagerPayloadBuilder .class , AlertManagerPayloadBuilder .newInstance ().getClass ());
28+ }
29+
30+ @ Test
31+ public void withStream () throws NoSuchFieldException , IllegalAccessException {
32+ // given: Access to private field stream
33+ Field streamField = AlertManagerPayloadBuilder .class .getDeclaredField ("stream" );
34+ streamField .setAccessible (true );
35+
36+ // and: instance
37+ AlertManagerPayloadBuilder alertManagerPayloadBuilder = AlertManagerPayloadBuilder .newInstance ();
38+
39+ // and: a stream mock
40+ Stream stream = mock (Stream .class );
41+
42+ // when: calling withStream with given stream
43+ alertManagerPayloadBuilder = alertManagerPayloadBuilder .withStream (stream );
44+
45+ // then: stream has been set
46+ assertEquals (stream , streamField .get (alertManagerPayloadBuilder ));
47+ }
48+
49+ @ Test
50+ public void withCheckResult () throws IllegalAccessException , NoSuchFieldException {
51+ // given: Access to private field checkResult
52+ Field checkResultField = AlertManagerPayloadBuilder .class .getDeclaredField ("checkResult" );
53+ checkResultField .setAccessible (true );
54+
55+ // and: instance
56+ AlertManagerPayloadBuilder alertManagerPayloadBuilder = AlertManagerPayloadBuilder .newInstance ();
57+
58+ // and: a checkResult mock
59+ AlertCondition .CheckResult checkResult = mock (AlertCondition .CheckResult .class );
60+
61+ // when: calling withCheckResult with given checkResult
62+ alertManagerPayloadBuilder = alertManagerPayloadBuilder .withCheckResult (checkResult );
63+
64+ // then: checkResult has been set
65+ assertEquals (checkResult , checkResultField .get (alertManagerPayloadBuilder ));
66+ }
67+
68+ @ Test
69+ public void withConfiguration () throws NoSuchFieldException , IllegalAccessException {
70+ // given: Access to private field configuration
71+ Field configurationField = AlertManagerPayloadBuilder .class .getDeclaredField ("configuration" );
72+ configurationField .setAccessible (true );
73+
74+ // and: instance
75+ AlertManagerPayloadBuilder alertManagerPayloadBuilder = AlertManagerPayloadBuilder .newInstance ();
76+
77+ // and: a configuration mock
78+ Configuration configuration = mock (Configuration .class );
79+
80+ // when: calling withConfiguration with given configuration
81+ alertManagerPayloadBuilder = alertManagerPayloadBuilder .withConfiguration (configuration );
82+
83+ // then: configuration has been set
84+ assertEquals (configuration , configurationField .get (alertManagerPayloadBuilder ));
85+ }
86+
87+ @ Test
88+ public void buildWithNoInformation () {
89+ // given: object with no information given
90+ AlertManagerPayload alertManagerPayload = AlertManagerPayloadBuilder .newInstance ().build ();
91+
92+ // expect: default values have been set if any
93+ assertNotNull (alertManagerPayload );
94+ assertTrue (alertManagerPayload .getAnnotations ().isEmpty ());
95+ assertFalse (alertManagerPayload .getLabels ().isEmpty ());
96+ assertTrue (alertManagerPayload .getLabels ().containsKey ("alertname" ));
97+ assertEquals ("Please add a valid configuration object to AlertManager plugin." , alertManagerPayload .getLabels ().get ("alertname" ));
98+ assertNull (alertManagerPayload .getGeneratorURL ());
99+ assertNotNull (alertManagerPayload .getStartsAt ());
100+ assertNotNull (alertManagerPayload .getEndsAt ());
101+ }
102+
103+ @ Test
104+ public void buildWithFullInformation () {
105+ // given: configuration mock
106+ Configuration configuration = mock (Configuration .class );
107+ when (configuration .getString ("alertmanager_alert_name" )).thenReturn ("AlertName" );
108+
109+ // and: stream mock
110+ Stream stream = mock (Stream .class );
111+ when (stream .getTitle ()).thenReturn ("StreamTitle" );
112+
113+ // and: checkResult mock
114+ AlertCondition .CheckResult checkResult = mock (AlertCondition .CheckResult .class );
115+ DateTime triggeredAt = new DateTime ();
116+ when (checkResult .getTriggeredAt ()).thenReturn (triggeredAt );
117+ AlertCondition alertCondition = mock (AlertCondition .class );
118+ when (alertCondition .getDescription ()).thenReturn ("aDescription" );
119+ when (alertCondition .getTitle ()).thenReturn ("aTitle" );
120+ Map <String , Object > parameters = new HashMap <>();
121+ parameters .put ("stream_url" , "aStreamUrl" );
122+ when (alertCondition .getParameters ()).thenReturn (parameters );
123+ when (alertCondition .getGrace ()).thenReturn (1337 );
124+ when (checkResult .getTriggeredCondition ()).thenReturn (alertCondition );
125+
126+ // and: instance with set mocks as values
127+ AlertManagerPayload alertManagerPayload = AlertManagerPayloadBuilder .newInstance ()
128+ .withStream (stream )
129+ .withConfiguration (configuration )
130+ .withCheckResult (checkResult )
131+ .build ();
132+
133+ // expect: correct values set
134+ // - labels
135+ assertEquals ("AlertName" , alertManagerPayload .getLabels ().get ("alertname" ));
136+
137+ // - annotations
138+ assertEquals ("StreamTitle" , alertManagerPayload .getAnnotations ().get ("stream_title" ));
139+ assertEquals (triggeredAt .toString (), alertManagerPayload .getAnnotations ().get ("triggered_at" ));
140+ assertEquals ("aDescription" , alertManagerPayload .getAnnotations ().get ("triggered_rule_description" ));
141+ assertEquals ("aTitle" , alertManagerPayload .getAnnotations ().get ("triggered_rule_title" ));
142+
143+ // - startsAt
144+ assertEquals (triggeredAt .toString (), alertManagerPayload .getStartsAt ());
145+
146+ // - endsAt
147+ assertEquals (triggeredAt .plusMinutes (1337 ).toString (), alertManagerPayload .getEndsAt ());
148+
149+ // - generatorUrl
150+ assertEquals ("aStreamUrl" , alertManagerPayload .getGeneratorURL ());
151+ }
152+
153+ @ Test
154+ public void buildWithNoInformationForLabels () {
155+ // given: configuration mock
156+ Configuration configuration = mock (Configuration .class );
157+ when (configuration .getString ("alertmanager_alert_name" )).thenReturn (null );
158+
159+
160+ // and: instance with set mocks as values
161+ AlertManagerPayload alertManagerPayload = AlertManagerPayloadBuilder .newInstance ()
162+ .withConfiguration (configuration )
163+ .build ();
164+
165+ // expect: correct values set
166+ // - labels
167+ assertEquals ("Please add a valid configuration object to AlertManager plugin." , alertManagerPayload .getLabels ().get ("alertname" ));
168+ }
169+
170+ @ Test
171+ public void buildWithNoInformationForAnnotations () {
172+ // given: stream mock
173+ Stream stream = mock (Stream .class );
174+ when (stream .getTitle ()).thenReturn (null );
175+
176+ // and: checkResult mock
177+ AlertCondition .CheckResult checkResult = mock (AlertCondition .CheckResult .class );
178+ when (checkResult .getTriggeredAt ()).thenReturn (null );
179+ AlertCondition alertCondition = mock (AlertCondition .class );
180+ when (alertCondition .getDescription ()).thenReturn (null );
181+ when (alertCondition .getTitle ()).thenReturn (null );
182+ when (checkResult .getTriggeredCondition ()).thenReturn (alertCondition );
183+
184+ // and: instance with set mocks as values
185+ AlertManagerPayload alertManagerPayload = AlertManagerPayloadBuilder .newInstance ()
186+ .withStream (stream )
187+ .withCheckResult (checkResult )
188+ .build ();
189+
190+ // expect: correct values set
191+ // - annotations
192+ assertFalse (alertManagerPayload .getAnnotations ().containsKey ("stream_title" ));
193+ assertNull (alertManagerPayload .getAnnotations ().get ("triggered_at" ));
194+ assertNull (alertManagerPayload .getAnnotations ().get ("triggered_rule_description" ));
195+ assertNull (alertManagerPayload .getAnnotations ().get ("triggered_rule_title" ));
196+ }
197+
198+ @ Test
199+ public void buildWithNoInformationForEndsAt () {
200+ // given: checkResult mock
201+ AlertCondition .CheckResult checkResult = mock (AlertCondition .CheckResult .class );
202+ when (checkResult .getTriggeredAt ()).thenReturn (null );
203+ AlertCondition alertCondition = mock (AlertCondition .class );
204+ when (alertCondition .getGrace ()).thenReturn (0 );
205+ when (checkResult .getTriggeredCondition ()).thenReturn (alertCondition );
206+
207+ // and: instance with set mocks as values
208+ AlertManagerPayload alertManagerPayload = AlertManagerPayloadBuilder .newInstance ()
209+ .withCheckResult (checkResult )
210+ .build ();
211+
212+ // expect: correct values set
213+ // - endsAt
214+ assertNotNull (alertManagerPayload .getEndsAt ());
215+ assertNotEquals ("" , alertManagerPayload .getEndsAt ());
216+ }
217+
218+ @ Test
219+ public void buildWithNoInformationForStartsAt () {
220+ // given: checkResult mock
221+ AlertCondition .CheckResult checkResult = mock (AlertCondition .CheckResult .class );
222+ when (checkResult .getTriggeredAt ()).thenReturn (null );
223+
224+ // and: instance with set mocks as values
225+ AlertManagerPayload alertManagerPayload = AlertManagerPayloadBuilder .newInstance ()
226+ .withCheckResult (checkResult )
227+ .build ();
228+
229+ // expect: correct values set
230+ // - startsAt
231+ assertNotNull (alertManagerPayload .getStartsAt ());
232+ assertNotEquals ("" , alertManagerPayload .getStartsAt ());
233+ }
234+
235+ @ Test
236+ public void buildWithNoInformationForStreamUrl () {
237+ // given: checkResult mock
238+ AlertCondition .CheckResult checkResult = mock (AlertCondition .CheckResult .class );
239+ AlertCondition alertCondition = mock (AlertCondition .class );
240+ Map <String , Object > parameters = new HashMap <>();
241+ parameters .put ("stream_url" , null );
242+ when (alertCondition .getParameters ()).thenReturn (parameters );
243+ when (checkResult .getTriggeredCondition ()).thenReturn (alertCondition );
244+
245+ // and: instance with set mocks as values
246+ AlertManagerPayload alertManagerPayload = AlertManagerPayloadBuilder .newInstance ()
247+ .withCheckResult (checkResult )
248+ .build ();
249+
250+ // expect: correct values set
251+ // - generatorUrl
252+ assertEquals ("null" , alertManagerPayload .getGeneratorURL ());
253+ }
254+
255+ @ Test
256+ public void buildWithNoInformationForStreamUrlParameters () {
257+ // given: checkResult mock
258+ AlertCondition .CheckResult checkResult = mock (AlertCondition .CheckResult .class );
259+ AlertCondition alertCondition = mock (AlertCondition .class );
260+ when (alertCondition .getParameters ()).thenReturn (null );
261+ when (checkResult .getTriggeredCondition ()).thenReturn (alertCondition );
262+
263+ // and: instance with set mocks as values
264+ AlertManagerPayload alertManagerPayload = AlertManagerPayloadBuilder .newInstance ()
265+ .withCheckResult (checkResult )
266+ .build ();
267+
268+ // expect: correct values set
269+ // - generatorUrl
270+ assertNull (alertManagerPayload .getGeneratorURL ());
271+ }
272+ }
0 commit comments