'''
    Copyright (C) 2019 - 2020 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''

import unittest
import json

from . import BaseTestCase


class TestWaters(BaseTestCase):
    @classmethod
    def setUpClass(self):
        self.base_url = '/mapservice/v1/waters'

    def test_get(self):
        """Test case getting waters (src)
		"""
        headers = {}
        response = self.client.open(
            self.base_url, method='GET', headers=headers
        )
        assert response.status_code == 200
        data = json.loads(response.data)
        assert len(data) == 1
        data = data[0]
        self.check_key(
            "manager", self.db_content.test_water.manager.username, data
        )
        self.check_key("name", self.db_content.test_water.name, data)
        self.check_key("waterId", self.db_content.test_water.id, data)
        self.check_key("osmId", self.db_content.test_water.osm_id, data)
        self.check_key(
            "osmType", self.db_content.test_water.osm_type.name, data
        )

    def test_get_by_name(self):
        """Test case getting waters by name (src)
		"""
        headers = {}

        response = self.client.open(
            '{}?name=Bla'.format(self.base_url), method='GET', headers=headers
        )
        assert response.status_code == 200
        data = json.loads(response.data)
        assert len(data) == 0

        response = self.client.open(
            '{}?name={}'.format(
                self.base_url, self.db_content.test_water.name[:3]
            ),
            method='GET',
            headers=headers
        )
        assert response.status_code == 200
        data = json.loads(response.data)
        assert len(data) == 1
        data = data[0]
        self.check_key(
            "manager", self.db_content.test_water.manager.username, data
        )
        self.check_key("name", self.db_content.test_water.name, data)
        self.check_key("waterId", self.db_content.test_water.id, data)
        self.check_key("osmId", self.db_content.test_water.osm_id, data)
        self.check_key(
            "osmType", self.db_content.test_water.osm_type.name, data
        )

    def test_get_by_manager(self):
        """Test case getting waters by manager (src)
		"""
        headers = {}

        response = self.client.open(
            '{}?manager=not_existent'.format(self.base_url),
            method='GET',
            headers=headers
        )
        assert response.status_code == 404, '{} != {}'.format(
            response.status_code, 404
        )

        response = self.client.open(
            '{}?manager={}'.format(
                self.base_url, self.db_content.test_user.username
            ),
            method='GET',
            headers=headers
        )
        assert response.status_code == 200
        data = json.loads(response.data)
        assert len(data) == 1
        data = data[0]
        self.check_key(
            "manager", self.db_content.test_water.manager.username, data
        )
        self.check_key("name", self.db_content.test_water.name, data)
        self.check_key("waterId", self.db_content.test_water.id, data)
        self.check_key("osmId", self.db_content.test_water.osm_id, data)
        self.check_key(
            "osmType", self.db_content.test_water.osm_type.name, data
        )

    def test_get_by_name_manager(self):
        """Test case getting waters by manager and water name (src)
		"""
        headers = {}

        response = self.client.open(
            '{}?manager={}&name=Bla'.format(
                self.base_url, self.db_content.test_user.username
            ),
            method='GET',
            headers=headers
        )
        assert response.status_code == 200

        response = self.client.open(
            '{}?manager=john&name={}'.format(
                self.base_url, self.db_content.test_water.name[:3]
            ),
            method='GET',
            headers=headers
        )
        assert response.status_code == 404

        response = self.client.open(
            '{}?name={}&manager={}'.format(
                self.base_url, self.db_content.test_water.name,
                self.db_content.test_user.username
            ),
            method='GET',
            headers=headers
        )
        assert response.status_code == 200
        data = json.loads(response.data)
        assert len(data) == 1
        data = data[0]
        self.check_key(
            "manager", self.db_content.test_water.manager.username, data
        )
        self.check_key("name", self.db_content.test_water.name, data)
        self.check_key("waterId", self.db_content.test_water.id, data)
        self.check_key("osmId", self.db_content.test_water.osm_id, data)
        self.check_key(
            "osmType", self.db_content.test_water.osm_type.name, data
        )


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