# Copyright 2015 Google 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.

import unittest

import mock


def _make_credentials():
    import google.auth.credentials

    return mock.Mock(spec=google.auth.credentials.Credentials)


class TestClient(unittest.TestCase):

    PROJECT = "PROJECT"
    ZONE_NAME = "zone-name"

    @staticmethod
    def _get_target_class():
        from google.cloud.dns.client import Client

        return Client

    def _make_one(self, *args, **kw):
        return self._get_target_class()(*args, **kw)

    def test_ctor(self):
        from google.cloud.dns._http import Connection

        creds = _make_credentials()
        http = object()
        client = self._make_one(project=self.PROJECT, credentials=creds, _http=http)
        self.assertIsInstance(client._connection, Connection)
        self.assertIs(client._connection.credentials, creds)
        self.assertIs(client._connection.http, http)

    def test_quotas_defaults(self):
        PATH = "projects/%s" % (self.PROJECT,)
        MANAGED_ZONES = 1234
        RRS_PER_RRSET = 23
        RRSETS_PER_ZONE = 345
        RRSET_ADDITIONS = 456
        RRSET_DELETIONS = 567
        TOTAL_SIZE = 67890
        DATA = {
            "quota": {
                "managedZones": str(MANAGED_ZONES),
                "resourceRecordsPerRrset": str(RRS_PER_RRSET),
                "rrsetsPerManagedZone": str(RRSETS_PER_ZONE),
                "rrsetAdditionsPerChange": str(RRSET_ADDITIONS),
                "rrsetDeletionsPerChange": str(RRSET_DELETIONS),
                "totalRrdataSizePerChange": str(TOTAL_SIZE),
            }
        }
        CONVERTED = {key: int(value) for key, value in DATA["quota"].items()}
        creds = _make_credentials()
        client = self._make_one(self.PROJECT, creds)
        conn = client._connection = _Connection(DATA)

        quotas = client.quotas()

        self.assertEqual(quotas, CONVERTED)

        self.assertEqual(len(conn._requested), 1)
        req = conn._requested[0]
        self.assertEqual(req["method"], "GET")
        self.assertEqual(req["path"], "/%s" % PATH)

    def test_quotas_w_kind_key(self):
        PATH = "projects/%s" % (self.PROJECT,)
        MANAGED_ZONES = 1234
        RRS_PER_RRSET = 23
        RRSETS_PER_ZONE = 345
        RRSET_ADDITIONS = 456
        RRSET_DELETIONS = 567
        TOTAL_SIZE = 67890
        DATA = {
            "quota": {
                "managedZones": str(MANAGED_ZONES),
                "resourceRecordsPerRrset": str(RRS_PER_RRSET),
                "rrsetsPerManagedZone": str(RRSETS_PER_ZONE),
                "rrsetAdditionsPerChange": str(RRSET_ADDITIONS),
                "rrsetDeletionsPerChange": str(RRSET_DELETIONS),
                "totalRrdataSizePerChange": str(TOTAL_SIZE),
            }
        }
        CONVERTED = {key: int(value) for key, value in DATA["quota"].items()}
        WITH_KIND = {"quota": DATA["quota"].copy()}
        WITH_KIND["quota"]["kind"] = "dns#quota"
        creds = _make_credentials()