Newer
Older
sisyphe-go / xml_test.go
@Nacim Nacim on 9 Mar 2022 2 KB refactoring logging
package main

import (
	"testing"

	"github.com/sirupsen/logrus"
	"github.com/stretchr/testify/assert"
)

var xmlData = GeneralInfo{
	corpusName: "test",
	name:       "test-default.xml",
	startAt:    1456693426,
	extension:  ".xml",
	path:       "./example/xml/test-default.xml",
	mimetype:   "text/xml",
	size:       7123,
}
var logger *logrus.Entry

func TestValidXML(t *testing.T) {
	result, _ := CheckIfXmlIsWellFormed(xmlData.path, logger)
	assert.Equal(t, result.doctype.Sysid, "note.dtd", "Doctype is valid")
	assert.Equal(t, result.isWellFormed, true, "XML is well formed")
	assert.Equal(t, len(result.wellFormedErrors), 0, "Return empty if xml is not well formed")

}

func TestInvalidXML(t *testing.T) {
	xmlData.path = "./example/xml/test-not-wellformed.xml"
	result, _ := CheckIfXmlIsWellFormed(xmlData.path, logger)
	assert.Equal(t, result.Data["doctype"], "", "Not get doctype if xml is invalid")
	assert.Equal(t, result.Data["isWellFormed"], false, "XML is not well formed")
	assert.Equal(t, result.Data["wellFormedErrors"].Message, "Opening and ending tag mismatch: from line 4 and Ffrom <from>Jani</Ffrom>", "Return empty if xml is not well formed")
	assert.Equal(t, result.Data["wellFormedErrors"].Line, "4", "Return empty if xml is not well formed")
}

func TestValidDTD(t *testing.T) {
	xmlData.path = "./example/xml/test-default.xml"
	configDetailledAnalyze.XML.ListDTD = append(configDetailledAnalyze.XML.ListDTD, "note.dtd")
	result, _ := CheckXMLValidation(xmlData.path, "note.dtd", logger)
	assert.Equal(t, result.isValidAgainstDTD, true, "XML must be valid according to the DTD")
}

func TestInvalidDTD(t *testing.T) {
	xmlData.path = "./example/xml/test-default-bad-doctype.xml"
	configDetailledAnalyze.XML.ListDTD = append(configDetailledAnalyze.XML.ListDTD, "bad-doctype.dtd")
	result, _ := CheckXMLValidation(xmlData.path, "bad-doctype.dtd", logger)
	assert.Equal(t, result.isValidAgainstDTD, false, "XML must not be valid according to the DTD")
}

func TestValidSchema(t *testing.T) {
	configDetailledAnalyze.XML.ListXSD = append(configDetailledAnalyze.XML.ListXSD, "note.xsd")
	xmlData.path = "./example/xml/test-default.xml"
	result, _ := CheckXMLValidation(xmlData.path, "", logger)
	assert.Equal(t, result.isValidAgainstSchema, true, "XML must be valid according to the Schema")
}