org.apache.curator.framework.api.ExistsBuilder Java Examples

The following examples show how to use org.apache.curator.framework.api.ExistsBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ActiveInstanceStateTest.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void testDataIsUpdatedWithAtlasServerAddress() throws Exception {
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX +"id1")).thenReturn(HOST_PORT);
    when(configuration.getString(
            HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
            thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);


    when(curatorFactory.clientInstance()).thenReturn(curatorFramework);
    ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
    when(curatorFramework.checkExists()).thenReturn(existsBuilder);
    when(existsBuilder.forPath(getPath())).thenReturn(new Stat());

    SetDataBuilder setDataBuilder = mock(SetDataBuilder.class);
    when(curatorFramework.setData()).thenReturn(setDataBuilder);

    ActiveInstanceState activeInstanceState = new ActiveInstanceState(configuration, curatorFactory);
    activeInstanceState.update("id1");

    verify(setDataBuilder).forPath(
            getPath(),
            SERVER_ADDRESS.getBytes(Charset.forName("UTF-8")));
}
 
Example #2
Source File: ActiveInstanceStateTest.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void testDataIsUpdatedWithAtlasServerAddress() throws Exception {
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX +"id1")).thenReturn(HOST_PORT);
    when(configuration.getString(
            HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
            thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);


    when(curatorFactory.clientInstance()).thenReturn(curatorFramework);
    ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
    when(curatorFramework.checkExists()).thenReturn(existsBuilder);
    when(existsBuilder.forPath(getPath())).thenReturn(new Stat());

    SetDataBuilder setDataBuilder = mock(SetDataBuilder.class);
    when(curatorFramework.setData()).thenReturn(setDataBuilder);

    ActiveInstanceState activeInstanceState = new ActiveInstanceState(configuration, curatorFactory);
    activeInstanceState.update("id1");

    verify(setDataBuilder).forPath(
            getPath(),
            SERVER_ADDRESS.getBytes(Charset.forName("UTF-8")));
}
 
Example #3
Source File: ConfigurationTest.java    From metron with Apache License 2.0 6 votes vote down vote up
@Test
public void testCanReadFromZookeeper() throws Exception {

    CuratorFramework curatorFramework = mock(CuratorFramework.class);
    ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
    GetDataBuilder getDataBuilder = mock(GetDataBuilder.class);
    GetChildrenBuilder getChildrenBuilder = mock(GetChildrenBuilder.class);

    when(getDataBuilder.forPath(ConfigurationType.GLOBAL.getZookeeperRoot())).thenReturn(mockGlobalData());
    when(curatorFramework.checkExists()).thenReturn(existsBuilder);
    when(curatorFramework.getData()).thenReturn(getDataBuilder);
    when(curatorFramework.getChildren()).thenReturn(getChildrenBuilder);
    when(getChildrenBuilder.forPath(any())).thenReturn(Collections.emptyList());

    Configuration configuration = new Configuration(Paths.get("foo"));
    configuration.curatorFramework = curatorFramework;
    configuration.update();

    checkResult(configuration);
}
 
Example #4
Source File: SetupStepsTest.java    From atlas with Apache License 2.0 5 votes vote down vote up
private Pair<CreateBuilder, DeleteBuilder> setupSetupInProgressPathMocks(List<ACL> acls, Stat stat) throws Exception {
    when(curatorFactory.clientInstance()).thenReturn(client);
    CreateBuilder createBuilder = mock(CreateBuilder.class);
    when(createBuilder.withACL(acls)).thenReturn(createBuilder);
    when(client.create()).thenReturn(createBuilder);
    DeleteBuilder deleteBuilder = mock(DeleteBuilder.class);
    when(client.delete()).thenReturn(deleteBuilder);
    Pair<CreateBuilder, DeleteBuilder> pair = Pair.of(createBuilder, deleteBuilder);
    ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
    when(client.checkExists()).thenReturn(existsBuilder);
    when(existsBuilder.forPath(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT+SetupSteps.SETUP_IN_PROGRESS_NODE)).
            thenReturn(stat);
    return pair;
}
 
Example #5
Source File: ActiveInstanceStateTest.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Test
public void testSharedPathIsCreatedIfNotExists() throws Exception {

    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX +"id1")).thenReturn(HOST_PORT);
    when(configuration.getString(
            HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
            thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);

    when(curatorFactory.clientInstance()).thenReturn(curatorFramework);

    ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
    when(curatorFramework.checkExists()).thenReturn(existsBuilder);
    when(existsBuilder.forPath(getPath())).thenReturn(null);

    CreateBuilder createBuilder = mock(CreateBuilder.class);
    when(curatorFramework.create()).thenReturn(createBuilder);
    when(createBuilder.withMode(CreateMode.EPHEMERAL)).thenReturn(createBuilder);
    when(createBuilder.withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)).thenReturn(createBuilder);

    SetDataBuilder setDataBuilder = mock(SetDataBuilder.class);
    when(curatorFramework.setData()).thenReturn(setDataBuilder);

    ActiveInstanceState activeInstanceState = new ActiveInstanceState(configuration, curatorFactory);
    activeInstanceState.update("id1");

    verify(createBuilder).forPath(getPath());
}
 
Example #6
Source File: ActiveInstanceStateTest.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Test
   public void testSharedPathIsCreatedWithRightACLIfNotExists() throws Exception {

       when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX +"id1")).thenReturn(HOST_PORT);
       when(configuration.getString(HAConfiguration.HA_ZOOKEEPER_ACL)).thenReturn("sasl:[email protected]");
       when(configuration.getString(
               HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
               thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);


       when(curatorFactory.clientInstance()).thenReturn(curatorFramework);

       ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
       when(curatorFramework.checkExists()).thenReturn(existsBuilder);
       when(existsBuilder.forPath(getPath())).thenReturn(null);

       CreateBuilder createBuilder = mock(CreateBuilder.class);
       when(curatorFramework.create()).thenReturn(createBuilder);
       when(createBuilder.withMode(CreateMode.EPHEMERAL)).thenReturn(createBuilder);
ACL expectedAcl = new ACL(ZooDefs.Perms.ALL, new Id("sasl", "[email protected]"));
ACL expectedAcl1 = new ACL(ZooDefs.Perms.READ, new Id("world", "anyone"));
       when(createBuilder.
               withACL(Arrays.asList(new ACL[]{expectedAcl,expectedAcl1}))).thenReturn(createBuilder);


       SetDataBuilder setDataBuilder = mock(SetDataBuilder.class);
       when(curatorFramework.setData()).thenReturn(setDataBuilder);

       ActiveInstanceState activeInstanceState = new ActiveInstanceState(configuration, curatorFactory);
       activeInstanceState.update("id1");

       verify(createBuilder).forPath(getPath());
   }
 
Example #7
Source File: CuratorHelperTest.java    From storm-dynamic-spout with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Tests that if we attempt to delete a node that doesnt actually exist
 * just silently returns.
 *
 * To simulate a race condition we do this using mocks.
 */
@Test
public void testDeleteNodeIfNoChildren_withNodeThatDoesntExist() throws Exception {
    final String basePath = "/testDeleteNodeIfNoChildren_withNodeThatDoesntExist";

    final CuratorFramework mockCurator = mock(CuratorFramework.class);

    // Exists builder should return true saying our basePath exists.
    final ExistsBuilder mockExistsBuilder = mock(ExistsBuilder.class);
    when(mockExistsBuilder.forPath(eq(basePath))).thenReturn(new Stat());
    when(mockCurator.checkExists()).thenReturn(mockExistsBuilder);

    // When we look for children, make sure it returns an empty list.
    final GetChildrenBuilder mockGetChildrenBuilder = mock(GetChildrenBuilder.class);
    when(mockGetChildrenBuilder.forPath(eq(basePath))).thenReturn(new ArrayList<>());
    when(mockCurator.getChildren()).thenReturn(mockGetChildrenBuilder);

    // When we go to delete the actual node, we toss a no-node exception.
    // This effectively simulates a race condition between checking if the node exists (our mock above says yes)
    // and it being removed before we call delete on it.
    final DeleteBuilder mockDeleteBuilder = mock(DeleteBuilder.class);
    when(mockDeleteBuilder.forPath(eq(basePath))).thenThrow(new KeeperException.NoNodeException());
    when(mockCurator.delete()).thenReturn(mockDeleteBuilder);

    // Now create our helper
    final CuratorHelper curatorHelper = new CuratorHelper(mockCurator, new HashMap<>());

    // Call our method
    curatorHelper.deleteNodeIfNoChildren(basePath);
}
 
Example #8
Source File: CuratorStateManagerTest.java    From incubator-heron with Apache License 2.0 5 votes vote down vote up
/**
 * Test nodeExists method
 * @throws Exception
 */
@Test
public void testExistNode() throws Exception {
  CuratorStateManager spyStateManager = spy(new CuratorStateManager());
  CuratorFramework mockClient = mock(CuratorFramework.class);
  ExistsBuilder mockExistsBuilder = mock(ExistsBuilder.class);

  final String correctPath = "/correct/path";
  final String wrongPath = "/wrong/path";

  doReturn(mockClient)
      .when(spyStateManager).getCuratorClient();
  doReturn(true)
      .when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class));
  doReturn(mockExistsBuilder)
      .when(mockClient).checkExists();
  doReturn(new Stat())
      .when(mockExistsBuilder).forPath(correctPath);
  doReturn(null)
      .when(mockExistsBuilder).forPath(wrongPath);

  spyStateManager.initialize(config);

  // Verify the result is true when path is correct
  ListenableFuture<Boolean> result1 = spyStateManager.nodeExists(correctPath);
  verify(mockExistsBuilder).forPath(correctPath);
  assertTrue(result1.get());

  // Verify the result is false when path is wrong
  ListenableFuture<Boolean> result2 = spyStateManager.nodeExists(wrongPath);
  verify(mockExistsBuilder).forPath(wrongPath);
  assertFalse(result2.get());
}
 
Example #9
Source File: DistributedLockServiceCuratorImplTest.java    From micro-server with Apache License 2.0 5 votes vote down vote up
@Test
public void createNonExisting() throws Exception {
	CuratorFramework client = mock(CuratorFramework.class);
	ExistsBuilder builder = mock(ExistsBuilder.class);
	CreateBuilder createBuilder = mock(CreateBuilder.class);
	
	
	ProtectACLCreateModeStatPathAndBytesable<String> protector = mock(ProtectACLCreateModeStatPathAndBytesable.class);
	when(builder.forPath(anyString())).thenReturn(null);
	when(client.checkExists()).thenReturn(builder);
	when(client.create()).thenReturn(createBuilder);
	when(createBuilder.creatingParentContainersIfNeeded()).thenReturn((ProtectACLCreateModeStatPathAndBytesable<String>)protector);
	new DistributedLockServiceCuratorImpl(client, "/", 0);
	verify(protector).forPath(anyString(), anyObject());
}
 
Example #10
Source File: DistributedLockServiceCuratorImplTest.java    From micro-server with Apache License 2.0 5 votes vote down vote up
@Test
public void createNonExisting2() throws Exception {
	CuratorFramework client = mock(CuratorFramework.class);
	ExistsBuilder builder = mock(ExistsBuilder.class);
	CreateBuilder createBuilder = mock(CreateBuilder.class);
	
	ProtectACLCreateModeStatPathAndBytesable<String> protector = mock(ProtectACLCreateModeStatPathAndBytesable.class);
	when(builder.forPath(anyString())).thenReturn(new Stat());
	when(client.checkExists()).thenReturn(builder);
	when(client.create()).thenReturn(createBuilder);
	when(createBuilder.creatingParentContainersIfNeeded()).thenReturn(protector);
	new DistributedLockServiceCuratorImpl(client, "/", 0);
	verify(protector, times(0)).forPath(anyString(), anyObject());
}
 
Example #11
Source File: SetupStepsTest.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
private Pair<CreateBuilder, DeleteBuilder> setupSetupInProgressPathMocks(List<ACL> acls, Stat stat) throws Exception {
    when(curatorFactory.clientInstance()).thenReturn(client);
    CreateBuilder createBuilder = mock(CreateBuilder.class);
    when(createBuilder.withACL(acls)).thenReturn(createBuilder);
    when(client.create()).thenReturn(createBuilder);
    DeleteBuilder deleteBuilder = mock(DeleteBuilder.class);
    when(client.delete()).thenReturn(deleteBuilder);
    Pair<CreateBuilder, DeleteBuilder> pair = Pair.of(createBuilder, deleteBuilder);
    ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
    when(client.checkExists()).thenReturn(existsBuilder);
    when(existsBuilder.forPath(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT+SetupSteps.SETUP_IN_PROGRESS_NODE)).
            thenReturn(stat);
    return pair;
}
 
Example #12
Source File: ActiveInstanceStateTest.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
@Test
public void testSharedPathIsCreatedIfNotExists() throws Exception {

    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX +"id1")).thenReturn(HOST_PORT);
    when(configuration.getString(
            HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
            thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);

    when(curatorFactory.clientInstance()).thenReturn(curatorFramework);

    ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
    when(curatorFramework.checkExists()).thenReturn(existsBuilder);
    when(existsBuilder.forPath(getPath())).thenReturn(null);

    CreateBuilder createBuilder = mock(CreateBuilder.class);
    when(curatorFramework.create()).thenReturn(createBuilder);
    when(createBuilder.withMode(CreateMode.EPHEMERAL)).thenReturn(createBuilder);
    when(createBuilder.withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)).thenReturn(createBuilder);

    SetDataBuilder setDataBuilder = mock(SetDataBuilder.class);
    when(curatorFramework.setData()).thenReturn(setDataBuilder);

    ActiveInstanceState activeInstanceState = new ActiveInstanceState(configuration, curatorFactory);
    activeInstanceState.update("id1");

    verify(createBuilder).forPath(getPath());
}
 
Example #13
Source File: ActiveInstanceStateTest.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
@Test
public void testSharedPathIsCreatedWithRightACLIfNotExists() throws Exception {

    when(configuration.getString(HAConfiguration.ATLAS_SERVER_ADDRESS_PREFIX +"id1")).thenReturn(HOST_PORT);
    when(configuration.getString(HAConfiguration.HA_ZOOKEEPER_ACL)).thenReturn("sasl:[email protected]");
    when(configuration.getString(
            HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
            thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);


    when(curatorFactory.clientInstance()).thenReturn(curatorFramework);

    ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
    when(curatorFramework.checkExists()).thenReturn(existsBuilder);
    when(existsBuilder.forPath(getPath())).thenReturn(null);

    CreateBuilder createBuilder = mock(CreateBuilder.class);
    when(curatorFramework.create()).thenReturn(createBuilder);
    when(createBuilder.withMode(CreateMode.EPHEMERAL)).thenReturn(createBuilder);
    ACL expectedAcl = new ACL(ZooDefs.Perms.ALL, new Id("sasl", "[email protected]"));
    when(createBuilder.
            withACL(Arrays.asList(new ACL[]{expectedAcl}))).thenReturn(createBuilder);

    SetDataBuilder setDataBuilder = mock(SetDataBuilder.class);
    when(curatorFramework.setData()).thenReturn(setDataBuilder);

    ActiveInstanceState activeInstanceState = new ActiveInstanceState(configuration, curatorFactory);
    activeInstanceState.update("id1");

    verify(createBuilder).forPath(getPath());
}
 
Example #14
Source File: ZookeeperInvoker.java    From Thunder with Apache License 2.0 5 votes vote down vote up
public Stat getPathStat(String path) throws Exception {
    validateStartedStatus();
    PathUtils.validatePath(path);

    ExistsBuilder builder = client.checkExists();
    if (builder == null) {
        return null;
    }

    Stat stat = builder.forPath(path);

    return stat;
}
 
Example #15
Source File: DynamicPropUtils.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
public boolean sync(String projectName) {

        if (uninitialized()) return false;

        // (local + non_local) * (remote + non_monitor)
        if (projectName == null) {
            LOGGER.error("Sync failed! Cause projectName cannot be null!");
            return false;
        }
        ExistsBuilder existsBuilder = curatorFramework.checkExists();
        Stat stat;
        try {
            stat = existsBuilder.forPath(ZNODE_PREFIX.concat(projectName));
        } catch (Exception e) {
            LOGGER.error("Sync failed!", e);
            return false;
        }
        Prop localProp = PROJECT_PROPERTIES.get(projectName);
        boolean isRemote, isLocal;
        if (stat == null) {
            LOGGER.debug("Configuration about project[{}] is not on remote.", projectName);
            isRemote = false;
        } else {
            LOGGER.debug("Configuration about project[{}] is on remote.", projectName);
            isRemote = true;
        }
        if (localProp == null) {
            LOGGER.debug("Configuration about project[{}] is not on local.", projectName);
            isLocal = false;
        } else {
            LOGGER.debug("Configuration about project[{}] is on local.", projectName);
            isLocal = true;
        }
        boolean isSynced = internalSync(projectName, localProp, isRemote, isLocal);
        if (isSynced) LOGGER.debug("Sync success!");
        return isSynced;
    }
 
Example #16
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public ExistsBuilder checkExists() {
    return new MockExistsBuilder();
}