]> granicus.if.org Git - icinga2/blob - test/icinga-notification.cpp
Merge pull request #5581 from Icinga/fix/api-crash-race-condition
[icinga2] / test / icinga-notification.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "icinga/notification.hpp"
21 #include <BoostTestTargetConfig.h>
22 #include <iostream>
23
24 using namespace icinga;
25
26 BOOST_AUTO_TEST_SUITE(icinga_notification)
27
28 BOOST_AUTO_TEST_CASE(state_filter)
29 {
30         unsigned long fstate;
31
32         Array::Ptr states = new Array();
33         states->Add("OK");
34         states->Add("Warning");
35
36         Notification::Ptr notification = new Notification();
37
38         notification->SetStateFilter(FilterArrayToInt(states, notification->GetStateFilterMap(), ~0));
39         notification->Activate();
40         notification->SetAuthority(true);
41
42         /* Test passing notification state */
43         fstate = StateFilterWarning;
44         std::cout << "#1 Notification state: " << fstate << " against " << notification->GetStateFilter() << " must pass. " << std::endl;
45         BOOST_CHECK(notification->GetStateFilter() & fstate);
46
47         /* Test filtered notification state */
48         fstate = StateFilterUnknown;
49         std::cout << "#2 Notification state: " << fstate << " against " << notification->GetStateFilter() << " must fail." << std::endl;
50         BOOST_CHECK(!(notification->GetStateFilter() & fstate));
51
52         /* Test unset states filter configuration */
53         notification->SetStateFilter(FilterArrayToInt(Array::Ptr(), notification->GetStateFilterMap(), ~0));
54
55         fstate = StateFilterOK;
56         std::cout << "#3 Notification state: " << fstate << " against " << notification->GetStateFilter() << " must pass." << std::endl;
57         BOOST_CHECK(notification->GetStateFilter() & fstate);
58
59         /* Test empty states filter configuration */
60         states->Clear();
61         notification->SetStateFilter(FilterArrayToInt(states, notification->GetStateFilterMap(), ~0));
62
63         fstate = StateFilterCritical;
64         std::cout << "#4 Notification state: " << fstate << " against " << notification->GetStateFilter() << " must fail." << std::endl;
65         BOOST_CHECK(!(notification->GetStateFilter() & fstate));
66 }
67 BOOST_AUTO_TEST_CASE(type_filter)
68 {
69         unsigned long ftype;
70
71         Array::Ptr types = new Array();
72         types->Add("Problem");
73         types->Add("DowntimeStart");
74         types->Add("DowntimeEnd");
75
76         Notification::Ptr notification = new Notification();
77
78         notification->SetTypeFilter(FilterArrayToInt(types, notification->GetTypeFilterMap(), ~0));
79         notification->Activate();
80         notification->SetAuthority(true);
81
82         /* Test passing notification type */
83         ftype = NotificationProblem;
84         std::cout << "#1 Notification type: " << ftype << " against " << notification->GetTypeFilter() << " must pass." << std::endl;
85         BOOST_CHECK(notification->GetTypeFilter() & ftype);
86
87         /* Test filtered notification type */
88         ftype = NotificationCustom;
89         std::cout << "#2 Notification type: " << ftype << " against " << notification->GetTypeFilter() << " must fail." << std::endl;
90         BOOST_CHECK(!(notification->GetTypeFilter() & ftype));
91
92         /* Test unset types filter configuration */
93         notification->SetTypeFilter(FilterArrayToInt(Array::Ptr(), notification->GetTypeFilterMap(), ~0));
94
95         ftype = NotificationRecovery;
96         std::cout << "#3 Notification type: " << ftype << " against " << notification->GetTypeFilter() << " must pass." << std::endl;
97         BOOST_CHECK(notification->GetTypeFilter() & ftype);
98
99         /* Test empty types filter configuration */
100         types->Clear();
101         notification->SetTypeFilter(FilterArrayToInt(types, notification->GetTypeFilterMap(), ~0));
102
103         ftype = NotificationProblem;
104         std::cout << "#4 Notification type: " << ftype << " against " << notification->GetTypeFilter() << " must fail." << std::endl;
105         BOOST_CHECK(!(notification->GetTypeFilter() & ftype));
106 }
107 BOOST_AUTO_TEST_SUITE_END()