Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional paths #169

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
fix xml tests
jcdietrich committed Apr 30, 2024
commit b8e2a3fa13a29d57afd6a86ca24b1c4228d7619b
5 changes: 5 additions & 0 deletions pkg/match/payload/xml_comparator.go
Original file line number Diff line number Diff line change
@@ -21,24 +21,29 @@ func (jc *XMLComparator) Compare(

b1, err := xj.Convert(strings.NewReader(s1))
if err != nil {
log.Errorf("XMLComparator s1 err: %v", err)
return false
}

b2, err := xj.Convert(strings.NewReader(s2))
if err != nil {
log.Errorf("XMLComparator s2 err: %v", err)
return false
}

err = json.Unmarshal(b1.Bytes(), &o1)
if err != nil {
log.Errorf("XMLComparator o1 err: %v", err)
return false
}
err = json.Unmarshal(b2.Bytes(), &o2)
if err != nil {
log.Errorf("XMLComparator o2 err: %v", err)
return false
}

json := &JSONComparator{}
log.Debugf("XMLComparator optionalPaths: %v", optionalPaths)

return json.doCompareJSONRegexUnmarshaled(
o1.(map[string]interface{}),
64 changes: 58 additions & 6 deletions pkg/match/payload/xml_comparator_test.go
Original file line number Diff line number Diff line change
@@ -3,26 +3,78 @@ package payload
import "testing"

func TestXMLComparator_Compare(t *testing.T) {
type pathMap map[string]bool

type args struct {
s1 string
s2 string
optionalPaths map[string]bool
optionalPaths pathMap
currentPath string
}

tests := []struct {
name string
args args
want bool
}{
{"Test same value order and format", args{"<note><to>Tove</to><from>Jani</from></note>", "<note><to>Tove</to><from>Jani</from></note>", map[string]bool{}, ""}, true},
{"Test different order", args{"<note><to>Tove</to><from>Jani</from></note>", "<note><from>Jani</from><to>Tove</to></note>", map[string]bool{}, ""}, true},
{"Test different format", args{"<note><to>Tove</to><from>Jani</from></note>", "<note> <to>Tove</to>\n<from>Jani</from></note>", map[string]bool{}, ""}, true},
{"Test different values", args{"<?xml version=\"1.0\" encoding=\"utf-8\"?><note><to>Tove</to><from>Janid</from></note>", "<note><to>Tove</to><from>Jani</from></note>", map[string]bool{}, ""}, false},
{
name: "Test same value order and format",
args: args{
s1: "<note><to>Tove</to><from>Jani</from></note>",
s2: "<note><to>Tove</to><from>Jani</from></note>",
optionalPaths: pathMap{},
currentPath: "",
},
want: true,
},
{
name: "Test optionalPaths",
args: args{
s1: "<note><to><first>Tove</first><last>Boo</last></to><from>Jani</from></note>",
s2: "<note><to><first>Tove</first></to><from>Jani</from></note>",
optionalPaths: pathMap{
".note.to.last": true,
},
currentPath: "",
},
want: true,
},
{
name: "Test different order",
args: args{
s1: "<note><to>Tove</to><from>Jani</from></note>",
s2: "<note><from>Jani</from><to>Tove</to></note>",
optionalPaths: pathMap{},
currentPath: "",
},
want: true,
},
{
name: "Test different format",
args: args{
s1: "<note><to>Tove</to><from>Jani</from></note>",
s2: "<note> <to>Tove</to>\n<from>Jani</from></note>",
optionalPaths: pathMap{},
currentPath: "",
},
want: true,
},
{
name: "Test different values",
args: args{
s1: "<?xml version=\"1.0\" encoding=\"utf-8\"?><note><to>Tove</to><from>Janid</from></note>",
s2: "<note><to>Tove</to><from>Jani</from></note>",
optionalPaths: pathMap{},
currentPath: "",
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
jc := &XMLComparator{}
if got := jc.Compare(tt.args.s1, tt.args.s2, tt.args.optionalPaths, tt.args.currentPath); got != tt.want {
got := jc.Compare(tt.args.s1, tt.args.s2, tt.args.optionalPaths, tt.args.currentPath)
if got != tt.want {
t.Errorf("XMLComparator.Compare() = %v, want %v", got, tt.want)
}
})