# coding: utf-8

"""
    OneRoster OpenAPI (JSON) Definition

    The OneRoster service binding is available in WSDL/XSD and REST/JSON. The model is based upon the IMS PSM modelling approach.  # noqa: E501

    OpenAPI spec version: 1.1
    Contact: lmattson@imsglobal.org
    Generated by: https://github.com/swagger-api/swagger-codegen.git
"""

from __future__ import absolute_import

import logging
import unittest

import requests
from faker import Faker
from mockito import mock, when, verify
from requests import Response, HTTPError

from oneroster_client.api.line_items_management_api import LineItemsManagementApi  # noqa: E501
from test.factories.oneroster_lineitem import OneRosterSingleLineItemFactory

logging.getLogger('faker.factory').setLevel(logging.ERROR)
logging.getLogger('factory.generate').setLevel(logging.ERROR)
fake = Faker()


class TestLineItemsManagementApi(unittest.TestCase):
    """LineItemsManagementApi unit test stubs"""

    def setUp(self):
        self.api = LineItemsManagementApi()  # noqa: E501
        self.logger = mock(logging.Logger)
        self.default_url = '/'
        self.default_headers = {'Accept': 'application/json',
                                'Content-Type': 'application/json',
                                'User-Agent': 'Swagger-Codegen/1.0.0/python',
                                'Authorization': 'Bearer '}

    def tearDown(self):
        pass

    def test_delete_line_item(self):
        """Test case for delete_line_item

        The REST delete request message for the deleteLineItem() API call.  # noqa: E501
        """
        pass

    def test_get_line_item(self):
        """Test case for get_line_item

        The REST read request message for the getLineItem() API call.  # noqa: E501
        """
        pass

    def test_get_line_items(self):
        """Test case for get_line_items

        The REST read request message for the getLineItems() API call.  # noqa: E501
        """
        pass

    def test_get_line_items_for_class(self):
        """Test case for get_line_items_for_class

        The REST read request message for the getLineItemsForClass() API call.  # noqa: E501
        """
        pass

    def test_put_line_item(self):
        """Test case for put_line_item

        The REST create request message for the putLineItem() API call.  # noqa: E501
        """
        # expected_response = self.api.api_client.sanitize_for_serialization(OneRosterSingleUserFactory())
        sourced_id = fake.uuid4()
        body = OneRosterSingleLineItemFactory()
        body_dict = self.api.api_client.sanitize_for_serialization(body)
        response = mock(Response)
        when(requests).request(...).thenReturn(response)
        when(response).raise_for_status(...)

        self.api.put_line_item(body, sourced_id)

        verify(requests).request('PUT', f'{self.default_url}/lineItems/{sourced_id}',
                                 params=[],
                                 json=body_dict,
                                 headers=self.default_headers,
                                 timeout=None)

    def test_put_line_item_raises_500(self):
        response = mock(Response)
        response.status_code = 500
        http_error = HTTPError()
        http_error.response = response
        sourced_id = fake.uuid4()
        body = OneRosterSingleLineItemFactory()
        body_dict = self.api.api_client.sanitize_for_serialization(body)
        when(requests).request(...).thenReturn(response)
        when(response).raise_for_status(...).thenRaise(http_error)

        with self.assertRaises(HTTPError) as context:
            self.api.put_line_item(body, sourced_id)

        exception = context.exception

        verify(requests).request('PUT', f'{self.default_url}/lineItems/{sourced_id}',
                                 params=[],
                                 json=body_dict,
                                 headers=self.default_headers,
                                 timeout=None)
        assert exception.response.status_code == 500
        assert exception.dependency_name == 'OneRoster'


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