#   Copyright 2021 Dynatrace LLC
#
#   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.

from unittest import TestCase
from logs import input_records_decoder
from util.context import Context


class Test(TestCase):

    def test_check_records_list_if_logs_end_decode(self):
        records = [
            {
                "recordId": "49615192443673540283798383997441439122248557324429950978000000",
                "approximateArrivalTimestamp": 1612438967376,
                "data": "<KEY>
            },
            {
                "recordId": "49615192443673540283798383997442648048068174358786342914000000",
                "approximateArrivalTimestamp": 1612439002747,
                "data": "<KEY>
            },
        ]

        is_logs, decoded_records = input_records_decoder.check_records_list_if_logs_end_decode(
            records, Context("function-name", "dt-url", "dt-token", False, False))

        self.assertTrue(is_logs)

        expected_first = r'{"messageType":"DATA_MESSAGE","owner":"444652832050","logGroup":"cloudwatch_customlog","logStream":"2021-02-04-logstream","subscriptionFilters":["b-SubscriptionFilter0-1I0DE5MAAFV5G"],"logEvents":[{"id":"35958590510527767165636549608812769529777864588249006080","timestamp":1612438965174,"message":"2021-02-04 12:42:47\tlog message"}]}'
        expected_second = r'{"messageType":"DATA_MESSAGE","owner":"444652832050","logGroup":"cloudwatch_customlog","logStream":"2021-02-04-logstream","subscriptionFilters":["b-SubscriptionFilter0-1I0DE5MAAFV5G"],"logEvents":[{"id":"35958591301289891160333915627296360039224104084779368448","timestamp":1612439000633,"message":"2021-02-04 12:43:22\tlog message"}]}'

        self.assertEqual(decoded_records[0], expected_first)
        self.assertEqual(decoded_records[1], expected_second)

    def test_check_records_list_if_logs_end_decode_not_logs(self):
        context = Context("function-name", "dt-url", "dt-token", False, False)

        records = [
            {
                "ZGRkCg==": "49615192443673540283798383997441439122248557324429950978000000",
                "approximateArrivalTimestamp": 1612438967376,
                "data": "ZGRkCg=="
            },
        ]

        is_logs, decoded_records = input_records_decoder.check_records_list_if_logs_end_decode(records, context)

        self.assertFalse(is_logs)

#   Copyright 2021 Dynatrace LLC
#
#   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.

from unittest import TestCase
from logs import input_records_decoder
from util.context import Context


class Test(TestCase):

    def test_check_records_list_if_logs_end_decode(self):
        records = [
            {
                "recordId": "49615192443673540283798383997441439122248557324429950978000000",
                "approximateArrivalTimestamp": 1612438967376,
                "data": "<KEY>
            },
            {
                "recordId": "49615192443673540283798383997442648048068174358786342914000000",
                "approximateArrivalTimestamp": 1612439002747,
                "data": "<KEY>
            },
        ]

        is_logs, decoded_records = input_records_decoder.check_records_list_if_logs_end_decode(
            records, Context("function-name", "dt-url", "dt-token", False, False))

        self.assertTrue(is_logs)

        expected_first = r'{"messageType":"DATA_MESSAGE","owner":"444652832050","logGroup":"cloudwatch_customlog","logStream":"2021-02-04-logstream","subscriptionFilters":["b-SubscriptionFilter0-1I0DE5MAAFV5G"],"logEvents":[{"id":"35958590510527767165636549608812769529777864588249006080","timestamp":1612438965174,"message":"2021-02-04 12:42:47\tlog message"}]}'
        expected_second = r'{"messageType":"DATA_MESSAGE","owner":"444652832050","logGroup":"cloudwatch_customlog","logStream":"2021-02-04-logstream","subscriptionFilters":["b-SubscriptionFilter0-1I0DE5MAAFV5G"],"logEvents":[{"id":"35958591301289891160333915627296360039224104084779368448","timestamp":1612439000633,"message":"2021-02-04 12:43:22\tlog message"}]}'

        self.assertEqual(decoded_records[0], expected_first)
        self.assertEqual(decoded_records[1], expected_second)

    def test_check_records_list_if_logs_end_decode_not_logs(self):
        context = Context("function-name", "dt-url", "dt-token", False, False)

        records = [
            {
                "ZGRkCg==": "49615192443673540283798383997441439122248557324429950978000000",
                "approximateArrivalTimestamp": 1612438967376,
                "data": "ZGRkCg=="
            },
        ]

        is_logs, decoded_records = input_records_decoder.check_records_list_if_logs_end_decode(records, context)

        self.assertFalse(is_logs)

#   Copyright 2021 Dynatrace LLC # #   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.