1

I'm setting up unit testing using marklogic-unit-test and one thing I'd like to do is check a given document has a particular permission. However, when I test my permission against a Sequence of permissions, I get an XDMP-NONMIXEDCOMPLEXCONT error. I assume this has to do with the fact that permissions are complex objects and not something like a simple string, because this works with collections.

const test = require("/test/test-helper.xqy");
let p1 = Sequence.from([xdmp.permission("rest-reader", "read", "element")]);
let p2 = Sequence.from([
  xdmp.permission("rest-reader", "read", "element"), 
  xdmp.permission("rest-writer", "update", "element")
]);
test.assertAtLeastOneEqual(p1, p2)

Which returns:

[javascript] XDMP-NONMIXEDCOMPLEXCONT: fn:data(<sec:permission 
xmlns:sec="http://marklogic.com/xdmp/security">
<sec:capability>...</sec:capability>...</sec:permission>) 
-- Node has complex type with non-mixed complex content

The best alternative I can come up with is to explicitly loop over the Sequence and do the comparison with fn.deepEqual on each element. Is there a better way?

eaolson
  • 14,717
  • 7
  • 43
  • 58

1 Answers1

2

The test.assertAtLeastOneEqual() function expects atomic values (item() signature). The only test helper function that can handle elements is test.assertEqualXml(), but that looks for exact matches. I think your best bet is to stringify the permissions. Something like this:

const test = require("/test/test-helper.xqy");
let p1 = [xdmp.permission("rest-reader", "read")];
let p2 = [
  xdmp.permission("rest-reader", "read"), 
  xdmp.permission("rest-writer", "update")
];
p1 = Sequence.from(p1.map(p => xdmp.roleName(p.roleId) + ':' + p.capability));
p2 = Sequence.from(p2.map(p => xdmp.roleName(p.roleId) + ':' + p.capability));
test.assertAtLeastOneEqual(p1, p2)
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
grtjn
  • 20,254
  • 1
  • 24
  • 35