Skip to content

Commit e080d4e

Browse files
committed
test empty child object/array
1 parent 09c12d8 commit e080d4e

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

main_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,76 @@ func TestEmptyParentArray(t *testing.T) {
588588
}
589589
}
590590

591+
func TestEmptyChildObject(t *testing.T) {
592+
parent, err := jwcc.Parse(strings.NewReader(`{
593+
"hosts": {
594+
"host1": "100.99.98.97",
595+
}
596+
}`))
597+
if err != nil {
598+
t.Fatalf("expected no error, got [%v]", err)
599+
}
600+
parentDoc := &ParsedDocument{
601+
Object: parent.Value.(*jwcc.Object),
602+
Path: "parent",
603+
}
604+
605+
child, err := jwcc.Parse(strings.NewReader(`{"hosts":{}}`))
606+
if err != nil {
607+
t.Fatalf("expected no error, got [%v]", err)
608+
}
609+
610+
childDoc := &ParsedDocument{
611+
Object: child.Value.(*jwcc.Object),
612+
Path: "child",
613+
}
614+
615+
err = mergeDocs(preDefinedAclSections, parentDoc, []*ParsedDocument{childDoc})
616+
if err != nil {
617+
t.Fatalf("expected no error, got [%v]", err)
618+
}
619+
620+
mergedValues := parentDoc.Object.Find("hosts").Value.(*jwcc.Object).Members
621+
if len(mergedValues) != 1 {
622+
t.Fatalf("section [%v] should be [1], not [%v]", "hosts", len(mergedValues))
623+
}
624+
}
625+
626+
func TestEmptyChildArray(t *testing.T) {
627+
parent, err := jwcc.Parse(strings.NewReader(`{
628+
"acls": [
629+
{"action": "accept", "src": ["finance1"], "dst": ["tag:demo-infra:22"]},
630+
]
631+
}`))
632+
if err != nil {
633+
t.Fatalf("expected no error, got [%v]", err)
634+
}
635+
parentDoc := &ParsedDocument{
636+
Object: parent.Value.(*jwcc.Object),
637+
Path: "parent",
638+
}
639+
640+
child, err := jwcc.Parse(strings.NewReader(`{"acls":[]}`))
641+
if err != nil {
642+
t.Fatalf("expected no error, got [%v]", err)
643+
}
644+
645+
childDoc := &ParsedDocument{
646+
Object: child.Value.(*jwcc.Object),
647+
Path: "child",
648+
}
649+
650+
err = mergeDocs(preDefinedAclSections, parentDoc, []*ParsedDocument{childDoc})
651+
if err != nil {
652+
t.Fatalf("expected no error, got [%v]", err)
653+
}
654+
655+
mergedValues := parentDoc.Object.Find("acls").Value.(*jwcc.Array).Values
656+
if len(mergedValues) != 1 {
657+
t.Fatalf("section [%v] should be [1], not [%v]", "acls", len(mergedValues))
658+
}
659+
}
660+
591661
func TestSort(t *testing.T) {
592662
parent, err := jwcc.Parse(strings.NewReader(ACL_PARENT))
593663
if err != nil {

0 commit comments

Comments
 (0)