Esempio n. 1
0
 def __init__(self, parent=None):
     super(AppLogin, self).__init__(parent)
     self.setWindowTitle('Login to Alignak')
     self.setWindowFlags(Qt.FramelessWindowHint)
     self.setStyleSheet(get_css())
     self.setWindowIcon(QIcon(get_image_path('icon')))
     self.setFixedSize(300, 330)
     # Fields
     self.app_backend = AppBackend()
     self.backend_url = None
     self.username_line = None
     self.password_line = None
     self.offset = None
Esempio n. 2
0
    def test_diff_last_state(self):
        """Diff Last Backend SState"""

        self.backend = AppBackend()
        self.backend.login()

        # TrayIcon and Dashboard for notifier
        tray_icon_test = TrayIcon(self.icon)
        dashboard_test = Dashboard()

        notifier_test = AppNotifier()
        notifier_test.app_backend = self.backend
        notifier_test.tray_icon = tray_icon_test
        notifier_test.dashboard = dashboard_test

        # Notifier check data to get first synthesys counts
        notifier_test.check_data()

        # Get synthesis test to test diff between check
        synthesis = self.backend.synthesis_count()
        under_test = notifier_test.diff_last_states(synthesis)

        fields_test = {
            'hosts': {
                'up': 0,
                'downtime': 0,
                'down': 0,
                'acknowledge': 0,
                'unreachable': 0
            },
            'services': {
                'ok': 0,
                'acknowledge': 0,
                'unknown': 0,
                'critical': 0,
                'downtime': 0,
                'unreachable': 0,
                'warning': 0
            }
        }

        self.assertTrue(under_test)
        self.assertTrue('hosts' in under_test)
        self.assertTrue('services' in under_test)

        for state in fields_test['hosts']:
            self.assertTrue(state in under_test['hosts'])
            self.assertTrue(isinstance(under_test['hosts'][state], int))
        for state in fields_test['services']:
            self.assertTrue(state in under_test['services'])
            self.assertTrue(isinstance(under_test['services'][state], int))
Esempio n. 3
0
    def test_states_change(self):
        """States and Notify Changes"""
        self.backend = AppBackend()
        self.backend.login()

        # TrayIcon and Dashboard for notifier
        dashboard = Dashboard()
        dashboard.initialize()

        tray_icon = TrayIcon(self.icon)
        tray_icon.build_menu(self.backend, dashboard)

        # Initialize Notifier
        under_test = AppNotifier()
        under_test.initialize(self.backend, tray_icon, dashboard)

        # Changes are True, first_start is True
        self.assertFalse(under_test.changes)
        self.assertTrue(under_test.first_start)
        self.assertIsNone(under_test.old_synthesis)
Esempio n. 4
0
class TestServicesView(unittest2.TestCase):
    """
        This file test the Synthesis class.
    """

    init_config()

    app_backend = AppBackend()
    app_backend.login()

    @classmethod
    def setUpClass(cls):
        """Create QApplication"""
        try:
            cls.app = QApplication(sys.argv)
            cls.widget = QWidget()
        except:
            pass

    def test_initialize(self):
        """Initialize Synthesis"""

        under_test = Synthesis()

        self.assertFalse(under_test.app_backend)
        self.assertFalse(under_test.action_manager)
        self.assertIsNone(under_test.host_synthesis)
        self.assertTrue(under_test.line_search)

        under_test.initialize(self.app_backend)

        self.assertTrue(under_test.app_backend)
        self.assertTrue(under_test.action_manager)
        self.assertIsNone(under_test.host_synthesis)
        self.assertTrue(under_test.line_search)

    def test_display_host_synthesis(self):
        """Display Host Synthesis"""

        under_test = Synthesis()

        under_test.initialize(self.app_backend)

        self.assertIsNone(under_test.host_synthesis)
        self.assertTrue(under_test.first_display)

        under_test.display_host_synthesis()

        self.assertIsNotNone(under_test.host_synthesis)
        self.assertFalse(under_test.first_display)
Esempio n. 5
0
    def run(self, app_backend=None):  # pragma: no cover
        """
        Start all Alignak-app processes and create AppBackend if connection by config file.

        :param app_backend: AppBackend object
        :type app_backend: alignak_app.core.backend.AppBackend | None
        """

        # If not login form, app try anyway to connect by token
        if not app_backend:
            app_backend = AppBackend()
            connect = app_backend.login()
            if connect:
                username = app_backend.get_user(projection=['name'])['name']
                send_banner('OK', 'Welcome %s, you are connected to Alignak Backend' % username)
            else:
                self.display_error_msg()

        if 'token' not in app_backend.user:
            app_backend.user['token'] = app_backend.backend.token

        # Dashboard
        self.dashboard = Dashboard()
        self.dashboard.initialize()

        # TrayIcon
        self.tray_icon = TrayIcon(QIcon(get_image_path('icon')))
        self.tray_icon.build_menu(app_backend, self.dashboard)
        self.tray_icon.show()

        app_backend.app = self
        start = bool(app_backend.get_user(projection=['name']))

        if start:
            # Notifier
            self.notifier = AppNotifier()
            self.notifier.initialize(app_backend, self.tray_icon, self.dashboard)

            self.notifier_timer.start(self.notifier.interval)
            self.notifier_timer.timeout.connect(self.notifier.check_data)

            self.reconnecting.connect(self.reconnect_to_backend)
        else:
            # In case of...
            self.display_error_msg()
Esempio n. 6
0
class AppLogin(QDialog):
    """
        Class who create login QDialog.
    """
    def __init__(self, parent=None):
        super(AppLogin, self).__init__(parent)
        self.setWindowTitle('Login to Alignak')
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.setStyleSheet(get_css())
        self.setWindowIcon(QIcon(get_image_path('icon')))
        self.setFixedSize(300, 330)
        # Fields
        self.app_backend = AppBackend()
        self.backend_url = None
        self.username_line = None
        self.password_line = None
        self.offset = None

    def showEvent(self, _):
        """ QDialog.showEvent(QShowEvent) """

        self.username_line.setFocus()

    def create_widget(self):
        """
        Create widget login

        """

        # Main layout
        main_layout = QVBoxLayout(self)
        main_layout.setContentsMargins(0, 0, 0, 0)

        main_layout.addWidget(self.get_logo_widget(self))

        title = QLabel('Welcome to <b>Alignak-app</b>')
        title.setObjectName('title_login')
        main_layout.addWidget(title)
        main_layout.setAlignment(title, Qt.AlignCenter)

        version = QLabel('Version %s' % __version__)
        version.setObjectName('version_login')
        main_layout.addWidget(version)
        main_layout.setAlignment(version, Qt.AlignCenter | Qt.AlignTop)

        # Login QWidget
        login_widget = QWidget(self)
        login_widget.setObjectName('login')
        login_layout = QGridLayout(login_widget)

        # Configuration button
        refresh_conf_btn = QPushButton()
        refresh_conf_btn.clicked.connect(init_config)
        refresh_conf_btn.setFixedSize(25, 25)
        refresh_conf_btn.setIcon(QIcon(get_image_path('refresh')))
        refresh_conf_btn.setToolTip('Reload configuration')
        login_layout.addWidget(refresh_conf_btn, 2, 1, 1, 1)

        # Server button
        server_btn = QPushButton()
        server_btn.clicked.connect(self.handle_server)
        server_btn.setFixedSize(25, 25)
        server_btn.setIcon(QIcon(get_image_path('host')))
        server_btn.setToolTip('Modify Alignak Server')
        login_layout.addWidget(server_btn, 2, 2, 1, 1)

        # Welcome text
        login_label = QLabel('<b>Log-in</b> to use the application')
        login_layout.addWidget(login_label, 2, 0, 1, 1)
        login_layout.setAlignment(login_label, Qt.AlignCenter)

        # Username field
        self.username_line = QLineEdit(self)
        self.username_line.setPlaceholderText('Username')
        login_layout.addWidget(self.username_line, 3, 0, 1, 3)

        # Password field
        self.password_line = QLineEdit(self)
        self.password_line.setPlaceholderText('Password')
        self.password_line.setEchoMode(QLineEdit.Password)
        login_layout.addWidget(self.password_line, 4, 0, 1, 3)

        # Login button
        login_button = QPushButton('LOGIN', self)
        login_button.clicked.connect(self.handle_login)
        login_button.setObjectName('valid')
        login_button.setMinimumHeight(30)
        login_button.setDefault(True)
        login_layout.addWidget(login_button, 5, 0, 1, 3)

        main_layout.addWidget(login_widget)
        self.setLayout(main_layout)

        self.center(self)

    @staticmethod
    def center(widget):
        """
        Center QWidget

        """

        screen = QApplication.desktop().screenNumber(
            QApplication.desktop().cursor().pos())
        center = QApplication.desktop().screenGeometry(screen).center()
        widget.move(center.x() - (widget.width() / 2),
                    center.y() - (widget.height() / 2))

    @staticmethod
    def get_logo_widget(widget):
        """
        Return the logo QWidget

        :param widget: widget parent, needed for action button
        :type widget: QWidget
        :return: logo QWidget
        :rtype: QWidget
        """

        logo_widget = QWidget()
        logo_widget.setFixedHeight(45)
        logo_widget.setObjectName('title')
        logo_layout = QHBoxLayout()
        logo_widget.setLayout(logo_layout)

        logo_label = QLabel()
        logo_label.setPixmap(QPixmap(get_image_path('alignak')))
        logo_label.setFixedSize(121, 35)
        logo_label.setScaledContents(True)

        logo_layout.addWidget(logo_label, 0)

        minimize_btn = QPushButton()
        minimize_btn.setIcon(QIcon(get_image_path('minimize')))
        minimize_btn.setFixedSize(24, 24)
        minimize_btn.setObjectName('app_widget')
        minimize_btn.clicked.connect(widget.showMinimized)
        logo_layout.addStretch(widget.width())
        logo_layout.addWidget(minimize_btn, 1)

        maximize_btn = QPushButton()
        maximize_btn.setIcon(QIcon(get_image_path('maximize')))
        maximize_btn.setFixedSize(24, 24)
        maximize_btn.setObjectName('app_widget')
        maximize_btn.clicked.connect(widget.showMaximized)
        logo_layout.addWidget(maximize_btn, 2)

        close_btn = QPushButton()
        close_btn.setIcon(QIcon(get_image_path('exit')))
        close_btn.setObjectName('app_widget')
        close_btn.setFixedSize(24, 24)
        close_btn.clicked.connect(widget.close)
        logo_layout.addWidget(close_btn, 3)

        return logo_widget

    def handle_login(self):
        """
        Handle for login button

        """

        username = self.username_line.text()
        password = self.password_line.text()

        self.app_backend.backend = Backend(get_app_config(
            'Alignak', 'backend'))

        resp = self.app_backend.login(str(username), str(password))

        if resp:
            send_banner(
                'OK',
                'Welcome %s, you are connected to Alignak Backend' % username)
            self.app_backend.user['username'] = str(username)
            self.app_backend.user['token'] = str(
                self.app_backend.backend.token)
            self.accept()
        else:
            send_banner('WARN',
                        'Backend connection refused...',
                        duration=10000)
            logger.warning('Connection informations are not accepted !')

    def handle_server(self):
        """
        Handle for server button

        """

        server_dialog = QDialog()
        server_dialog.setWindowTitle('Server Configuration')
        server_dialog.setWindowFlags(Qt.FramelessWindowHint)
        server_dialog.setStyleSheet(get_css())
        server_dialog.setFixedSize(300, 300)

        main_layout = QVBoxLayout(server_dialog)
        main_layout.setContentsMargins(0, 0, 0, 0)
        main_layout.addWidget(self.get_logo_widget(server_dialog))

        server_widget = QWidget(self)
        server_widget.setObjectName('login')
        server_layout = QGridLayout(server_widget)

        # Description
        desc_label = QLabel(
            '<h3>Alignak Backend</h3><p>Here you can define alignak settings.</p>'
            '<b>Be sure to enter a valid address</b>')
        desc_label.setWordWrap(True)
        server_layout.addWidget(desc_label, 0, 0, 1, 3)

        # Server URL
        server_layout.addWidget(QLabel('Server'), 1, 0, 1, 1)

        server_url = QLineEdit()
        server_url.setPlaceholderText('alignak backend url')
        server_url.setText(get_app_config('Alignak', 'url'))
        server_layout.addWidget(server_url, 1, 1, 1, 2)

        # Server Port
        server_layout.addWidget(QLabel('Port'), 2, 0, 1, 1)

        server_port = QLineEdit()
        server_port.setPlaceholderText('alignak backend port')
        cur_port = get_app_config('Alignak', 'backend').split(':')[2]
        server_port.setText(cur_port)
        server_layout.addWidget(server_port, 2, 1, 1, 2)

        # Server Processes
        server_layout.addWidget(QLabel('Processes'), 3, 0, 1, 1)

        server_proc = QLineEdit()
        if 'win32' in sys.platform:
            server_proc.setEnabled(False)
        server_proc.setPlaceholderText('alignak backend processes')
        cur_proc = get_app_config('Alignak', 'processes')
        server_proc.setText(cur_proc)
        server_layout.addWidget(server_proc, 3, 1, 1, 2)

        # Valid Button
        valid_btn = QPushButton('Valid')
        valid_btn.clicked.connect(server_dialog.accept)
        server_layout.addWidget(valid_btn, 4, 0, 1, 3)

        main_layout.addWidget(server_widget)

        self.center(server_widget)

        if server_dialog.exec_() == QDialog.Accepted:
            backend_url = '%(url)s:' + str(server_port.text()).rstrip()
            set_app_config('Alignak', 'backend', backend_url)
            set_app_config('Alignak', 'url', str(server_url.text()).rstrip())
            set_app_config('Alignak', 'processes',
                           str(server_proc.text()).rstrip())

    def mousePressEvent(self, event):
        """ QWidget.mousePressEvent(QMouseEvent) """

        self.offset = event.pos()

    def mouseMoveEvent(self, event):
        """ QWidget.mousePressEvent(QMouseEvent) """

        try:
            x = event.globalX()
            y = event.globalY()
            x_w = self.offset.x()
            y_w = self.offset.y()
            self.move(x - x_w, y - y_w)
        except AttributeError as e:
            logger.warning('Move Event %s: %s', self.objectName(), str(e))
Esempio n. 7
0
class TestAppNotifier(unittest2.TestCase):
    """
        This file test the AppNotifier class.
    """

    init_config()

    icon = QIcon(get_image_path('icon'))

    backend = AppBackend()
    backend.login()

    @classmethod
    def setUpClass(cls):
        """Create QApplication"""
        try:
            cls.app = QApplication(sys.argv)
        except:
            pass

    def test_other_objects_do_not_modify_notifier(self):
        """Other objects do not Modify Notifier"""

        # TrayIcon and Dashboard for notifier
        tray_icon = TrayIcon(self.icon)
        dashboard = Dashboard()
        dashboard.initialize()

        under_test = AppNotifier()
        under_test.initialize(self.backend, tray_icon, dashboard)

        self.assertIsNotNone(under_test.tray_icon)
        self.assertIsNotNone(under_test.app_backend)
        self.assertIsNotNone(under_test.dashboard)
        self.assertFalse(under_test.changes)

        tray_icon.build_menu(self.backend, dashboard)

        self.assertIsNotNone(under_test.tray_icon)
        self.assertIsNotNone(under_test.app_backend)
        self.assertIsNotNone(under_test.dashboard)
        self.assertFalse(under_test.changes)

    def test_check_data(self):
        """Check Data modify TrayIcon Actions"""

        # TrayIcon and Dashboard for notifier
        tray_icon = TrayIcon(self.icon)
        dashboard = Dashboard()
        dashboard.initialize()

        under_test = AppNotifier()
        under_test.initialize(self.backend, tray_icon, dashboard)
        tray_icon.build_menu(self.backend, dashboard)

        # Start notifier
        under_test.set_interval()

        # Check Actions are pending
        self.assertEqual(
            'Hosts UP, Wait...',
            under_test.tray_icon.qaction_factory.get('hosts_up').text())
        self.assertEqual(
            'Services OK, Wait...',
            under_test.tray_icon.qaction_factory.get('services_ok').text())

        # Check data...
        under_test.check_data()

        # ...so menu actions should be update
        self.assertNotEqual(
            'Hosts UP, Wait...',
            under_test.tray_icon.qaction_factory.get('hosts_up').text())
        self.assertNotEqual(
            'Services OK, Wait...',
            under_test.tray_icon.qaction_factory.get('services_ok').text())

    def test_states_change(self):
        """States and Notify Changes"""
        self.backend = AppBackend()
        self.backend.login()

        # TrayIcon and Dashboard for notifier
        dashboard = Dashboard()
        dashboard.initialize()

        tray_icon = TrayIcon(self.icon)
        tray_icon.build_menu(self.backend, dashboard)

        # Initialize Notifier
        under_test = AppNotifier()
        under_test.initialize(self.backend, tray_icon, dashboard)

        # Changes are True, first_start is True
        self.assertFalse(under_test.changes)
        self.assertTrue(under_test.first_start)
        self.assertIsNone(under_test.old_synthesis)

    def test_diff_last_state(self):
        """Diff Last Backend SState"""

        self.backend = AppBackend()
        self.backend.login()

        # TrayIcon and Dashboard for notifier
        tray_icon_test = TrayIcon(self.icon)
        dashboard_test = Dashboard()

        notifier_test = AppNotifier()
        notifier_test.app_backend = self.backend
        notifier_test.tray_icon = tray_icon_test
        notifier_test.dashboard = dashboard_test

        # Notifier check data to get first synthesys counts
        notifier_test.check_data()

        # Get synthesis test to test diff between check
        synthesis = self.backend.synthesis_count()
        under_test = notifier_test.diff_last_states(synthesis)

        fields_test = {
            'hosts': {
                'up': 0,
                'downtime': 0,
                'down': 0,
                'acknowledge': 0,
                'unreachable': 0
            },
            'services': {
                'ok': 0,
                'acknowledge': 0,
                'unknown': 0,
                'critical': 0,
                'downtime': 0,
                'unreachable': 0,
                'warning': 0
            }
        }

        self.assertTrue(under_test)
        self.assertTrue('hosts' in under_test)
        self.assertTrue('services' in under_test)

        for state in fields_test['hosts']:
            self.assertTrue(state in under_test['hosts'])
            self.assertTrue(isinstance(under_test['hosts'][state], int))
        for state in fields_test['services']:
            self.assertTrue(state in under_test['services'])
            self.assertTrue(isinstance(under_test['services'][state], int))
Esempio n. 8
0
class TestTrayIcon(unittest2.TestCase):
    """
        This file test the TrayIcon class.
    """

    init_config()

    icon = QIcon(get_image_path('icon'))

    backend = AppBackend()
    backend.login()

    @classmethod
    def setUpClass(cls):
        """Create QApplication"""
        try:
            cls.app = QApplication(sys.argv)
        except:
            pass

    def test_tray_icon(self):
        """Init TrayIcon and QMenu"""
        under_test = TrayIcon(TestTrayIcon.icon)

        self.assertIsInstance(under_test.menu, QMenu)

    def test_host_actions(self):
        """Hosts QActions are created"""
        under_test = TrayIcon(TestTrayIcon.icon)

        self.assertFalse(under_test.qaction_factory.actions)

        under_test.create_hosts_actions()

        self.assertIsInstance(under_test.qaction_factory.get('hosts_up'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('hosts_down'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('hosts_unreachable'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('hosts_acknowledge'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('hosts_downtime'), QAction)

    def test_services_actions(self):
        """Services QActions are created"""
        under_test = TrayIcon(TestTrayIcon.icon)

        self.assertFalse(under_test.qaction_factory.actions)

        under_test.create_services_actions()

        self.assertIsInstance(under_test.qaction_factory.get('services_ok'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('services_warning'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('services_critical'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('services_unknown'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('services_acknowledge'), QAction)
        self.assertIsInstance(under_test.qaction_factory.get('services_downtime'), QAction)

    def test_about_action(self):
        """About QAction is created"""
        under_test = TrayIcon(TestTrayIcon.icon)

        self.assertFalse(under_test.qaction_factory.actions)

        under_test.create_about_action()

        self.assertIsNotNone(under_test.qaction_factory)
        self.assertIsInstance(under_test.qaction_factory.get('about'), QAction)

    def test_quit_action(self):
        """Quit QAction is created"""
        under_test = TrayIcon(TestTrayIcon.icon)

        self.assertFalse(under_test.qaction_factory.actions)

        under_test.create_quit_action()

        self.assertIsNotNone(under_test.qaction_factory.get('exit'))
        self.assertIsInstance(under_test.qaction_factory.get('exit'), QAction)

    def test_build_menu(self):
        """Build Menu add QActions"""
        under_test = TrayIcon(TestTrayIcon.icon)
        dashboard_test = Dashboard()

        # Assert no actions in Menu
        self.assertFalse(under_test.menu.actions())
        self.assertIsNone(under_test.app_about)
        self.assertIsNone(under_test.synthesis)
        self.assertIsNone(under_test.alignak_status)
        self.assertIsNotNone(under_test.qaction_factory)

        under_test.build_menu(self.backend, dashboard_test)

        # Assert actions are added in Menu
        self.assertTrue(under_test.menu.actions())
        self.assertIsNotNone(under_test.app_about)
        self.assertIsNotNone(under_test.synthesis)
        self.assertIsNotNone(under_test.alignak_status)
        self.assertIsNotNone(under_test.qaction_factory)

    def test_update_menus_actions(self):
        """Update Menu QActions"""
        under_test = TrayIcon(TestTrayIcon.icon)

        dashboard_test = Dashboard()
        under_test.build_menu(self.backend, dashboard_test)

        self.assertEqual('Hosts UP, Wait...',
                         under_test.qaction_factory.get('hosts_up').text())
        self.assertEqual('Hosts DOWN, Wait...',
                         under_test.qaction_factory.get('hosts_down').text())
        self.assertEqual('Hosts UNREACHABLE, Wait...',
                         under_test.qaction_factory.get('hosts_unreachable').text())

        self.assertEqual('Services OK, Wait...',
                         under_test.qaction_factory.get('services_ok').text())
        self.assertEqual('Services WARNING, Wait...',
                         under_test.qaction_factory.get('services_warning').text())
        self.assertEqual('Services CRITICAL, Wait...',
                         under_test.qaction_factory.get('services_critical').text())
        self.assertEqual('Services UNKNOWN, Wait...',
                         under_test.qaction_factory.get('services_unknown').text())

        synthesis = {
            'hosts': {
                'up': 1,
                'down': 2,
                'unreachable': 3,
                'acknowledge': 4,
                'downtime': 5,
            },
            'services': {
                'ok': 4,
                'warning': 5,
                'critical': 6,
                'unknown': 7,
                'unreachable': 8,
                'acknowledge': 9,
                'downtime': 10,

            }
        }

        under_test.update_menu_actions(synthesis)

        self.assertEqual('Hosts UP (1)',
                         under_test.qaction_factory.get('hosts_up').text())
        self.assertEqual('Hosts DOWN (2)',
                         under_test.qaction_factory.get('hosts_down').text())
        self.assertEqual('Hosts UNREACHABLE (3)',
                         under_test.qaction_factory.get('hosts_unreachable').text())
        self.assertEqual('Hosts ACKNOWLEDGE (4)',
                         under_test.qaction_factory.get('hosts_acknowledge').text())
        self.assertEqual('Hosts DOWNTIME (5)',
                         under_test.qaction_factory.get('hosts_downtime').text())

        self.assertEqual('Services OK (4)',
                         under_test.qaction_factory.get('services_ok').text())
        self.assertEqual('Services WARNING (5)',
                         under_test.qaction_factory.get('services_warning').text())
        self.assertEqual('Services CRITICAL (6)',
                         under_test.qaction_factory.get('services_critical').text())
        self.assertEqual('Services UNKNOWN (7)',
                         under_test.qaction_factory.get('services_unknown').text())
        self.assertEqual('Services UNREACHABLE (8)',
                         under_test.qaction_factory.get('services_unreachable').text())
        self.assertEqual('Services ACKNOWLEDGE (9)',
                         under_test.qaction_factory.get('services_acknowledge').text())
        self.assertEqual('Services DOWNTIME (10)',
                         under_test.qaction_factory.get('services_downtime').text())
class TestBanner(unittest2.TestCase):
    """
        This file test methods of ActionManager class.
    """

    test_backend = AppBackend()

    @classmethod
    def setUpClass(cls):
        """Create QApplication"""

        try:
            cls.app = QApplication(sys.argv)
        except:
            pass

    def test_action_manager_creation(self):
        """Create ActionManager"""

        under_test = ActionManager(self.test_backend)

        self.assertEqual('actionacknowledge', ACK)
        self.assertEqual('actiondowntime', DOWNTIME)
        self.assertEqual('processed', PROCESS)

        self.assertIsNotNone(under_test.app_backend)

        self.assertFalse(under_test.acks_to_check['hosts'])
        self.assertFalse(under_test.acks_to_check['services'])
        self.assertFalse(under_test.downtimes_to_check['hosts'])
        self.assertFalse(under_test.downtimes_to_check['services'])
        self.assertFalse(under_test.processed_to_check)

    def test_check_items(self):
        """ActionManager Check Items"""

        under_test = ActionManager(self.test_backend)

        actions_items = under_test.check_items()

        # If no items are added, return all list are empty
        self.assertFalse(actions_items[ACK]['hosts'])
        self.assertFalse(actions_items[ACK]['services'])
        self.assertFalse(actions_items[DOWNTIME]['hosts'])
        self.assertFalse(actions_items[DOWNTIME]['services'])
        self.assertFalse(actions_items[PROCESS])

        # Assure actions VAR are not modified
        self.assertEqual('actionacknowledge', ACK)
        self.assertEqual('actiondowntime', DOWNTIME)
        self.assertEqual('processed', PROCESS)

    def test_add_none_item(self):
        """ActionManager Add None Item"""

        under_test = ActionManager(self.test_backend)

        # test with None item
        under_test.add_item(dict())

        actions_items = under_test.check_items()

        # If no items are added, all list are empty
        self.assertFalse(under_test.acks_to_check['hosts'])
        self.assertFalse(under_test.acks_to_check['services'])
        self.assertFalse(under_test.downtimes_to_check['hosts'])
        self.assertFalse(under_test.downtimes_to_check['services'])
        self.assertFalse(under_test.processed_to_check)

        self.assertFalse(actions_items[ACK]['hosts'])
        self.assertFalse(actions_items[ACK]['services'])
        self.assertFalse(actions_items[DOWNTIME]['hosts'])
        self.assertFalse(actions_items[DOWNTIME]['services'])
        self.assertFalse(actions_items[PROCESS])

    def test_add_ack_item(self):
        """ActionManager Add ACK Item"""

        under_test = ActionManager(self.test_backend)

        self.assertFalse(under_test.acks_to_check['hosts'])
        self.assertFalse(under_test.acks_to_check['services'])

        item_with_host = {
            'action': ACK,
            'host_id': '000',
            'service_id': None
        }

        under_test.add_item(item_with_host)

        # If "service_id" is None, ACK list add to 'hosts'
        self.assertTrue(under_test.acks_to_check['hosts'])
        self.assertFalse(under_test.acks_to_check['services'])

        item_with_service = {
            'action': ACK,
            'host_id': '000',
            'service_id': '111'
        }

        under_test.add_item(item_with_service)

        # If "service_id", ACK list add to 'services'
        self.assertTrue(under_test.acks_to_check['hosts'])
        self.assertTrue(under_test.acks_to_check['services'])

    def test_add_downtime_item(self):
        """ActionManager Add DOWNTIME Item"""

        under_test = ActionManager(self.test_backend)

        self.assertFalse(under_test.downtimes_to_check['hosts'])
        self.assertFalse(under_test.downtimes_to_check['services'])

        item_with_host = {
            'action': DOWNTIME,
            'host_id': '000',
            'service_id': None
        }

        under_test.add_item(item_with_host)

        # If "service_id" is None, ACK list add to 'hosts'
        self.assertTrue(under_test.downtimes_to_check['hosts'])
        self.assertFalse(under_test.downtimes_to_check['services'])

        item_with_service = {
            'action': DOWNTIME,
            'host_id': '000',
            'service_id': '111'
        }

        under_test.add_item(item_with_service)

        # If "service_id", ACK list add to 'services'
        self.assertTrue(under_test.downtimes_to_check['hosts'])
        self.assertTrue(under_test.downtimes_to_check['services'])

    def test_add_processed_item(self):
        """ActionManager Add PROCESS Item"""

        under_test = ActionManager(self.test_backend)

        item_with_host = {
            'action': PROCESS,
            'name': 'my name',
            'post': 'item/000'
        }

        self.assertFalse(under_test.processed_to_check)

        under_test.add_item(item_with_host)

        self.assertTrue(under_test.processed_to_check)