Introduce more test cases to improve code coverage
[smo/ves.git] / tests / dmaap_adaptor / test_appConfig.py
1 # Copyright 2021 Xoriant Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15
16 import argparse
17 import os
18 import sys
19 from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
20 import configparser
21 from unittest import mock
22 from unittest.mock import patch
23 from pytest_mock import MockerFixture
24 import logging
25 from app_config import AppConfig
26 import pytest
27
28 def get_path():
29     project_path = os.getcwd()
30     return project_path
31
32 def get_config_path():
33     project_path=get_path()
34     config_path = os.path.join(
35     project_path,"dmaapadapter/adapter/config/adapter.conf")
36     return config_path
37
38 @pytest.fixture
39 def kafkaBroker():
40     kafkaBroker='broker'
41     return kafkaBroker
42
43 @pytest.fixture
44 def logger():
45     logger = logging.getLogger('DMaaP')
46     logger.setLevel(logging.DEBUG)
47     logger.setLevel(logging.ERROR)
48     logger.setLevel(logging.INFO)
49     return logger
50
51 @pytest.fixture
52 def enable_assert():
53     assert_value='enable'
54     return assert_value
55
56
57 #test init function in appconfig
58 @mock.patch('app_config.AppConfig.setLogger')
59 @mock.patch('argparse.ArgumentParser.parse_args',
60 return_value=argparse.Namespace(config=get_config_path(),section='default'))
61 def test___init__(parser,mock_setLogger):
62     AppConfig.__init__(AppConfig)
63     mock_setLogger.assert_called_with('dmaap.log','error')
64
65
66 #test kafka broker
67 def test_getKafkaBroker(kafkaBroker):
68     AppConfig.kafka_broker=kafkaBroker
69     res=AppConfig.getKafkaBroker(AppConfig)
70     assert res == kafkaBroker
71
72 #test getLogger
73 def test_getLogger(logger):
74     AppConfig.logger=logger
75     res=AppConfig.getLogger(AppConfig)
76     assert res.getEffectiveLevel()==20
77
78
79 #test logger level Info
80 def test_setLogger(logger):
81     log_file= 'dmaap.log'
82     log_level='INFO'
83     with mock.patch.object(logger,'info') as mock_info:
84         AppConfig.setLogger(AppConfig,log_file,log_level)
85         mock_info.assert_called_with('Log level INFO and log file dmaap.log : ')
86
87
88 #test setLogger Debug
89 def test_setLogger_debug(logger):
90     log_file= 'dmaap.log'
91     log_level= 'DEBUG'
92     with mock.patch.object(logger,'info') as mock_debug:
93         AppConfig.setLogger(AppConfig,log_file,log_level)
94         mock_debug.assert_called_with('Log level DEBUG and log file dmaap.log : ')
95
96
97 #test setLogger error
98 def test_setLogger_error(logger):
99     log_file= 'dmaap.log'
100     log_level='ERROR'
101     with mock.patch.object(logger,'info') as mock_error:
102         AppConfig.setLogger(AppConfig,log_file,log_level)
103         mock_error.assert_called_with('Log level ERROR and log file dmaap.log : ')
104
105         
106
107 #test AssertConfigValue
108 def test_getAssertConfigValue(enable_assert):
109     AppConfig.enable_assert=enable_assert
110     res=AppConfig.getAssertConfigValue(AppConfig)
111     assert res==enable_assert