private Tree getCache(@NotNull Authorizable authorizable) throws Exception { ContentSession cs = Subject.doAs(SystemSubject.INSTANCE, new PrivilegedExceptionAction<ContentSession>() { @Override public ContentSession run() throws LoginException, NoSuchWorkspaceException { return login(null); } }); try { Root r = cs.getLatestRoot(); NodeUtil n = new NodeUtil(r.getTree(authorizable.getPath())); NodeUtil c = n.getOrAddChild(CacheConstants.REP_CACHE, CacheConstants.NT_REP_CACHE); c.setLong(CacheConstants.REP_EXPIRATION, 1); r.commit(CacheValidatorProvider.asCommitAttributes()); } finally { cs.close(); } root.refresh(); return root.getTree(authorizable.getPath()).getChild(CacheConstants.REP_CACHE); }
@Test public void testCreateCacheByNodeType() throws RepositoryException { for (Authorizable a : authorizables) { try { NodeUtil node = new NodeUtil(getAuthorizableTree(a)); NodeUtil cache = node.addChild("childNode", CacheConstants.NT_REP_CACHE); cache.setLong(CacheConstants.REP_EXPIRATION, 1); root.commit(); fail("Creating node with nt rep:Cache below a user or group must fail."); } catch (CommitFailedException e) { assertTrue(e.isConstraintViolation()); assertEquals(34, e.getCode()); } finally { root.refresh(); } } }
@Test public void testCreateCacheWithCommitInfo() throws RepositoryException { for (Authorizable a : authorizables) { try { NodeUtil node = new NodeUtil(getAuthorizableTree(a)); NodeUtil cache = node.addChild(CacheConstants.REP_CACHE, CacheConstants.NT_REP_CACHE); cache.setLong(CacheConstants.REP_EXPIRATION, 1); root.commit(CacheValidatorProvider.asCommitAttributes()); fail("Creating rep:cache node below a user or group must fail."); } catch (CommitFailedException e) { assertTrue(e.isConstraintViolation()); assertEquals(34, e.getCode()); } finally { root.refresh(); } } }
@Test public void testNestedCache() throws Exception { NodeUtil cache = new NodeUtil(getCache(getTestUser())); try { NodeUtil c = cache.getOrAddChild(CacheConstants.REP_CACHE, CacheConstants.NT_REP_CACHE); c.setLong(CacheConstants.REP_EXPIRATION, 223); root.commit(CacheValidatorProvider.asCommitAttributes()); fail("Creating nested cache must fail."); } catch (CommitFailedException e) { assertTrue(e.isConstraintViolation()); assertEquals(34, e.getCode()); } finally { root.refresh(); } }
@Test public void testCreateCacheBelowProfile() throws Exception { try { NodeUtil node = new NodeUtil(getAuthorizableTree(getTestUser())); NodeUtil child = node.addChild("profile", NodeTypeConstants.NT_OAK_UNSTRUCTURED); child.addChild(CacheConstants.REP_CACHE, CacheConstants.NT_REP_CACHE).setLong(CacheConstants.REP_EXPIRATION, 23); root.commit(CacheValidatorProvider.asCommitAttributes()); fail("Creating rep:cache node below a user or group must fail."); } catch (CommitFailedException e) { assertTrue(e.isConstraintViolation()); assertEquals(34, e.getCode()); } finally { root.refresh(); } }
@Test public void testChangePrimaryType() throws RepositoryException { for (Authorizable a : authorizables) { try { NodeUtil node = new NodeUtil(getAuthorizableTree(a)); NodeUtil cache = node.addChild("childNode", JcrConstants.NT_UNSTRUCTURED); root.commit(); cache.setName(JcrConstants.JCR_PRIMARYTYPE, CacheConstants.NT_REP_CACHE); cache.setLong(CacheConstants.REP_EXPIRATION, 1); root.commit(); fail("Changing primary type of residual node below an user/group to rep:Cache must fail."); } catch (CommitFailedException e) { assertTrue(e.isConstraintViolation()); assertEquals(34, e.getCode()); } finally { root.refresh(); } } }
@Test public void testCreateCacheBelowPersistedProfile() throws Exception { try { NodeUtil node = new NodeUtil(getAuthorizableTree(getTestUser())); NodeUtil child = node.addChild("profile", NodeTypeConstants.NT_OAK_UNSTRUCTURED); root.commit(); child.addChild(CacheConstants.REP_CACHE, CacheConstants.NT_REP_CACHE).setLong(CacheConstants.REP_EXPIRATION, 23); root.commit(CacheValidatorProvider.asCommitAttributes()); fail("Creating rep:cache node below a user or group must fail."); } catch (CommitFailedException e) { assertTrue(e.isConstraintViolation()); assertEquals(34, e.getCode()); } finally { root.refresh(); } }
@Test public void testCreateCacheOutsideOfAuthorizable() throws Exception { NodeUtil n = new NodeUtil(root.getTree("/")); try { NodeUtil child = n.addChild(CacheConstants.REP_CACHE, CacheConstants.NT_REP_CACHE); child.setLong(CacheConstants.REP_EXPIRATION, 1); root.commit(); fail("Using rep:cache/rep:Cache outside a user or group must fail."); } catch (CommitFailedException e) { assertTrue(e.isConstraintViolation()); assertEquals(34, e.getCode()); } finally { root.refresh(); Tree c = n.getTree().getChild(CacheConstants.REP_CACHE); if (c.exists()) { c.remove(); root.commit(); } } } }