use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class SecurityManagerTest method deleteAccount.
@Test
public void deleteAccount() throws EXistException, PermissionDeniedException, XPathException, LockException {
final BrokerPool brokerPool = existEmbeddedServer.getBrokerPool();
final SecurityManager securityManager = brokerPool.getSecurityManager();
try (final DBBroker broker = brokerPool.get(Optional.of(securityManager.getSystemSubject()))) {
// 1. pre-check - assert the account exists
assertTrue(securityManager.hasAccount(TEST_USER_NAME));
// 2. pre-check - assert the XML document for the account and group exists
try (final LockedDocument document = broker.getXMLResource(ACCOUNTS_URI.append(TEST_USER_NAME + ".xml"), Lock.LockMode.READ_LOCK)) {
assertNotNull(document);
}
try (final LockedDocument document = broker.getXMLResource(GROUPS_URI.append(TEST_GROUP_NAME + ".xml"), Lock.LockMode.READ_LOCK)) {
assertNotNull(document);
}
// 3. pre-check - check that both the accounts collection and groups collection have sub-collections for removed accounts and groups
try (final Collection collection = broker.openCollection(ACCOUNTS_URI, Lock.LockMode.READ_LOCK)) {
assertNotNull(collection);
assertEquals(1, collection.getChildCollectionCount(broker));
assertTrue(collection.hasChildCollection(broker, XmldbURI.create(REMOVED_COLLECTION_NAME)));
}
try (final Collection collection = broker.openCollection(GROUPS_URI, Lock.LockMode.READ_LOCK)) {
assertNotNull(collection);
assertEquals(1, collection.getChildCollectionCount(broker));
assertTrue(collection.hasChildCollection(broker, XmldbURI.create(REMOVED_COLLECTION_NAME)));
}
// 4. pre-check - assert that the removed Collections do exist for accounts and groups, but is empty
try (final Collection collection = broker.openCollection(REMOVED_ACCOUNTS_URI, Lock.LockMode.READ_LOCK)) {
assertNotNull(collection);
assertEquals(0, collection.getChildCollectionCount(broker));
assertEquals(0, collection.getDocumentCount(broker));
}
try (final Collection collection = broker.openCollection(REMOVED_GROUPS_URI, Lock.LockMode.READ_LOCK)) {
assertNotNull(collection);
assertEquals(0, collection.getChildCollectionCount(broker));
assertEquals(0, collection.getDocumentCount(broker));
}
// 5. pre-check - assert the XML document for any removed account or group does NOT exist
assertFalse(removedAccountExists(broker, TEST_USER_NAME));
assertFalse(removedGroupExists(broker, TEST_GROUP_NAME));
// 6. DELETE THE ACCOUNT
securityManager.deleteAccount(TEST_USER_NAME);
// 7. post-check - assert the account does NOT exist
assertFalse(securityManager.hasAccount(TEST_USER_NAME));
// 8. post-check - assert the XML document for the account does NOT exist, but that the group still exists
try (final LockedDocument document = broker.getXMLResource(ACCOUNTS_URI.append(TEST_USER_NAME + ".xml"), Lock.LockMode.READ_LOCK)) {
assertNull(document);
}
try (final LockedDocument document = broker.getXMLResource(GROUPS_URI.append(TEST_GROUP_NAME + ".xml"), Lock.LockMode.READ_LOCK)) {
assertNotNull(document);
}
// 9. post-check - check that both the accounts collection and groups collection still have sub-collections for removed accounts and groups
try (final Collection collection = broker.openCollection(ACCOUNTS_URI, Lock.LockMode.READ_LOCK)) {
assertNotNull(collection);
assertEquals(1, collection.getChildCollectionCount(broker));
assertTrue(collection.hasChildCollection(broker, XmldbURI.create(REMOVED_COLLECTION_NAME)));
}
try (final Collection collection = broker.openCollection(GROUPS_URI, Lock.LockMode.READ_LOCK)) {
assertNotNull(collection);
assertEquals(1, collection.getChildCollectionCount(broker));
assertTrue(collection.hasChildCollection(broker, XmldbURI.create(REMOVED_COLLECTION_NAME)));
}
// 10. post-check - assert that the removed Collections do exist for accounts and groups, but contain only 1 document (i.e. for the removed account)
try (final Collection collection = broker.openCollection(REMOVED_ACCOUNTS_URI, Lock.LockMode.READ_LOCK)) {
assertNotNull(collection);
assertEquals(0, collection.getChildCollectionCount(broker));
assertEquals(1, collection.getDocumentCount(broker));
}
try (final Collection collection = broker.openCollection(REMOVED_GROUPS_URI, Lock.LockMode.READ_LOCK)) {
assertNotNull(collection);
assertEquals(0, collection.getChildCollectionCount(broker));
assertEquals(0, collection.getDocumentCount(broker));
}
// 11. post-check - assert the XML document for the removed account does exist, but no such document exists for the group
assertTrue(removedAccountExists(broker, TEST_USER_NAME));
assertFalse(removedGroupExists(broker, TEST_GROUP_NAME));
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class XqueryApiTest method executeQuery.
private Sequence executeQuery(final String uid, final String pwd, final String query) throws ApiException {
try {
final BrokerPool pool = server.getBrokerPool();
final XQuery xquery = pool.getXQueryService();
final Subject user = pool.getSecurityManager().authenticate(uid, pwd);
try (final DBBroker broker = pool.get(Optional.of(user))) {
return xquery.execute(broker, query, null);
}
} catch (final AuthenticationException | EXistException | PermissionDeniedException | XPathException e) {
throw new ApiException(e.getMessage(), e);
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class EmbeddedXMLStreamReaderTest method assertNodesIn.
public void assertNodesIn(final NamedEvent[] expected, final Function<Document, NodeHandle> initialNodeFun, final Optional<Function<Document, NodeHandle>> containerFun) throws EXistException, PermissionDeniedException, IOException, XMLStreamException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
try (final LockedDocument lockedDocument = broker.getXMLResource(TEST_MIXED_XML_COLLECTION.append(MIXED_XML_NAME), Lock.LockMode.WRITE_LOCK)) {
assertNotNull(lockedDocument);
final Document document = lockedDocument.getDocument();
assertNotNull(document);
final NodeHandle initialNode = initialNodeFun.apply(document);
final Optional<NodeHandle> maybeContainerNode = containerFun.map(f -> f.apply(document));
final IEmbeddedXMLStreamReader xmlStreamReader = broker.getXMLStreamReader(initialNode, false);
final NamedEvent[] actual = readAllEvents(maybeContainerNode, xmlStreamReader);
assertArrayEquals(formatExpectedActual(expected, actual), expected, actual);
}
transaction.commit();
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class EmbeddedXMLStreamReaderTest method cleanup.
@AfterClass
public static void cleanup() throws EXistException, PermissionDeniedException, IOException, TriggerException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
deleteCollection(broker, transaction, TEST_MIXED_XML_COLLECTION);
transaction.commit();
}
}
use of org.exist.storage.BrokerPool in project exist by eXist-db.
the class NodeTest method attributeAxis.
@Test
public void attributeAxis() throws EXistException, LockException, PermissionDeniedException {
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final LockedDocument lockedDoc = root.getDocumentWithLock(broker, XmldbURI.create("test.xml"), LockMode.READ_LOCK)) {
Element docElement = lockedDoc.getDocument().getDocumentElement();
Element first = (Element) docElement.getFirstChild();
assertEquals("a", first.getNodeName());
assertEquals("1", first.getAttribute("ns:a"));
assertEquals("1", first.getAttributeNS("http://foo.org", "a"));
Attr attr = first.getAttributeNode("ns:a");
assertNotNull(attr);
assertEquals("a", attr.getLocalName());
assertEquals("http://foo.org", attr.getNamespaceURI());
assertEquals("1", attr.getValue());
Node parent = attr.getOwnerElement();
assertNotNull(parent);
assertEquals("a", parent.getNodeName());
parent = attr.getParentNode();
assertNull(parent);
attr = first.getAttributeNodeNS("http://foo.org", "a");
assertNotNull(attr);
assertEquals("a", attr.getLocalName());
assertEquals("http://foo.org", attr.getNamespaceURI());
assertEquals("1", attr.getValue());
NamedNodeMap map = first.getAttributes();
assertEquals(2, map.getLength());
attr = (Attr) map.getNamedItemNS("http://foo.org", "b");
assertNotNull(attr);
assertEquals("b", attr.getLocalName());
assertEquals("http://foo.org", attr.getNamespaceURI());
assertEquals("m", attr.getValue());
}
}
Aggregations