<commit_before>'''
Copyright 2017-present, Airbnb Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import json

from collections import OrderedDict
from mock import patch, call

from nose.tools import assert_equal

from stream_alert.alert_processor.main import (
    _load_output_config,
    _sort_dict,
    handler
)

from unit.stream_alert_alert_processor import (
    REGION,
    FUNCTION_NAME
)

from unit.stream_alert_alert_processor.helpers import _get_mock_context


@patch('logging.Logger.error')
def test_handler_malformed_message(log_mock):
    """Main handler decode failure logging"""
    # The @patch() decorator allows us to 'introspect' what occurs down the chain
    # and verify the params a function was LAST called with. For instance, here
    # we are checking the last call to `logging.Logger.error` and verifying that the
    # function was called with two params, the first being 'Malformed SNS: %s' and
    # the second being the dictionary contained within `message`
    # This call should happen at stream_alert/alert_processor/main.py:62
    context = _get_mock_context()
    message = {'not_default': {'record': {'size': '9982'}}}
    event = {'Records': [{'Sns': {'Message': json.dumps(message)}}]}
    handler(event, context)

    log_mock.assert_called_with('Malformed SNS: %s', message)


@patch('logging.Logger.error')
def test_handler_bad_message(log_mock):
    """Main handler decode failure logging"""
    context = _get_mock_context()
    event = {'Records': [{'Sns': {'Message': 'this\nvalue\nshould\nfail\nto\ndecode'}}]}
    handler(event, context)

    assert_equal(str(log_mock.call_args_list[0]),
                 str(call('An error occurred while decoding message to JSON: %s',
                          ValueError('No JSON object could be decoded',))))


@patch('stream_alert.alert_processor.main.run')
def test_handler_run(run_mock):
    """Main handler `run` call params"""
    context = _get_mock_context()
    message = {'default': {'record': {'size': '9982'}}}
    event = {'Records': [{'Sns': {'Message': json.dumps(message)}}]}
    handler(event, context)

    # This test will load the actual config, so we should compare the
    # function call against the same config here.
    run_mock.assert_called_with(message, REGION, FUNCTION_NAME, _load_output_config())


def test_load_output_config():
    """Load outputs configuration file"""
    config = _load_output_config('test/unit/conf/outputs.json')

    assert_equal(set(config.keys()), {
        'aws-s3', 'aws-lambda', 'pagerduty', 'phantom', 'slack'})


def test_sort_dict():
    """Sorted Dict"""
    dict_to_sort = {'c': 100, 'd': 1000, 'a': 1, 'b': 10, 'e': 100, 'f': 10, 'g': 1}
    sorted_dict = _sort_dict(dict_to_sort)

    assert_equal(type(sorted_dict), OrderedDict)

    index = 0
    keys = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    for key in sorted_dict.keys():
        assert_equal(keys[index], key)
        index += 1
<commit_msg>[tests] adding test for return value type of handler function<commit_after>'''
Copyright 2017-present, Airbnb Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
'''
import json

from collections import OrderedDict
from mock import patch, call

from nose.tools import assert_equal, assert_is_instance

from stream_alert.alert_processor.main import (
    _load_output_config,
    _sort_dict,
    handler
)

from unit.stream_alert_alert_processor import (
    REGION,
    FUNCTION_NAME
)

from unit.stream_alert_alert_processor.helpers import _get_mock_context


@patch('logging.Logger.error')
def test_handler_malformed_message(log_mock):
    """Main handler decode failure logging"""
    # The @patch() decorator allows us to 'introspect' what occurs down the chain
    # and verify the params a function was LAST called with. For instance, here
    # we are checking the last call to `logging.Logger.error` and verifying that the
    # function was called with two params, the first being 'Malformed SNS: %s' and
    # the second being the dictionary contained within `message`
    # This call should happen at stream_alert/alert_processor/main.py:62
    context = _get_mock_context()
    message = {'not_default': {'record': {'size': '9982'}}}
    event = {'Records': [{'Sns': {'Message': json.dumps(message)}}]}
    handler(event, context)

    log_mock.assert_called_with('Malformed SNS: %s', message)


@patch('logging.Logger.error')
def test_handler_bad_message(log_mock):
    """Main handler decode failure logging"""
    context = _get_mock_context()
    event = {'Records': [{'Sns': {'Message': 'this\nvalue\nshould\nfail\nto\ndecode'}}]}
    handler(event, context)

    assert_equal(str(log_mock.call_args_list[0]),
                 str(call('An error occurred while decoding message to JSON: %s',
                          ValueError('No JSON object could be decoded',))))


@patch('stream_alert.alert_processor.main.run')
def test_handler_run(run_mock):
    """Main handler `run` call params"""
    context = _get_mock_context()
    message = {'default': {'record': {'size': '9982'}}}
    event = {'Records': [{'Sns': {'Message': json.dumps(message)}}]}
    handler(event, context)

    # This test will load the actual config, so we should compare the
    # function call against the same config here.
    run_mock.assert_called_with(message, REGION, FUNCTION_NAME, _load_output_config())


def test_handler_return():
    """Main handler return value"""
    context = _get_mock_context()
    event = {'Records': []}
    value = handler(event, context)

    assert_is_instance(value, list)


def test_load_output_config():
    """Load outputs configuration file"""
    config = _load_output_config('test/unit/conf/outputs.json')

    assert_equal(set(config.keys()), {
        'aws-s3', 'aws-lambda', 'pagerduty', 'phantom', 'slack'})


def test_sort_dict():
    """Sorted Dict"""
    dict_to_sort = {'c': 100, 'd': 1000, 'a': 1, 'b': 10, 'e': 100, 'f': 10, 'g': 1}
    sorted_dict = _sort_dict(dict_to_sort)

    assert_equal(type(sorted_dict), OrderedDict)

    index = 0
    keys = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
    for key in sorted_dict.keys():
        assert_equal(keys[index], key)
        index += 1

{'fraction_non_alphanumeric': 0.12128851540616246, 'fraction_numerical': 0.010644257703081233, 'mean_word_length': 4.126346015793252, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '250019', 'n_tokens_mistral': 2217, 'n_tokens_neox': 2117, 'n_words': 697}