### Question:
ZooAuthenticationKeyDistributor { public synchronized void initialize() throws KeeperException, InterruptedException { if (initialized.get()) { return; } if (zk.exists(baseNode)) { List<ACL> acls = zk.getACL(baseNode, new Stat()); if (acls.size() == 1) { ACL actualAcl = acls.get(0), expectedAcl = ZooUtil.PRIVATE.get(0); Id actualId = actualAcl.getId(); if (actualAcl.getPerms() == expectedAcl.getPerms() && actualId.getScheme().equals("digest") && actualId.getId().startsWith("accumulo:")) { initialized.set(true); return; } } else { log.error("Saw more than one ACL on the node"); } log.error("Expected {} to have ACLs {} but was {}", baseNode, ZooUtil.PRIVATE, acls); throw new IllegalStateException( "Delegation token secret key node in ZooKeeper is not protected."); } else { if (!zk.putPrivatePersistentData(baseNode, new byte[0], NodeExistsPolicy.FAIL)) { throw new AssertionError("Got false from putPrivatePersistentData method"); } } initialized.set(true); } ZooAuthenticationKeyDistributor(ZooReaderWriter zk, String baseNode); synchronized void initialize(); List<AuthenticationKey> getCurrentKeys(); synchronized void advertise(AuthenticationKey newKey); synchronized void remove(AuthenticationKey key);  }### Answer:
@Test(expected = AuthFailedException.class) public void testInitialize() throws Exception { ZooAuthenticationKeyDistributor distributor = new ZooAuthenticationKeyDistributor(zrw, baseNode); expect(zrw.exists(baseNode)).andReturn(false); expect( zrw.putPrivatePersistentData(eq(baseNode), aryEq(new byte[0]), eq(NodeExistsPolicy.FAIL))) .andThrow(new AuthFailedException()); replay(zrw); distributor.initialize(); verify(zrw); }
@Test public void testInitializeCreatesParentNode() throws Exception { ZooAuthenticationKeyDistributor distributor = new ZooAuthenticationKeyDistributor(zrw, baseNode); expect(zrw.exists(baseNode)).andReturn(false); expect(zrw.putPrivatePersistentData(eq(baseNode), anyObject(), eq(NodeExistsPolicy.FAIL))) .andReturn(true); replay(zrw); distributor.initialize(); verify(zrw); }
@Test(expected = IllegalStateException.class) public void testMissingAcl() throws Exception { ZooAuthenticationKeyDistributor distributor = new ZooAuthenticationKeyDistributor(zrw, baseNode); expect(zrw.exists(baseNode)).andReturn(true); expect(zrw.getACL(eq(baseNode), anyObject(Stat.class))).andReturn(Collections.emptyList()); replay(zrw); try { distributor.initialize(); } finally { verify(zrw); } }
@Test(expected = IllegalStateException.class) public void testBadAcl() throws Exception { ZooAuthenticationKeyDistributor distributor = new ZooAuthenticationKeyDistributor(zrw, baseNode); expect(zrw.exists(baseNode)).andReturn(true); expect(zrw.getACL(eq(baseNode), anyObject(Stat.class))).andReturn(Collections.singletonList( new ACL(ZooUtil.PRIVATE.get(0).getPerms(), new Id("digest", "somethingweird")))); replay(zrw); try { distributor.initialize(); } finally { verify(zrw); } }