ActiveInstanceState { public String getActiveServerAddress() { CuratorFramework client = curatorFactory.clientInstance(); String serverAddress = null; try { HAConfiguration.ZookeeperProperties zookeeperProperties = HAConfiguration.getZookeeperProperties(configuration); byte[] bytes = client.getData().forPath(getZnodePath(zookeeperProperties)); serverAddress = new String(bytes, Charset.forName("UTF-8")); } catch (Exception e) { LOG.error("Error getting active server address", e); } return serverAddress; } @Inject  ActiveInstanceState(CuratorFactory curatorFactory);  ActiveInstanceState(Configuration configuration, CuratorFactory curatorFactory); void update(String serverId); String getActiveServerAddress(); static final String APACHE_ATLAS_ACTIVE_SERVER_INFO; }
@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); }
@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()); }