configurationengine/source/plugins/common/ConeLegacyRulePlugin/legacyruleplugin/tests/unittest_rule_plugin_errors.py
changeset 3 e7e0ae78773e
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 #
       
    16 
       
    17 import unittest
       
    18 import os, shutil
       
    19 import sys
       
    20 import logging
       
    21 
       
    22 from cone.public import exceptions,plugin,api,container
       
    23 from cone.storage import filestorage
       
    24 from legacyruleplugin import ruleml
       
    25 from testautomation.base_testcase import BaseTestCase
       
    26 
       
    27 # Hardcoded value of testdata folder that must be under the current working dir
       
    28 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
       
    29 testdata  = os.path.join(ROOT_PATH,'errorruleproject')
       
    30 
       
    31 class TestErrorReporting(BaseTestCase):
       
    32     def setUp(self):
       
    33         pass
       
    34       
       
    35     def tearDown(self):
       
    36         pass
       
    37     
       
    38     def _prepare_workdir(self, workdir):
       
    39         workdir = os.path.join(ROOT_PATH, workdir)
       
    40         self.recreate_dir(workdir)
       
    41         return workdir
       
    42 
       
    43     def _prepare_log(self, log_file, level=logging.DEBUG, formatter="%(levelname)s - %(name)s - %(message)s", logger='cone'):
       
    44         FULL_PATH = os.path.join(ROOT_PATH, "temp", log_file)
       
    45         self.remove_if_exists(FULL_PATH)
       
    46         self.create_dir_for_file_path(FULL_PATH)
       
    47         
       
    48         handler = logging.FileHandler(FULL_PATH)
       
    49         handler.setLevel(level)
       
    50         frm = logging.Formatter(formatter)
       
    51         handler.setFormatter(frm)
       
    52         logger = logging.getLogger(logger)
       
    53         logger.addHandler(handler)
       
    54         
       
    55         return [FULL_PATH, handler, logger]
       
    56     
       
    57     def _execute_rules(self, project_location):
       
    58         project = api.Project(api.Storage.open(os.path.join(ROOT_PATH, project_location)))
       
    59         config = project.get_configuration('root.confml')
       
    60         context = plugin.GenerationContext(configuration=config)
       
    61         implcontainer = plugin.get_impl_set(config, r'\.ruleml$')
       
    62         implcontainer.get_relation_container().execute(context)
       
    63         lastconfig = config.get_last_configuration()
       
    64         project.close()
       
    65     
       
    66     def test_terminal_expression_repr(self):
       
    67         log_file, handler, logger = self._prepare_log('test1.log')
       
    68         self._execute_rules('errorruleproject/test1')
       
    69         logger.removeHandler(handler)
       
    70         
       
    71         self.assert_file_does_not_contain(log_file, "<cone.public.rules.TerminalExpression object at")
       
    72 
       
    73     def test_invalid_python_code_eval(self):
       
    74         log_file, handler, logger = self._prepare_log('test2.log')
       
    75         self._execute_rules('errorruleproject/test2')
       
    76         logger.removeHandler(handler)
       
    77         self.assert_file_contains(log_file, "INFO - cone.ruleml - Set EvalTest2.StringResult = None from ConfigureRelation(ref='implml/invalid_python_eval.ruleml', lineno=3)")
       
    78         self.assert_file_contains(log_file, "WARNING - cone.ruleml - Invalid syntax in eval: -> this is invalid python code")
       
    79         self.assert_file_contains(log_file, "ERROR - cone.ruleml_relation_container(implml/invalid_python_eval.ruleml:3) - '-> this is invalid python code'")
       
    80 
       
    81     def test_invalid_python_code_eval_globals(self):
       
    82         log_file, handler, logger = self._prepare_log('test3.log')
       
    83         self._execute_rules('errorruleproject/test3')
       
    84         logger.removeHandler(handler)
       
    85         self.assert_file_contains(log_file, "WARNING - cone.ruleml(implml/invalid_python_eval.ruleml) - Cannot import eval file: implml/scripts/test_eval.py. Exception: invalid syntax (<string>, line 17)")
       
    86         
       
    87     def test_invalid_file_reference_in_eval_globals_file_attribute(self):
       
    88         log_file, handler, logger = self._prepare_log('test4.log')
       
    89         self._execute_rules('errorruleproject/test4')
       
    90         logger.removeHandler(handler)
       
    91         self.assert_file_contains(log_file, "WARNING - cone.ruleml(implml/invalid_python_eval.ruleml) - Cannot import eval file: implml/scripts/not_valid_filename.py. Exception: implml/scripts/not_valid_filename.py, [Errno 2] No such file or directory:")
       
    92         self.assert_file_contains(log_file, '/implml/scripts/not_valid_filename.py')
       
    93     
       
    94     def test_runtime_error_when_running_an_eval_block_inside_rule(self):
       
    95         log_file, handler, logger = self._prepare_log('test5.log')
       
    96         self._execute_rules('errorruleproject/test5')
       
    97         logger.removeHandler(handler)
       
    98 
       
    99         self.assert_file_contains(log_file, "Execution failed for eval: 7/0 <type 'exceptions.ZeroDivisionError'>: integer division or modulo by zero")
       
   100 
       
   101     def test_references_non_existent_settings(self):
       
   102         log_file, handler, logger = self._prepare_log('test6.log')
       
   103         self._execute_rules('errorruleproject/test6')
       
   104         logger.removeHandler(handler)
       
   105         #self.assert_file_contains(log_file, "Execution failed for eval: 7/0 <type 'exceptions.ZeroDivisionError'>: integer division or modulo by zero")
       
   106 
       
   107     
       
   108 if __name__ == '__main__':
       
   109     unittest.main()