Search in sources :

Example 6 with ExistsBuilder

use of org.apache.curator.framework.api.ExistsBuilder in project heron by twitter.

the class CuratorStateManagerTest method testExistNode.

/**
   * 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());
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Stat(org.apache.zookeeper.data.Stat) ExistsBuilder(org.apache.curator.framework.api.ExistsBuilder) TimeUnit(java.util.concurrent.TimeUnit) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) Test(org.junit.Test)

Example 7 with ExistsBuilder

use of org.apache.curator.framework.api.ExistsBuilder in project atlas by apache.

the class ActiveInstanceStateTest method testDataIsUpdatedWithAtlasServerAddress.

@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")));
}
Also used : SetDataBuilder(org.apache.curator.framework.api.SetDataBuilder) Stat(org.apache.zookeeper.data.Stat) ExistsBuilder(org.apache.curator.framework.api.ExistsBuilder) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 8 with ExistsBuilder

use of org.apache.curator.framework.api.ExistsBuilder in project atlas by apache.

the class SetupStepsTest method setupSetupInProgressPathMocks.

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;
}
Also used : CreateBuilder(org.apache.curator.framework.api.CreateBuilder) ExistsBuilder(org.apache.curator.framework.api.ExistsBuilder) DeleteBuilder(org.apache.curator.framework.api.DeleteBuilder)

Example 9 with ExistsBuilder

use of org.apache.curator.framework.api.ExistsBuilder in project incubator-heron by apache.

the class CuratorStateManagerTest method testExistNode.

/**
 * 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());
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Stat(org.apache.zookeeper.data.Stat) ExistsBuilder(org.apache.curator.framework.api.ExistsBuilder) TimeUnit(java.util.concurrent.TimeUnit) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) Test(org.junit.Test)

Example 10 with ExistsBuilder

use of org.apache.curator.framework.api.ExistsBuilder in project heron by twitter.

the class CuratorStateManagerTest method testExistNode.

/**
 * 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());
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Stat(org.apache.zookeeper.data.Stat) ExistsBuilder(org.apache.curator.framework.api.ExistsBuilder) TimeUnit(java.util.concurrent.TimeUnit) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) Test(org.junit.Test)

Aggregations

ExistsBuilder (org.apache.curator.framework.api.ExistsBuilder)12 CreateBuilder (org.apache.curator.framework.api.CreateBuilder)6 SetDataBuilder (org.apache.curator.framework.api.SetDataBuilder)6 BeforeTest (org.testng.annotations.BeforeTest)6 Test (org.testng.annotations.Test)6 Stat (org.apache.zookeeper.data.Stat)5 CuratorFramework (org.apache.curator.framework.CuratorFramework)4 TimeUnit (java.util.concurrent.TimeUnit)3 Test (org.junit.Test)3 Matchers.anyBoolean (org.mockito.Matchers.anyBoolean)3 Matchers.anyString (org.mockito.Matchers.anyString)3 DeleteBuilder (org.apache.curator.framework.api.DeleteBuilder)2 ACL (org.apache.zookeeper.data.ACL)2 Id (org.apache.zookeeper.data.Id)2 GetChildrenBuilder (org.apache.curator.framework.api.GetChildrenBuilder)1 GetDataBuilder (org.apache.curator.framework.api.GetDataBuilder)1 Test (org.junit.jupiter.api.Test)1