# Path: fmiapi/fmicatalogservice.py
# def get_station_metadata(fmisid):
#     result = _parse_data(_retrieve_metadata_by_fmisid(fmisid))
#     if len(result) == 0:
#         raise NoDataSetsException(fmisid)
#     return result
# 
# Path: fmiapi/fmierrors.py
# class RequestException(Exception):
# 
#     def __init__(self, text, error_code, html=""):
#         self.error_code = 0
#         self.message = text
#         self.error_code = error_code
#         self.html = html
# 
#     def __str__(self):
#         if self.html != "":
#             return self.html
#         else:
#             return self.message
# 
# class NoDataSetsException(Exception):
# 
#     def __init__(self, fmisid):
#         self.message = "No data sets found for {}".format(fmisid)
#         self.error_code = "NODATASETS"
# 
#     def __str__(self):
#         return self.message


from fmiapi.fmicatalogservice import get_station_metadata
from tests.fmiapi.testdata.expected_data import EXPECTED_LAMMI_CATALOG_METADATA
from tests.testUtils import *
from tests.fmiapi.commonmocks import *
from unittest import mock
from lxml import etree
from fmiapi.fmierrors import RequestException, NoDataSetsException
import pytest


        @mock.patch('http.client.HTTPConnection', spec=True)
        def should_return_proper_data_after_first_failed_request(mock_httpconn):
            # Looks like occasionally FMI api returns Java exception response when doing completely valid
            # searches... We should handle it properly and just try again couple of times and hope it goes away.
            mock_connection = mock_httpconn.return_value

            # Mocked responses. Note that the first one is the exception response
            mock_connection.getresponse.side_effect = [
                MockResponse(200, etree.tostring(search_exception_response)),
                MockResponse(200, etree.tostring(lammi_catalog_metadata))
            ]

            result = get_station_metadata('1234')
            assert_equal(2, mock_connection.getresponse.call_count)

    @mock.patch('http.client.HTTPConnection', spec=True)
    def should_throw_exception_when_request_fails(mock_httpconn):
        mock_connection = mock_httpconn.return_value
        mock_connection.getresponse.return_value = MockResponse(400, '')

        with pytest.raises(RequestException) as e:
            get_station_metadata('1234')
        assert_equal('METADATA_RETRIEVAL', e.value.error_code)

    @mock.patch('fmiapi.fmicatalogservice._parse_data', spec=True)
    def should_throw_exception_when_request_returns_empty_list(mock_parse):
        mock_parse.return_value = []


# Path: fmiapi/fmicatalogservice.py
# def get_station_metadata(fmisid):
#     result = _parse_data(_retrieve_metadata_by_fmisid(fmisid))
#     if len(result) == 0:
#         raise NoDataSetsException(fmisid)
#     return result
# 
# Path: fmiapi/fmierrors.py
# class RequestException(Exception):
# 
#     def __init__(self, text, error_code, html=""):
#         self.error_code = 0
#         self.message = text
#         self.error_code = error_code
#         self.html = html
# 
#     def __str__(self):
#         if self.html != "":
#             return self.html
#         else:
#             return self.message
# 
# class NoDataSetsException(Exception):
# 
#     def __init__(self, fmisid):
#         self.message = "No data sets found for {}".format(fmisid)
#         self.error_code = "NODATASETS"
# 
#     def __str__(self):
#         return self.message


# Path: tests/fmiapi/test_fmicatalogservice.py
from fmiapi.fmicatalogservice import get_station_metadata
from tests.fmiapi.testdata.expected_data import EXPECTED_LAMMI_CATALOG_METADATA
from tests.testUtils import *
from tests.fmiapi.commonmocks import *
from unittest import mock
from lxml import etree
from fmiapi.fmierrors import RequestException, NoDataSetsException
import pytest



        @mock.patch('http.client.HTTPConnection', spec=True)
        def should_return_proper_data_after_first_failed_request(mock_httpconn):
            # Looks like occasionally FMI api returns Java exception response when doing completely valid
            # searches... We should handle it properly and just try again couple of times and hope it goes away.
            mock_connection = mock_httpconn.return_value

            # Mocked responses. Note that the first one is the exception response
            mock_connection.getresponse.side_effect = [
                MockResponse(200, etree.tostring(search_exception_response)),
                MockResponse(200, etree.tostring(lammi_catalog_metadata))
            ]

            result = get_station_metadata('1234')
            assert_equal(2, mock_connection.getresponse.call_count)

    @mock.patch('http.client.HTTPConnection', spec=True)
    def should_throw_exception_when_request_fails(mock_httpconn):
        mock_connection = mock_httpconn.return_value
        mock_connection.getresponse.return_value = MockResponse(400, '')

        with pytest.raises(RequestException) as e:
            get_station_metadata('1234')
        assert_equal('METADATA_RETRIEVAL', e.value.error_code)

    @mock.patch('fmiapi.fmicatalogservice._parse_data', spec=True)
    def should_throw_exception_when_request_returns_empty_list(mock_parse):
        mock_parse.return_value = []


        with pytest.raises(NoDataSetsException) as e: