Commit 5417df3b authored by Karel Hanák's avatar Karel Hanák
Browse files

updated enum definition


Signed-off-by: Karel Hanák's avatarKarel Hanák <karel.hanak@iqrf.org>
parent ae0cd405
Pipeline #4847 passed with stages
in 37 seconds
......@@ -99,7 +99,7 @@ class CMqtt(mqtt.Client):
If the request is not valid, the method publishes error message.
@param request MQTTMessage object representing the published message
@return TrRet.OK if valid, TrRet.SchemaErr otherwise
@return TrRet.OK if valid, TrRet.SCHEMA_ERROR otherwise
"""
topic_endpoint = self._extract_endpoint(request.topic)
if re.fullmatch('(get|post|put|patch|delete)/.*', topic_endpoint) is None:
......@@ -107,7 +107,7 @@ class CMqtt(mqtt.Client):
self.logger.error(error_string)
resp_topic = request.topic.replace('requests', 'responses')
self.publish(resp_topic, error_string, 0, False)
return TrRet.TopicErr
return TrRet.TOPIC_ERROR
if request.payload:
try:
......@@ -121,7 +121,7 @@ class CMqtt(mqtt.Client):
self.publish(resp_topic, 'Invalid request format: ' +
json_error.msg + ' at line ' + str(json_error.lineno) +
', column ' + str(json_error.colno) + ' on\n' + json_error.doc, 0, False)
return TrRet.JSONErr
return TrRet.JSON_ERROR
schema_errors = sorted(self.validator.iter_errors(payload), key=str)
if schema_errors:
......@@ -131,7 +131,7 @@ class CMqtt(mqtt.Client):
error_string += error.message
resp_topic = request.topic.replace('requests', 'responses')
self.publish(resp_topic, error_string, 0, False)
return TrRet.SchemaErr
return TrRet.SCHEMA_ERROR
return TrRet.OK
......@@ -272,10 +272,10 @@ class CMqtt(mqtt.Client):
return TrRet.OK
except IOError as io_error:
self.logger.error('%s on file %s', io_error.strerror, io_error.filename)
return TrRet.IOErr
return TrRet.IO_ERROR
except json.JSONDecodeError as json_error:
self.logger.error(
json_error.msg + ' at line ' + str(json_error.lineno) +
', column ' + str(json_error.colno) + ' on\n' + json_error.doc
)
return TrRet.JSONErr
return TrRet.JSON_ERROR
......@@ -66,8 +66,8 @@ class Config:
Loads json files containing configuration and json schema for validation.
The configuration file is then validated against the schema.
@return dictionary with translator configuration, JSONErr if configuration file is not valid json file,
SchemaErr if configuration is invalid, IOErr if an IOError occurs
@return dictionary with translator configuration, JSON_ERROR if configuration file is not valid json file,
SCHEMA_ERROR if configuration is invalid, IO_ERROR if an IO_ERRORor occurs
"""
try:
config = self.open_and_load(self.config)
......@@ -82,16 +82,16 @@ class Config:
if schema_errors:
for error in schema_errors:
self.logger.error(error)
return TrRet.SchemaErr, None
return TrRet.SCHEMA_ERROR, None
self.logger.info('Configuration file successfully validated.')
return TrRet.OK, config
except IOError as io_error:
print('Failed to open file ' + io_error.filename, file=sys.stderr)
self.logger.error('%s on file %s', io_error.strerror, io_error.filename)
return TrRet.IOErr, None
return TrRet.IO_ERROR, None
except json.JSONDecodeError as json_error:
print('Failed to load json file due to invalid json file content.', file=sys.stderr)
self.logger.error(json_error.msg + ' at line ' + str(json_error.lineno) +
', column ' + str(json_error.colno) + ' on\n' + json_error.doc)
return TrRet.JSONErr, None
return TrRet.JSON_ERROR, None
......@@ -22,7 +22,7 @@ class TrRet(Enum):
Enum class for Translator return codes.
"""
OK = 0
IOErr = 1
JSONErr = 2
SchemaErr = 3
TopicErr = 4
IO_ERROR = 1
JSON_ERROR = 2
SCHEMA_ERROR = 3
TOPIC_ERROR = 4
......@@ -35,15 +35,15 @@ class TestCMqtt(unittest.TestCase):
self.assertEqual(self.mqtt_client._validate_request(self.request), TrRet.OK)
def test_validate_request_jsonerr(self):
def test_validate_request_JSON_ERROR(self):
self.request.payload = b'\x7b\x22\x74\x65\x73\x74\x2c\x7d'
self.assertEqual(self.mqtt_client._validate_request(self.request), TrRet.JSONErr)
self.assertEqual(self.mqtt_client._validate_request(self.request), TrRet.JSON_ERROR)
def test_validate_request_schemaerr(self):
def test_validate_request_SCHEMA_ERROR(self):
self.request.payload = b'\x7b\x22\x68\x65\x61\x64\x65\x72\x73\x22\x3a\x20\x7b\x22' \
b'\x43\x6f\x6e\x74\x65\x6e\x74\x2d\x54\x79\x70\x65\x22\x3a' \
b'\x20\x22\x74\x65\x78\x74\x2f\x70\x6c\x61\x69\x6e\x22\x7d\x7d'
self.assertEqual(self.mqtt_client._validate_request(self.request), TrRet.SchemaErr)
self.assertEqual(self.mqtt_client._validate_request(self.request), TrRet.SCHEMA_ERROR)
if __name__ == '__main__':
......
......@@ -44,17 +44,17 @@ class TestConfig(unittest.TestCase):
def test_read_invalid(self):
config = Config(self.config_invalid_path, self.schema_path)
self.assertEqual(config.read(), (TrRet.SchemaErr, None))
self.assertEqual(config.read(), (TrRet.SCHEMA_ERROR, None))
def test_read_ioerr(self):
def test_read_IO_ERROR(self):
config = Config('nonexistant_file.json', self.schema_path)
self.assertEqual(config.read(), (TrRet.IOErr, None))
self.assertEqual(config.read(), (TrRet.IO_ERROR, None))
def test_read_jsonerr(self):
def test_read_JSON_ERROR(self):
config = Config(self.config_error_path, self.schema_path)
self.assertEqual(config.read(), (TrRet.JSONErr, None))
self.assertEqual(config.read(), (TrRet.JSON_ERROR, None))
if __name__ == '__main__':
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment