sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hshreedha...@apache.org
Subject git commit: SQOOP-1183 Sqoop2. Add getConnector(String) to the client API
Date Tue, 27 Aug 2013 23:17:22 GMT
Updated Branches:
  refs/heads/sqoop2 7f2712b75 -> 93270b804


SQOOP-1183 Sqoop2. Add getConnector(String) to the client API

(Jarek Jarcec Cecho via Hari Shreedharan)


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

Branch: refs/heads/sqoop2
Commit: 93270b804d174c96660aac3f5b5548fa0071761b
Parents: 7f2712b
Author: Hari Shreedharan <hshreedharan@apache.org>
Authored: Tue Aug 27 16:16:10 2013 -0700
Committer: Hari Shreedharan <hshreedharan@apache.org>
Committed: Tue Aug 27 16:16:10 2013 -0700

----------------------------------------------------------------------
 .../org/apache/sqoop/client/ClientError.java    |  2 +
 .../org/apache/sqoop/client/SqoopClient.java    | 55 +++++++++++++++++++-
 .../apache/sqoop/client/TestSqoopClient.java    | 35 +++++++++++++
 .../connector/jdbc/generic/TableExportTest.java |  2 +-
 .../connector/jdbc/generic/TableImportTest.java |  2 +-
 .../jdbc/generic/imports/PartitionerTest.java   |  2 +-
 6 files changed, 94 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/93270b80/client/src/main/java/org/apache/sqoop/client/ClientError.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/ClientError.java b/client/src/main/java/org/apache/sqoop/client/ClientError.java
index aafb5b0..cd1edfe 100644
--- a/client/src/main/java/org/apache/sqoop/client/ClientError.java
+++ b/client/src/main/java/org/apache/sqoop/client/ClientError.java
@@ -30,6 +30,8 @@ public enum ClientError implements ErrorCode {
   /** Polling time of submission status cannot be negative */
   CLIENT_0002("Polling time of submission status cannot be negative"),
 
+  /** Given connector is not known to the server **/
+  CLIENT_0003("Connector do not exists"),
   ;
 
   private final String message;

http://git-wip-us.apache.org/repos/asf/sqoop/blob/93270b80/client/src/main/java/org/apache/sqoop/client/SqoopClient.java
----------------------------------------------------------------------
diff --git a/client/src/main/java/org/apache/sqoop/client/SqoopClient.java b/client/src/main/java/org/apache/sqoop/client/SqoopClient.java
index f9e7ddd..05ea6d6 100644
--- a/client/src/main/java/org/apache/sqoop/client/SqoopClient.java
+++ b/client/src/main/java/org/apache/sqoop/client/SqoopClient.java
@@ -86,7 +86,7 @@ public class SqoopClient {
     SUBMITTED,
     UPDATED,
     FINISHED
-  };
+  }
 
   public SqoopClient(String serverUrl) {
     requests = new SqoopRequests();
@@ -142,6 +142,44 @@ public class SqoopClient {
   }
 
   /**
+   * Return connector with given name.
+   *
+   * @param connectorName Connector name
+   * @return Connector model or NULL if the connector do not exists.
+   */
+  public MConnector getConnector(String connectorName) {
+    // Firstly try if we have this connector already in cache
+    MConnector connector = getConnectorFromCache(connectorName);
+    if(connector != null) return connector;
+
+    // If the connector wasn't in cache and we have all connectors,
+    // it simply do not exists.
+    if(allConnectors) return null;
+
+    // Retrieve all connectors from server
+    getConnectors();
+    return getConnectorFromCache(connectorName);
+  }
+
+  /**
+   * Iterate over cached connectors and return connector of given name.
+   * This method will not contact server in case that the connector is
+   * not found in the cache.
+   *
+   * @param connectorName Connector name
+   * @return
+   */
+  private MConnector getConnectorFromCache(String connectorName) {
+    for(MConnector connector : connectors.values()) {
+      if(connector.getUniqueName().equals(connectorName)) {
+        return connector;
+      }
+    }
+
+    return null;
+  }
+
+  /**
    * Retrieve connector structure from server and cache it.
    *
    * @param cid Connector id
@@ -240,6 +278,21 @@ public class SqoopClient {
   }
 
   /**
+   * Create new connection object for given connector.
+   *
+   * @param connectorName Connector name
+   * @return
+   */
+  public MConnection newConnection(String connectorName) {
+    MConnector connector = getConnector(connectorName);
+    if(connector == null) {
+      throw new SqoopException(ClientError.CLIENT_0003, connectorName);
+    }
+
+    return newConnection(connector.getPersistenceId());
+  }
+
+  /**
    * Retrieve connection with given id.
    *
    * @param xid Connnection id

http://git-wip-us.apache.org/repos/asf/sqoop/blob/93270b80/client/src/test/java/org/apache/sqoop/client/TestSqoopClient.java
----------------------------------------------------------------------
diff --git a/client/src/test/java/org/apache/sqoop/client/TestSqoopClient.java b/client/src/test/java/org/apache/sqoop/client/TestSqoopClient.java
index 3e0544a..eeffbb7 100644
--- a/client/src/test/java/org/apache/sqoop/client/TestSqoopClient.java
+++ b/client/src/test/java/org/apache/sqoop/client/TestSqoopClient.java
@@ -18,6 +18,7 @@
 package org.apache.sqoop.client;
 
 import org.apache.sqoop.client.request.SqoopRequests;
+import org.apache.sqoop.common.SqoopException;
 import org.apache.sqoop.json.ConnectorBean;
 import org.apache.sqoop.json.FrameworkBean;
 import org.apache.sqoop.model.MConnectionForms;
@@ -37,6 +38,7 @@ import java.util.Map;
 import java.util.ResourceBundle;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
 import static org.mockito.Mockito.*;
 
 public class TestSqoopClient {
@@ -66,6 +68,19 @@ public class TestSqoopClient {
     verify(requests, times(1)).readConnector(1L);
   }
 
+  @Test
+  public void testGetConnectorByString() {
+    when(requests.readConnector(null)).thenReturn(connectorBean(connector(1)));
+    MConnector connector = client.getConnector("A1");
+    assertEquals(1, connector.getPersistenceId());
+    assertEquals("A1", connector.getUniqueName());
+
+    client.getResourceBundle(1L);
+
+    verify(requests, times(0)).readConnector(1L);
+    verify(requests, times(1)).readConnector(null);
+  }
+
   /**
    * Retrieve connector bundle, request for metadata for same connector should
    * not require additional HTTP request.
@@ -132,6 +147,17 @@ public class TestSqoopClient {
     connectors = client.getConnectors();
     assertEquals(2, connectors.size());
 
+    connector = client.getConnector("A1");
+    assertEquals(1, connector.getPersistenceId());
+    assertEquals("A1", connector.getUniqueName());
+
+    connector = client.getConnector("A2");
+    assertEquals(2, connector.getPersistenceId());
+    assertEquals("A2", connector.getUniqueName());
+
+    connector = client.getConnector("A3");
+    assertNull(connector);
+
     verify(requests, times(1)).readConnector(null);
     verifyNoMoreInteractions(requests);
   }
@@ -163,6 +189,15 @@ public class TestSqoopClient {
     verifyNoMoreInteractions(requests);
   }
 
+  /**
+   * Connection for non-existing connector can't be created.
+   */
+  @Test(expected = SqoopException.class)
+  public void testNewConnection() {
+    when(requests.readConnector(null)).thenReturn(connectorBean(connector(1)));
+    client.newConnection("non existing connector");
+  }
+
   private ConnectorBean connectorBean(MConnector...connectors) {
     List<MConnector> connectorList = new ArrayList<MConnector>();
     Map<Long, ResourceBundle> bundles = new HashMap<Long, ResourceBundle>();

http://git-wip-us.apache.org/repos/asf/sqoop/blob/93270b80/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableExportTest.java
----------------------------------------------------------------------
diff --git a/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableExportTest.java
b/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableExportTest.java
index ee8b97b..436fdfb 100644
--- a/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableExportTest.java
+++ b/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableExportTest.java
@@ -46,7 +46,7 @@ public class TableExportTest extends ConnectorTestCase {
     );
 
     // Connection creation
-    MConnection connection = getClient().newConnection(1L);
+    MConnection connection = getClient().newConnection("generic-jdbc-connector");
     fillConnectionForm(connection);
     createConnection(connection);
 

http://git-wip-us.apache.org/repos/asf/sqoop/blob/93270b80/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableImportTest.java
----------------------------------------------------------------------
diff --git a/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableImportTest.java
b/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableImportTest.java
index b500828..465a16d 100644
--- a/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableImportTest.java
+++ b/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/TableImportTest.java
@@ -41,7 +41,7 @@ public class TableImportTest extends ConnectorTestCase {
     createAndLoadTableCities();
 
     // Connection creation
-    MConnection connection = getClient().newConnection(1L);
+    MConnection connection = getClient().newConnection("generic-jdbc-connector");
     fillConnectionForm(connection);
     createConnection(connection);
 

http://git-wip-us.apache.org/repos/asf/sqoop/blob/93270b80/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/imports/PartitionerTest.java
----------------------------------------------------------------------
diff --git a/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/imports/PartitionerTest.java
b/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/imports/PartitionerTest.java
index 50fce74..3642833 100644
--- a/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/imports/PartitionerTest.java
+++ b/test/src/test/java/org/apache/sqoop/integration/connector/jdbc/generic/imports/PartitionerTest.java
@@ -76,7 +76,7 @@ public class PartitionerTest extends ConnectorTestCase {
     createAndLoadTableUbuntuReleases();
 
     // Connection creation
-    MConnection connection = getClient().newConnection(1L);
+    MConnection connection = getClient().newConnection("generic-jdbc-connector");
     fillConnectionForm(connection);
     createConnection(connection);
 


Mime
View raw message