@Test
  public void testServiceNodeIsDeletedWhenRegistryIsClosed() throws Exception {
    ZooKeeperPersistentEphemeralNode node = mock(ZooKeeperPersistentEphemeralNode.class);
    when(_nodeFactory.create(anyString(), any(byte[].class))).thenReturn(node);

    _registry.register(FOO);

    _registry.close();

    verify(node).close(anyLong(), any(TimeUnit.class));
  }
  @Test
  public void testDuplicateRegister() throws Exception {
    ZooKeeperPersistentEphemeralNode firstNode = mock(ZooKeeperPersistentEphemeralNode.class);
    ZooKeeperPersistentEphemeralNode secondNode = mock(ZooKeeperPersistentEphemeralNode.class);
    when(_nodeFactory.create(anyString(), any(byte[].class))).thenReturn(firstNode, secondNode);

    _registry.register(FOO);
    _registry.register(FOO);

    verify(_nodeFactory, times(2)).create(eq(FOO_PATH), Matchers.<byte[]>any());
    verify(firstNode).close(anyLong(), any(TimeUnit.class));
  }
  @Test
  public void testRegister() throws Exception {
    ZooKeeperPersistentEphemeralNode node = mock(ZooKeeperPersistentEphemeralNode.class);
    when(_nodeFactory.create(anyString(), any(byte[].class))).thenReturn(node);

    _registry.register(FOO);

    ArgumentCaptor<byte[]> dataCaptor = ArgumentCaptor.forClass(byte[].class);

    verify(_nodeFactory).create(eq(FOO_PATH), dataCaptor.capture());
    assertEquals(FOO, ServiceEndPointJsonCodec.fromJson(new String(dataCaptor.getValue())));
    verify(node, never()).close(anyLong(), any(TimeUnit.class));
  }
 @Before
 public void setup() {
   _nodeFactory = mock(ZooKeeperServiceRegistry.NodeFactory.class);
   when(_nodeFactory.create(anyString(), any(byte[].class)))
       .thenAnswer(
           new Answer<ZooKeeperPersistentEphemeralNode>() {
             @Override
             public ZooKeeperPersistentEphemeralNode answer(InvocationOnMock invocation)
                 throws Throwable {
               return mock(ZooKeeperPersistentEphemeralNode.class);
             }
           });
   _registry = new ZooKeeperServiceRegistry(_nodeFactory);
 }