mousio.etcd4j.requests.EtcdKeyGetRequest Java Examples

The following examples show how to use mousio.etcd4j.requests.EtcdKeyGetRequest. 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: EtcdUtil.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the content of the key recursively as a JsonObject
 * @param path root path (i.e. /path1/path2)
 * @param etcdClient EtcdClient
 * @return JsonNode
 * @throws IOException
 * @throws EtcdAuthenticationException
 * @throws TimeoutException
 * @throws EtcdException
 */
public static JsonNode getAsJson(String path, EtcdClient etcdClient)
        throws IOException, EtcdAuthenticationException, TimeoutException, EtcdException {

  EtcdKeyGetRequest etcdKeyGetRequest = etcdClient.get(path).recursive();
  EtcdKeysResponse dataTree = etcdKeyGetRequest.send().get();

  ObjectNode jNode = JsonNodeFactory.instance.objectNode();

  if (dataTree.getNode().getNodes().isEmpty()) {
    iterateOverNodes(jNode, dataTree.getNode());
  } else {
    for (EtcdNode node : dataTree.getNode().getNodes()) {
      iterateOverNodes(jNode, node);
    }
  }

  return dotNotationToStandardJson(jNode.at(path));
}
 
Example #2
Source File: EtcdConfigWatcherRegisterTest.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Before
@Test
public void shouldReadConfigs() throws Exception {
    final String group = "skywalking";
    final String testKey1 = "receiver-trace.default.slowDBAccessThreshold";
    final String testVal1 = "test";
    final String testKey2 = "testKey";
    final String testVal2 = "testVal";

    final EtcdServerSettings mockSettings = mock(EtcdServerSettings.class);
    when(mockSettings.getGroup()).thenReturn(group);
    mockStatic(EtcdUtils.class);

    List<URI> uris = mock(List.class);
    when(EtcdUtils.parse(any())).thenReturn(uris);

    final EtcdClient client = PowerMockito.mock(EtcdClient.class);
    whenNew(EtcdClient.class).withAnyArguments().thenReturn(client);

    String port = System.getProperty("etcd.port");
    URI uri = new URI("http://localhost:" + port);
    List<URI> urisArray = spy(ArrayList.class);
    urisArray.add(uri);
    URI[] array = urisArray.toArray(new URI[] {});
    when(uris.toArray(new URI[] {})).thenReturn(array);

    final EtcdConfigWatcherRegister mockRegister = spy(new EtcdConfigWatcherRegister(mockSettings));

    Whitebox.setInternalState(mockRegister, "client", client);
    Whitebox.setInternalState(mockRegister, "settings", mockSettings);

    final EtcdKeysResponse response = PowerMockito.mock(EtcdKeysResponse.class);
    final EtcdKeysResponse response1 = PowerMockito.mock(EtcdKeysResponse.class);

    final EtcdKeyGetRequest request = PowerMockito.mock(EtcdKeyGetRequest.class);

    when(client.get("/skywalking/receiver-trace.default.slowDBAccessThreshold")).thenReturn(request);
    when(request.waitForChange()).thenReturn(request);

    final EtcdResponsePromise<EtcdKeysResponse> promise = mock(EtcdResponsePromise.class);
    final ResponsePromise<EtcdKeysResponse> responseResponsePromise = mock(ResponsePromise.class);
    when(request.send()).thenReturn(promise);
    when(promise.get()).thenReturn(response);
    when(responseResponsePromise.get()).thenReturn(response);

    final EtcdKeysResponse.EtcdNode node = mock(EtcdKeysResponse.EtcdNode.class);
    when(response.getNode()).thenReturn(node);
    when(node.getKey()).thenReturn("/skywalking/receiver-trace.default.slowDBAccessThreshold");
    when(node.getValue()).thenReturn("test");

    final EtcdKeyGetRequest request1 = mock(EtcdKeyGetRequest.class);
    when(client.get("/skywalking/testKey")).thenReturn(request1);
    when(request1.waitForChange()).thenReturn(request1);
    final EtcdResponsePromise<EtcdKeysResponse> promise1 = mock(EtcdResponsePromise.class);
    final ResponsePromise<EtcdKeysResponse> responseResponsePromise1 = mock(ResponsePromise.class);
    when(request1.send()).thenReturn(promise1);
    when(promise1.get()).thenReturn(response1);
    when(responseResponsePromise1.get()).thenReturn(response1);

    final EtcdKeysResponse.EtcdNode node1 = mock(EtcdKeysResponse.EtcdNode.class);
    when(response1.getNode()).thenReturn(node1);
    when(node1.getKey()).thenReturn("/skywalking/testKey");
    when(node1.getValue()).thenReturn("testVal");

    final ConfigTable configTable = mockRegister.readConfig(Sets.newHashSet(testKey1, testKey2)).get();

    assertEquals(2, configTable.getItems().size());
    Map<String, String> kvs = new HashMap<>();
    for (ConfigTable.ConfigItem item : configTable.getItems()) {
        kvs.put(item.getName(), item.getValue());
    }
    assertEquals(testVal1, kvs.get(testKey1));
    assertEquals(testVal2, kvs.get(testKey2));
}