package main import ( "bytes" "io/ioutil" "testing" "github.com/goccy/go-json" "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 contextLogger = logrus.WithFields(logrus.Fields{ "test": "only for xml test", }) func TestValidXML(t *testing.T) { result, _ := CheckIfXmlIsWellFormed(xmlData.path, contextLogger) assert.Equal(t, result.doctype.Sysid, "note.dtd", "Doctype is valid") assert.Equal(t, result.isWellFormed, true, "XML is well formed") assert.Equal(t, result.wellFormedErrors, ErrorXML{}, "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, contextLogger) assert.Equal(t, result.doctype.Sysid, "", "Not get sysid doctype if xml is invalid") assert.Equal(t, result.isWellFormed, false, "XML is not well formed") assert.Equal(t, result.wellFormedErrors, ErrorXML{Message: "Opening and ending tag mismatch: from line 4 and FfromJani", Line: "4", File: ""}, "Return empty if xml is not well formed") } func TestValidDTD(t *testing.T) { xmlData.path = "./example/xml/test-default.xml" configDetailledAnalyze.XML.ListDTD = []string{"note.dtd"} result, _ := CheckXMLValidation(xmlData.path, XMLInfo{doctype: DoctypeXML{Sysid: "note.dtd"}}, contextLogger) assert.Equal(t, result.isValidAgainstDTD, true, "XML must be valid according to the DTD") assert.Equal(t, result.validationDTDInfos, "note.dtd", "Should match with dtd") assert.Equal(t, len(result.validationsErrors), 0, "Return empty if dtd is valid") } func TestInvalidDTD(t *testing.T) { xmlData.path = "./example/xml/test-default.xml" configDetailledAnalyze.XML.ListDTD = []string{"bad-dtd.dtd"} result, _ := CheckXMLValidation(xmlData.path, XMLInfo{doctype: DoctypeXML{Sysid: "bad-dtd.dtd"}}, contextLogger) assert.Equal(t, result.isValidAgainstDTD, false, "XML must not be valid according to the DTD") assert.Equal(t, result.validationDTDInfos, "", "Should be empty if dtd is not valid") assert.Equal(t, result.validationsErrors, []ErrorXML{ {Message: "No declaration for element note", Line: "3", File: "bad-dtd.dtd"}, {Message: "No declaration for element to", Line: "4", File: "bad-dtd.dtd"}, {Message: "No declaration for element from", Line: "5", File: "bad-dtd.dtd"}, {Message: "No declaration for element heading", Line: "6", File: "bad-dtd.dtd"}, {Message: "No declaration for element body", Line: "7", File: "bad-dtd.dtd"}, }, "Return error if dtd is not valid") } func TestValidSchema(t *testing.T) { xmlData.path = "./example/xml/test-default.xml" configDetailledAnalyze.XML.ListDTD = []string{} configDetailledAnalyze.XML.ListXSD = []string{"note.xsd"} result, _ := CheckXMLValidation(xmlData.path, XMLInfo{doctype: DoctypeXML{Sysid: "note.xsd"}}, contextLogger) assert.Equal(t, result.isValidAgainstSchema, true, "XML must be valid according to the Schema") } func TestInValidSchema(t *testing.T) { xmlData.path = "./example/xml/test-default.xml" configDetailledAnalyze.XML.ListDTD = []string{} configDetailledAnalyze.XML.ListXSD = []string{"bad-xsd.xsd"} result, _ := CheckXMLValidation(xmlData.path, XMLInfo{}, contextLogger) assert.Equal(t, result.isValidAgainstSchema, false, "XML must not be valid according to the Schema") assert.Equal(t, result.validationSchemaErrors, []ErrorXML{ {Message: "Schemas validity error : Element 'note': No matching global declaration available for the validation root.", Line: "3", File: "bad-xsd.xsd", }, }, "Return error if xsd is not valid") } func TestXPath(t *testing.T) { xmlData.path = "./example/xml/xml-for-xpath.xml" jsonFile, _ := ioutil.ReadFile("./example/sisyphe-conf.json") json.Unmarshal(bytes.TrimPrefix(jsonFile, []byte("\xef\xbb\xbf")), &configDetailledAnalyze) resultWellFormed, _ := CheckIfXmlIsWellFormed(xmlData.path, contextLogger) assert.Equal(t, resultWellFormed.doctype.Sysid, "JATS-journalpublishing1.dtd", "Doctype is valid") assert.Equal(t, resultWellFormed.isWellFormed, true, "XML is well formed") assert.Equal(t, resultWellFormed.wellFormedErrors, ErrorXML{}, "Return empty if xml is not well formed") resultXpath, _ := ProcessXpath(xmlData.path, resultWellFormed, contextLogger) assert.Equal(t, len(resultXpath.xpath), 659, "xpath is present") }