http://git-wip-us.apache.org/repos/asf/ambari/blob/82e72e51/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/JobTest.java ---------------------------------------------------------------------- diff --git a/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/JobTest.java b/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/JobTest.java index 736aa68..7f117e9 100644 --- a/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/JobTest.java +++ b/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/JobTest.java @@ -19,6 +19,7 @@ package org.apache.ambari.view.pig.test; import org.apache.ambari.view.pig.BasePigTest; +import org.apache.ambari.view.pig.resources.jobs.JobResourceManager; import org.apache.ambari.view.pig.resources.jobs.JobService; import org.apache.ambari.view.pig.resources.jobs.models.PigJob; import org.apache.ambari.view.pig.templeton.client.TempletonApi; @@ -38,6 +39,7 @@ import javax.ws.rs.core.UriInfo; import java.io.*; import java.net.URI; import java.util.HashMap; +import java.util.List; import static org.easymock.EasyMock.*; @@ -45,6 +47,16 @@ public class JobTest extends BasePigTest { private JobService jobService; @Rule public ExpectedException thrown = ExpectedException.none(); + @BeforeClass + public static void startUp() throws Exception { + BasePigTest.startUp(); // super + } + + @AfterClass + public static void shutDown() throws Exception { + BasePigTest.shutDown(); // super + } + @Override @Before public void setUp() throws Exception { @@ -57,20 +69,21 @@ public class JobTest extends BasePigTest { public void tearDown() throws Exception { super.tearDown(); jobService.getResourceManager().setTempletonApi(null); - JobService.setHdfsApi(null); + HdfsApi.dropAllConnections(); } - private Response doCreateJob(String title, String pigScript, String templetonArguments) { - return doCreateJob(title, pigScript, templetonArguments, null); + public static Response doCreateJob(String title, String pigScript, String templetonArguments, JobService jobService) { + return doCreateJob(title, pigScript, templetonArguments, null, null, jobService); } - private Response doCreateJob(String title, String pigScript, String templetonArguments, String forcedContent) { + public static Response doCreateJob(String title, String pigScript, String templetonArguments, String forcedContent, String scriptId, JobService jobService) { JobService.PigJobRequest request = new JobService.PigJobRequest(); request.job = new PigJob(); request.job.setTitle(title); request.job.setPigScript(pigScript); request.job.setTempletonArguments(templetonArguments); request.job.setForcedContent(forcedContent); + request.job.setScriptId(scriptId); UriInfo uriInfo = createNiceMock(UriInfo.class); URI uri = UriBuilder.fromUri("http://host/a/b").build(); @@ -94,7 +107,7 @@ public class JobTest extends BasePigTest { FSDataOutputStream stream = new FSDataOutputStream(do_stream); expect(hdfsApi.create(anyString(), eq(true))).andReturn(stream); replay(hdfsApi); - JobService.setHdfsApi(hdfsApi); + HdfsApi.setInstance(context, hdfsApi); TempletonApi api = createNiceMock(TempletonApi.class); jobService.getResourceManager().setTempletonApi(api); @@ -102,7 +115,7 @@ public class JobTest extends BasePigTest { expect(api.runPigQuery((File) anyObject(), anyString(), eq("-useHCatalog"))).andReturn(data); replay(api); - Response response = doCreateJob("Test", "/tmp/script.pig", "-useHCatalog"); + Response response = doCreateJob("Test", "/tmp/script.pig", "-useHCatalog", jobService); Assert.assertEquals("-useHCatalog", do_stream.toString()); Assert.assertEquals(201, response.getStatus()); @@ -111,14 +124,54 @@ public class JobTest extends BasePigTest { Assert.assertTrue(obj.containsKey("job")); Assert.assertNotNull(((PigJob) obj.get("job")).getId()); Assert.assertFalse(((PigJob) obj.get("job")).getId().isEmpty()); - Assert.assertTrue(((PigJob) obj.get("job")).getStatusDir().startsWith("/tmp/.pigjobs/ambari-qa/test")); + Assert.assertTrue(((PigJob) obj.get("job")).getStatusDir().startsWith("/tmp/.pigjobs/test")); PigJob job = ((PigJob) obj.get("job")); - Assert.assertEquals(PigJob.Status.SUBMITTED, job.getStatus()); + Assert.assertEquals(PigJob.PIG_JOB_STATE_SUBMITTED, job.getStatus()); Assert.assertTrue(job.isInProgress()); } @Test + public void testListJobs() throws Exception { + HdfsApi hdfsApi = createNiceMock(HdfsApi.class); + expect(hdfsApi.copy(eq("/tmp/script.pig"), startsWith("/tmp/.pigjobs/"))).andReturn(true).anyTimes(); + + ByteArrayOutputStream do_stream = new ByteArrayOutputStream(); + + FSDataOutputStream stream = new FSDataOutputStream(do_stream); + expect(hdfsApi.create(anyString(), eq(true))).andReturn(stream).anyTimes(); + replay(hdfsApi); + HdfsApi.setInstance(context, hdfsApi); + + TempletonApi api = createNiceMock(TempletonApi.class); + jobService.getResourceManager().setTempletonApi(api); + TempletonApi.JobData data = api.new JobData(); + expect(api.runPigQuery((File) anyObject(), anyString(), (String) isNull())).andReturn(data).anyTimes(); + replay(api); + + Response response = doCreateJob("Test", "/tmp/script.pig", null, null, "x42", jobService); + Assert.assertEquals(201, response.getStatus()); + + response = doCreateJob("Test", "/tmp/script.pig", null, null, "x42", jobService); + Assert.assertEquals(201, response.getStatus()); + + response = doCreateJob("Test", "/tmp/script.pig", null, null, "100", jobService); + Assert.assertEquals(201, response.getStatus()); + + response = jobService.getJobList("x42"); + Assert.assertEquals(200, response.getStatus()); + JSONObject obj = (JSONObject)response.getEntity(); + Assert.assertTrue(obj.containsKey("jobs")); + Assert.assertEquals(2, ((List) obj.get("jobs")).size()); + + response = jobService.getJobList(null); + Assert.assertEquals(200, response.getStatus()); + obj = (JSONObject)response.getEntity(); + Assert.assertTrue(obj.containsKey("jobs")); + Assert.assertTrue(((List) obj.get("jobs")).size() > 2); + } + + @Test public void testSubmitJobUsernameProvided() throws Exception { HdfsApi hdfsApi = createNiceMock(HdfsApi.class); expect(hdfsApi.copy(eq("/tmp/script.pig"), startsWith("/tmp/.pigjobs/"))).andReturn(true); @@ -128,7 +181,7 @@ public class JobTest extends BasePigTest { FSDataOutputStream stream = new FSDataOutputStream(do_stream); expect(hdfsApi.create(anyString(), eq(true))).andReturn(stream); replay(hdfsApi); - JobService.setHdfsApi(hdfsApi); + HdfsApi.setInstance(context, hdfsApi); TempletonApi api = createNiceMock(TempletonApi.class); jobService.getResourceManager().setTempletonApi(api); @@ -137,10 +190,10 @@ public class JobTest extends BasePigTest { replay(api); properties.put("dataworker.username", "luke"); - Response response = doCreateJob("Test", "/tmp/script.pig", "-useHCatalog"); + Response response = doCreateJob("Test", "/tmp/script.pig", "-useHCatalog", jobService); JSONObject obj = (JSONObject)response.getEntity(); Assert.assertTrue(obj.containsKey("job")); - Assert.assertTrue(((PigJob) obj.get("job")).getStatusDir().startsWith("/tmp/.pigjobs/luke/test")); + Assert.assertTrue(((PigJob) obj.get("job")).getStatusDir().startsWith("/tmp/.pigjobs/test")); } @Test @@ -153,7 +206,7 @@ public class JobTest extends BasePigTest { FSDataOutputStream stream = new FSDataOutputStream(do_stream); expect(hdfsApi.create(anyString(), eq(true))).andReturn(stream); replay(hdfsApi); - JobService.setHdfsApi(hdfsApi); + HdfsApi.setInstance(context, hdfsApi); TempletonApi api = createNiceMock(TempletonApi.class); jobService.getResourceManager().setTempletonApi(api); @@ -161,7 +214,7 @@ public class JobTest extends BasePigTest { expect(api.runPigQuery((File) anyObject(), anyString(), (String) isNull())).andReturn(data); replay(api); - Response response = doCreateJob("Test", "/tmp/script.pig", null); + Response response = doCreateJob("Test", "/tmp/script.pig", null, jobService); Assert.assertEquals("", do_stream.toString()); Assert.assertEquals(201, response.getStatus()); @@ -170,10 +223,10 @@ public class JobTest extends BasePigTest { Assert.assertTrue(obj.containsKey("job")); Assert.assertNotNull(((PigJob) obj.get("job")).getId()); Assert.assertFalse(((PigJob) obj.get("job")).getId().isEmpty()); - Assert.assertTrue(((PigJob) obj.get("job")).getStatusDir().startsWith("/tmp/.pigjobs/ambari-qa/test")); + Assert.assertTrue(((PigJob) obj.get("job")).getStatusDir().startsWith("/tmp/.pigjobs/test")); PigJob job = ((PigJob) obj.get("job")); - Assert.assertEquals(PigJob.Status.SUBMITTED, job.getStatus()); + Assert.assertEquals(PigJob.PIG_JOB_STATE_SUBMITTED, job.getStatus()); Assert.assertTrue(job.isInProgress()); } @@ -187,7 +240,7 @@ public class JobTest extends BasePigTest { FSDataOutputStream stream = new FSDataOutputStream(do_stream); expect(hdfsApi.create(anyString(), eq(true))).andReturn(stream); replay(hdfsApi); - JobService.setHdfsApi(hdfsApi); + HdfsApi.setInstance(context, hdfsApi); TempletonApi api = createNiceMock(TempletonApi.class); jobService.getResourceManager().setTempletonApi(api); @@ -196,7 +249,7 @@ public class JobTest extends BasePigTest { replay(api); thrown.expect(ServiceFormattedException.class); - doCreateJob("Test", null, "-useHCatalog"); + doCreateJob("Test", null, "-useHCatalog", jobService); } @Test @@ -211,7 +264,7 @@ public class JobTest extends BasePigTest { expect(hdfsApi.create(endsWith("script.pig"), eq(true))).andReturn(scriptStream); expect(hdfsApi.create(endsWith("params"), eq(true))).andReturn(templetonArgsStream); replay(hdfsApi); - JobService.setHdfsApi(hdfsApi); + HdfsApi.setInstance(context, hdfsApi); TempletonApi api = createNiceMock(TempletonApi.class); jobService.getResourceManager().setTempletonApi(api); @@ -219,7 +272,7 @@ public class JobTest extends BasePigTest { expect(api.runPigQuery((File) anyObject(), anyString(), eq("-useHCatalog"))).andReturn(data); replay(api); - Response response = doCreateJob("Test", null, "-useHCatalog", "pwd"); // with forcedContent + Response response = doCreateJob("Test", null, "-useHCatalog", "pwd", null, jobService); // with forcedContent Assert.assertEquals(201, response.getStatus()); Assert.assertEquals("-useHCatalog", baTempletonArgsStream.toString()); Assert.assertEquals("pwd", baScriptStream.toString()); @@ -235,7 +288,7 @@ public class JobTest extends BasePigTest { FSDataOutputStream stream = new FSDataOutputStream(do_stream); expect(hdfsApi.create(anyString(), eq(true))).andReturn(stream); replay(hdfsApi); - JobService.setHdfsApi(hdfsApi); + HdfsApi.setInstance(context, hdfsApi); TempletonApi api = createNiceMock(TempletonApi.class); jobService.getResourceManager().setTempletonApi(api); @@ -244,7 +297,7 @@ public class JobTest extends BasePigTest { replay(api); thrown.expect(BadRequestFormattedException.class); - doCreateJob(null, "/tmp/1.pig", "-useHCatalog"); + doCreateJob(null, "/tmp/1.pig", "-useHCatalog", jobService); } @Test @@ -257,7 +310,7 @@ public class JobTest extends BasePigTest { FSDataOutputStream stream = new FSDataOutputStream(do_stream); expect(hdfsApi.create(anyString(), eq(true))).andReturn(stream); replay(hdfsApi); - JobService.setHdfsApi(hdfsApi); + HdfsApi.setInstance(context, hdfsApi); TempletonApi api = createNiceMock(TempletonApi.class); jobService.getResourceManager().setTempletonApi(api); @@ -266,7 +319,7 @@ public class JobTest extends BasePigTest { replay(api); thrown.expect(ServiceFormattedException.class); - doCreateJob("Test", "/tmp/script.pig", "-useHCatalog"); + doCreateJob("Test", "/tmp/script.pig", "-useHCatalog", jobService); } @Test @@ -279,7 +332,7 @@ public class JobTest extends BasePigTest { FSDataOutputStream stream = new FSDataOutputStream(do_stream); expect(hdfsApi.create(anyString(), eq(true))).andReturn(stream); replay(hdfsApi); - JobService.setHdfsApi(hdfsApi); + HdfsApi.setInstance(context, hdfsApi); TempletonApi api = createNiceMock(TempletonApi.class); jobService.getResourceManager().setTempletonApi(api); @@ -289,11 +342,11 @@ public class JobTest extends BasePigTest { replay(api); thrown.expect(ServiceFormattedException.class); - doCreateJob("Test", "/tmp/script.pig", "-useHCatalog"); + doCreateJob("Test", "/tmp/script.pig", "-useHCatalog", jobService); } @Test - public void testKillJob() throws Exception { + public void testKillJobNoRemove() throws Exception { HdfsApi hdfsApi = createNiceMock(HdfsApi.class); expect(hdfsApi.copy(eq("/tmp/script.pig"), startsWith("/tmp/.pigjobs/"))).andReturn(true); @@ -302,7 +355,7 @@ public class JobTest extends BasePigTest { FSDataOutputStream stream = new FSDataOutputStream(do_stream); expect(hdfsApi.create(anyString(), eq(true))).andReturn(stream); replay(hdfsApi); - JobService.setHdfsApi(hdfsApi); + HdfsApi.setInstance(context, hdfsApi); TempletonApi api = createStrictMock(TempletonApi.class); jobService.getResourceManager().setTempletonApi(api); @@ -311,16 +364,55 @@ public class JobTest extends BasePigTest { expect(api.runPigQuery((File) anyObject(), anyString(), eq("-useHCatalog"))).andReturn(data); replay(api); - Response response = doCreateJob("Test", "/tmp/script.pig", "-useHCatalog"); + Response response = doCreateJob("Test", "/tmp/script.pig", "-useHCatalog", jobService); Assert.assertEquals(201, response.getStatus()); reset(api); api.killJob(eq("job_id_##")); + expect(api.checkJob(anyString())).andReturn(api.new JobInfo()).anyTimes(); replay(api); JSONObject obj = (JSONObject)response.getEntity(); PigJob job = ((PigJob)obj.get("job")); - response = jobService.killJob(job.getId()); + response = jobService.killJob(job.getId(), null); Assert.assertEquals(204, response.getStatus()); + + response = jobService.getJob(job.getId()); // it should still be present in DB + Assert.assertEquals(200, response.getStatus()); + } + + @Test + public void testKillJobWithRemove() throws Exception { + HdfsApi hdfsApi = createNiceMock(HdfsApi.class); + expect(hdfsApi.copy(eq("/tmp/script.pig"), startsWith("/tmp/.pigjobs/"))).andReturn(true); + + ByteArrayOutputStream do_stream = new ByteArrayOutputStream(); + + FSDataOutputStream stream = new FSDataOutputStream(do_stream); + expect(hdfsApi.create(anyString(), eq(true))).andReturn(stream); + replay(hdfsApi); + HdfsApi.setInstance(context, hdfsApi); + + TempletonApi api = createStrictMock(TempletonApi.class); + jobService.getResourceManager().setTempletonApi(api); + TempletonApi.JobData data = api.new JobData(); + data.id = "job_id_##"; + expect(api.runPigQuery((File) anyObject(), anyString(), eq("-useHCatalog"))).andReturn(data); + replay(api); + + Response response = doCreateJob("Test", "/tmp/script.pig", "-useHCatalog", jobService); + Assert.assertEquals(201, response.getStatus()); + + reset(api); + api.killJob(eq("job_id_##")); + expect(api.checkJob(anyString())).andReturn(api.new JobInfo()).anyTimes(); + replay(api); + JSONObject obj = (JSONObject)response.getEntity(); + PigJob job = ((PigJob)obj.get("job")); + response = jobService.killJob(job.getId(), "true"); + Assert.assertEquals(204, response.getStatus()); + + thrown.expect(NotFoundFormattedException.class); // it should not be present in DB + jobService.getJob(job.getId()); } @Test @@ -333,7 +425,7 @@ public class JobTest extends BasePigTest { FSDataOutputStream stream = new FSDataOutputStream(do_stream); expect(hdfsApi.create(anyString(), eq(true))).andReturn(stream); replay(hdfsApi); - JobService.setHdfsApi(hdfsApi); + HdfsApi.setInstance(context, hdfsApi); TempletonApi api = createNiceMock(TempletonApi.class); jobService.getResourceManager().setTempletonApi(api); @@ -342,13 +434,13 @@ public class JobTest extends BasePigTest { expect(api.runPigQuery((File) anyObject(), anyString(), eq("-useHCatalog"))).andReturn(data); replay(api); - Response response = doCreateJob("Test", "/tmp/script.pig", "-useHCatalog"); + Response response = doCreateJob("Test", "/tmp/script.pig", "-useHCatalog", jobService); Assert.assertEquals("-useHCatalog", do_stream.toString()); Assert.assertEquals(201, response.getStatus()); PigJob job = ((PigJob) ((JSONObject)response.getEntity()).get("job")); - Assert.assertEquals(PigJob.Status.SUBMITTED, job.getStatus()); + Assert.assertEquals(PigJob.PIG_JOB_STATE_SUBMITTED, job.getStatus()); Assert.assertTrue(job.isInProgress()); // Retrieve status: @@ -360,20 +452,20 @@ public class JobTest extends BasePigTest { response = jobService.getJob(job.getId()); Assert.assertEquals(200, response.getStatus()); job = ((PigJob) ((JSONObject)response.getEntity()).get("job")); - Assert.assertEquals(PigJob.Status.SUBMITTED, job.getStatus()); + Assert.assertEquals(PigJob.PIG_JOB_STATE_SUBMITTED, job.getStatus()); // RUNNING reset(api); info = api.new JobInfo(); info.status = new HashMap<String, Object>(); - info.status.put("runState", (double)PigJob.RUN_STATE_RUNNING); + info.status.put("runState", (double) JobResourceManager.RUN_STATE_RUNNING); info.percentComplete = "30% complete"; expect(api.checkJob(eq("job_id_#"))).andReturn(info); replay(api); response = jobService.getJob(job.getId()); Assert.assertEquals(200, response.getStatus()); job = ((PigJob) ((JSONObject)response.getEntity()).get("job")); - Assert.assertEquals(PigJob.Status.RUNNING, job.getStatus()); + Assert.assertEquals(PigJob.PIG_JOB_STATE_RUNNING, job.getStatus()); Assert.assertTrue(job.isInProgress()); Assert.assertEquals(30, (Object) job.getPercentComplete()); @@ -381,13 +473,13 @@ public class JobTest extends BasePigTest { reset(api); info = api.new JobInfo(); info.status = new HashMap<String, Object>(); - info.status.put("runState", (double)PigJob.RUN_STATE_SUCCEEDED); + info.status.put("runState", (double) JobResourceManager.RUN_STATE_SUCCEEDED); expect(api.checkJob(eq("job_id_#"))).andReturn(info); replay(api); response = jobService.getJob(job.getId()); Assert.assertEquals(200, response.getStatus()); job = ((PigJob) ((JSONObject)response.getEntity()).get("job")); - Assert.assertEquals(PigJob.Status.COMPLETED, job.getStatus()); + Assert.assertEquals(PigJob.PIG_JOB_STATE_COMPLETED, job.getStatus()); Assert.assertFalse(job.isInProgress()); Assert.assertNull(job.getPercentComplete()); @@ -395,38 +487,38 @@ public class JobTest extends BasePigTest { reset(api); info = api.new JobInfo(); info.status = new HashMap<String, Object>(); - info.status.put("runState", (double)PigJob.RUN_STATE_PREP); + info.status.put("runState", (double) JobResourceManager.RUN_STATE_PREP); expect(api.checkJob(eq("job_id_#"))).andReturn(info); replay(api); response = jobService.getJob(job.getId()); Assert.assertEquals(200, response.getStatus()); job = ((PigJob) ((JSONObject)response.getEntity()).get("job")); - Assert.assertEquals(PigJob.Status.RUNNING, job.getStatus()); + Assert.assertEquals(PigJob.PIG_JOB_STATE_RUNNING, job.getStatus()); // FAILED reset(api); info = api.new JobInfo(); info.status = new HashMap<String, Object>(); - info.status.put("runState", (double)PigJob.RUN_STATE_FAILED); + info.status.put("runState", (double) JobResourceManager.RUN_STATE_FAILED); expect(api.checkJob(eq("job_id_#"))).andReturn(info); replay(api); response = jobService.getJob(job.getId()); Assert.assertEquals(200, response.getStatus()); job = ((PigJob) ((JSONObject)response.getEntity()).get("job")); - Assert.assertEquals(PigJob.Status.FAILED, job.getStatus()); + Assert.assertEquals(PigJob.PIG_JOB_STATE_FAILED, job.getStatus()); Assert.assertFalse(job.isInProgress()); // KILLED reset(api); info = api.new JobInfo(); info.status = new HashMap<String, Object>(); - info.status.put("runState", (double)PigJob.RUN_STATE_KILLED); + info.status.put("runState", (double) JobResourceManager.RUN_STATE_KILLED); expect(api.checkJob(eq("job_id_#"))).andReturn(info); replay(api); response = jobService.getJob(job.getId()); Assert.assertEquals(200, response.getStatus()); job = ((PigJob) ((JSONObject)response.getEntity()).get("job")); - Assert.assertEquals(PigJob.Status.KILLED, job.getStatus()); + Assert.assertEquals(PigJob.PIG_JOB_STATE_KILLED, job.getStatus()); Assert.assertFalse(job.isInProgress()); } }
http://git-wip-us.apache.org/repos/asf/ambari/blob/82e72e51/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/ScriptTest.java ---------------------------------------------------------------------- diff --git a/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/ScriptTest.java b/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/ScriptTest.java index 2bf3152..87e7d6b 100644 --- a/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/ScriptTest.java +++ b/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/ScriptTest.java @@ -23,6 +23,7 @@ import org.apache.ambari.view.pig.resources.files.FileService; import org.apache.ambari.view.pig.resources.scripts.ScriptService; import org.apache.ambari.view.pig.resources.scripts.models.PigScript; import org.apache.ambari.view.pig.utils.BadRequestFormattedException; +import org.apache.ambari.view.pig.utils.HdfsApi; import org.apache.ambari.view.pig.utils.NotFoundFormattedException; import org.json.simple.JSONObject; import org.junit.*; @@ -40,140 +41,140 @@ import java.util.Map; import static org.easymock.EasyMock.*; public class ScriptTest extends HDFSTest { - private ScriptService scriptService; - @Rule public ExpectedException thrown = ExpectedException.none(); - - @BeforeClass - public static void startUp() throws Exception { - HDFSTest.startUp(); // super - } - - @AfterClass - public static void shutDown() throws Exception { - HDFSTest.shutDown(); // super - FileService.setHdfsApi(null); //cleanup API connection - } - - @Override - @Before - public void setUp() throws Exception { - super.setUp(); - scriptService = getService(ScriptService.class, handler, context); - } - - @Override - protected void setupProperties(Map<String, String> properties, File baseDir) throws Exception { - super.setupProperties(properties, baseDir); - properties.put("dataworker.scripts.path", "/tmp/.pigscripts"); - } - - private Response doCreateScript() { - return doCreateScript("Luke", "/tmp/luke.pig", scriptService); - } - - public static Response doCreateScript(String title, String path, ScriptService scriptService) { - ScriptService.PigScriptRequest request = new ScriptService.PigScriptRequest(); - request.script = new PigScript(); - request.script.setTitle(title); - request.script.setPigScript(path); - - UriInfo uriInfo = createNiceMock(UriInfo.class); - URI uri = UriBuilder.fromUri("http://host/a/b").build(); - expect(uriInfo.getAbsolutePath()).andReturn(uri); - - HttpServletResponse resp_obj = createNiceMock(HttpServletResponse.class); - - resp_obj.setHeader(eq("Location"), anyString()); - - replay(uriInfo, resp_obj); - return scriptService.saveScript(request, resp_obj, uriInfo); - } - - private Response doCreateScript(String title, String path) { - return doCreateScript(title, path, scriptService); - } - - @Test - public void createScript() { - Response response = doCreateScript(); - Assert.assertEquals(201, response.getStatus()); - - JSONObject obj = (JSONObject)response.getEntity(); - Assert.assertTrue(obj.containsKey("script")); - Assert.assertNotNull(((PigScript) obj.get("script")).getId()); - Assert.assertFalse(((PigScript) obj.get("script")).getId().isEmpty()); - } - - @Test - public void createScriptAutoCreate() { - Response response = doCreateScript("Test", null); - Assert.assertEquals(201, response.getStatus()); - - JSONObject obj = (JSONObject)response.getEntity(); - Assert.assertTrue(obj.containsKey("script")); - Assert.assertNotNull(((PigScript) obj.get("script")).getId()); - Assert.assertFalse(((PigScript) obj.get("script")).getId().isEmpty()); - Assert.assertFalse(((PigScript) obj.get("script")).getPigScript().isEmpty()); - } - - @Test - public void scriptNotFound() { - thrown.expect(NotFoundFormattedException.class); - scriptService.getScript("4242"); - } - - @Test - public void updateScript() { - Response createdScript = doCreateScript(); - String createdScriptId = ((PigScript) ((JSONObject) createdScript.getEntity()).get("script")).getId(); - - ScriptService.PigScriptRequest request = new ScriptService.PigScriptRequest(); - request.script = new PigScript(); - request.script.setTitle("Updated Script"); - - Response response = scriptService.updateScript(request, createdScriptId); - Assert.assertEquals(204, response.getStatus()); - - Response response2 = scriptService.getScript(createdScriptId); - Assert.assertEquals(200, response2.getStatus()); - - JSONObject obj = ((JSONObject) response2.getEntity()); - Assert.assertTrue(obj.containsKey("script")); - Assert.assertEquals(((PigScript) obj.get("script")).getTitle(), request.script.getTitle()); - } - - @Test - public void deleteScript() { - Response createdScript = doCreateScript(); - String createdScriptId = ((PigScript) ((JSONObject) createdScript.getEntity()).get("script")).getId(); - - Response response = scriptService.deleteScript(createdScriptId); - Assert.assertEquals(204, response.getStatus()); - - thrown.expect(NotFoundFormattedException.class); - scriptService.getScript(createdScriptId); - } - - @Test - public void listScripts() { - Response createdScript1 = doCreateScript("Title 1", "/path/to/file.pig"); - Response createdScript2 = doCreateScript("Title 2", "/path/to/file.pig"); - String createdScriptId = ((PigScript) ((JSONObject) createdScript1.getEntity()).get("script")).getId(); - - Response response = scriptService.getScriptList(); - Assert.assertEquals(200, response.getStatus()); - - JSONObject obj = (JSONObject) response.getEntity(); - Assert.assertTrue(obj.containsKey("scripts")); - List<PigScript> scripts = (List<PigScript>) obj.get("scripts"); - boolean containsTitle = false; - for(PigScript script : scripts) - containsTitle = containsTitle || script.getTitle().compareTo("Title 1") == 0; - Assert.assertTrue(containsTitle); - - containsTitle = false; - for(PigScript script : scripts) - containsTitle = containsTitle || script.getTitle().compareTo("Title 2") == 0; - Assert.assertTrue(containsTitle); - } + private ScriptService scriptService; + @Rule public ExpectedException thrown = ExpectedException.none(); + + @BeforeClass + public static void startUp() throws Exception { + HDFSTest.startUp(); // super + } + + @AfterClass + public static void shutDown() throws Exception { + HDFSTest.shutDown(); // super + HdfsApi.dropAllConnections(); //cleanup API connection + } + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + scriptService = getService(ScriptService.class, handler, context); + } + + @Override + protected void setupProperties(Map<String, String> properties, File baseDir) throws Exception { + super.setupProperties(properties, baseDir); + properties.put("scripts.dir", "/tmp/.pigscripts"); + } + + private Response doCreateScript() { + return doCreateScript("Luke", "/tmp/luke.pig", scriptService); + } + + public static Response doCreateScript(String title, String path, ScriptService scriptService) { + ScriptService.PigScriptRequest request = new ScriptService.PigScriptRequest(); + request.script = new PigScript(); + request.script.setTitle(title); + request.script.setPigScript(path); + + UriInfo uriInfo = createNiceMock(UriInfo.class); + URI uri = UriBuilder.fromUri("http://host/a/b").build(); + expect(uriInfo.getAbsolutePath()).andReturn(uri); + + HttpServletResponse resp_obj = createNiceMock(HttpServletResponse.class); + + resp_obj.setHeader(eq("Location"), anyString()); + + replay(uriInfo, resp_obj); + return scriptService.saveScript(request, resp_obj, uriInfo); + } + + private Response doCreateScript(String title, String path) { + return doCreateScript(title, path, scriptService); + } + + @Test + public void createScript() { + Response response = doCreateScript(); + Assert.assertEquals(201, response.getStatus()); + + JSONObject obj = (JSONObject)response.getEntity(); + Assert.assertTrue(obj.containsKey("script")); + Assert.assertNotNull(((PigScript) obj.get("script")).getId()); + Assert.assertFalse(((PigScript) obj.get("script")).getId().isEmpty()); + } + + @Test + public void createScriptAutoCreate() { + Response response = doCreateScript("Test", null); + Assert.assertEquals(201, response.getStatus()); + + JSONObject obj = (JSONObject)response.getEntity(); + Assert.assertTrue(obj.containsKey("script")); + Assert.assertNotNull(((PigScript) obj.get("script")).getId()); + Assert.assertFalse(((PigScript) obj.get("script")).getId().isEmpty()); + Assert.assertFalse(((PigScript) obj.get("script")).getPigScript().isEmpty()); + } + + @Test + public void scriptNotFound() { + thrown.expect(NotFoundFormattedException.class); + scriptService.getScript("4242"); + } + + @Test + public void updateScript() { + Response createdScript = doCreateScript(); + String createdScriptId = ((PigScript) ((JSONObject) createdScript.getEntity()).get("script")).getId(); + + ScriptService.PigScriptRequest request = new ScriptService.PigScriptRequest(); + request.script = new PigScript(); + request.script.setTitle("Updated Script"); + + Response response = scriptService.updateScript(request, createdScriptId); + Assert.assertEquals(204, response.getStatus()); + + Response response2 = scriptService.getScript(createdScriptId); + Assert.assertEquals(200, response2.getStatus()); + + JSONObject obj = ((JSONObject) response2.getEntity()); + Assert.assertTrue(obj.containsKey("script")); + Assert.assertEquals(((PigScript) obj.get("script")).getTitle(), request.script.getTitle()); + } + + @Test + public void deleteScript() { + Response createdScript = doCreateScript(); + String createdScriptId = ((PigScript) ((JSONObject) createdScript.getEntity()).get("script")).getId(); + + Response response = scriptService.deleteScript(createdScriptId); + Assert.assertEquals(204, response.getStatus()); + + thrown.expect(NotFoundFormattedException.class); + scriptService.getScript(createdScriptId); + } + + @Test + public void listScripts() { + Response createdScript1 = doCreateScript("Title 1", "/path/to/file.pig"); + Response createdScript2 = doCreateScript("Title 2", "/path/to/file.pig"); + String createdScriptId = ((PigScript) ((JSONObject) createdScript1.getEntity()).get("script")).getId(); + + Response response = scriptService.getScriptList(); + Assert.assertEquals(200, response.getStatus()); + + JSONObject obj = (JSONObject) response.getEntity(); + Assert.assertTrue(obj.containsKey("scripts")); + List<PigScript> scripts = (List<PigScript>) obj.get("scripts"); + boolean containsTitle = false; + for(PigScript script : scripts) + containsTitle = containsTitle || script.getTitle().compareTo("Title 1") == 0; + Assert.assertTrue(containsTitle); + + containsTitle = false; + for(PigScript script : scripts) + containsTitle = containsTitle || script.getTitle().compareTo("Title 2") == 0; + Assert.assertTrue(containsTitle); + } } http://git-wip-us.apache.org/repos/asf/ambari/blob/82e72e51/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/ScriptTestHDFSUnmanaged.java ---------------------------------------------------------------------- diff --git a/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/ScriptTestHDFSUnmanaged.java b/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/ScriptTestHDFSUnmanaged.java index 5b88d98..2db4f18 100644 --- a/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/ScriptTestHDFSUnmanaged.java +++ b/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/ScriptTestHDFSUnmanaged.java @@ -22,6 +22,7 @@ package org.apache.ambari.view.pig.test; import org.apache.ambari.view.ViewContext; import org.apache.ambari.view.ViewResourceHandler; import org.apache.ambari.view.pig.HDFSTest; +import org.apache.ambari.view.pig.persistence.DataStoreStorage; import org.apache.ambari.view.pig.persistence.InstanceKeyValueStorage; import org.apache.ambari.view.pig.persistence.Storage; import org.apache.ambari.view.pig.resources.files.FileService; @@ -29,6 +30,7 @@ import org.apache.ambari.view.pig.resources.scripts.ScriptService; import org.apache.ambari.view.pig.persistence.utils.StorageUtil; import org.apache.ambari.view.pig.resources.scripts.models.PigScript; import org.apache.ambari.view.pig.services.BaseService; +import org.apache.ambari.view.pig.utils.HdfsApi; import org.apache.ambari.view.pig.utils.MisconfigurationFormattedException; import org.apache.ambari.view.pig.utils.NotFoundFormattedException; import org.json.simple.JSONObject; @@ -56,7 +58,7 @@ public class ScriptTestHDFSUnmanaged extends HDFSTest { @AfterClass public static void shutDown() throws Exception { HDFSTest.shutDown(); // super - FileService.setHdfsApi(null); //cleanup API connection + HdfsApi.dropAllConnections(); } @Override @@ -64,8 +66,8 @@ public class ScriptTestHDFSUnmanaged extends HDFSTest { public void setUp() throws Exception { handler = createNiceMock(ViewResourceHandler.class); context = createNiceMock(ViewContext.class); - FileService.setHdfsApi(null); //cleanup API connection - StorageUtil.setStorage(null); + HdfsApi.dropAllConnections(); + StorageUtil.dropAllConnections(); } @Test @@ -77,8 +79,8 @@ public class ScriptTestHDFSUnmanaged extends HDFSTest { .getAbsoluteFile(); properties.put("dataworker.storagePath", pigStorageFile.toString()); -// properties.put("dataworker.scripts.path", "/tmp/.pigscripts"); - properties.put("dataworker.defaultFs", hdfsURI); +// properties.put("scripts.dir", "/tmp/.pigscripts"); + properties.put("webhdfs.url", hdfsURI); expect(context.getProperties()).andReturn(properties).anyTimes(); expect(context.getUsername()).andReturn("ambari-qa").anyTimes(); @@ -99,8 +101,8 @@ public class ScriptTestHDFSUnmanaged extends HDFSTest { .getAbsoluteFile(); properties.put("dataworker.storagePath", pigStorageFile.toString()); - properties.put("dataworker.scripts.path", "/tmp/.pigscripts"); - properties.put("dataworker.defaultFs", hdfsURI); + properties.put("scripts.dir", "/tmp/.pigscripts"); + properties.put("webhdfs.url", hdfsURI); expect(context.getProperties()).andReturn(properties).anyTimes(); expect(context.getUsername()).andReturn("ambari-qa").anyTimes(); @@ -110,12 +112,12 @@ public class ScriptTestHDFSUnmanaged extends HDFSTest { Response createdScript = doCreateScript("Test", null); String createdScriptPath = ((PigScript) ((JSONObject) createdScript.getEntity()).get("script")).getPigScript(); - Assert.assertTrue(createdScriptPath.startsWith("/tmp/.pigscripts/ambari-qa/")); + Assert.assertTrue(createdScriptPath.startsWith("/tmp/.pigscripts/")); properties.put("dataworker.username", "luke"); Response createdScript2 = doCreateScript("Test", null); String createdScriptPath2 = ((PigScript) ((JSONObject) createdScript2.getEntity()).get("script")).getPigScript(); - Assert.assertTrue(createdScriptPath2.startsWith("/tmp/.pigscripts/luke/")); + Assert.assertTrue(createdScriptPath2.startsWith("/tmp/.pigscripts/")); } @Test @@ -127,34 +129,34 @@ public class ScriptTestHDFSUnmanaged extends HDFSTest { .getAbsoluteFile(); // properties.put("dataworker.storagePath", pigStorageFile.toString()); - properties.put("dataworker.scripts.path", "/tmp/.pigscripts"); - properties.put("dataworker.defaultFs", hdfsURI); + properties.put("scripts.dir", "/tmp/.pigscripts"); + properties.put("webhdfs.url", hdfsURI); expect(context.getProperties()).andReturn(properties).anyTimes(); expect(context.getUsername()).andReturn("ambari-qa").anyTimes(); replay(handler, context); - Storage storage = StorageUtil.getStorage(context); - Assert.assertEquals(InstanceKeyValueStorage.class.getSimpleName(), storage.getClass().getSimpleName()); + Storage storage = StorageUtil.getInstance(context).getStorage(); + Assert.assertEquals(DataStoreStorage.class.getSimpleName(), storage.getClass().getSimpleName()); } @Test public void hdfsApiNoUsernameProvided() throws IOException, InterruptedException { Map<String, String> properties = new HashMap<String, String>(); - properties.put("dataworker.defaultFs", hdfsURI); + properties.put("webhdfs.url", hdfsURI); expect(context.getProperties()).andReturn(properties).anyTimes(); expect(context.getUsername()).andReturn("ambari-qa").anyTimes(); replay(context); - // no dataworker.hdfs.username property - Assert.assertEquals("ambari-qa", BaseService.getHdfsUsername(context)); + // no webhdfs.username property + Assert.assertEquals("ambari-qa", HdfsApi.getHdfsUsername(context)); - // with dataworker.hdfs.username property - properties.put("dataworker.hdfs.username", "luke"); - Assert.assertEquals("luke", BaseService.getHdfsUsername(context)); + // with webhdfs.username property + properties.put("webhdfs.username", "luke"); + Assert.assertEquals("luke", HdfsApi.getHdfsUsername(context)); } private Response doCreateScript(String title, String path) { http://git-wip-us.apache.org/repos/asf/ambari/blob/82e72e51/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/ScriptTestUnmanaged.java ---------------------------------------------------------------------- diff --git a/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/ScriptTestUnmanaged.java b/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/ScriptTestUnmanaged.java index e61ac19..b4af66c 100644 --- a/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/ScriptTestUnmanaged.java +++ b/contrib/views/pig/src/test/java/org/apache/ambari/view/pig/test/ScriptTestUnmanaged.java @@ -23,6 +23,7 @@ import org.apache.ambari.view.ViewResourceHandler; import org.apache.ambari.view.pig.BasePigTest; import org.apache.ambari.view.pig.resources.files.FileService; import org.apache.ambari.view.pig.resources.scripts.ScriptService; +import org.apache.ambari.view.pig.utils.HdfsApi; import org.apache.ambari.view.pig.utils.MisconfigurationFormattedException; import org.junit.*; import org.junit.rules.ExpectedException; @@ -45,7 +46,7 @@ public class ScriptTestUnmanaged extends BasePigTest { @AfterClass public static void shutDown() throws Exception { - FileService.setHdfsApi(null); //cleanup API connection + HdfsApi.dropAllConnections(); //cleanup API connection } @Before @@ -67,7 +68,7 @@ public class ScriptTestUnmanaged extends BasePigTest { public void createScriptAutoCreateNoDefaultFS() { Map<String, String> properties = new HashMap<String, String>(); properties.put("dataworker.storagePath", pigStorageFile.toString()); - properties.put("dataworker.scripts.path", "/tmp/.pigscripts"); + properties.put("scripts.dir", "/tmp/.pigscripts"); expect(context.getProperties()).andReturn(properties).anyTimes(); expect(context.getUsername()).andReturn("ambari-qa").anyTimes();