class TestOpenSearchReader(unittest.TestCase):
    def setUp(self):
        with open('tests/test_data/basic_osdd_c1576284036448b5ef3d16b2cd37acbc.txt', 'r') as f:
            text = f.read()
        text = text.replace('\\n', ' ')
        self.reader = OpenSearchReader(text, '')
        self.reader._load_xml()

    def test_return_descriptors(self):
        descriptors = self.reader.return_service_descriptors()

        self.assertTrue('CEOS' in descriptors['title'][0])
        self.assertTrue('version' not in descriptors)
        self.assertTrue(descriptors['abstract'][0] is None)
class TestOpenSearchReader(unittest.TestCase):
    def setUp(self):
        with open("tests/test_data/basic_osdd_c1576284036448b5ef3d16b2cd37acbc.txt", "r") as f:
            text = f.read()
        text = text.replace("\\n", " ")
        self.reader = OpenSearchReader(text, "")
        self.reader._load_xml()

    def test_return_descriptors(self):
        descriptors = self.reader.return_service_descriptors()

        self.assertTrue("CEOS" in descriptors["title"][0])
        self.assertTrue("version" not in descriptors)
        self.assertTrue(descriptors["abstract"][0] is None)
class TestOpenSearchReaderWithEndpoints(unittest.TestCase):
    def setUp(self):
        with open('tests/test_data/opensearch-nasa.xml', 'r') as f:
            text = f.read()
        self.reader = OpenSearchReader(text, '')
        self.reader._load_xml()

    def test_parse_service(self):
        '''
        just the remainders right now
        '''

        should_not_have = '{http://a9.com/-/spec/opensearch/1.1/}' + \
                          'OpenSearchDescription/' + \
                          '{http://a9.com/-/spec/opensearch/1.1/}Url'
        service = self.reader.parse_service()

        self.assertTrue('remainder' in service)

        remainder = service['remainder']

        self.assertTrue(should_not_have in remainder)

    def test_parse_endpoints(self):
        endpoints = self.reader.parse_endpoints()

        print endpoints

        expected_endpoint = 'http://modwebsrv.modaps.eosdis.nasa.gov/axis2/' + \
                            'services/MODAPSservices/getOpenSearch?' + \
                            'products={MODAPSParameters:products}&' + \
                            'collection={MODAPSParameters:collection?}' + \
                            '&start={time:start}&stop={time:stop}&bbox={geo:box}' + \
                            '&coordsOrTiles={MODAPSParameters:coordsOrTiles?}' + \
                            '&dayNightBoth={MODAPSParameters:dayNightBoth?}'

        self.assertTrue(len(endpoints) == 2)
        self.assertTrue(len(endpoints[0]['parameters'][0]) == 5)
        self.assertTrue(isinstance(endpoints[0]['parameters'][0]['namespaces'], dict))
        self.assertTrue(endpoints[0]['parameters'][0]['prefix'] == 'MODAPSParameters')
        self.assertTrue(endpoints[0]['url'] == expected_endpoint)

    def test_extract_parameter_type(self):
        with_brackets = '{geo:bbox}'
        without_brackets = 'time:start'
        singleton = 'coords'

        test_tuple = self.reader._extract_parameter_type(singleton)
        self.assertTrue(test_tuple == ('', singleton))

        test_tuple = self.reader._extract_parameter_type(with_brackets)
        self.assertTrue(test_tuple[0] == 'geo')
        self.assertTrue(test_tuple[1] == 'bbox')

        test_tuple = self.reader._extract_parameter_type(without_brackets)
        self.assertTrue(test_tuple[0] == 'time')
        self.assertTrue(test_tuple[1] == 'start')

    def test_extract_url_parameters(self):
        url_xpath = '/{http://a9.com/-/spec/opensearch/1.1/}OpenSearchDescription/' +\
                    '{http://a9.com/-/spec/opensearch/1.1/}Url'
        url = self.reader.parser.find(url_xpath)
        url = url[0].get('template', '')
        params = self.reader._extract_url_parameters(url)

        self.assertTrue(len(params) == 7)
        self.assertTrue(params[0]['name'] == 'coordsOrTiles')
        self.assertTrue(params[6]['formats'] == 'west, south, east, north')
class TestOpenSearchReaderWithEndpoints(unittest.TestCase):
    def setUp(self):
        with open("tests/test_data/opensearch-nasa.xml", "r") as f:
            text = f.read()
        self.reader = OpenSearchReader(text, "")
        self.reader._load_xml()

    def test_parse_service(self):
        """
        just the remainders right now
        """

        should_not_have = (
            "{http://a9.com/-/spec/opensearch/1.1/}"
            + "OpenSearchDescription/"
            + "{http://a9.com/-/spec/opensearch/1.1/}Url"
        )
        service = self.reader.parse_service()

        self.assertTrue("remainder" in service)

        remainder = service["remainder"]

        self.assertTrue(should_not_have in remainder)

    def test_parse_endpoints(self):
        endpoints = self.reader.parse_endpoints()

        print endpoints

        expected_endpoint = (
            "http://modwebsrv.modaps.eosdis.nasa.gov/axis2/"
            + "services/MODAPSservices/getOpenSearch?"
            + "products={MODAPSParameters:products}&"
            + "collection={MODAPSParameters:collection?}"
            + "&start={time:start}&stop={time:stop}&bbox={geo:box}"
            + "&coordsOrTiles={MODAPSParameters:coordsOrTiles?}"
            + "&dayNightBoth={MODAPSParameters:dayNightBoth?}"
        )

        self.assertTrue(len(endpoints) == 2)
        self.assertTrue(len(endpoints[0]["parameters"][0]) == 5)
        self.assertTrue(isinstance(endpoints[0]["parameters"][0]["namespaces"], dict))
        self.assertTrue(endpoints[0]["parameters"][0]["prefix"] == "MODAPSParameters")
        self.assertTrue(endpoints[0]["url"] == expected_endpoint)

    def test_extract_parameter_type(self):
        with_brackets = "{geo:bbox}"
        without_brackets = "time:start"
        singleton = "coords"

        test_tuple = self.reader._extract_parameter_type(singleton)
        self.assertTrue(test_tuple == ("", singleton))

        test_tuple = self.reader._extract_parameter_type(with_brackets)
        self.assertTrue(test_tuple[0] == "geo")
        self.assertTrue(test_tuple[1] == "bbox")

        test_tuple = self.reader._extract_parameter_type(without_brackets)
        self.assertTrue(test_tuple[0] == "time")
        self.assertTrue(test_tuple[1] == "start")

    def test_extract_url_parameters(self):
        url_xpath = (
            "/{http://a9.com/-/spec/opensearch/1.1/}OpenSearchDescription/"
            + "{http://a9.com/-/spec/opensearch/1.1/}Url"
        )
        url = self.reader.parser.find(url_xpath)
        url = url[0].get("template", "")
        params = self.reader._extract_url_parameters(url)

        self.assertTrue(len(params) == 7)
        self.assertTrue(params[0]["name"] == "coordsOrTiles")
        self.assertTrue(params[6]["formats"] == "west, south, east, north")