Search in sources :

Example 1 with GetDataBuilder

use of org.apache.curator.framework.api.GetDataBuilder in project incubator-atlas by apache.

the class ActiveInstanceStateTest method testShouldReturnActiveServerAddress.

@Test
public void testShouldReturnActiveServerAddress() throws Exception {
    when(curatorFactory.clientInstance()).thenReturn(curatorFramework);
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);
    GetDataBuilder getDataBuilder = mock(GetDataBuilder.class);
    when(curatorFramework.getData()).thenReturn(getDataBuilder);
    when(getDataBuilder.forPath(getPath())).thenReturn(SERVER_ADDRESS.getBytes(Charset.forName("UTF-8")));
    ActiveInstanceState activeInstanceState = new ActiveInstanceState(configuration, curatorFactory);
    String actualServerAddress = activeInstanceState.getActiveServerAddress();
    assertEquals(SERVER_ADDRESS, actualServerAddress);
}
Also used : GetDataBuilder(org.apache.curator.framework.api.GetDataBuilder) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 2 with GetDataBuilder

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

the class CuratorStateManagerTest method testGetNodeData.

/**
   * Test getNodeData method
   * @throws Exception
   */
@Test
public void testGetNodeData() throws Exception {
    CuratorStateManager spyStateManager = spy(new CuratorStateManager());
    final CuratorFramework mockClient = mock(CuratorFramework.class);
    GetDataBuilder mockGetBuilder = mock(GetDataBuilder.class);
    // Mockito doesn't support mock type-parametrized class, thus suppress the warning
    @SuppressWarnings("rawtypes") BackgroundPathable mockBackPathable = mock(BackgroundPathable.class);
    final CuratorEvent mockEvent = mock(CuratorEvent.class);
    Message.Builder mockBuilder = mock(Message.Builder.class);
    Message mockMessage = mock(Message.class);
    final byte[] data = "wy_1989".getBytes();
    doReturn(mockMessage).when(mockBuilder).build();
    doReturn(data).when(mockEvent).getData();
    doReturn(PATH).when(mockEvent).getPath();
    doReturn(mockClient).when(spyStateManager).getCuratorClient();
    doReturn(true).when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class));
    doReturn(mockGetBuilder).when(mockClient).getData();
    doReturn(mockBackPathable).when(mockGetBuilder).usingWatcher(any(Watcher.class));
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] objests = invocationOnMock.getArguments();
            // the first object is the BackgroundCallback
            ((BackgroundCallback) objests[0]).processResult(mockClient, mockEvent);
            return null;
        }
    }).when(mockBackPathable).inBackground(any(BackgroundCallback.class));
    spyStateManager.initialize(config);
    // Verify the data on node is fetched correctly
    ListenableFuture<Message> result = spyStateManager.getNodeData(null, PATH, mockBuilder);
    assertTrue(result.get().equals(mockMessage));
}
Also used : Message(com.google.protobuf.Message) Watcher(org.apache.zookeeper.Watcher) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) GetDataBuilder(org.apache.curator.framework.api.GetDataBuilder) CuratorFramework(org.apache.curator.framework.CuratorFramework) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TimeUnit(java.util.concurrent.TimeUnit) BackgroundPathable(org.apache.curator.framework.api.BackgroundPathable) Test(org.junit.Test)

Example 3 with GetDataBuilder

use of org.apache.curator.framework.api.GetDataBuilder in project incubator-atlas by apache.

the class ActiveInstanceStateTest method testShouldHandleExceptionsInFetchingServerAddress.

@Test
public void testShouldHandleExceptionsInFetchingServerAddress() throws Exception {
    when(curatorFactory.clientInstance()).thenReturn(curatorFramework);
    when(configuration.getString(HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);
    GetDataBuilder getDataBuilder = mock(GetDataBuilder.class);
    when(curatorFramework.getData()).thenReturn(getDataBuilder);
    when(getDataBuilder.forPath(getPath())).thenThrow(new Exception());
    ActiveInstanceState activeInstanceState = new ActiveInstanceState(configuration, curatorFactory);
    assertNull(activeInstanceState.getActiveServerAddress());
}
Also used : GetDataBuilder(org.apache.curator.framework.api.GetDataBuilder) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

GetDataBuilder (org.apache.curator.framework.api.GetDataBuilder)3 BeforeTest (org.testng.annotations.BeforeTest)2 Test (org.testng.annotations.Test)2 Message (com.google.protobuf.Message)1 TimeUnit (java.util.concurrent.TimeUnit)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)1 BackgroundPathable (org.apache.curator.framework.api.BackgroundPathable)1 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)1 Watcher (org.apache.zookeeper.Watcher)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1