org.apache.curator.framework.api.GetDataBuilder Java Examples
The following examples show how to use
org.apache.curator.framework.api.GetDataBuilder.
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: ZookeeperRuleConfigSourceTest.java From ratelimiter4j with Apache License 2.0 | 6 votes |
@Test(expectedExceptions = {ConfigurationResolveException.class}) public void testLoad_withValidData() throws Exception { CuratorFramework client = Mockito.mock(CuratorFramework.class); doNothing().when(client).start(); when(client.blockUntilConnected(anyInt(), any())).thenReturn(true); String ruleStr = "invalid string"; GetDataBuilder dataBuilder = new GetDataBuilderImpl(null, null, null, null, false) { @Override public byte[] forPath(String path) throws Exception { return ruleStr.getBytes("UTF-8"); } }; when(client.getData()).thenReturn(dataBuilder); RuleConfigSource source = new ZookeeperRuleConfigSource(client, new YamlRuleConfigParser()); source.load(); }
Example #2
Source File: ZookeeperConfigGroup.java From config-toolkit with Apache License 2.0 | 6 votes |
private Tuple<String, String> loadKey(final String nodePath) throws Exception { final String nodeName = ZKPaths.getNodeFromPath(nodePath); final Set<String> keysSpecified = configProfile.getKeysSpecified(); switch (configProfile.getKeyLoadingMode()) { case INCLUDE: if (keysSpecified == null || !keysSpecified.contains(nodeName)) { return null; } break; case EXCLUDE: if (keysSpecified.contains(nodeName)) { return null; } break; case ALL: break; default: break; } final GetDataBuilder data = client.getData(); final String value = new String(data.watched().forPath(nodePath), "UTF-8"); return new Tuple<>(nodeName, value); }
Example #3
Source File: NodeService.java From config-toolkit with Apache License 2.0 | 6 votes |
@Override public List<PropertyItem> findProperties(String node) { LOGGER.debug("Find properties in node: [{}]", node); List<PropertyItem> properties = Lists.newArrayList(); try { Stat stat = client.checkExists().forPath(node); if (stat != null) { GetChildrenBuilder childrenBuilder = client.getChildren(); List<String> children = childrenBuilder.forPath(node); GetDataBuilder dataBuilder = client.getData(); if (children != null) { for (String child : children) { String propPath = ZKPaths.makePath(node, child); PropertyItem item = new PropertyItem(child, new String(dataBuilder.forPath(propPath), Charsets.UTF_8)); properties.add(item); } } } } catch (Exception e) { LOGGER.error(e.getMessage(), e); } return properties; }
Example #4
Source File: ConfigurationTest.java From metron with Apache License 2.0 | 6 votes |
@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 #5
Source File: ActiveInstanceStateTest.java From incubator-atlas with Apache License 2.0 | 6 votes |
@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()); }
Example #6
Source File: ActiveInstanceStateTest.java From incubator-atlas with Apache License 2.0 | 6 votes |
@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); }
Example #7
Source File: ActiveInstanceStateTest.java From atlas with Apache License 2.0 | 6 votes |
@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()); }
Example #8
Source File: ActiveInstanceStateTest.java From atlas with Apache License 2.0 | 6 votes |
@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); }
Example #9
Source File: ZookeeperRuleConfigSourceTest.java From ratelimiter4j with Apache License 2.0 | 6 votes |
public void testLoad_withEmptyData() throws Exception { CuratorFramework client = Mockito.mock(CuratorFramework.class); doNothing().when(client).start(); when(client.blockUntilConnected(anyInt(), any())).thenReturn(true); String ruleStr = ""; GetDataBuilder dataBuilder = new GetDataBuilderImpl(null, null, null, null, false) { @Override public byte[] forPath(String path) throws Exception { return ruleStr.getBytes("UTF-8"); } }; when(client.getData()).thenReturn(dataBuilder); RuleConfigSource source = new ZookeeperRuleConfigSource(client, new YamlRuleConfigParser()); UniformRuleConfigMapping mapping = source.load(); assertNull(mapping); mapping = source.load(); assertNull(mapping); }
Example #10
Source File: ZkUtils.java From GoPush with GNU General Public License v2.0 | 5 votes |
/** * 读取指定节点的子菜单的值 * * @param path * @return */ public Map<String, String> readTargetChildsData(String path) { if (!ObjectUtils.allNotNull(zkClient, path)) { return null; } Map<String, String> map = null; try { Stat stat = exists(path); if (stat != null) { List<String> childrens = zkClient.getChildren().forPath(path); GetDataBuilder dataBuilder = zkClient.getData(); if (childrens != null) { map = childrens.stream().collect(Collectors.toMap(Function.identity(), (child) -> { try { return new String(dataBuilder.forPath(ZKPaths.makePath(path, child)), Charsets.UTF_8); } catch (Exception e1) { return null; } })); } } } catch (Exception e) { log.error("get target childs data fail!, path:{} , error:{}", path, e); } return map; }
Example #11
Source File: ZkSubscriptionImpl.java From nakadi with MIT License | 5 votes |
@Override protected byte[] query(final boolean setListener) throws NakadiRuntimeException { final GetDataBuilder builder = curatorFramework.getData(); if (setListener) { builder.usingWatcher(this); } try { return builder.forPath(key); } catch (final Exception ex) { throw new NakadiRuntimeException(ex); } }
Example #12
Source File: ParserFunctionsTest.java From metron with Apache License 2.0 | 5 votes |
/** * Create a mock Zookeeper client that returns a value for a given path. * * @param path The path within Zookeeper that will be requested. * @param value The value to return when the path is requested. * @return The mock Zookeeper client. */ private CuratorFramework zkClientForPath(String path, byte[] value) throws Exception { GetDataBuilder getDataBuilder = mock(GetDataBuilder.class); when(getDataBuilder.forPath(path)).thenReturn(value); CuratorFramework zkClient = mock(CuratorFramework.class); when(zkClient.getData()).thenReturn(getDataBuilder); return zkClient; }
Example #13
Source File: ParserFunctionsTest.java From metron with Apache License 2.0 | 5 votes |
/** * Create a mock Zookeeper client that will indicate the given path does not exist. * * @param path The path that will 'not exist'. * @return The mock Zookeeper client. */ private CuratorFramework zkClientMissingPath(String path) throws Exception { GetDataBuilder getDataBuilder = mock(GetDataBuilder.class); when(getDataBuilder.forPath(path)).thenThrow(new KeeperException.NoNodeException(path)); CuratorFramework zkClient = mock(CuratorFramework.class); when(zkClient.getData()).thenReturn(getDataBuilder); return zkClient; }
Example #14
Source File: GlobalConfigServiceImplTest.java From metron with Apache License 2.0 | 5 votes |
@Test public void getShouldWrapNonNoNodeExceptionInRestException() throws Exception { GetDataBuilder getDataBuilder = mock(GetDataBuilder.class); when(getDataBuilder.forPath(ConfigurationType.GLOBAL.getZookeeperRoot())).thenThrow(Exception.class); when(curatorFramework.getData()).thenReturn(getDataBuilder); assertThrows(RestException.class, () -> globalConfigService.get()); }
Example #15
Source File: ZookeeperRuleConfigSourceTest.java From ratelimiter4j with Apache License 2.0 | 5 votes |
public void testLoad() throws Exception { CuratorFramework client = Mockito.mock(CuratorFramework.class); doNothing().when(client).start(); when(client.blockUntilConnected(anyInt(), any())).thenReturn(true); String ruleStr = "configs:\n" + "- appId: app-1\n" + " limits:\n" + " - unit: 50\n" + " limit: 100\n" + " api: v1/user\n" + " - unit: 20\n" + " limit: 50\n" + " api: v1/order\n" + "- appId: app-2\n" + " limits:\n" + " - limit: 50\n" + " api: v1/user\n" + " - unit: 30\n" + " limit: 50\n" + " api: v1/order\n"; GetDataBuilder dataBuilder = new GetDataBuilderImpl(null, null, null, null, false) { @Override public byte[] forPath(String path) throws Exception { return ruleStr.getBytes("UTF-8"); } }; when(client.getData()).thenReturn(dataBuilder); RuleConfigSource source = new ZookeeperRuleConfigSource(client, new YamlRuleConfigParser()); UniformRuleConfigMapping mapping = source.load(); assertNotNull(mapping); assertEquals(mapping.getConfigs().size(), 2); assertEquals(mapping.getConfigs().get(0).getAppId(), "app-1"); }
Example #16
Source File: MockCurator.java From vespa with Apache License 2.0 | 4 votes |
@Override public GetDataBuilder getData() { return new MockGetDataBuilder(); }
Example #17
Source File: CuratorStateManagerTest.java From incubator-heron with Apache License 2.0 | 4 votes |
/** * 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)); }
Example #18
Source File: CuratorManager.java From Singularity with Apache License 2.0 | 4 votes |
private <T> Optional<T> getData( String path, Optional<Stat> stat, Transcoder<T> transcoder, Optional<ZkCache<T>> zkCache, Optional<Boolean> shouldCheckExists ) { if (!stat.isPresent() && zkCache.isPresent()) { Optional<T> cachedValue = zkCache.get().get(path); if ( cachedValue.isPresent() && ( !shouldCheckExists.isPresent() || (shouldCheckExists.get().booleanValue() && checkExists(path).isPresent()) ) ) { return cachedValue; } } final long start = System.currentTimeMillis(); int bytes = 0; try { GetDataBuilder bldr = curator.getData(); if (stat.isPresent()) { bldr.storingStatIn(stat.get()); } byte[] data = bldr.forPath(path); if (data == null || data.length == 0) { LOG.trace("Empty data found for path {}", path); return Optional.empty(); } bytes = data.length; final T object = transcoder.fromBytes(data); if (zkCache.isPresent()) { zkCache.get().set(path, object); } return Optional.of(object); } catch (NoNodeException nne) { LOG.trace("No node found for path {}", path); return Optional.empty(); } catch (Throwable t) { throw new RuntimeException(t); } finally { log(OperationType.GET, Optional.empty(), Optional.<Integer>of(bytes), start, path); } }