# Buggy code
def __asn_query_parse(text):
    """
    Example:   "23028 | US | arin    | 2002-01-04 | TEAM-CYMRU - Team Cymru
    Inc.,US"
    Exception: "1930  | EU | ripencc |            | RCCN Rede Ciencia
    Tecnologia e Sociedade (RCTS),PT"
    Unicode: "10417 | BR | lacnic | 2000-02-15 | Funda\195\131\194\167\195\131\194\163o de Desenvolvimento da Pesquisa, BR"
    """

    result = {}

    if not text:
        return result

    items = Cymru.__query_parse(text)

    if items[4]:
        # unicode characters need to be decoded explicitly
        # with the help of https://stackoverflow.com/questions/60890590/
        result["as_name"] = items[4].encode("latin1").decode("utf8")

    return result


# Traceback (AssertionError)
======================================================================
FAIL: test_empty_result (intelmq.tests.bots.experts.cymru_whois.test_expert.TestCymruExpertBot)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/sebastianw/dev/intelmq/intelmq/tests/bots/experts/cymru_whois/test_expert.py", line 115, in test_empty_result
self.assertMessageEqual(0, EMPTY_INPUT)
File "/home/sebastianw/dev/intelmq/intelmq/lib/test.py", line 507, in assertMessageEqual
self.assertDictEqual(expected, event_dict)
AssertionError: {'__type': 'Event', 'source.ip': '198.105.125.77'} != {'source.ip': '198.105.125.77', 'source.asn[224 chars]ent'}
- {'__type': 'Event', 'source.ip': '198.105.125.77'}
+ {'__type': 'Event',
+  'source.allocated': '2014-10-06T00:00:00+00:00',
+  'source.as_name': 'ASDETUK http://www.heficed.com, GB',
+  'source.asn': 61317,
+  'source.geolocation.cc': 'EU',
+  'source.ip': '198.105.125.77',
+  'source.network': '198.105.125.0/24',
+  'source.registry': 'RIPE'}
-------------------- >> begin captured logging << --------------------
test-bot: INFO: CymruExpertBot initialized with id test-bot and intelmq 2.3.0.alpha1 and python 3.8.3 (default, May 17 2020, 14:48:56) [GCC] as process 14096.
test-bot: INFO: Bot is starting.
test-bot: INFO: Bot initialization completed.
test-bot: INFO: Processed 1 messages since last logging.
test-bot: INFO: Bot stopped.
--------------------- >> end captured logging << ---------------------

======================================================================
FAIL: test_unicode_as_name (intelmq.tests.bots.experts.cymru_whois.test_expert.TestCymruExpertBot)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/sebastianw/dev/intelmq/intelmq/tests/bots/experts/cymru_whois/test_expert.py", line 110, in test_unicode_as_name
self.assertMessageEqual(0, UNICODE_OUTPUT)
File "/home/sebastianw/dev/intelmq/intelmq/lib/test.py", line 507, in assertMessageEqual
self.assertDictEqual(expected, event_dict)
AssertionError: {'__type': 'Event', 'destination.ip': '200.[269 chars]/18'} != {'destination.ip': '200.236.128.1', 'destin[269 chars]ent'}
{'__type': 'Event',
'destination.allocated': '2000-02-15T00:00:00+00:00',
-  'destination.as_name': 'Fundação de Desenvolvimento da Pesquisa, BR',
?                               ^^

+  'destination.as_name': 'Fundacao de Desenvolvimento da Pesquisa, BR',
?                               ^^

'destination.asn': 10417,
'destination.geolocation.cc': 'BR',
'destination.ip': '200.236.128.1',
'destination.network': '200.236.128.0/18',
'destination.registry': 'LACNIC'}
-------------------- >> begin captured logging << --------------------
test-bot: INFO: CymruExpertBot initialized with id test-bot and intelmq 2.3.0.alpha1 and python 3.8.3 (default, May 17 2020, 14:48:56) [GCC] as process 14096.
test-bot: INFO: Bot is starting.
test-bot: INFO: Bot initialization completed.
test-bot: INFO: Processed 1 messages since last logging.
test-bot: INFO: Bot stopped.
--------------------- >> end captured logging << ---------------------

# Fixed code
def __asn_query_parse(text):
    """
    Example:   "23028 | US | arin    | 2002-01-04 | TEAM-CYMRU - Team Cymru
    Inc.,US"
    Exception: "1930  | EU | ripencc |            | RCCN Rede Ciencia
    Tecnologia e Sociedade (RCTS),PT"
    """

    result = {}

    if not text:
        return result

    items = Cymru.__query_parse(text)

    if items[4]:
        # unicode characters need to be decoded explicitly
        # with the help of https://stackoverflow.com/questions/60890590/
        result["as_name"] = items[4].encode("latin1").decode("utf8")

    return result