You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@oozie.apache.org by vi...@apache.org on 2013/05/14 19:24:36 UTC

svn commit: r1482475 - in /oozie/trunk: ./ core/src/test/java/org/apache/oozie/client/ core/src/test/java/org/apache/oozie/servlet/

Author: virag
Date: Tue May 14 17:24:36 2013
New Revision: 1482475

URL: http://svn.apache.org/r1482475
Log:
OOZIE-1313 coverage fix for org.apache.oozie.client (aleksgor via virag)

Added:
    oozie/trunk/core/src/test/java/org/apache/oozie/client/TestOozieClientWithFakeServlet.java
Modified:
    oozie/trunk/core/src/test/java/org/apache/oozie/client/TestWorkflowClient.java
    oozie/trunk/core/src/test/java/org/apache/oozie/client/TestWorkflowXClient.java
    oozie/trunk/core/src/test/java/org/apache/oozie/servlet/TestAuthFilterAuthOozieClient.java
    oozie/trunk/release-log.txt

Added: oozie/trunk/core/src/test/java/org/apache/oozie/client/TestOozieClientWithFakeServlet.java
URL: http://svn.apache.org/viewvc/oozie/trunk/core/src/test/java/org/apache/oozie/client/TestOozieClientWithFakeServlet.java?rev=1482475&view=auto
==============================================================================
--- oozie/trunk/core/src/test/java/org/apache/oozie/client/TestOozieClientWithFakeServlet.java (added)
+++ oozie/trunk/core/src/test/java/org/apache/oozie/client/TestOozieClientWithFakeServlet.java Tue May 14 17:24:36 2013
@@ -0,0 +1,197 @@
+/**
+ * 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.oozie.client;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.oozie.BundleJobBean;
+import org.apache.oozie.CoordinatorActionBean;
+import org.apache.oozie.CoordinatorJobBean;
+import org.apache.oozie.WorkflowJobBean;
+import org.apache.oozie.client.CoordinatorAction.Status;
+import org.apache.oozie.client.rest.BulkResponseImpl;
+import org.apache.oozie.client.rest.JsonTags;
+import org.json.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
+import org.junit.Test;
+import static org.mockito.Mockito.*;
+import static org.junit.Assert.*;
+
+
+/**
+ * Test some client functions with fake http connect
+ *
+ */
+public class TestOozieClientWithFakeServlet {
+
+    private int answer = 0;
+    private boolean check = true;
+
+    /**
+     * Test method getJMSTopicName
+     */
+    @Test
+    public void testGetJMSTopicName() throws Exception {
+        answer = 0;
+        check = true;
+        FakeOozieClient client = new FakeOozieClient("http://url");
+        String answer = client.getJMSTopicName("jobId");
+        assertEquals("topicName", answer);
+
+    }
+
+    /**
+     * Test method getJMSConnectionInfo
+     */
+    @Test
+    public void testGetJMSConnectionInfo() throws Exception {
+        answer = 1;
+        check = true;
+        FakeOozieClient client = new FakeOozieClient("http://url");
+        JMSConnectionInfo answer = client.getJMSConnectionInfo();
+        assertNotNull(answer);
+
+    }
+
+    /**
+     * Test method getCoordActionInfo
+     */
+    @Test
+    public void testGetCoordActionInfo() throws Exception {
+        answer = 1;
+        check = true;
+        FakeOozieClient client = new FakeOozieClient("http://url");
+        CoordinatorAction answer = client.getCoordActionInfo("actiomId");
+        assertNotNull(answer);
+    }
+
+    /**
+     * Test method getBundleJobsInfo
+     */
+    @Test
+    public void testGetBundleJobsInfo() throws Exception {
+        answer = 2;
+        check = true;
+        FakeOozieClient client = new FakeOozieClient("http://url");
+        List<BundleJob> answer = client.getBundleJobsInfo("", 0, 10);
+        assertNotNull(answer);
+        assertEquals(1, answer.size());
+
+    }
+
+    /**
+     * Test method getBulkInfo
+     */
+    @Test
+    public void testGetBulkInfo() throws Exception {
+        answer = 3;
+        check = true;
+        FakeOozieClient client = new FakeOozieClient("http://url");
+        List<BulkResponse> answer = client.getBulkInfo("", 0, 10);
+        assertNotNull(answer);
+        assertEquals(2, answer.size());
+        assertEquals(Status.READY, answer.get(0).getAction().getStatus());
+
+    }
+
+    /**
+     * Test method FakeOozieClient
+     */
+    @Test
+    public void testBundleRerun() throws Exception {
+        answer = 1;
+        check = true;
+        FakeOozieClient client = new FakeOozieClient("http://url");
+        Void answer = client.reRunBundle("jobId", "", "", true, true);
+        assertNull(answer);
+
+    }
+
+    private class FakeOozieClient extends OozieClient {
+
+        public FakeOozieClient(String oozieUrl) {
+            super(oozieUrl);
+        }
+
+        @Override
+        protected HttpURLConnection createConnection(URL url, String method) throws IOException, OozieClientException {
+            HttpURLConnection result = mock(HttpURLConnection.class);
+            when(result.getResponseCode()).thenReturn(200);
+            when(result.getInputStream()).thenReturn(getIs());
+            return result;
+        }
+
+        @SuppressWarnings("unchecked")
+        private InputStream getIs() {
+            ByteArrayInputStream result = new ByteArrayInputStream("".getBytes());
+            if (check) {
+                JSONArray array = new JSONArray();
+                array.put(2L);
+                String s = JSONValue.toJSONString(array);
+                result = new ByteArrayInputStream(s.getBytes());
+                check = false;
+                return result;
+            }
+            if (answer == 0) {
+                JSONObject json = new JSONObject();
+                json.put(JsonTags.JMS_TOPIC_NAME, "topicName");
+                result = new ByteArrayInputStream(json.toJSONString().getBytes());
+
+            }
+            if (answer == 1) {
+                JSONObject json = new JSONObject();
+                result = new ByteArrayInputStream(json.toJSONString().getBytes());
+
+            }
+            if (answer == 2) {
+                JSONObject json = new JSONObject();
+                List<WorkflowJobBean> jsonWorkflows = new ArrayList<WorkflowJobBean>();
+                jsonWorkflows.add(new WorkflowJobBean());
+                json.put(JsonTags.BUNDLE_JOBS, WorkflowJobBean.toJSONArray(jsonWorkflows, "GMT"));
+                result = new ByteArrayInputStream(json.toJSONString().getBytes());
+
+            }
+            if (answer == 3) {
+                JSONObject json = new JSONObject();
+                List<BulkResponseImpl> jsonWorkflows = new ArrayList<BulkResponseImpl>();
+                BulkResponseImpl bulk = new BulkResponseImpl();
+                bulk.setBundle(new BundleJobBean());
+                bulk.setCoordinator(new CoordinatorJobBean());
+                CoordinatorActionBean action = new CoordinatorActionBean();
+                action.setStatus(Status.READY);
+                bulk.setAction(action);
+                jsonWorkflows.add(bulk);
+                jsonWorkflows.add(bulk);
+                json.put(JsonTags.BULK_RESPONSES, BulkResponseImpl.toJSONArray(jsonWorkflows, "GMT"));
+                result = new ByteArrayInputStream(json.toJSONString().getBytes());
+
+            }
+
+            return result;
+        }
+
+    }
+}

Modified: oozie/trunk/core/src/test/java/org/apache/oozie/client/TestWorkflowClient.java
URL: http://svn.apache.org/viewvc/oozie/trunk/core/src/test/java/org/apache/oozie/client/TestWorkflowClient.java?rev=1482475&r1=1482474&r2=1482475&view=diff
==============================================================================
--- oozie/trunk/core/src/test/java/org/apache/oozie/client/TestWorkflowClient.java (original)
+++ oozie/trunk/core/src/test/java/org/apache/oozie/client/TestWorkflowClient.java Tue May 14 17:24:36 2013
@@ -27,6 +27,7 @@ import org.apache.oozie.client.OozieClie
 import org.apache.oozie.client.rest.RestConstants;
 import org.apache.oozie.servlet.DagServletTestCase;
 import org.apache.oozie.servlet.MockDagEngineService;
+import org.apache.oozie.servlet.SLAServlet;
 import org.apache.oozie.servlet.V0JobServlet;
 import org.apache.oozie.servlet.V0JobsServlet;
 import org.apache.oozie.servlet.V1AdminServlet;
@@ -34,6 +35,14 @@ import org.apache.oozie.servlet.V1JobSer
 import org.apache.oozie.servlet.V1JobsServlet;
 import org.apache.oozie.servlet.V2AdminServlet;
 import org.apache.oozie.servlet.V2JobServlet;
+import org.json.simple.JSONArray;
+
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.util.Iterator;
+
+import static org.mockito.Mockito.*;
 
 public class TestWorkflowClient extends DagServletTestCase {
 
@@ -46,14 +55,17 @@ public class TestWorkflowClient extends 
         new V1JobServlet();
         new V2JobServlet();
         new V2AdminServlet();
+        new SLAServlet();
     }
 
     private static final boolean IS_SECURITY_ENABLED = false;
     static final String VERSION = "/v" + OozieClient.WS_PROTOCOL_VERSION;
-    static final String[] END_POINTS = {"/versions", VERSION + "/jobs", VERSION + "/job/*", VERSION + "/admin/*"};
-    static final Class[] SERVLET_CLASSES = { HeaderTestingVersionServlet.class, V0JobsServlet.class,
-            V0JobServlet.class, V1AdminServlet.class, V2AdminServlet.class, V1JobServlet.class, V2JobServlet.class,
-            V1JobsServlet.class };
+    static final String[] END_POINTS = {"/versions", VERSION + "/jobs", VERSION + "/job/*", VERSION + "/admin/*",
+            VERSION + "/sla/*" };
+    @SuppressWarnings("rawtypes")
+    static final Class[] SERVLET_CLASSES = {HeaderTestingVersionServlet.class, V0JobsServlet.class,
+            V0JobServlet.class, V1AdminServlet.class, SLAServlet.class, V2AdminServlet.class,  V1JobServlet.class,
+            V2JobServlet.class, V1JobsServlet.class};
 
     protected void setUp() throws Exception {
         super.setUp();
@@ -71,6 +83,9 @@ public class TestWorkflowClient extends 
     //
     //    }
 
+    /**
+     * Test methods for headers manipulation
+     */
     public void testHeaders() throws Exception {
         runTest(END_POINTS, SERVLET_CLASSES, IS_SECURITY_ENABLED, new Callable<Void>() {
             public Void call() throws Exception {
@@ -78,9 +93,21 @@ public class TestWorkflowClient extends 
                 String oozieUrl = getContextURL();
                 OozieClient wc = new OozieClient(oozieUrl);
                 wc.setHeader("header", "test");
+                assertEquals("test", wc.getHeader("header"));
+                assertEquals("test", wc.getHeaders().get("header"));
+
+                boolean found = false;
+                for (Iterator<String> headers = wc.getHeaderNames(); headers.hasNext();) {
+                    if ("header".equals(headers.next())) {
+                        found = true;
+                    }
+                }
+                assertTrue("headers does not contain header!", found);
                 wc.validateWSVersion();
                 assertTrue(HeaderTestingVersionServlet.OOZIE_HEADERS.containsKey("header"));
                 assertTrue(HeaderTestingVersionServlet.OOZIE_HEADERS.containsValue("test"));
+                wc.removeHeader("header");
+                assertNull(wc.getHeader("header"));
                 return null;
             }
         });
@@ -94,6 +121,15 @@ public class TestWorkflowClient extends 
                 OozieClient wc = new OozieClient(oozieUrl);
                 assertEquals(oozieUrl, wc.getOozieUrl().substring(0, wc.getOozieUrl().length() - 1));
                 assertTrue(wc.getProtocolUrl().startsWith(wc.getOozieUrl() + "v"));
+
+                try {
+                    wc = new OozieClientForTest(oozieUrl);
+                    wc.getProtocolUrl();
+                    fail("wrong version should run throw exception");
+                }
+                catch (OozieClientException e) {
+                    assertEquals("UNSUPPORTED_VERSION : Supported version [2] or less, Unsupported versions[-11-10]", e.toString());
+                }
                 return null;
             }
         });
@@ -383,4 +419,89 @@ public class TestWorkflowClient extends 
             }
         });
     }
-}
+
+    /**
+     * Test client's methods getWorkflowActionInfo and getBundleJobInfo
+     */
+    public void testJobInformation() throws Exception {
+        runTest(END_POINTS, SERVLET_CLASSES, IS_SECURITY_ENABLED, new Callable<Void>() {
+            public Void call() throws Exception {
+                String oozieUrl = getContextURL();
+                OozieClient wc = new OozieClient(oozieUrl);
+                String jobId = MockDagEngineService.JOB_ID + "1" + MockDagEngineService.JOB_ID_END;
+                assertEquals(RestConstants.JOB_SHOW_LOG, wc.getJobLog(jobId));
+
+                WorkflowAction wfAction = wc.getWorkflowActionInfo(jobId);
+
+                assertEquals(jobId, wfAction.getId());
+                CoordinatorJob job = wc.getCoordJobInfo(jobId);
+
+                assertEquals("group", job.getAcl());
+                assertEquals("SUCCEEDED", job.getStatus().toString());
+                assertEquals("user", job.getUser());
+
+                BundleJob bundleJob = wc.getBundleJobInfo(jobId);
+                assertEquals("SUCCEEDED", bundleJob.getStatus().toString());
+                assertEquals("user", bundleJob.getUser());
+
+                return null;
+            }
+        });
+    }
+
+
+    /**
+     * Test SlaServlet and client's method getSlaInfo
+     */
+    public void testSla() throws Exception {
+        runTest(END_POINTS, SERVLET_CLASSES, IS_SECURITY_ENABLED, new Callable<Void>() {
+            public Void call() throws Exception {
+                String oozieUrl = getContextURL();
+                OozieClient wc = new OozieClient(oozieUrl);
+
+                PrintStream oldStream = System.out;
+                ByteArrayOutputStream data = new ByteArrayOutputStream();
+                System.setOut(new PrintStream(data));
+                try {
+                    wc.getSlaInfo(0, 10, null);
+                }
+                finally {
+                    System.setOut(oldStream);
+                }
+                assertTrue(data.toString().contains("<sla-message>"));
+                assertTrue(data.toString().contains("<last-sequence-id>0</last-sequence-id>"));
+                assertTrue(data.toString().contains("</sla-message>"));
+
+                return null;
+            }
+        });
+    }
+
+    /**
+     * Fake class for test reaction on a bad version
+     */
+    private class OozieClientForTest extends OozieClient {
+
+        public OozieClientForTest(String oozieUrl) {
+            super(oozieUrl);
+        }
+
+        @SuppressWarnings("unchecked")
+        @Override
+        protected HttpURLConnection createConnection(URL url, String method) throws IOException, OozieClientException {
+            HttpURLConnection result = mock(HttpURLConnection.class);
+            when(result.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
+
+            JSONArray versions = new JSONArray();
+            versions.add(-11);
+            versions.add(-10);
+            Writer writer = new StringWriter();
+            versions.writeJSONString(writer);
+            writer.flush();
+
+            when(result.getInputStream()).thenReturn(new ByteArrayInputStream(writer.toString().getBytes()));
+            return result;
+        }
+
+    }
+}
\ No newline at end of file

Modified: oozie/trunk/core/src/test/java/org/apache/oozie/client/TestWorkflowXClient.java
URL: http://svn.apache.org/viewvc/oozie/trunk/core/src/test/java/org/apache/oozie/client/TestWorkflowXClient.java?rev=1482475&r1=1482474&r2=1482475&view=diff
==============================================================================
--- oozie/trunk/core/src/test/java/org/apache/oozie/client/TestWorkflowXClient.java (original)
+++ oozie/trunk/core/src/test/java/org/apache/oozie/client/TestWorkflowXClient.java Tue May 14 17:24:36 2013
@@ -6,9 +6,9 @@
  * 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.
@@ -28,6 +28,8 @@ import org.apache.oozie.servlet.MockDagE
 import org.apache.oozie.servlet.V1JobsServlet;
 import org.apache.oozie.servlet.V1AdminServlet;
 
+import java.io.File;
+
 public class TestWorkflowXClient extends DagServletTestCase {
 
     static {
@@ -39,8 +41,8 @@ public class TestWorkflowXClient extends
     private static final boolean IS_SECURITY_ENABLED = false;
     static final String VERSION = "/v" + OozieClient.WS_PROTOCOL_VERSION;
     static final String[] END_POINTS = { "/versions", VERSION + "/jobs", VERSION + "/admin/*" };
-    static final Class[] SERVLET_CLASSES = { HeaderTestingVersionServlet.class, V1JobsServlet.class,
-            V1AdminServlet.class };
+    @SuppressWarnings("rawtypes")
+    static final Class[] SERVLET_CLASSES = { HeaderTestingVersionServlet.class, V1JobsServlet.class, V1AdminServlet.class };
 
     protected void setUp() throws Exception {
         super.setUp();
@@ -115,17 +117,90 @@ public class TestWorkflowXClient extends
                 Properties conf = wc.createConfiguration();
                 Path libPath = new Path(getFsTestCaseDir(), "lib");
                 getFileSystem().mkdirs(libPath);
-                conf.setProperty(OozieClient.LIBPATH, libPath.toString());
+
+                // try to submit without JT and NN
+                try {
+                    wc.submitMapReduce(conf);
+                    fail("submit client without JT should throw exception");
+                }
+                catch (RuntimeException exception) {
+                    assertEquals("java.lang.RuntimeException: jobtracker is not specified in conf", exception.toString());
+                }
                 conf.setProperty(XOozieClient.JT, "localhost:9001");
+                try {
+                    wc.submitMapReduce(conf);
+                    fail("submit client without NN should throuhg exception");
+                }
+                catch (RuntimeException exception) {
+                    assertEquals("java.lang.RuntimeException: namenode is not specified in conf", exception.toString());
+                }
                 conf.setProperty(XOozieClient.NN, "hdfs://localhost:9000");
-
+                try {
+                    wc.submitMapReduce(conf);
+                    fail("submit client without LIBPATH should throuhg exception");
+                }
+                catch (RuntimeException exception) {
+                    assertEquals("java.lang.RuntimeException: libpath is not specified in conf", exception.toString());
+                }
+
+                File tmp = new File("target");
+                int startPosition = libPath.toString().indexOf(tmp.getAbsolutePath());
+                String localPath = libPath.toString().substring(startPosition);
+
+                wc.setLib(conf, libPath.toString());
+
+                conf.setProperty(OozieClient.LIBPATH, localPath.substring(1));
+
+                try {
+                    wc.submitMapReduce(conf);
+                    fail("lib path can not be relative");
+                }
+                catch (RuntimeException e) {
+                    assertEquals("java.lang.RuntimeException: libpath should be absolute", e.toString());
+                }
+                wc.setLib(conf, libPath.toString());
 
                 assertEquals(MockDagEngineService.JOB_ID + wfCount + MockDagEngineService.JOB_ID_END,
-                             wc.submitMapReduce(conf));
+                        wc.submitMapReduce(conf));
 
                 assertTrue(MockDagEngineService.started.get(wfCount));
                 return null;
             }
         });
     }
+
+    /**
+     * Test some simple clint's methods
+     */
+    public void testSomeMethods() throws Exception {
+
+        runTest(END_POINTS, SERVLET_CLASSES, IS_SECURITY_ENABLED, new Callable<Void>() {
+            public Void call() throws Exception {
+                String oozieUrl = getContextURL();
+                XOozieClient wc = new XOozieClient(oozieUrl);
+                Properties configuration = wc.createConfiguration();
+                try {
+                    wc.addFile(configuration, null);
+                }
+                catch (IllegalArgumentException e) {
+                    assertEquals("file cannot be null or empty", e.getMessage());
+                }
+                wc.addFile(configuration, "file1");
+                wc.addFile(configuration, "file2");
+                assertEquals("file1,file2", configuration.get(XOozieClient.FILES));
+                // test archive
+                try {
+                    wc.addArchive(configuration, null);
+                }
+                catch (IllegalArgumentException e) {
+                    assertEquals("file cannot be null or empty", e.getMessage());
+                }
+                wc.addArchive(configuration, "archive1");
+                wc.addArchive(configuration, "archive2");
+                assertEquals("archive1,archive2", configuration.get(XOozieClient.ARCHIVES));
+
+                return null;
+            }
+        });
+    }
 }

Modified: oozie/trunk/core/src/test/java/org/apache/oozie/servlet/TestAuthFilterAuthOozieClient.java
URL: http://svn.apache.org/viewvc/oozie/trunk/core/src/test/java/org/apache/oozie/servlet/TestAuthFilterAuthOozieClient.java?rev=1482475&r1=1482474&r2=1482475&view=diff
==============================================================================
--- oozie/trunk/core/src/test/java/org/apache/oozie/servlet/TestAuthFilterAuthOozieClient.java (original)
+++ oozie/trunk/core/src/test/java/org/apache/oozie/servlet/TestAuthFilterAuthOozieClient.java Tue May 14 17:24:36 2013
@@ -33,10 +33,8 @@ import org.apache.oozie.util.IOUtils;
 
 import java.io.FileReader;
 import java.io.IOException;
-import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLEncoder;
-import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.Callable;
 
@@ -193,4 +191,30 @@ public class TestAuthFilterAuthOozieClie
         assertEquals(currentCache, newCache);
     }
 
+    /**
+     * Test authentication
+     */
+    public void testClientAuthMethod() throws Exception {
+
+        runTest(new Callable<Void>() {
+            public Void call() throws Exception {
+                String oozieUrl = getContextURL();
+                String[] args = new String[] { "admin", "-status", "-oozie",
+                        oozieUrl, "-auth", "SIMPLE" };
+                assertEquals(0, new OozieCLI().run(args));
+                return null;
+            }
+        });
+        // bad method
+        runTest(new Callable<Void>() {
+            public Void call() throws Exception {
+                String oozieUrl = getContextURL();
+                String[] args = new String[] { "admin", "-status", "-oozie",
+                        oozieUrl, "-auth", "fake" };
+                assertEquals(-1, new OozieCLI().run(args));
+                return null;
+            }
+        });
+
+    }
 }

Modified: oozie/trunk/release-log.txt
URL: http://svn.apache.org/viewvc/oozie/trunk/release-log.txt?rev=1482475&r1=1482474&r2=1482475&view=diff
==============================================================================
--- oozie/trunk/release-log.txt (original)
+++ oozie/trunk/release-log.txt Tue May 14 17:24:36 2013
@@ -1,5 +1,6 @@
 -- Oozie 4.1.0 release (trunk - unreleased)
 
+OOZIE-1313 coverage fix for org.apache.oozie.client (aleksgor via virag)
 OOZIE-1360 Oozie CLI shows created time of workflow as started time (ryota via virag)
 OOZIE-1359 mention default value of throttle in doc (ryota via virag)
 OOZIE-1370 oozie create db script throws classNotFound exception (bowenzhangusa via virag)