sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject sqoop git commit: SQOOP-2469: Sqoop2: Custom HTTP requests test base class
Date Thu, 13 Aug 2015 20:55:33 GMT
Repository: sqoop
Updated Branches:
  refs/heads/sqoop2 194a4dbaf -> 6f5d42893


SQOOP-2469: Sqoop2: Custom HTTP requests test base class

Provide base integration test that will run custom HTTP requests and validating the server
response.

(Jarek Jarcec Cecho via Abraham Elmahrek)


Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/6f5d4289
Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/6f5d4289
Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/6f5d4289

Branch: refs/heads/sqoop2
Commit: 6f5d4289316b86758c37d26f24b4d1ec69bac66f
Parents: 194a4db
Author: Abraham Elmahrek <abe@apache.org>
Authored: Thu Aug 13 13:51:51 2015 -0700
Committer: Abraham Elmahrek <abe@apache.org>
Committed: Thu Aug 13 13:55:07 2015 -0700

----------------------------------------------------------------------
 .../server/InvalidRESTCallsTest.java            | 145 +++++++++++++++++++
 1 file changed, 145 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/6f5d4289/test/src/test/java/org/apache/sqoop/integration/server/InvalidRESTCallsTest.java
----------------------------------------------------------------------
diff --git a/test/src/test/java/org/apache/sqoop/integration/server/InvalidRESTCallsTest.java
b/test/src/test/java/org/apache/sqoop/integration/server/InvalidRESTCallsTest.java
new file mode 100644
index 0000000..f1ef336
--- /dev/null
+++ b/test/src/test/java/org/apache/sqoop/integration/server/InvalidRESTCallsTest.java
@@ -0,0 +1,145 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sqoop.integration.server;
+
+import org.apache.log4j.Logger;
+import com.google.common.collect.Iterables;
+import org.apache.commons.io.IOUtils;
+import org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticatedURL;
+import org.apache.sqoop.test.testcases.ConnectorTestCase;
+import org.apache.sqoop.test.utils.ParametrizedUtils;
+import org.testng.ITest;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Factory;
+import org.testng.annotations.Test;
+
+import java.io.DataOutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.charset.Charset;
+
+import static org.testng.Assert.assertEquals;
+
+public class InvalidRESTCallsTest extends ConnectorTestCase implements ITest {
+
+  private static final Logger LOG = Logger.getLogger(InvalidRESTCallsTest.class);
+
+  // Validate returned response from server
+  public static abstract class Validator {
+
+    // Persisted connection object that we're testing against
+    HttpURLConnection connection;
+
+    // Persisted data from connection
+    String error;
+    String input;
+
+    public void setConnection(HttpURLConnection connection) throws Exception {
+      this.connection = connection;
+
+      try { this.input = (connection.getInputStream() != null) ? IOUtils.toString(connection.getInputStream())
: "NOT PRESENT"; } catch(Exception e) { this.input = "NOT PRESENT"; }
+      this.error = connection.getErrorStream() != null ? IOUtils.toString(connection.getErrorStream())
: "NOT PRESENT";
+    }
+
+    // Each test should implement whatever is needed here
+    abstract void validate() throws Exception;
+
+    // Verify HTTP response code
+    public void assertResponseCode(int expected) throws Exception {
+      assertEquals(connection.getResponseCode(), expected);
+    }
+  }
+
+  // Small internal class describing our test case
+  public static class TestDescription {
+    public String name;       // Name of the test
+    public String rest;       // Rest endpoint where we'll send request
+    public String method;     // Method that we should use
+    public String data;       // Data that we'll be sending as part of POST (NULL for nothing)
+    public Validator validator; // Routine to validate the response from the server
+
+    public TestDescription(String name, String rest, String method, String data, Validator
validator) {
+      this.name = name;
+      this.rest = rest;
+      this.method = method;
+      this.data = data;
+      this.validator = validator;
+    }
+  }
+
+  /**
+   * Poisoned requests that we'll be running with expected responses from the server
+   */
+  public static TestDescription []PROVDER_DATA = new TestDescription[] {
+    // End point /version/
+    new TestDescription("Valid", "version", "GET", null, new Validator() {
+      @Override
+      void validate() throws Exception {
+        assertResponseCode(200);
+    }}),
+    new TestDescription("Invalid post request", "version", "POST", "Random text", new Validator()
{
+      @Override
+      void validate() throws Exception {
+        assertResponseCode(405);
+      }}),
+  };
+
+  @DataProvider(name="invalid-rest-calls-test", parallel=false)
+  public static Object[][] data() {
+    return Iterables.toArray(ParametrizedUtils.toArrayOfArrays(PROVDER_DATA), Object[].class);
+  }
+
+  private TestDescription desc;
+
+  @Factory(dataProvider = "invalid-rest-calls-test")
+  public InvalidRESTCallsTest(TestDescription desc) {
+    this.desc = desc;
+  }
+
+  @Override
+  public String getTestName() {
+    return InvalidRESTCallsTest.class.getName() + " " + desc.rest + "[" + desc.method + "]:
" + desc.name;
+  }
+
+  @Test
+  public void test() throws Exception {
+    LOG.info("Start: " + getTestName());
+
+    URL url = new URL(getServerUrl() +  desc.rest);
+    HttpURLConnection connection = new DelegationTokenAuthenticatedURL().openConnection(url,
new DelegationTokenAuthenticatedURL.Token());
+    connection.setRequestMethod(desc.method);
+
+
+    if(desc.data != null) {
+      connection.setDoOutput(true);
+
+      byte []byteData = desc.data.getBytes(Charset.forName("UTF-8"));
+      connection.setRequestProperty("Content-Length", Integer.toString(byteData.length));
+      DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
+      wr.write(byteData, byteData.length, 0);
+      wr.flush();
+      wr.close();
+    }
+
+    desc.validator.setConnection(connection);
+    LOG.info("error = " + desc.validator.error);
+    LOG.info("input = " + desc.validator.input);
+    desc.validator.validate();
+  }
+
+}


Mime
View raw message