def test_when_other_exception_raised_create_validation_object(self, validate_against_schema):
        exception_obj = Exception("error msg")
        validate_against_schema.side_effect = mock.Mock(side_effect=exception_obj)

        ip = InformationPackage.objects.create()
        task = ProcessTask.objects.create(information_package=ip)

        validator = XMLSchemaValidator(
            context="dummy_schema.xsd",
            options={'rootdir': "dummy_rootdir"},
            ip=ip.id,
            task=task
        )

        with self.assertRaises(Exception):
            validator.validate("some_xml_file_path")

        validations = Validation.objects.filter(
            information_package_id=ip.id,
            message='error msg',
            passed=False,
            task_id=task.id,
            validator='XMLSchemaValidator'
        )
        self.assertEqual(validations.count(), 1)
Beispiel #2
0
def ValidateXMLFile(self,
                    xml_filename=None,
                    schema_filename=None,
                    rootdir=None):
    """
    Validates (using LXML) an XML file using a specified schema file
    """

    Validation.objects.filter(task=self.get_processtask()).delete()
    xml_filename, schema_filename = self.parse_params(xml_filename,
                                                      schema_filename)
    if rootdir is None and self.ip is not None:
        ip = InformationPackage.objects.get(pk=self.ip)
        rootdir = ip.object_path
    else:
        rootdir, = self.parse_params(rootdir)

    validator = XMLSchemaValidator(context=schema_filename,
                                   options={'rootdir': rootdir},
                                   ip=self.ip,
                                   task=self.get_processtask())
    validator.validate(xml_filename)

    msg = "Validated %s against schema" % xml_filename
    self.create_success_event(msg)
    return "Success"
Beispiel #3
0
    def test_validate_schema_against_valid_file(self):
        schema_file_name = "schema.xsd"
        schema_file_path = self.create_schema_file(schema_file_name)
        xml_file_path = self.create_xml("xml_file.xml")

        validator = XMLSchemaValidator(
            context=schema_file_path,
            options={'rootdir': self.datadir},
        )
        validator.validate(xml_file_path)
    def test_validate_schema_against_bad_file(self):
        schema_file_name = "schema.xsd"
        schema_file_path = self.create_schema_file(schema_file_name)
        xml_file_path = self.create_bad_xml("bad_xml_file.xml")

        validator = XMLSchemaValidator(
            context=schema_file_path,
            options={'rootdir': self.datadir},
        )

        with self.assertRaises(ValidationError):
            validator.validate(xml_file_path)

        expected_error_message = "Element 'price': 'foo' is not a valid value of the atomic type 'xs:decimal'"
        self.assertTrue(Validation.objects.filter(message__icontains=expected_error_message).exists())
Beispiel #5
0
    def test_when_successful_create_validation_object(self,
                                                      validate_against_schema):
        ip = InformationPackage.objects.create()
        task = ProcessTask.objects.create(information_package=ip)

        validator = XMLSchemaValidator(context="dummy_schema.xsd",
                                       options={'rootdir': "dummy_rootdir"},
                                       ip=ip.id,
                                       task=task)

        validator.validate("some_xml_file_path")

        validations = Validation.objects.filter(information_package_id=ip.id,
                                                passed=True,
                                                task_id=task.id,
                                                validator='XMLSchemaValidator')
        self.assertEqual(validations.count(), 1)
Beispiel #6
0
    def run(self, xml_filename=None, schema_filename=None, rootdir=None):
        """
        Validates (using LXML) an XML file using a specified schema file
        """

        Validation.objects.filter(task=self.task_id).delete()
        xml_filename, schema_filename = self.parse_params(
            xml_filename, schema_filename)
        if rootdir is None and self.ip is not None:
            ip = InformationPackage.objects.get(pk=self.ip)
            rootdir = ip.object_path
        else:
            rootdir, = self.parse_params(rootdir)

        validator = XMLSchemaValidator(context=schema_filename,
                                       options={'rootdir': rootdir},
                                       ip=self.ip,
                                       task=self.task_id)
        validator.validate(xml_filename)
        return "Success"
Beispiel #7
0
    def test_validate_with_imported_schema(self, mock_download):
        schema_file_name = "schema.xsd"
        schema_file_path = self.create_schema_file_with_import(
            schema_file_name)
        xml_file_path = self.create_xml("xml_file.xml")

        validator = XMLSchemaValidator(
            context=schema_file_path,
            options={'rootdir': self.datadir},
        )
        validator.validate(xml_file_path)
        mock_download.assert_called_once()

        mock_download.reset_mock()
        bad_xml_file_path = self.create_bad_xml("xml_file.xml")
        with self.assertRaises(ValidationError):
            validator.validate(bad_xml_file_path)

        mock_download.assert_called_once()

        expected_error_message = "Element 'price': 'foo' is not a valid value of the atomic type 'xs:decimal'"
        self.assertTrue(
            Validation.objects.filter(
                message__icontains=expected_error_message).exists())

        # ensure that the schema has only been modified in memory and not the file
        schema_doc = etree.parse(schema_file_path)
        schemaLocation = schema_doc.xpath(
            '//*[local-name()="import"]/@schemaLocation')[0]
        self.assertEqual(schemaLocation,
                         "https://www.loc.gov/standards/mets/mets.xsd")
    def test_when_documentInvalid_raised_create_validation_objects(self, validate_against_schema):
        exception_obj = DocumentInvalid("error msg")
        exception_obj.error_log = [
            create_mocked_error_log(123, "error msg 1"),
            create_mocked_error_log(456, "error msg 2"),
        ]
        validate_against_schema.side_effect = mock.Mock(side_effect=exception_obj)

        ip = InformationPackage.objects.create()
        task = ProcessTask.objects.create(information_package=ip)

        validator = XMLSchemaValidator(
            context="dummy_schema.xsd",
            options={'rootdir': "dummy_rootdir"},
            ip=ip.id,
            task=task
        )
        with self.assertRaises(ValidationError):
            validator.validate("some_xml_file_path")

        validations = Validation.objects.filter(passed=False, validator="XMLSchemaValidator")
        self.assertEqual(validations.count(), 2)