class TestExceptionIdentification(unittest.TestCase):
    def setUp(self):
        yaml_file = 'tests/test_data/complex_identifier_test.yaml'

        with open('tests/test_data/wms_exception.xml', 'r') as f:
            content = f.read()
        url = 'http://www.mapserver.com/cgi?SERVICE=WMS&VERSION=1.3.0&REQUEST=GETCAPABILITIES'

        self.identifier = Identify([yaml_file], content, url)
        self.identifier.identify()

    def test_is_error(self):
        protocol_data = next(p for p in self.identifier.yaml if p['name'] == 'OGC:error')
        returned_error = self.identifier._is_protocol_error(protocol_data)
        self.assertTrue(returned_error)
class TestExceptionIdentification(unittest.TestCase):
    def setUp(self):
        yaml_file = 'tests/test_data/complex_identifier_test.yaml'

        with open('tests/test_data/wms_exception.xml', 'r') as f:
            content = f.read()
        url = 'http://www.mapserver.com/cgi?SERVICE=WMS&VERSION=1.3.0&REQUEST=GETCAPABILITIES'

        self.identifier = Identify([yaml_file], content, url)
        self.identifier.identify()

    def test_is_error(self):
        protocol_data = next(p for p in self.identifier.yaml
                             if p['name'] == 'OGC:error')
        returned_error = self.identifier._is_protocol_error(protocol_data)
        self.assertTrue(returned_error)
class TestComplexIdentifiers(unittest.TestCase):
    def setUp(self):
        yaml_file = 'tests/test_data/complex_identifier_test.yaml'

        with open(
                'tests/test_data/esri_wms_35bd4e2ce8cd13e8697b03976ffe1ee6.txt',
                'r') as f:
            content = f.read()
        url = 'http://www.mapserver.com/cgi?SERVICE=WMS&VERSION=1.3.0&REQUEST=GETCAPABILITIES'

        parser_content = content.replace('\\n', '')
        parser = Parser(parser_content)
        options = {'parser': parser}

        self.identifier = Identify([yaml_file], content, url, **options)
        self.identifier.identify()

    def test_load_yaml(self):
        self.assertTrue(self.identifier.yaml[2]['name'] == 'OGC:WMS')

        names = [p['name'] for p in self.identifier.yaml]
        self.assertTrue(len(names) == 6)

        ogc_protocol = self.identifier.yaml[0]
        self.assertTrue('service_description' in ogc_protocol)
        self.assertTrue(len(ogc_protocol['service_description']) == 1)

    def test_identify_protocol(self):
        expected_protocol = 'OGC:WMS'
        returned_protocol, returned_subtype = self.identifier._identify_protocol(
        )
        self.assertTrue(expected_protocol == returned_protocol)

    def test_identify_service_from_protocol(self):
        expected_service = 'GetCapabilities'
        protocol_data = next(p for p in self.identifier.yaml
                             if p['name'] == 'OGC:WMS')
        returned_service = self.identifier._identify_service_of_protocol(
            protocol_data)

        self.assertTrue(returned_service)
        self.assertTrue(expected_service == returned_service)

    def test_is_error(self):
        protocol_data = next(p for p in self.identifier.yaml
                             if p['name'] == 'OGC:WMS')
        returned_error = self.identifier._is_protocol_error(protocol_data)
        self.assertFalse(returned_error)

    def test_identify(self):
        # just run it again?

        self.identifier.identify()

        self.assertFalse(self.identifier.is_error)
        self.assertTrue(self.identifier.protocol == 'OGC:WMS')
        self.assertTrue(self.identifier.service == 'GetCapabilities')
        self.assertTrue(self.identifier.version)
        self.assertTrue(self.identifier.version == '1.3.0')

    def test_generate_urn(self):
        expected_urn = 'urn:OGC:WMS:GetCapabilities:1.3.0'
        self.identifier.identify()

        urn = self.identifier.generate_urn()

        self.assertTrue(urn == expected_urn)
class TestComplexIdentifiers(unittest.TestCase):
    def setUp(self):
        yaml_file = 'tests/test_data/complex_identifier_test.yaml'

        with open('tests/test_data/esri_wms_35bd4e2ce8cd13e8697b03976ffe1ee6.txt', 'r') as f:
            content = f.read()
        url = 'http://www.mapserver.com/cgi?SERVICE=WMS&VERSION=1.3.0&REQUEST=GETCAPABILITIES'

        parser_content = content.replace('\\n', '')
        parser = Parser(parser_content)
        options = {'parser': parser}

        self.identifier = Identify([yaml_file], content, url, **options)
        self.identifier.identify()

    def test_load_yaml(self):
        self.assertTrue(self.identifier.yaml[2]['name'] == 'OGC:WMS')

        names = [p['name'] for p in self.identifier.yaml]
        self.assertTrue(len(names) == 6)

        ogc_protocol = self.identifier.yaml[0]
        self.assertTrue('service_description' in ogc_protocol)
        self.assertTrue(len(ogc_protocol['service_description']) == 1)

    def test_identify_protocol(self):
        expected_protocol = 'OGC:WMS'
        returned_protocol, returned_subtype = self.identifier._identify_protocol()
        self.assertTrue(expected_protocol == returned_protocol)

    def test_identify_service_from_protocol(self):
        expected_service = 'GetCapabilities'
        protocol_data = next(p for p in self.identifier.yaml if p['name'] == 'OGC:WMS')
        returned_service = self.identifier._identify_service_of_protocol(protocol_data)

        self.assertTrue(returned_service)
        self.assertTrue(expected_service == returned_service)

    def test_is_error(self):
        protocol_data = next(p for p in self.identifier.yaml if p['name'] == 'OGC:WMS')
        returned_error = self.identifier._is_protocol_error(protocol_data)
        self.assertFalse(returned_error)

    def test_identify(self):
        # just run it again?

        self.identifier.identify()

        self.assertFalse(self.identifier.is_error)
        self.assertTrue(self.identifier.protocol == 'OGC:WMS')
        self.assertTrue(self.identifier.service == 'GetCapabilities')
        self.assertTrue(self.identifier.version)
        self.assertTrue(self.identifier.version == '1.3.0')

    def test_generate_urn(self):
        expected_urn = 'urn:OGC:WMS:GetCapabilities:1.3.0'
        self.identifier.identify()

        urn = self.identifier.generate_urn()

        self.assertTrue(urn == expected_urn)