You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by hu...@apache.org on 2019/05/22 23:41:32 UTC

[helix] 07/14: Create util class to make it easier to make rest request

This is an automated email from the ASF dual-hosted git repository.

hulee pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/helix.git

commit 0bee2b1e07853cbb206634d4cd65d00c3191fc23
Author: ywang4 <yw...@linkedin.com>
AuthorDate: Fri Feb 22 10:12:20 2019 -0800

    Create util class to make it easier to make rest request
    
    RB=1573157
    G=helix-reviewers
    A=jxue,hulee
    
    Signed-off-by: Hunter Lee <hu...@linkedin.com>
---
 helix-rest/pom.xml                                 |   5 ++
 .../helix/rest/server/TestInstanceAccessor.java    |   5 +-
 .../rest/server/util/JerseyUriRequestBuilder.java  | 100 +++++++++++++++++++++
 3 files changed, 108 insertions(+), 2 deletions(-)

diff --git a/helix-rest/pom.xml b/helix-rest/pom.xml
index ee5bcfd..c3e9403 100644
--- a/helix-rest/pom.xml
+++ b/helix-rest/pom.xml
@@ -62,6 +62,11 @@ under the License.
       <artifactId>helix-core</artifactId>
     </dependency>
     <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-lang3</artifactId>
+      <version>3.8.1</version>
+    </dependency>
+    <dependency>
       <groupId>org.eclipse.jetty</groupId>
       <artifactId>jetty-server</artifactId>
       <version>9.1.0.RC0</version>
diff --git a/helix-rest/src/test/java/org/apache/helix/rest/server/TestInstanceAccessor.java b/helix-rest/src/test/java/org/apache/helix/rest/server/TestInstanceAccessor.java
index 86c52e1..172c848 100644
--- a/helix-rest/src/test/java/org/apache/helix/rest/server/TestInstanceAccessor.java
+++ b/helix-rest/src/test/java/org/apache/helix/rest/server/TestInstanceAccessor.java
@@ -40,6 +40,7 @@ import org.apache.helix.model.InstanceConfig;
 import org.apache.helix.model.Message;
 import org.apache.helix.rest.server.resources.AbstractResource;
 import org.apache.helix.rest.server.resources.helix.InstanceAccessor;
+import org.apache.helix.rest.server.util.JerseyUriRequestBuilder;
 import org.codehaus.jackson.JsonNode;
 import org.testng.Assert;
 import org.testng.annotations.Test;
@@ -67,8 +68,8 @@ public class TestInstanceAccessor extends AbstractTestClass {
     HelixDataAccessor helixDataAccessor = new ZKHelixDataAccessor(CLUSTER_NAME, _baseAccessor);
     helixDataAccessor.setProperty(helixDataAccessor.keyBuilder().message(INSTANCE_NAME, messageId), message);
 
-    String body =
-        get("clusters/" + CLUSTER_NAME + "/instances/" + INSTANCE_NAME + "/messages", null, Response.Status.OK.getStatusCode(), true);
+    String body = new JerseyUriRequestBuilder("clusters/{}/instances/{}/messages")
+        .format(CLUSTER_NAME, INSTANCE_NAME).get(this);
     JsonNode node = OBJECT_MAPPER.readTree(body);
     int newMessageCount =
         node.get(InstanceAccessor.InstanceProperties.total_message_count.name()).getIntValue();
diff --git a/helix-rest/src/test/java/org/apache/helix/rest/server/util/JerseyUriRequestBuilder.java b/helix-rest/src/test/java/org/apache/helix/rest/server/util/JerseyUriRequestBuilder.java
new file mode 100644
index 0000000..b1d91ef
--- /dev/null
+++ b/helix-rest/src/test/java/org/apache/helix/rest/server/util/JerseyUriRequestBuilder.java
@@ -0,0 +1,100 @@
+package org.apache.helix.rest.server.util;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.Response;
+
+import org.apache.commons.lang3.StringUtils;
+import org.glassfish.jersey.test.JerseyTestNg;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
+
+
+public class JerseyUriRequestBuilder {
+  private static final String PLACE_HOLDER = "{}";
+
+  private final StringBuilder _uriBuilder;
+  private final Map<String, String> _queryParams;
+  private final int _requiredParameters;
+  private final String _query;
+
+  public JerseyUriRequestBuilder(String uri) {
+    String[] uris = uri.split("\\?");
+    if (uris.length > 1) {
+      _queryParams = Splitter.on('&').trimResults().withKeyValueSeparator("=").split(uris[1]);
+      _query = uris[1];
+    } else {
+      _queryParams = new HashMap<>();
+      _query = "";
+    }
+    _uriBuilder = new StringBuilder(uris[0]);
+    _requiredParameters = StringUtils.countMatches(uris[0], PLACE_HOLDER);
+  }
+
+  public JerseyUriRequestBuilder format(String... parameters) {
+    Preconditions.checkArgument(_requiredParameters == parameters.length);
+    for (String param : parameters) {
+      int index = _uriBuilder.indexOf(PLACE_HOLDER);
+      _uriBuilder.replace(index, index + PLACE_HOLDER.length(), param);
+    }
+
+    return this;
+  }
+
+  private WebTarget buildWebTarget(JerseyTestNg.ContainerPerClassTest container) {
+    WebTarget webTarget = container.target(_uriBuilder.toString());
+    for (Map.Entry<String, String> entry : _queryParams.entrySet()) {
+      webTarget = webTarget.queryParam(entry.getKey(), entry.getValue());
+    }
+
+    return webTarget;
+  }
+
+  public String get(JerseyTestNg.ContainerPerClassTest container, int expectedReturnStatus, boolean expectBodyReturned) {
+    final Response response = buildWebTarget(container).request().get();
+
+    Assert.assertEquals(response.getStatus(), expectedReturnStatus);
+
+    // NOT_FOUND will throw text based html
+    if (expectedReturnStatus != Response.Status.NOT_FOUND.getStatusCode()) {
+      Assert.assertEquals(response.getMediaType().getType(), "application");
+    } else {
+      Assert.assertEquals(response.getMediaType().getType(), "text");
+    }
+
+    String body = response.readEntity(String.class);
+    if (expectBodyReturned) {
+      Assert.assertNotNull(body);
+    }
+
+    return body;
+  }
+
+  public String get(JerseyTestNg.ContainerPerClassTest container) {
+    final Response response = buildWebTarget(container).request().get();
+
+    return response.readEntity(String.class);
+  }
+
+  public String getPath() {
+    if (StringUtils.isEmpty(_query)) {
+      return _uriBuilder.toString();
+    } else {
+      return _uriBuilder.toString() + "?" + _query;
+    }
+  }
+
+  @Test
+  public void testUriBuilderGetPath() {
+    JerseyUriRequestBuilder uriBuilder = new JerseyUriRequestBuilder("clusters/{}/instances/{}/messages?stateModelDef=MasterSlave")
+        .format("TEST-CLUSTER", "instance1");
+    String path = uriBuilder.getPath();
+    Assert.assertEquals(path, "clusters/TEST-CLUSTER/instances/instance1/messages?stateModelDef=MasterSlave");
+    Assert.assertEquals(uriBuilder._queryParams.get("stateModelDef"), "MasterSlave");
+  }
+}