sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jar...@apache.org
Subject sqoop git commit: SQOOP-2626: Sqoop2: Provide test for CreateCommand
Date Wed, 21 Oct 2015 16:17:00 GMT
Repository: sqoop
Updated Branches:
  refs/heads/sqoop2 4e2204504 -> 42d268bc9


SQOOP-2626: Sqoop2: Provide test for CreateCommand

(Dian Fu via Jarek Jarcec Cecho)


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

Branch: refs/heads/sqoop2
Commit: 42d268bc9e079d960118e7c856a23c00bf10c176
Parents: 4e22045
Author: Jarek Jarcec Cecho <jarcec@apache.org>
Authored: Wed Oct 21 09:16:36 2015 -0700
Committer: Jarek Jarcec Cecho <jarcec@apache.org>
Committed: Wed Oct 21 09:16:36 2015 -0700

----------------------------------------------------------------------
 shell/pom.xml                                   |  26 ++++
 .../apache/sqoop/shell/CreateJobFunction.java   |  16 +--
 .../apache/sqoop/shell/CreateLinkFunction.java  |  14 +-
 .../apache/sqoop/shell/ShellEnvironment.java    |   4 +
 .../apache/sqoop/shell/TestCreateCommand.java   | 142 +++++++++++++++++++
 .../org/apache/sqoop/shell/TestShellError.java  |  39 +++++
 6 files changed, 226 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/42d268bc/shell/pom.xml
----------------------------------------------------------------------
diff --git a/shell/pom.xml b/shell/pom.xml
index a699f71..7fa815b 100644
--- a/shell/pom.xml
+++ b/shell/pom.xml
@@ -76,6 +76,16 @@ limitations under the License.
       <artifactId>groovy-all</artifactId>
       <version>1.8.5</version>
     </dependency>
+    <dependency>
+      <groupId>org.testng</groupId>
+      <artifactId>testng</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-all</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <profiles>
@@ -108,4 +118,20 @@ limitations under the License.
     </profile>
   </profiles>
 
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-jar-plugin</artifactId>
+        <executions>
+          <execution>
+            <goals>
+              <goal>test-jar</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+
 </project>

http://git-wip-us.apache.org/repos/asf/sqoop/blob/42d268bc/shell/src/main/java/org/apache/sqoop/shell/CreateJobFunction.java
----------------------------------------------------------------------
diff --git a/shell/src/main/java/org/apache/sqoop/shell/CreateJobFunction.java b/shell/src/main/java/org/apache/sqoop/shell/CreateJobFunction.java
index 4091c2d..4a520d7 100644
--- a/shell/src/main/java/org/apache/sqoop/shell/CreateJobFunction.java
+++ b/shell/src/main/java/org/apache/sqoop/shell/CreateJobFunction.java
@@ -74,25 +74,25 @@ public class CreateJobFunction extends  SqoopFunction {
     printlnResource(Constants.RES_CREATE_CREATING_JOB, fromLinkArg, toLinkArg);
 
     ConsoleReader reader = new ConsoleReader();
-    MJob job = client.createJob(fromLinkArg, toLinkArg);
+    MJob job = getClient().createJob(fromLinkArg, toLinkArg);
 
-    MConnector fromConnector = client.getConnector(job.getFromConnectorId());
+    MConnector fromConnector = getClient().getConnector(job.getFromConnectorId());
     if (!fromConnector.getSupportedDirections().isDirectionSupported(Direction.FROM)) {
       errorMessage("Connector " + fromConnector.getUniqueName() + " does not support direction
" + Direction.FROM);
       return Status.ERROR;
     }
 
-    MConnector toConnector = client.getConnector(job.getToConnectorId());
+    MConnector toConnector = getClient().getConnector(job.getToConnectorId());
     if (!toConnector.getSupportedDirections().isDirectionSupported(Direction.TO)) {
       errorMessage("Connector " + toConnector.getUniqueName() + " does not support direction
" + Direction.TO);
       return Status.ERROR;
     }
 
-    ResourceBundle fromConfigBundle = client.getConnectorConfigBundle(
+    ResourceBundle fromConfigBundle = getClient().getConnectorConfigBundle(
         job.getFromConnectorId());
-    ResourceBundle toConfigBundle = client.getConnectorConfigBundle(
+    ResourceBundle toConfigBundle = getClient().getConnectorConfigBundle(
         job.getToConnectorId());
-    ResourceBundle driverConfigBundle = client.getDriverConfigBundle();
+    ResourceBundle driverConfigBundle = getClient().getDriverConfigBundle();
 
     Status status = Status.OK;
 
@@ -111,14 +111,14 @@ public class CreateJobFunction extends  SqoopFunction {
         }
 
         // Try to create
-        status = client.saveJob(job);
+        status = getClient().saveJob(job);
       } while(!status.canProceed());
     } else {
       JobDynamicConfigOptions options = new JobDynamicConfigOptions();
       options.prepareOptions(job);
       CommandLine line = ConfigOptions.parseOptions(options, 0, args, false);
       if (fillJob(line, job)) {
-        status = client.saveJob(job);
+        status = getClient().saveJob(job);
         if (!status.canProceed()) {
           printJobValidationMessages(job);
           return null;

http://git-wip-us.apache.org/repos/asf/sqoop/blob/42d268bc/shell/src/main/java/org/apache/sqoop/shell/CreateLinkFunction.java
----------------------------------------------------------------------
diff --git a/shell/src/main/java/org/apache/sqoop/shell/CreateLinkFunction.java b/shell/src/main/java/org/apache/sqoop/shell/CreateLinkFunction.java
index 21e4092..224f844 100644
--- a/shell/src/main/java/org/apache/sqoop/shell/CreateLinkFunction.java
+++ b/shell/src/main/java/org/apache/sqoop/shell/CreateLinkFunction.java
@@ -64,17 +64,17 @@ public class CreateLinkFunction extends SqoopFunction {
     MLink link = null;
     Long cid;
     String connectorName = line.getOptionValue(Constants.OPT_CID);
-    MConnector connector = client.getConnector(connectorName);
+    MConnector connector = getClient().getConnector(connectorName);
     if (null == connector) {
       //Now check if command line argument is a connector id
       //This works as getConnector(String...) does not throw an exception
       cid = getLong(line, Constants.OPT_CID);
-      client.getConnector(cid);
+      getClient().getConnector(cid);
 
       //Would have thrown an exception before this if input was neither a valid name nor
an id
       //This will do an extra getConnector() call again inside createLink()
       //but should not matter as connectors are cached
-      link = client.createLink(cid);
+      link = getClient().createLink(cid);
       printlnResource(Constants.RES_CREATE_CREATING_LINK, cid);
     }
     else {
@@ -82,13 +82,13 @@ public class CreateLinkFunction extends SqoopFunction {
       //This will do an extra getConnector() call again inside createLink() but
       //should not matter as connectors are cached
       cid = connector.getPersistenceId();
-      link = client.createLink(connectorName);
+      link = getClient().createLink(connectorName);
       printlnResource(Constants.RES_CREATE_CREATING_LINK, connectorName);
     }
 
     ConsoleReader reader = new ConsoleReader();
 
-    ResourceBundle connectorConfigBundle = client.getConnectorConfigBundle(cid);
+    ResourceBundle connectorConfigBundle = getClient().getConnectorConfigBundle(cid);
 
     Status status = Status.OK;
     if (isInteractive) {
@@ -106,14 +106,14 @@ public class CreateLinkFunction extends SqoopFunction {
         }
 
         // Try to create
-        status = client.saveLink(link);
+        status = getClient().saveLink(link);
       } while(!status.canProceed());
     } else {
       LinkDynamicConfigOptions options = new LinkDynamicConfigOptions();
       options.prepareOptions(link);
       CommandLine linkoptsline = ConfigOptions.parseOptions(options, 0, args, false);
       if (fillLink(linkoptsline, link)) {
-        status = client.saveLink(link);
+        status = getClient().saveLink(link);
         if (!status.canProceed()) {
           printLinkValidationMessages(link);
           return null;

http://git-wip-us.apache.org/repos/asf/sqoop/blob/42d268bc/shell/src/main/java/org/apache/sqoop/shell/ShellEnvironment.java
----------------------------------------------------------------------
diff --git a/shell/src/main/java/org/apache/sqoop/shell/ShellEnvironment.java b/shell/src/main/java/org/apache/sqoop/shell/ShellEnvironment.java
index 8be2e54..caf4b53 100644
--- a/shell/src/main/java/org/apache/sqoop/shell/ShellEnvironment.java
+++ b/shell/src/main/java/org/apache/sqoop/shell/ShellEnvironment.java
@@ -66,6 +66,10 @@ public final class ShellEnvironment {
     return client;
   }
 
+  public static void setClient(SqoopClient sc) {
+    client = sc;
+  }
+
   public static void setIo(IO ioObject) {
     io = ioObject;
   }

http://git-wip-us.apache.org/repos/asf/sqoop/blob/42d268bc/shell/src/test/java/org/apache/sqoop/shell/TestCreateCommand.java
----------------------------------------------------------------------
diff --git a/shell/src/test/java/org/apache/sqoop/shell/TestCreateCommand.java b/shell/src/test/java/org/apache/sqoop/shell/TestCreateCommand.java
new file mode 100644
index 0000000..80ce55e
--- /dev/null
+++ b/shell/src/test/java/org/apache/sqoop/shell/TestCreateCommand.java
@@ -0,0 +1,142 @@
+/**
+ * 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.shell;
+
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import org.apache.sqoop.client.SqoopClient;
+import org.apache.sqoop.common.SqoopException;
+import org.apache.sqoop.model.MConfig;
+import org.apache.sqoop.model.MConnector;
+import org.apache.sqoop.model.MDriverConfig;
+import org.apache.sqoop.model.MFromConfig;
+import org.apache.sqoop.model.MJob;
+import org.apache.sqoop.model.MLink;
+import org.apache.sqoop.model.MLinkConfig;
+import org.apache.sqoop.model.MToConfig;
+import org.apache.sqoop.model.MValidator;
+import org.apache.sqoop.shell.core.Constants;
+import org.apache.sqoop.shell.core.ShellError;
+import org.apache.sqoop.validation.Status;
+import org.codehaus.groovy.tools.shell.Groovysh;
+import org.testng.Assert;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
+
+public class TestCreateCommand {
+  CreateCommand createCmd;
+  SqoopClient client;
+
+  @BeforeTest(alwaysRun = true)
+  public void setup() {
+    Groovysh shell = new Groovysh();
+    createCmd = new CreateCommand(shell);
+    ShellEnvironment.setInteractive(false);
+    ShellEnvironment.setIo(shell.getIo());
+    client = mock(SqoopClient.class);
+    ShellEnvironment.setClient(client);
+  }
+
+  @Test
+  public void testCreateLink() {
+    when(client.getConnector("connector_1")).thenReturn(new MConnector("", "", "", null,
null, null));
+    when(client.createLink("connector_1")).thenReturn(new MLink(1, new MLinkConfig(new ArrayList<MConfig>(),
new ArrayList<MValidator>())));
+    when(client.saveLink(any(MLink.class))).thenReturn(Status.OK);
+
+    // create link -c connector_1
+    Status status = (Status) createCmd.execute(Arrays.asList(Constants.FN_LINK, "-c", "connector_1"));
+    Assert.assertTrue(status != null && status == Status.OK);
+
+    // create link -cid connector_1
+    status = (Status) createCmd.execute(Arrays.asList(Constants.FN_LINK, "-cid", "connector_1"));
+    Assert.assertTrue(status != null && status == Status.OK);
+
+    // incorrect command: create link -c
+    try {
+      status = (Status) createCmd.execute(Arrays.asList(Constants.FN_LINK, "-c"));
+      Assert.fail("Create link should fail as connector id/name is missing!");
+    } catch (SqoopException e) {
+      Assert.assertEquals(ShellError.SHELL_0003, e.getErrorCode());
+      Assert.assertTrue(e.getMessage().contains("Missing argument for option"));
+    }
+  }
+
+  @Test
+  public void testCreateLinkWithNonExistingConnector() {
+    when(client.getConnector(any(String.class))).thenThrow(new SqoopException(TestShellError.TEST_SHELL_0000,
"Connector doesn't exist"));
+    when(client.getConnector(any(Integer.class))).thenThrow(new SqoopException(TestShellError.TEST_SHELL_0000,
"Connector doesn't exist"));
+
+    try {
+      createCmd.execute(Arrays.asList(Constants.FN_LINK, "-c", "connector_1"));
+      Assert.fail("Create link should fail as requested connector doesn't exist!");
+    } catch (SqoopException e) {
+      Assert.assertEquals(TestShellError.TEST_SHELL_0000, e.getErrorCode());
+    }
+  }
+
+  @Test
+  public void testCreateJob() {
+    MConnector fromConnector = new MConnector("connector_from", "", "", null, new MFromConfig(new
ArrayList<MConfig>(), new ArrayList<MValidator>()), null);
+    MConnector toConnector = new MConnector("connector_to", "", "", null, null, new MToConfig(new
ArrayList<MConfig>(), new ArrayList<MValidator>()));
+    when(client.createJob("link_from", "link_to")).thenReturn(
+        new MJob(1, 2, 1, 2, new MFromConfig(new ArrayList<MConfig>(), new ArrayList<MValidator>()),
+            new MToConfig(new ArrayList<MConfig>(), new ArrayList<MValidator>()),
+            new MDriverConfig(new ArrayList<MConfig>(), new ArrayList<MValidator>())));
+    when(client.getConnector(1)).thenReturn(fromConnector);
+    when(client.getConnector(2)).thenReturn(toConnector);
+    when(client.saveJob(any(MJob.class))).thenReturn(Status.OK);
+
+    // create job -f link_from -to link_to
+    Status status = (Status) createCmd.execute(Arrays.asList(Constants.FN_JOB, "-f", "link_from",
"-to", "link_to"));
+    Assert.assertTrue(status != null && status == Status.OK);
+
+    // incorrect command: create job -f link_from
+    try {
+      status = (Status) createCmd.execute(Arrays.asList(Constants.FN_JOB, "-f", "link_from"));
+      Assert.fail("Create Job should fail as the to link id/name is missing!");
+    } catch (SqoopException e) {
+      Assert.assertEquals(ShellError.SHELL_0003, e.getErrorCode());
+      Assert.assertTrue(e.getMessage().contains("Missing required option"));
+    }
+  }
+
+  @Test
+  public void testCreateJobWithNonExistingLink() {
+    when(client.createJob("link_from", "link_to")).thenThrow(new SqoopException(TestShellError.TEST_SHELL_0000,
"From link doesn't exist"));
+
+    try {
+      createCmd.execute(Arrays.asList(Constants.FN_JOB, "-f", "link_from", "-to", "link_to"));
+      Assert.fail("Create Job should fail as from link doesn't exist!");
+    } catch (SqoopException e) {
+      Assert.assertEquals(TestShellError.TEST_SHELL_0000, e.getErrorCode());
+    }
+  }
+
+  @Test
+  public void testCreateRole() {
+    // create role -r role_1
+    Status status = (Status) createCmd.execute(Arrays.asList(Constants.FN_ROLE, "-r", "role_1"));
+    Assert.assertTrue(status != null && status == Status.OK);
+  }
+}

http://git-wip-us.apache.org/repos/asf/sqoop/blob/42d268bc/shell/src/test/java/org/apache/sqoop/shell/TestShellError.java
----------------------------------------------------------------------
diff --git a/shell/src/test/java/org/apache/sqoop/shell/TestShellError.java b/shell/src/test/java/org/apache/sqoop/shell/TestShellError.java
new file mode 100644
index 0000000..a03c2fd
--- /dev/null
+++ b/shell/src/test/java/org/apache/sqoop/shell/TestShellError.java
@@ -0,0 +1,39 @@
+/**
+ * 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.shell;
+
+import org.apache.sqoop.common.ErrorCode;
+
+public enum TestShellError implements ErrorCode {
+  TEST_SHELL_0000("An error has occurred");
+
+  private final String message;
+
+  private TestShellError(String message) {
+    this.message = message;
+  }
+
+  public String getCode() {
+    return name();
+  }
+
+  public String getMessage() {
+    return message;
+  }
+}


Mime
View raw message