示例#1
0
    def _perform_dynamic_validations(self, payload: dict):
        # if the payload is not a dictionary some other parsing error occurred
        if not isinstance(payload, dict):
            return

        template = self._get_template()

        if not isinstance(template, central_models.TemplateV1):
            return

        # if component name is not defined then data should be mapped to root/inherited interfaces
        if not self.component_name:
            self._validate_payload(payload=payload,
                                   template=template,
                                   is_component=False)
            return

        if not template.components:
            # template does not have any valid components
            details = strings.invalid_component_name(self.component_name,
                                                     list())
            self._add_central_issue(severity=Severity.warning, details=details)
            return

        # if component name is defined check to see if its a valid name
        if self.component_name not in template.components:
            details = strings.invalid_component_name(
                self.component_name, list(template.components.keys()))
            self._add_central_issue(severity=Severity.warning, details=details)
            return

        # if component name is valid check to see if payload is valid
        self._validate_payload(payload=payload,
                               template=template,
                               is_component=True)
示例#2
0
    def test_validate_against_invalid_component_template_should_fail(self):
        # setup
        device_template = Template(
            load_json(FileNames.central_property_validation_template_file))

        properties = MessageProperties(content_encoding=self.encoding,
                                       content_type=self.content_type)
        message = Message(
            body=json.dumps(self.bad_dcm_payload).encode(),
            properties=properties,
            annotations={
                common_parser.DEVICE_ID_IDENTIFIER:
                self.device_id.encode(),
                common_parser.COMPONENT_NAME_IDENTIFIER:
                self.component_name.encode(),
            },
            application_properties=_encode_app_props(self.app_properties),
        )
        args = CommonParserArguments(properties=["all"])
        parser = self._create_parser(device_template=device_template,
                                     message=message,
                                     args=args)

        # act
        parsed_msg = parser.parse_message()

        # verify
        assert parsed_msg["event"]["payload"] == self.bad_dcm_payload
        assert parsed_msg["event"]["origin"] == self.device_id
        device_identifier = str(common_parser.DEVICE_ID_IDENTIFIER, "utf8")
        assert parsed_msg["event"]["annotations"][
            device_identifier] == self.device_id
        component_identifier = str(common_parser.COMPONENT_NAME_IDENTIFIER,
                                   "utf8")
        assert (parsed_msg["event"]["annotations"][component_identifier] ==
                self.component_name)
        properties = parsed_msg["event"]["properties"]
        assert properties["system"]["content_encoding"] == self.encoding
        assert properties["system"]["content_type"] == self.content_type
        assert properties["application"] == self.app_properties

        expected_details = strings.invalid_component_name(
            self.component_name, list(device_template.components.keys()))

        _validate_issues(parser, Severity.warning, 1, 1, [expected_details])