Exemple #1
0
    def test_parse_message_bad_field_name_should_fail(self):
        # setup
        device_template = self._get_template()

        properties = MessageProperties(content_encoding=self.encoding,
                                       content_type=self.content_type)
        message = Message(
            body=json.dumps(self.bad_field_name).encode(),
            properties=properties,
            annotations={
                common_parser.DEVICE_ID_IDENTIFIER: self.device_id.encode()
            },
        )
        args = CommonParserArguments()
        parser = self._create_parser(device_template=device_template,
                                     message=message,
                                     args=args)

        # act
        parsed_msg = parser.parse_message()

        # verify
        # parsing should attempt to place raw payload into result even if parsing fails
        assert parsed_msg["event"]["payload"] == self.bad_field_name

        # field name contains '-' character error
        expected_details_1 = strings.invalid_field_name(
            list(self.bad_field_name.keys()))
        _validate_issues(parser, Severity.error, 2, 1, [expected_details_1])

        # field name not present in template warning
        expected_details_2 = strings.invalid_field_name_mismatch_template(
            list(self.bad_field_name.keys()), device_template.schema_names)

        _validate_issues(parser, Severity.warning, 2, 1, [expected_details_2])
Exemple #2
0
    def _validate_field_names(self, payload: dict):
        # source:
        # https://github.com/Azure/IoTPlugandPlay/tree/master/DTDL
        regex = "^[a-zA-Z_][a-zA-Z0-9_]*$"

        # if a field name does not match the above regex, it is an invalid field name
        invalid_field_names = [
            field_name for field_name in payload.keys()
            if not re.search(regex, field_name)
        ]
        if invalid_field_names:
            details = strings.invalid_field_name(invalid_field_names)
            self._add_issue(severity=Severity.error, details=details)