# Copyright 2021 The FedLearner Authors. All Rights Reserved.
#
# 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.

# coding: utf-8
import unittest

from http import HTTPStatus
from fedlearner_webconsole.exceptions import (InvalidArgumentException,
                                              NotFoundException)


class ExceptionsTest(unittest.TestCase):

    def test_invalid_argument_exception(self):
        """Checks if the information of the exception is correct."""
        exception = InvalidArgumentException(['123', 'df'])
        self.assertEqual(exception.status_code, HTTPStatus.BAD_REQUEST)
        self.assertEqual(
            exception.to_dict(), {
                'code': 400,
                'message': 'Invalid argument or payload.',
                'details': [
                    '123',
                    'df',
                ]
            })

    def test_not_found_exception(self):
        exception1 = NotFoundException('User A not found.')
        self.assertEqual(exception1.status_code, HTTPStatus.NOT_FOUND)
        self.assertEqual(
            exception1.to_dict(), {
                'code': 404,
                'message': 'User A not found.',
            })
        exception2 = NotFoundException()
        self.assertEqual(exception2.status_code, HTTPStatus.NOT_FOUND)
        self.assertEqual(
            exception2.to_dict(), {
                'code': 404,
                'message': 'Resource not found.',
            })


if __name__ == '__main__':
    unittest.main()
