Newer
Older
sisyphe-go / xml_test.go
@Nacim Nacim on 4 Mar 2022 2 KB add xpath
package main

import (
	"testing"

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

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

func TestValidXML(t *testing.T) {
	result, _ := getXMlData(xmlData.path)
	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, _ := getXMlData(xmlData.path)
	assert.Equal(t, result.doctype.Sysid, "", "Not get doctype if xml is invalid")
	assert.Equal(t, result.isWellFormed, false, "XML is not well formed")
	assert.Equal(t, result.wellFormedErrors[0].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.wellFormedErrors[0].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 := processDetailledAnalysis(xmlData.path, "", "note.dtd")
	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 := processDetailledAnalysis(xmlData.path, "", "bad-doctype.dtd")
	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 := processDetailledAnalysis(xmlData.path, "", "")
	assert.Equal(t, result.isValidAgainstSchema, true, "XML must be valid according to the Schema")
}