sqoop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From af...@apache.org
Subject sqoop git commit: SQOOP-2848: Sqoop2: RESTiliency: Simplify JobRequestHandler.getJobs similarly as was done for getLinks
Date Mon, 07 Mar 2016 20:47:50 GMT
Repository: sqoop
Updated Branches:
  refs/heads/sqoop2 dc39a5aaf -> fd8299cba


SQOOP-2848: Sqoop2: RESTiliency: Simplify JobRequestHandler.getJobs similarly as was done
for getLinks

(Jarek Jarcec Cecho via Abraham Fine)


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

Branch: refs/heads/sqoop2
Commit: fd8299cba1cbf43a520cd9d641a55de13438ed19
Parents: dc39a5a
Author: Abraham Fine <afine@apache.org>
Authored: Mon Mar 7 12:45:05 2016 -0800
Committer: Abraham Fine <afine@apache.org>
Committed: Mon Mar 7 12:45:05 2016 -0800

----------------------------------------------------------------------
 .../apache/sqoop/handler/JobRequestHandler.java | 61 +++++++----------
 .../integration/server/rest/JobRestTest.java    | 72 ++++++++++++++++++++
 .../sqoop/integration/server/rest/RestTest.java | 10 +++
 3 files changed, 107 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/sqoop/blob/fd8299cb/server/src/main/java/org/apache/sqoop/handler/JobRequestHandler.java
----------------------------------------------------------------------
diff --git a/server/src/main/java/org/apache/sqoop/handler/JobRequestHandler.java b/server/src/main/java/org/apache/sqoop/handler/JobRequestHandler.java
index 3070059..c86f8f0 100644
--- a/server/src/main/java/org/apache/sqoop/handler/JobRequestHandler.java
+++ b/server/src/main/java/org/apache/sqoop/handler/JobRequestHandler.java
@@ -20,9 +20,11 @@ package org.apache.sqoop.handler;
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.Date;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Locale;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
 import org.apache.sqoop.audit.AuditLoggerManager;
 import org.apache.sqoop.common.Direction;
@@ -257,48 +259,35 @@ public class JobRequestHandler implements RequestHandler {
   }
 
   private JsonBean getJobs(RequestContext ctx) {
-    String connectorIdentifier = ctx.getLastURLElement();
-    JobBean jobBean;
+    String jobName = ctx.getLastURLElement();
+    List<MJob> jobs;
     Locale locale = ctx.getAcceptLanguageHeader();
     Repository repository = RepositoryManager.getInstance().getRepository();
-    // jobs by connector
-    if (ctx.getParameterValue(CONNECTOR_NAME_QUERY_PARAM) != null) {
-      connectorIdentifier = ctx.getParameterValue(CONNECTOR_NAME_QUERY_PARAM);
-      AuditLoggerManager.getInstance().logAuditEvent(ctx.getUserName(),
-          ctx.getRequest().getRemoteAddr(), "get", "jobsByConnector", connectorIdentifier);
-      List<MJob> jobList = repository.findJobsForConnector(connectorIdentifier);
-
-      // Authorization check
-      jobList = AuthorizationEngine.filterResource(ctx.getUserName(), MResource.TYPE.JOB,
jobList);
-
-      jobBean = createJobBean(jobList, locale);
-    } else
-    // all jobs in the system
-    if (ctx.getPath().contains(JOBS_PATH)
-        || (ctx.getPath().contains(JOB_PATH) && connectorIdentifier.equals("all")))
{
-      AuditLoggerManager.getInstance().logAuditEvent(ctx.getUserName(),
-          ctx.getRequest().getRemoteAddr(), "get", "jobs", "all");
-      List<MJob> jobList = repository.findJobs();
-
-      // Authorization check
-      jobList = AuthorizationEngine.filterResource(ctx.getUserName(), MResource.TYPE.JOB,
jobList);
-
-      jobBean = createJobBean(jobList, locale);
-    }
-    // job by Id
-    else {
-      AuditLoggerManager.getInstance().logAuditEvent(ctx.getUserName(),
-          ctx.getRequest().getRemoteAddr(), "get", "job", connectorIdentifier);
 
-      MJob job = HandlerUtils.getJobFromIdentifier(connectorIdentifier);
-      String jobName = job.getName();
+    AuditLoggerManager.getInstance().logAuditEvent(ctx.getUserName(), ctx.getRequest().getRemoteAddr(),
"get", "job", jobName);
 
-      // Authorization check
-      AuthorizationEngine.readJob(ctx.getUserName(), jobName);
+    if(jobName.equals("all")) { // Return all links (by perhaps only for given connector)
+      String connectorName = ctx.getParameterValue(CONNECTOR_NAME_QUERY_PARAM);
 
-      jobBean = createJobBean(Arrays.asList(job), locale);
+      if(StringUtils.isEmpty(connectorName)) {
+        jobs = repository.findJobs();
+      } else {
+        if(repository.findConnector(connectorName) == null) {
+          throw new SqoopException(ServerError.SERVER_0006, "Invalid connector: " + connectorName);
+        }
+        jobs = repository.findJobsForConnector(connectorName);
+      }
+    } else { // Return one specific job with name or id stored in identifier
+      MJob job = HandlerUtils.getJobFromIdentifier(jobName);
+      jobs = new LinkedList<>();
+      jobs.add(job);
     }
-    return jobBean;
+
+    // Authorization check
+    jobs = AuthorizationEngine.filterResource(ctx.getUserName(), MResource.TYPE.JOB, jobs);
+
+    // And return resulting links
+    return createJobBean(jobs, locale);
   }
 
   private JobBean createJobBean(List<MJob> jobs, Locale locale) {

http://git-wip-us.apache.org/repos/asf/sqoop/blob/fd8299cb/test/src/test/java/org/apache/sqoop/integration/server/rest/JobRestTest.java
----------------------------------------------------------------------
diff --git a/test/src/test/java/org/apache/sqoop/integration/server/rest/JobRestTest.java
b/test/src/test/java/org/apache/sqoop/integration/server/rest/JobRestTest.java
new file mode 100644
index 0000000..41c8da3
--- /dev/null
+++ b/test/src/test/java/org/apache/sqoop/integration/server/rest/JobRestTest.java
@@ -0,0 +1,72 @@
+/**
+ * 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.rest;
+
+import org.apache.sqoop.test.utils.ParametrizedUtils;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Factory;
+
+import java.util.Iterator;
+
+public class JobRestTest extends RestTest {
+
+  @BeforeMethod
+  public void setUp() {
+    createFirstJob();
+  }
+
+  private static Validator firstJob = new Validator() {
+    @Override
+    void validate() throws Exception {
+      assertResponseCode(200);
+      assertContains("first-job");
+    }
+  };
+
+  public static TestDescription[] PROVIDER_DATA = new TestDescription[]{
+    // Get
+    new TestDescription("Get all jobs", "v1/job/all", "GET", null, firstJob),
+    new TestDescription("Get job by name", "v1/job/first-job", "GET", null, firstJob),
+    new TestDescription("Get all jobs for connector", "v1/job/all?cname=hdfs-connector",
"GET", null, firstJob),
+    new TestDescription("Get non existing job", "v1/job/i-dont-exists", "GET", null, new
Validator() {
+      @Override
+      void validate() throws Exception {
+        assertResponseCode(500);
+        assertServerException("Entity requested doesn't exist", "SERVER_0006");
+        assertContains("Job: i-dont-exists doesn't exist");
+      }}),
+    new TestDescription("Get jobs for non existing connector", "v1/job/all?cname=i-dont-exists",
"GET", null, new Validator() {
+      @Override
+      void validate() throws Exception {
+        assertResponseCode(500);
+        assertServerException("Entity requested doesn't exist", "SERVER_0006");
+        assertContains("Invalid connector: i-dont-exists");
+      }}),
+  };
+
+  @DataProvider(name="job-rest-test")
+  public static Iterator<Object[]> data() {
+    return ParametrizedUtils.toArrayOfArrays(PROVIDER_DATA).iterator();
+  }
+
+  @Factory(dataProvider = "job-rest-test")
+  public JobRestTest(TestDescription desc) {
+    super(desc);
+  }
+}

http://git-wip-us.apache.org/repos/asf/sqoop/blob/fd8299cb/test/src/test/java/org/apache/sqoop/integration/server/rest/RestTest.java
----------------------------------------------------------------------
diff --git a/test/src/test/java/org/apache/sqoop/integration/server/rest/RestTest.java b/test/src/test/java/org/apache/sqoop/integration/server/rest/RestTest.java
index 5b3a7bf..d40d932 100644
--- a/test/src/test/java/org/apache/sqoop/integration/server/rest/RestTest.java
+++ b/test/src/test/java/org/apache/sqoop/integration/server/rest/RestTest.java
@@ -20,7 +20,9 @@ package org.apache.sqoop.integration.server.rest;
 import org.apache.log4j.Logger;
 import org.apache.commons.io.IOUtils;
 import org.apache.hadoop.security.token.delegation.web.DelegationTokenAuthenticatedURL;
+import org.apache.sqoop.connector.hdfs.configuration.ToFormat;
 import org.apache.sqoop.model.MConfigList;
+import org.apache.sqoop.model.MJob;
 import org.apache.sqoop.model.MLink;
 import org.apache.sqoop.test.infrastructure.Infrastructure;
 import org.apache.sqoop.test.infrastructure.SqoopTestCase;
@@ -123,6 +125,14 @@ public abstract class RestTest extends SqoopTestCase {
     hdfsLinkFrom.setName("first-link");
     saveLink(hdfsLinkFrom);
   }
+  public void createFirstJob() {
+    createFirstLink();
+    MJob job = getClient().createJob("first-link", "first-link");
+    job.setName("first-job");
+    fillHdfsFromConfig(job);
+    fillHdfsToConfig(job, ToFormat.TEXT_FILE);
+    saveJob(job);
+  }
 
   @AfterMethod
   public void dropTestData() {


Mime
View raw message