hadoop-mapreduce-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ss...@apache.org
Subject svn commit: r1376283 [20/22] - in /hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client: ./ hadoop-mapreduce-client-app2/ hadoop-mapreduce-client-app2/src/ hadoop-mapreduce-client-app2/src/main/ hadoop-mapreduce-client-app2/s...
Date Wed, 22 Aug 2012 22:11:48 GMT
Added: hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/metrics/TestMRAppMetrics.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/metrics/TestMRAppMetrics.java?rev=1376283&view=auto
==============================================================================
--- hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/metrics/TestMRAppMetrics.java (added)
+++ hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/metrics/TestMRAppMetrics.java Wed Aug 22 22:11:39 2012
@@ -0,0 +1,121 @@
+/*
+ * 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.hadoop.mapreduce.v2.app2.metrics;
+
+import org.apache.hadoop.mapreduce.v2.app2.job.Job;
+import org.apache.hadoop.mapreduce.v2.api.records.TaskType;
+import org.apache.hadoop.mapreduce.v2.app2.job.Task;
+import org.apache.hadoop.metrics2.MetricsRecordBuilder;
+
+import static org.apache.hadoop.test.MetricsAsserts.*;
+import static org.apache.hadoop.test.MockitoMaker.*;
+
+import org.junit.Test;
+
+import static org.mockito.Mockito.*;
+
+public class TestMRAppMetrics {
+
+  @Test public void testNames() {
+    Job job = mock(Job.class);
+    Task mapTask = make(stub(Task.class).returning(TaskType.MAP).
+                        from.getType());
+    Task reduceTask = make(stub(Task.class).returning(TaskType.REDUCE).
+                           from.getType());
+    MRAppMetrics metrics = MRAppMetrics.create();
+
+    metrics.submittedJob(job);
+    metrics.waitingTask(mapTask);
+    metrics.waitingTask(reduceTask);
+    metrics.preparingJob(job);
+    metrics.submittedJob(job);
+    metrics.waitingTask(mapTask);
+    metrics.waitingTask(reduceTask);
+    metrics.preparingJob(job);
+    metrics.submittedJob(job);
+    metrics.waitingTask(mapTask);
+    metrics.waitingTask(reduceTask);
+    metrics.preparingJob(job);
+    metrics.endPreparingJob(job);
+    metrics.endPreparingJob(job);
+    metrics.endPreparingJob(job);
+
+    metrics.runningJob(job);
+    metrics.launchedTask(mapTask);
+    metrics.runningTask(mapTask);
+    metrics.failedTask(mapTask);
+    metrics.endWaitingTask(reduceTask);
+    metrics.endRunningTask(mapTask);
+    metrics.endRunningJob(job);
+    metrics.failedJob(job);
+
+    metrics.runningJob(job);
+    metrics.launchedTask(mapTask);
+    metrics.runningTask(mapTask);
+    metrics.killedTask(mapTask);
+    metrics.endWaitingTask(reduceTask);
+    metrics.endRunningTask(mapTask);
+    metrics.endRunningJob(job);
+    metrics.killedJob(job);
+
+    metrics.runningJob(job);
+    metrics.launchedTask(mapTask);
+    metrics.runningTask(mapTask);
+    metrics.completedTask(mapTask);
+    metrics.endRunningTask(mapTask);
+    metrics.launchedTask(reduceTask);
+    metrics.runningTask(reduceTask);
+    metrics.completedTask(reduceTask);
+    metrics.endRunningTask(reduceTask);
+    metrics.endRunningJob(job);
+    metrics.completedJob(job);
+
+    checkMetrics(/*job*/3, 1, 1, 1, 0, 0,
+                 /*map*/3, 1, 1, 1, 0, 0,
+                 /*reduce*/1, 1, 0, 0, 0, 0);
+  }
+
+  private void checkMetrics(int jobsSubmitted, int jobsCompleted,
+      int jobsFailed, int jobsKilled, int jobsPreparing, int jobsRunning,
+      int mapsLaunched, int mapsCompleted, int mapsFailed, int mapsKilled,
+      int mapsRunning, int mapsWaiting, int reducesLaunched,
+      int reducesCompleted, int reducesFailed, int reducesKilled,
+      int reducesRunning, int reducesWaiting) {
+    MetricsRecordBuilder rb = getMetrics("MRAppMetrics");
+    assertCounter("JobsSubmitted", jobsSubmitted, rb);
+    assertCounter("JobsCompleted", jobsCompleted, rb);
+    assertCounter("JobsFailed", jobsFailed, rb);
+    assertCounter("JobsKilled", jobsKilled, rb);
+    assertGauge("JobsPreparing", jobsPreparing, rb);
+    assertGauge("JobsRunning", jobsRunning, rb);
+
+    assertCounter("MapsLaunched", mapsLaunched, rb);
+    assertCounter("MapsCompleted", mapsCompleted, rb);
+    assertCounter("MapsFailed", mapsFailed, rb);
+    assertCounter("MapsKilled", mapsKilled, rb);
+    assertGauge("MapsRunning", mapsRunning, rb);
+    assertGauge("MapsWaiting", mapsWaiting, rb);
+
+    assertCounter("ReducesLaunched", reducesLaunched, rb);
+    assertCounter("ReducesCompleted", reducesCompleted, rb);
+    assertCounter("ReducesFailed", reducesFailed, rb);
+    assertCounter("ReducesKilled", reducesKilled, rb);
+    assertGauge("ReducesRunning", reducesRunning, rb);
+    assertGauge("ReducesWaiting", reducesWaiting, rb);
+  }
+}

Added: hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/speculate/TestDataStatistics.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/speculate/TestDataStatistics.java?rev=1376283&view=auto
==============================================================================
--- hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/speculate/TestDataStatistics.java (added)
+++ hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/speculate/TestDataStatistics.java Wed Aug 22 22:11:39 2012
@@ -0,0 +1,73 @@
+/**
+* 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.hadoop.mapreduce.v2.app2.speculate;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestDataStatistics {
+
+  private static final double TOL = 0.001;
+
+  @Test
+  public void testEmptyDataStatistics() throws Exception {
+    DataStatistics statistics = new DataStatistics();
+    Assert.assertEquals(0, statistics.count(), TOL);
+    Assert.assertEquals(0, statistics.mean(), TOL);
+    Assert.assertEquals(0, statistics.var(), TOL);
+    Assert.assertEquals(0, statistics.std(), TOL);
+    Assert.assertEquals(0, statistics.outlier(1.0f), TOL);
+  }
+  
+  @Test
+  public void testSingleEntryDataStatistics() throws Exception {
+    DataStatistics statistics = new DataStatistics(17.29);
+    Assert.assertEquals(1, statistics.count(), TOL);
+    Assert.assertEquals(17.29, statistics.mean(), TOL);
+    Assert.assertEquals(0, statistics.var(), TOL);
+    Assert.assertEquals(0, statistics.std(), TOL);
+    Assert.assertEquals(17.29, statistics.outlier(1.0f), TOL);
+  }
+  
+  @Test
+  public void testMutiEntryDataStatistics() throws Exception {
+    DataStatistics statistics = new DataStatistics();
+    statistics.add(17);
+    statistics.add(29);
+    Assert.assertEquals(2, statistics.count(), TOL);
+    Assert.assertEquals(23.0, statistics.mean(), TOL);
+    Assert.assertEquals(36.0, statistics.var(), TOL);
+    Assert.assertEquals(6.0, statistics.std(), TOL);
+    Assert.assertEquals(29.0, statistics.outlier(1.0f), TOL);
+ }
+  
+  @Test
+  public void testUpdateStatistics() throws Exception {
+    DataStatistics statistics = new DataStatistics(17);
+    statistics.add(29);
+    Assert.assertEquals(2, statistics.count(), TOL);
+    Assert.assertEquals(23.0, statistics.mean(), TOL);
+    Assert.assertEquals(36.0, statistics.var(), TOL);
+
+    statistics.updateStatistics(17, 29);
+    Assert.assertEquals(2, statistics.count(), TOL);
+    Assert.assertEquals(29.0, statistics.mean(), TOL);
+    Assert.assertEquals(0.0, statistics.var(), TOL);
+  }
+}

Added: hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebApp.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebApp.java?rev=1376283&view=auto
==============================================================================
--- hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebApp.java (added)
+++ hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebApp.java Wed Aug 22 22:11:39 2012
@@ -0,0 +1,220 @@
+///**
+//* 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.hadoop.mapreduce.v2.app2.webapp;
+//
+//import static org.apache.hadoop.mapreduce.v2.app2.webapp.AMParams.APP_ID;
+//import static org.junit.Assert.assertEquals;
+//
+//import java.util.HashMap;
+//import java.util.Map;
+//import java.util.Map.Entry;
+//
+//import org.apache.hadoop.mapreduce.v2.api.records.JobId;
+//import org.apache.hadoop.mapreduce.v2.api.records.TaskId;
+//import org.apache.hadoop.mapreduce.v2.app2.AppContext;
+//import org.apache.hadoop.mapreduce.v2.app2.MockJobs;
+//import org.apache.hadoop.mapreduce.v2.app2.job.Job;
+//import org.apache.hadoop.mapreduce.v2.app2.job.Task;
+//import org.apache.hadoop.mapreduce.v2.app2.job.TaskAttempt;
+//import org.apache.hadoop.mapreduce.v2.util.MRApps;
+//import org.apache.hadoop.yarn.Clock;
+//import org.apache.hadoop.yarn.ClusterInfo;
+//import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
+//import org.apache.hadoop.yarn.api.records.ApplicationId;
+//import org.apache.hadoop.yarn.event.EventHandler;
+//import org.apache.hadoop.yarn.webapp.WebApps;
+//import org.apache.hadoop.yarn.webapp.test.WebAppTests;
+//import org.junit.Test;
+//
+//import com.google.inject.Injector;
+//
+//public class TestAMWebApp {
+//
+//  static class TestAppContext implements AppContext {
+//    final ApplicationAttemptId appAttemptID;
+//    final ApplicationId appID;
+//    final String user = MockJobs.newUserName();
+//    final Map<JobId, Job> jobs;
+//    final long startTime = System.currentTimeMillis();
+//
+//    TestAppContext(int appid, int numJobs, int numTasks, int numAttempts) {
+//      appID = MockJobs.newAppID(appid);
+//      appAttemptID = MockJobs.newAppAttemptID(appID, 0);
+//      jobs = MockJobs.newJobs(appID, numJobs, numTasks, numAttempts);
+//    }
+//
+//    TestAppContext() {
+//      this(0, 1, 1, 1);
+//    }
+//
+//    @Override
+//    public ApplicationAttemptId getApplicationAttemptId() {
+//      return appAttemptID;
+//    }
+//
+//    @Override
+//    public ApplicationId getApplicationID() {
+//      return appID;
+//    }
+//
+//    @Override
+//    public CharSequence getUser() {
+//      return user;
+//    }
+//
+//    @Override
+//    public Job getJob(JobId jobID) {
+//      return jobs.get(jobID);
+//    }
+//
+//    @Override
+//    public Map<JobId, Job> getAllJobs() {
+//      return jobs; // OK
+//    }
+//
+//    @SuppressWarnings("rawtypes")
+//    @Override
+//    public EventHandler getEventHandler() {
+//      return null;
+//    }
+//
+//    @Override
+//    public Clock getClock() {
+//      return null;
+//    }
+//
+//    @Override
+//    public String getApplicationName() {
+//      return "TestApp";
+//    }
+//
+//    @Override
+//    public long getStartTime() {
+//      return startTime;
+//    }
+//
+//    @Override
+//    public ClusterInfo getClusterInfo() {
+//      return null;
+//    }
+//  }
+//
+//  @Test public void testAppControllerIndex() {
+//    TestAppContext ctx = new TestAppContext();
+//    Injector injector = WebAppTests.createMockInjector(AppContext.class, ctx);
+//    AppController controller = injector.getInstance(AppController.class);
+//    controller.index();
+//    assertEquals(ctx.appID.toString(), controller.get(APP_ID,""));
+//  }
+//
+//  @Test public void testAppView() {
+//    WebAppTests.testPage(AppView.class, AppContext.class, new TestAppContext());
+//  }
+//
+//
+//  
+//  @Test public void testJobView() {
+//    AppContext appContext = new TestAppContext();
+//    Map<String, String> params = getJobParams(appContext);
+//    WebAppTests.testPage(JobPage.class, AppContext.class, appContext, params);
+//  }
+//
+//  @Test public void testTasksView() {
+//    AppContext appContext = new TestAppContext();
+//    Map<String, String> params = getTaskParams(appContext);
+//    WebAppTests.testPage(TasksPage.class, AppContext.class, appContext, params);
+//  }
+//
+//  @Test public void testTaskView() {
+//    AppContext appContext = new TestAppContext();
+//    Map<String, String> params = getTaskParams(appContext);
+//    WebAppTests.testPage(TaskPage.class, AppContext.class, appContext, params);
+//  }
+//
+//  public static Map<String, String> getJobParams(AppContext appContext) {
+//    JobId jobId = appContext.getAllJobs().entrySet().iterator().next().getKey();
+//    Map<String, String> params = new HashMap<String, String>();
+//    params.put(AMParams.JOB_ID, MRApps.toString(jobId));
+//    return params;
+//  }
+//  
+//  public static Map<String, String> getTaskParams(AppContext appContext) {
+//    JobId jobId = appContext.getAllJobs().entrySet().iterator().next().getKey();
+//    Entry<TaskId, Task> e = appContext.getJob(jobId).getTasks().entrySet().iterator().next();
+//    e.getValue().getType();
+//    Map<String, String> params = new HashMap<String, String>();
+//    params.put(AMParams.JOB_ID, MRApps.toString(jobId));
+//    params.put(AMParams.TASK_ID, MRApps.toString(e.getKey()));
+//    params.put(AMParams.TASK_TYPE, MRApps.taskSymbol(e.getValue().getType()));
+//    return params;
+//  }
+//
+//  @Test public void testConfView() {
+//    WebAppTests.testPage(JobConfPage.class, AppContext.class,
+//                         new TestAppContext());
+//  }
+//
+//  @Test public void testCountersView() {
+//    AppContext appContext = new TestAppContext();
+//    Map<String, String> params = getJobParams(appContext);
+//    WebAppTests.testPage(CountersPage.class, AppContext.class,
+//                         appContext, params);
+//  }
+//  
+//  @Test public void testSingleCounterView() {
+//    AppContext appContext = new TestAppContext();
+//    Map<String, String> params = getJobParams(appContext);
+//    params.put(AMParams.COUNTER_GROUP, 
+//        "org.apache.hadoop.mapreduce.FileSystemCounter");
+//    params.put(AMParams.COUNTER_NAME, "HDFS_WRITE_OPS");
+//    WebAppTests.testPage(SingleCounterPage.class, AppContext.class,
+//                         appContext, params);
+//  }
+//
+//  @Test public void testTaskCountersView() {
+//    AppContext appContext = new TestAppContext();
+//    Map<String, String> params = getTaskParams(appContext);
+//    WebAppTests.testPage(CountersPage.class, AppContext.class,
+//                         appContext, params);
+//  }
+//
+//  @Test public void testSingleTaskCounterView() {
+//    AppContext appContext = new TestAppContext(0, 1, 1, 2);
+//    Map<String, String> params = getTaskParams(appContext);
+//    params.put(AMParams.COUNTER_GROUP, 
+//        "org.apache.hadoop.mapreduce.FileSystemCounter");
+//    params.put(AMParams.COUNTER_NAME, "HDFS_WRITE_OPS");
+//    
+//    // remove counters from one task attempt
+//    // to test handling of missing counters
+//    TaskId taskID = MRApps.toTaskID(params.get(AMParams.TASK_ID));
+//    Job job = appContext.getJob(taskID.getJobId());
+//    Task task = job.getTask(taskID);
+//    TaskAttempt attempt = task.getAttempts().values().iterator().next();
+//    attempt.getReport().setCounters(null);
+//    
+//    WebAppTests.testPage(SingleCounterPage.class, AppContext.class,
+//                         appContext, params);
+//  }
+//  
+//  public static void main(String[] args) {
+//    WebApps.$for("yarn", AppContext.class, new TestAppContext(0, 8, 88, 4)).
+//        at(58888).inDevMode().start(new AMWebApp()).joinThread();
+//  }
+//}

Added: hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebServices.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebServices.java?rev=1376283&view=auto
==============================================================================
--- hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebServices.java (added)
+++ hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebServices.java Wed Aug 22 22:11:39 2012
@@ -0,0 +1,365 @@
+///**
+// * 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.hadoop.mapreduce.v2.app2.webapp;
+//
+//import static org.junit.Assert.assertEquals;
+//import static org.junit.Assert.assertTrue;
+//import static org.junit.Assert.fail;
+//
+//import java.io.StringReader;
+//import java.util.Map;
+//
+//import javax.ws.rs.core.MediaType;
+//import javax.xml.parsers.DocumentBuilder;
+//import javax.xml.parsers.DocumentBuilderFactory;
+//
+//import org.apache.hadoop.conf.Configuration;
+//import org.apache.hadoop.mapreduce.v2.api.records.JobId;
+//import org.apache.hadoop.mapreduce.v2.app2.AppContext;
+//import org.apache.hadoop.mapreduce.v2.app2.MockJobs;
+//import org.apache.hadoop.mapreduce.v2.app2.job.Job;
+//import org.apache.hadoop.yarn.Clock;
+//import org.apache.hadoop.yarn.ClusterInfo;
+//import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
+//import org.apache.hadoop.yarn.api.records.ApplicationId;
+//import org.apache.hadoop.yarn.event.EventHandler;
+//import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
+//import org.apache.hadoop.yarn.webapp.WebServicesTestUtils;
+//import org.codehaus.jettison.json.JSONException;
+//import org.codehaus.jettison.json.JSONObject;
+//import org.junit.Before;
+//import org.junit.Test;
+//import org.w3c.dom.Document;
+//import org.w3c.dom.Element;
+//import org.w3c.dom.NodeList;
+//import org.xml.sax.InputSource;
+//
+//import com.google.inject.Guice;
+//import com.google.inject.Injector;
+//import com.google.inject.servlet.GuiceServletContextListener;
+//import com.google.inject.servlet.ServletModule;
+//import com.sun.jersey.api.client.ClientResponse;
+//import com.sun.jersey.api.client.ClientResponse.Status;
+//import com.sun.jersey.api.client.UniformInterfaceException;
+//import com.sun.jersey.api.client.WebResource;
+//import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
+//import com.sun.jersey.test.framework.JerseyTest;
+//import com.sun.jersey.test.framework.WebAppDescriptor;
+//
+///**
+// * Test the MapReduce Application master info web services api's. Also test
+// * non-existent urls.
+// *
+// *  /ws/v1/mapreduce
+// *  /ws/v1/mapreduce/info
+// */
+//public class TestAMWebServices extends JerseyTest {
+//
+//  private static Configuration conf = new Configuration();
+//  private static TestAppContext appContext;
+//
+//  static class TestAppContext implements AppContext {
+//    final ApplicationAttemptId appAttemptID;
+//    final ApplicationId appID;
+//    final String user = MockJobs.newUserName();
+//    final Map<JobId, Job> jobs;
+//    final long startTime = System.currentTimeMillis();
+//
+//    TestAppContext(int appid, int numJobs, int numTasks, int numAttempts) {
+//      appID = MockJobs.newAppID(appid);
+//      appAttemptID = MockJobs.newAppAttemptID(appID, 0);
+//      jobs = MockJobs.newJobs(appID, numJobs, numTasks, numAttempts);
+//    }
+//
+//    TestAppContext() {
+//      this(0, 1, 1, 1);
+//    }
+//
+//    @Override
+//    public ApplicationAttemptId getApplicationAttemptId() {
+//      return appAttemptID;
+//    }
+//
+//    @Override
+//    public ApplicationId getApplicationID() {
+//      return appID;
+//    }
+//
+//    @Override
+//    public CharSequence getUser() {
+//      return user;
+//    }
+//
+//    @Override
+//    public Job getJob(JobId jobID) {
+//      return jobs.get(jobID);
+//    }
+//
+//    @Override
+//    public Map<JobId, Job> getAllJobs() {
+//      return jobs; // OK
+//    }
+//
+//    @SuppressWarnings("rawtypes")
+//    @Override
+//    public EventHandler getEventHandler() {
+//      return null;
+//    }
+//
+//    @Override
+//    public Clock getClock() {
+//      return null;
+//    }
+//
+//    @Override
+//    public String getApplicationName() {
+//      return "TestApp";
+//    }
+//
+//    @Override
+//    public long getStartTime() {
+//      return startTime;
+//    }
+//
+//    @Override
+//    public ClusterInfo getClusterInfo() {
+//      return null;
+//    }
+//  }
+//
+//  private Injector injector = Guice.createInjector(new ServletModule() {
+//    @Override
+//    protected void configureServlets() {
+//
+//      appContext = new TestAppContext();
+//      bind(JAXBContextResolver.class);
+//      bind(AMWebServices.class);
+//      bind(GenericExceptionHandler.class);
+//      bind(AppContext.class).toInstance(appContext);
+//      bind(Configuration.class).toInstance(conf);
+//
+//      serve("/*").with(GuiceContainer.class);
+//    }
+//  });
+//
+//  public class GuiceServletConfig extends GuiceServletContextListener {
+//
+//    @Override
+//    protected Injector getInjector() {
+//      return injector;
+//    }
+//  }
+//
+//  @Before
+//  @Override
+//  public void setUp() throws Exception {
+//    super.setUp();
+//  }
+//
+//  public TestAMWebServices() {
+//    super(new WebAppDescriptor.Builder(
+//        "org.apache.hadoop.mapreduce.v2.app2.webapp")
+//        .contextListenerClass(GuiceServletConfig.class)
+//        .filterClass(com.google.inject.servlet.GuiceFilter.class)
+//        .contextPath("jersey-guice-filter").servletPath("/").build());
+//  }
+//
+//  @Test
+//  public void testAM() throws JSONException, Exception {
+//    WebResource r = resource();
+//    ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//        .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
+//    assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//    JSONObject json = response.getEntity(JSONObject.class);
+//    assertEquals("incorrect number of elements", 1, json.length());
+//    verifyAMInfo(json.getJSONObject("info"), appContext);
+//  }
+//
+//  @Test
+//  public void testAMSlash() throws JSONException, Exception {
+//    WebResource r = resource();
+//    ClientResponse response = r.path("ws").path("v1").path("mapreduce/")
+//        .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
+//    assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//    JSONObject json = response.getEntity(JSONObject.class);
+//    assertEquals("incorrect number of elements", 1, json.length());
+//    verifyAMInfo(json.getJSONObject("info"), appContext);
+//  }
+//
+//  @Test
+//  public void testAMDefault() throws JSONException, Exception {
+//    WebResource r = resource();
+//    ClientResponse response = r.path("ws").path("v1").path("mapreduce/")
+//        .get(ClientResponse.class);
+//    assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//    JSONObject json = response.getEntity(JSONObject.class);
+//    assertEquals("incorrect number of elements", 1, json.length());
+//    verifyAMInfo(json.getJSONObject("info"), appContext);
+//  }
+//
+//  @Test
+//  public void testAMXML() throws JSONException, Exception {
+//    WebResource r = resource();
+//    ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//        .accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
+//    assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
+//    String xml = response.getEntity(String.class);
+//    verifyAMInfoXML(xml, appContext);
+//  }
+//
+//  @Test
+//  public void testInfo() throws JSONException, Exception {
+//    WebResource r = resource();
+//    ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//        .path("info").accept(MediaType.APPLICATION_JSON)
+//        .get(ClientResponse.class);
+//    assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//    JSONObject json = response.getEntity(JSONObject.class);
+//    assertEquals("incorrect number of elements", 1, json.length());
+//    verifyAMInfo(json.getJSONObject("info"), appContext);
+//  }
+//
+//  @Test
+//  public void testInfoSlash() throws JSONException, Exception {
+//    WebResource r = resource();
+//    ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//        .path("info/").accept(MediaType.APPLICATION_JSON)
+//        .get(ClientResponse.class);
+//    assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//    JSONObject json = response.getEntity(JSONObject.class);
+//    assertEquals("incorrect number of elements", 1, json.length());
+//    verifyAMInfo(json.getJSONObject("info"), appContext);
+//  }
+//
+//  @Test
+//  public void testInfoDefault() throws JSONException, Exception {
+//    WebResource r = resource();
+//    ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//        .path("info/").get(ClientResponse.class);
+//    assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//    JSONObject json = response.getEntity(JSONObject.class);
+//    assertEquals("incorrect number of elements", 1, json.length());
+//    verifyAMInfo(json.getJSONObject("info"), appContext);
+//  }
+//
+//  @Test
+//  public void testInfoXML() throws JSONException, Exception {
+//    WebResource r = resource();
+//    ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//        .path("info/").accept(MediaType.APPLICATION_XML)
+//        .get(ClientResponse.class);
+//    assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
+//    String xml = response.getEntity(String.class);
+//    verifyAMInfoXML(xml, appContext);
+//  }
+//
+//  @Test
+//  public void testInvalidUri() throws JSONException, Exception {
+//    WebResource r = resource();
+//    String responseStr = "";
+//    try {
+//      responseStr = r.path("ws").path("v1").path("mapreduce").path("bogus")
+//          .accept(MediaType.APPLICATION_JSON).get(String.class);
+//      fail("should have thrown exception on invalid uri");
+//    } catch (UniformInterfaceException ue) {
+//      ClientResponse response = ue.getResponse();
+//      assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
+//      WebServicesTestUtils.checkStringMatch(
+//          "error string exists and shouldn't", "", responseStr);
+//    }
+//  }
+//
+//  @Test
+//  public void testInvalidUri2() throws JSONException, Exception {
+//    WebResource r = resource();
+//    String responseStr = "";
+//    try {
+//      responseStr = r.path("ws").path("v1").path("invalid")
+//          .accept(MediaType.APPLICATION_JSON).get(String.class);
+//      fail("should have thrown exception on invalid uri");
+//    } catch (UniformInterfaceException ue) {
+//      ClientResponse response = ue.getResponse();
+//      assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
+//      WebServicesTestUtils.checkStringMatch(
+//          "error string exists and shouldn't", "", responseStr);
+//    }
+//  }
+//
+//  @Test
+//  public void testInvalidAccept() throws JSONException, Exception {
+//    WebResource r = resource();
+//    String responseStr = "";
+//    try {
+//      responseStr = r.path("ws").path("v1").path("mapreduce")
+//          .accept(MediaType.TEXT_PLAIN).get(String.class);
+//      fail("should have thrown exception on invalid uri");
+//    } catch (UniformInterfaceException ue) {
+//      ClientResponse response = ue.getResponse();
+//      assertEquals(Status.INTERNAL_SERVER_ERROR,
+//          response.getClientResponseStatus());
+//      WebServicesTestUtils.checkStringMatch(
+//          "error string exists and shouldn't", "", responseStr);
+//    }
+//  }
+//
+//  public void verifyAMInfo(JSONObject info, TestAppContext ctx)
+//      throws JSONException {
+//    assertEquals("incorrect number of elements", 5, info.length());
+//
+//    verifyAMInfoGeneric(ctx, info.getString("appId"), info.getString("user"),
+//        info.getString("name"), info.getLong("startedOn"),
+//        info.getLong("elapsedTime"));
+//  }
+//
+//  public void verifyAMInfoXML(String xml, TestAppContext ctx)
+//      throws JSONException, Exception {
+//    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+//    DocumentBuilder db = dbf.newDocumentBuilder();
+//    InputSource is = new InputSource();
+//    is.setCharacterStream(new StringReader(xml));
+//    Document dom = db.parse(is);
+//    NodeList nodes = dom.getElementsByTagName("info");
+//    assertEquals("incorrect number of elements", 1, nodes.getLength());
+//
+//    for (int i = 0; i < nodes.getLength(); i++) {
+//      Element element = (Element) nodes.item(i);
+//      verifyAMInfoGeneric(ctx,
+//          WebServicesTestUtils.getXmlString(element, "appId"),
+//          WebServicesTestUtils.getXmlString(element, "user"),
+//          WebServicesTestUtils.getXmlString(element, "name"),
+//          WebServicesTestUtils.getXmlLong(element, "startedOn"),
+//          WebServicesTestUtils.getXmlLong(element, "elapsedTime"));
+//    }
+//  }
+//
+//  public void verifyAMInfoGeneric(TestAppContext ctx, String id, String user,
+//      String name, long startedOn, long elapsedTime) {
+//
+//    WebServicesTestUtils.checkStringMatch("id", ctx.getApplicationID()
+//        .toString(), id);
+//    WebServicesTestUtils.checkStringMatch("user", ctx.getUser().toString(),
+//        user);
+//    WebServicesTestUtils.checkStringMatch("name", ctx.getApplicationName(),
+//        name);
+//
+//    assertEquals("startedOn incorrect", ctx.getStartTime(), startedOn);
+//    assertTrue("elapsedTime not greater then 0", (elapsedTime > 0));
+//
+//  }
+//}

Added: hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebServicesAttempts.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebServicesAttempts.java?rev=1376283&view=auto
==============================================================================
--- hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebServicesAttempts.java (added)
+++ hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebServicesAttempts.java Wed Aug 22 22:11:39 2012
@@ -0,0 +1,738 @@
+///**
+// * 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.hadoop.mapreduce.v2.app2.webapp;
+//
+//import static org.junit.Assert.assertEquals;
+//import static org.junit.Assert.assertNotNull;
+//import static org.junit.Assert.assertTrue;
+//import static org.junit.Assert.fail;
+//
+//import java.io.StringReader;
+//import java.util.List;
+//import java.util.Map;
+//
+//import javax.ws.rs.core.MediaType;
+//import javax.xml.parsers.DocumentBuilder;
+//import javax.xml.parsers.DocumentBuilderFactory;
+//
+//import org.apache.hadoop.conf.Configuration;
+//import org.apache.hadoop.mapreduce.v2.api.records.JobId;
+//import org.apache.hadoop.mapreduce.v2.api.records.TaskAttemptId;
+//import org.apache.hadoop.mapreduce.v2.api.records.TaskType;
+//import org.apache.hadoop.mapreduce.v2.app2.AppContext;
+//import org.apache.hadoop.mapreduce.v2.app2.MockJobs;
+//import org.apache.hadoop.mapreduce.v2.app2.job.Job;
+//import org.apache.hadoop.mapreduce.v2.app2.job.Task;
+//import org.apache.hadoop.mapreduce.v2.app2.job.TaskAttempt;
+//import org.apache.hadoop.mapreduce.v2.util.MRApps;
+//import org.apache.hadoop.yarn.Clock;
+//import org.apache.hadoop.yarn.ClusterInfo;
+//import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
+//import org.apache.hadoop.yarn.api.records.ApplicationId;
+//import org.apache.hadoop.yarn.event.EventHandler;
+//import org.apache.hadoop.yarn.util.ConverterUtils;
+//import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
+//import org.apache.hadoop.yarn.webapp.WebServicesTestUtils;
+//import org.codehaus.jettison.json.JSONArray;
+//import org.codehaus.jettison.json.JSONException;
+//import org.codehaus.jettison.json.JSONObject;
+//import org.junit.Before;
+//import org.junit.Test;
+//import org.w3c.dom.Document;
+//import org.w3c.dom.Element;
+//import org.w3c.dom.NodeList;
+//import org.xml.sax.InputSource;
+//
+//import com.google.inject.Guice;
+//import com.google.inject.Injector;
+//import com.google.inject.servlet.GuiceServletContextListener;
+//import com.google.inject.servlet.ServletModule;
+//import com.sun.jersey.api.client.ClientResponse;
+//import com.sun.jersey.api.client.UniformInterfaceException;
+//import com.sun.jersey.api.client.WebResource;
+//import com.sun.jersey.api.client.ClientResponse.Status;
+//import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
+//import com.sun.jersey.test.framework.JerseyTest;
+//import com.sun.jersey.test.framework.WebAppDescriptor;
+//
+///**
+// * Test the app master web service Rest API for getting task attempts, a
+// * specific task attempt, and task attempt counters
+// *
+// * /ws/v1/mapreduce/jobs/{jobid}/tasks/{taskid}/attempts
+// * /ws/v1/mapreduce/jobs/{jobid}/tasks/{taskid}/attempts/{attemptid}
+// * /ws/v1/mapreduce/jobs/{jobid}/tasks/{taskid}/attempts/{attemptid}/counters
+// */
+//public class TestAMWebServicesAttempts extends JerseyTest {
+//
+//  private static Configuration conf = new Configuration();
+//  private static TestAppContext appContext;
+//
+//  static class TestAppContext implements AppContext {
+//    final ApplicationAttemptId appAttemptID;
+//    final ApplicationId appID;
+//    final String user = MockJobs.newUserName();
+//    final Map<JobId, Job> jobs;
+//    final long startTime = System.currentTimeMillis();
+//
+//    TestAppContext(int appid, int numJobs, int numTasks, int numAttempts) {
+//      appID = MockJobs.newAppID(appid);
+//      appAttemptID = MockJobs.newAppAttemptID(appID, 0);
+//      jobs = MockJobs.newJobs(appID, numJobs, numTasks, numAttempts);
+//    }
+//
+//    TestAppContext() {
+//      this(0, 1, 2, 1);
+//    }
+//
+//    @Override
+//    public ApplicationAttemptId getApplicationAttemptId() {
+//      return appAttemptID;
+//    }
+//
+//    @Override
+//    public ApplicationId getApplicationID() {
+//      return appID;
+//    }
+//
+//    @Override
+//    public CharSequence getUser() {
+//      return user;
+//    }
+//
+//    @Override
+//    public Job getJob(JobId jobID) {
+//      return jobs.get(jobID);
+//    }
+//
+//    @Override
+//    public Map<JobId, Job> getAllJobs() {
+//      return jobs; // OK
+//    }
+//
+//    @SuppressWarnings("rawtypes")
+//    @Override
+//    public EventHandler getEventHandler() {
+//      return null;
+//    }
+//
+//    @Override
+//    public Clock getClock() {
+//      return null;
+//    }
+//
+//    @Override
+//    public String getApplicationName() {
+//      return "TestApp";
+//    }
+//
+//    @Override
+//    public long getStartTime() {
+//      return startTime;
+//    }
+//
+//    @Override
+//    public ClusterInfo getClusterInfo() {
+//      return null;
+//    }
+//  }
+//
+//  private Injector injector = Guice.createInjector(new ServletModule() {
+//    @Override
+//    protected void configureServlets() {
+//
+//      appContext = new TestAppContext();
+//      bind(JAXBContextResolver.class);
+//      bind(AMWebServices.class);
+//      bind(GenericExceptionHandler.class);
+//      bind(AppContext.class).toInstance(appContext);
+//      bind(Configuration.class).toInstance(conf);
+//
+//      serve("/*").with(GuiceContainer.class);
+//    }
+//  });
+//
+//  public class GuiceServletConfig extends GuiceServletContextListener {
+//
+//    @Override
+//    protected Injector getInjector() {
+//      return injector;
+//    }
+//  }
+//
+//  @Before
+//  @Override
+//  public void setUp() throws Exception {
+//    super.setUp();
+//
+//  }
+//
+//  public TestAMWebServicesAttempts() {
+//    super(new WebAppDescriptor.Builder(
+//        "org.apache.hadoop.mapreduce.v2.app2.webapp")
+//        .contextListenerClass(GuiceServletConfig.class)
+//        .filterClass(com.google.inject.servlet.GuiceFilter.class)
+//        .contextPath("jersey-guice-filter").servletPath("/").build());
+//  }
+//
+//  @Test
+//  public void testTaskAttempts() throws JSONException, Exception {
+//    WebResource r = resource();
+//    Map<JobId, Job> jobsMap = appContext.getAllJobs();
+//    for (JobId id : jobsMap.keySet()) {
+//      String jobId = MRApps.toString(id);
+//      for (Task task : jobsMap.get(id).getTasks().values()) {
+//
+//        String tid = MRApps.toString(task.getID());
+//        ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//            .path("jobs").path(jobId).path("tasks").path(tid).path("attempts")
+//            .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
+//        assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//        JSONObject json = response.getEntity(JSONObject.class);
+//        verifyAMTaskAttempts(json, task);
+//      }
+//    }
+//  }
+//
+//  @Test
+//  public void testTaskAttemptsSlash() throws JSONException, Exception {
+//    WebResource r = resource();
+//    Map<JobId, Job> jobsMap = appContext.getAllJobs();
+//    for (JobId id : jobsMap.keySet()) {
+//      String jobId = MRApps.toString(id);
+//      for (Task task : jobsMap.get(id).getTasks().values()) {
+//
+//        String tid = MRApps.toString(task.getID());
+//        ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//            .path("jobs").path(jobId).path("tasks").path(tid).path("attempts/")
+//            .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
+//        assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//        JSONObject json = response.getEntity(JSONObject.class);
+//        verifyAMTaskAttempts(json, task);
+//      }
+//    }
+//  }
+//
+//  @Test
+//  public void testTaskAttemptsDefault() throws JSONException, Exception {
+//    WebResource r = resource();
+//    Map<JobId, Job> jobsMap = appContext.getAllJobs();
+//    for (JobId id : jobsMap.keySet()) {
+//      String jobId = MRApps.toString(id);
+//      for (Task task : jobsMap.get(id).getTasks().values()) {
+//
+//        String tid = MRApps.toString(task.getID());
+//        ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//            .path("jobs").path(jobId).path("tasks").path(tid).path("attempts")
+//            .get(ClientResponse.class);
+//        assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//        JSONObject json = response.getEntity(JSONObject.class);
+//        verifyAMTaskAttempts(json, task);
+//      }
+//    }
+//  }
+//
+//  @Test
+//  public void testTaskAttemptsXML() throws JSONException, Exception {
+//    WebResource r = resource();
+//    Map<JobId, Job> jobsMap = appContext.getAllJobs();
+//    for (JobId id : jobsMap.keySet()) {
+//      String jobId = MRApps.toString(id);
+//      for (Task task : jobsMap.get(id).getTasks().values()) {
+//
+//        String tid = MRApps.toString(task.getID());
+//        ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//            .path("jobs").path(jobId).path("tasks").path(tid).path("attempts")
+//            .accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
+//
+//        assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
+//        String xml = response.getEntity(String.class);
+//        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+//        DocumentBuilder db = dbf.newDocumentBuilder();
+//        InputSource is = new InputSource();
+//        is.setCharacterStream(new StringReader(xml));
+//        Document dom = db.parse(is);
+//        NodeList attempts = dom.getElementsByTagName("taskAttempts");
+//        assertEquals("incorrect number of elements", 1, attempts.getLength());
+//
+//        NodeList nodes = dom.getElementsByTagName("taskAttempt");
+//        verifyAMTaskAttemptsXML(nodes, task);
+//      }
+//    }
+//  }
+//
+//  @Test
+//  public void testTaskAttemptId() throws JSONException, Exception {
+//    WebResource r = resource();
+//    Map<JobId, Job> jobsMap = appContext.getAllJobs();
+//
+//    for (JobId id : jobsMap.keySet()) {
+//      String jobId = MRApps.toString(id);
+//
+//      for (Task task : jobsMap.get(id).getTasks().values()) {
+//        String tid = MRApps.toString(task.getID());
+//
+//        for (TaskAttempt att : task.getAttempts().values()) {
+//          TaskAttemptId attemptid = att.getID();
+//          String attid = MRApps.toString(attemptid);
+//
+//          ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//              .path("jobs").path(jobId).path("tasks").path(tid)
+//              .path("attempts").path(attid).accept(MediaType.APPLICATION_JSON)
+//              .get(ClientResponse.class);
+//          assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//          JSONObject json = response.getEntity(JSONObject.class);
+//          assertEquals("incorrect number of elements", 1, json.length());
+//          JSONObject info = json.getJSONObject("taskAttempt");
+//          verifyAMTaskAttempt(info, att, task.getType());
+//        }
+//      }
+//    }
+//  }
+//
+//  @Test
+//  public void testTaskAttemptIdSlash() throws JSONException, Exception {
+//    WebResource r = resource();
+//    Map<JobId, Job> jobsMap = appContext.getAllJobs();
+//
+//    for (JobId id : jobsMap.keySet()) {
+//      String jobId = MRApps.toString(id);
+//
+//      for (Task task : jobsMap.get(id).getTasks().values()) {
+//        String tid = MRApps.toString(task.getID());
+//
+//        for (TaskAttempt att : task.getAttempts().values()) {
+//          TaskAttemptId attemptid = att.getID();
+//          String attid = MRApps.toString(attemptid);
+//
+//          ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//              .path("jobs").path(jobId).path("tasks").path(tid)
+//              .path("attempts").path(attid + "/")
+//              .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
+//          assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//          JSONObject json = response.getEntity(JSONObject.class);
+//          assertEquals("incorrect number of elements", 1, json.length());
+//          JSONObject info = json.getJSONObject("taskAttempt");
+//          verifyAMTaskAttempt(info, att, task.getType());
+//        }
+//      }
+//    }
+//  }
+//
+//  @Test
+//  public void testTaskAttemptIdDefault() throws JSONException, Exception {
+//    WebResource r = resource();
+//    Map<JobId, Job> jobsMap = appContext.getAllJobs();
+//
+//    for (JobId id : jobsMap.keySet()) {
+//      String jobId = MRApps.toString(id);
+//
+//      for (Task task : jobsMap.get(id).getTasks().values()) {
+//        String tid = MRApps.toString(task.getID());
+//
+//        for (TaskAttempt att : task.getAttempts().values()) {
+//          TaskAttemptId attemptid = att.getID();
+//          String attid = MRApps.toString(attemptid);
+//
+//          ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//              .path("jobs").path(jobId).path("tasks").path(tid)
+//              .path("attempts").path(attid).get(ClientResponse.class);
+//          assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//          JSONObject json = response.getEntity(JSONObject.class);
+//          assertEquals("incorrect number of elements", 1, json.length());
+//          JSONObject info = json.getJSONObject("taskAttempt");
+//          verifyAMTaskAttempt(info, att, task.getType());
+//        }
+//      }
+//    }
+//  }
+//
+//  @Test
+//  public void testTaskAttemptIdXML() throws JSONException, Exception {
+//    WebResource r = resource();
+//    Map<JobId, Job> jobsMap = appContext.getAllJobs();
+//    for (JobId id : jobsMap.keySet()) {
+//      String jobId = MRApps.toString(id);
+//      for (Task task : jobsMap.get(id).getTasks().values()) {
+//
+//        String tid = MRApps.toString(task.getID());
+//        for (TaskAttempt att : task.getAttempts().values()) {
+//          TaskAttemptId attemptid = att.getID();
+//          String attid = MRApps.toString(attemptid);
+//
+//          ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//              .path("jobs").path(jobId).path("tasks").path(tid)
+//              .path("attempts").path(attid).accept(MediaType.APPLICATION_XML)
+//              .get(ClientResponse.class);
+//
+//          assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
+//          String xml = response.getEntity(String.class);
+//          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+//          DocumentBuilder db = dbf.newDocumentBuilder();
+//          InputSource is = new InputSource();
+//          is.setCharacterStream(new StringReader(xml));
+//          Document dom = db.parse(is);
+//          NodeList nodes = dom.getElementsByTagName("taskAttempt");
+//          for (int i = 0; i < nodes.getLength(); i++) {
+//            Element element = (Element) nodes.item(i);
+//            verifyAMTaskAttemptXML(element, att, task.getType());
+//          }
+//        }
+//      }
+//    }
+//  }
+//
+//  @Test
+//  public void testTaskAttemptIdBogus() throws JSONException, Exception {
+//
+//    testTaskAttemptIdErrorGeneric("bogusid",
+//        "java.lang.Exception: TaskAttemptId string : bogusid is not properly formed");
+//  }
+//
+//  @Test
+//  public void testTaskAttemptIdNonExist() throws JSONException, Exception {
+//
+//    testTaskAttemptIdErrorGeneric(
+//        "attempt_0_12345_m_000000_0",
+//        "java.lang.Exception: Error getting info on task attempt id attempt_0_12345_m_000000_0");
+//  }
+//
+//  @Test
+//  public void testTaskAttemptIdInvalid() throws JSONException, Exception {
+//
+//    testTaskAttemptIdErrorGeneric("attempt_0_12345_d_000000_0",
+//        "java.lang.Exception: Bad TaskType identifier. TaskAttemptId string : attempt_0_12345_d_000000_0 is not properly formed.");
+//  }
+//
+//  @Test
+//  public void testTaskAttemptIdInvalid2() throws JSONException, Exception {
+//
+//    testTaskAttemptIdErrorGeneric("attempt_12345_m_000000_0",
+//        "java.lang.Exception: TaskAttemptId string : attempt_12345_m_000000_0 is not properly formed");
+//  }
+//
+//  @Test
+//  public void testTaskAttemptIdInvalid3() throws JSONException, Exception {
+//
+//    testTaskAttemptIdErrorGeneric("attempt_0_12345_m_000000",
+//        "java.lang.Exception: TaskAttemptId string : attempt_0_12345_m_000000 is not properly formed");
+//  }
+//
+//  private void testTaskAttemptIdErrorGeneric(String attid, String error)
+//      throws JSONException, Exception {
+//    WebResource r = resource();
+//    Map<JobId, Job> jobsMap = appContext.getAllJobs();
+//
+//    for (JobId id : jobsMap.keySet()) {
+//      String jobId = MRApps.toString(id);
+//
+//      for (Task task : jobsMap.get(id).getTasks().values()) {
+//        String tid = MRApps.toString(task.getID());
+//
+//        try {
+//          r.path("ws").path("v1").path("mapreduce").path("jobs").path(jobId)
+//              .path("tasks").path(tid).path("attempts").path(attid)
+//              .accept(MediaType.APPLICATION_JSON).get(JSONObject.class);
+//          fail("should have thrown exception on invalid uri");
+//        } catch (UniformInterfaceException ue) {
+//          ClientResponse response = ue.getResponse();
+//          assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
+//          assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//          JSONObject msg = response.getEntity(JSONObject.class);
+//          JSONObject exception = msg.getJSONObject("RemoteException");
+//          assertEquals("incorrect number of elements", 3, exception.length());
+//          String message = exception.getString("message");
+//          String type = exception.getString("exception");
+//          String classname = exception.getString("javaClassName");
+//          WebServicesTestUtils.checkStringMatch("exception message", error,
+//              message);
+//          WebServicesTestUtils.checkStringMatch("exception type",
+//              "NotFoundException", type);
+//          WebServicesTestUtils.checkStringMatch("exception classname",
+//              "org.apache.hadoop.yarn.webapp.NotFoundException", classname);
+//        }
+//      }
+//    }
+//  }
+//
+//  public void verifyAMTaskAttemptXML(Element element, TaskAttempt att,
+//      TaskType ttype) {
+//    verifyTaskAttemptGeneric(att, ttype,
+//        WebServicesTestUtils.getXmlString(element, "id"),
+//        WebServicesTestUtils.getXmlString(element, "state"),
+//        WebServicesTestUtils.getXmlString(element, "type"),
+//        WebServicesTestUtils.getXmlString(element, "rack"),
+//        WebServicesTestUtils.getXmlString(element, "nodeHttpAddress"),
+//        WebServicesTestUtils.getXmlString(element, "diagnostics"),
+//        WebServicesTestUtils.getXmlString(element, "assignedContainerId"),
+//        WebServicesTestUtils.getXmlLong(element, "startTime"),
+//        WebServicesTestUtils.getXmlLong(element, "finishTime"),
+//        WebServicesTestUtils.getXmlLong(element, "elapsedTime"),
+//        WebServicesTestUtils.getXmlFloat(element, "progress"));
+//
+//    if (ttype == TaskType.REDUCE) {
+//      verifyReduceTaskAttemptGeneric(att,
+//          WebServicesTestUtils.getXmlLong(element, "shuffleFinishTime"),
+//          WebServicesTestUtils.getXmlLong(element, "mergeFinishTime"),
+//          WebServicesTestUtils.getXmlLong(element, "elapsedShuffleTime"),
+//          WebServicesTestUtils.getXmlLong(element, "elapsedMergeTime"),
+//          WebServicesTestUtils.getXmlLong(element, "elapsedReduceTime"));
+//    }
+//  }
+//
+//  public void verifyAMTaskAttempt(JSONObject info, TaskAttempt att,
+//      TaskType ttype) throws JSONException {
+//    if (ttype == TaskType.REDUCE) {
+//      assertEquals("incorrect number of elements", 16, info.length());
+//    } else {
+//      assertEquals("incorrect number of elements", 11, info.length());
+//    }
+//
+//    verifyTaskAttemptGeneric(att, ttype, info.getString("id"),
+//        info.getString("state"), info.getString("type"),
+//        info.getString("rack"), info.getString("nodeHttpAddress"),
+//        info.getString("diagnostics"), info.getString("assignedContainerId"),
+//        info.getLong("startTime"), info.getLong("finishTime"),
+//        info.getLong("elapsedTime"), (float) info.getDouble("progress"));
+//
+//    if (ttype == TaskType.REDUCE) {
+//      verifyReduceTaskAttemptGeneric(att, info.getLong("shuffleFinishTime"),
+//          info.getLong("mergeFinishTime"), info.getLong("elapsedShuffleTime"),
+//          info.getLong("elapsedMergeTime"), info.getLong("elapsedReduceTime"));
+//    }
+//  }
+//
+//  public void verifyAMTaskAttempts(JSONObject json, Task task)
+//      throws JSONException {
+//    assertEquals("incorrect number of elements", 1, json.length());
+//    JSONObject attempts = json.getJSONObject("taskAttempts");
+//    assertEquals("incorrect number of elements", 1, json.length());
+//    JSONArray arr = attempts.getJSONArray("taskAttempt");
+//    for (TaskAttempt att : task.getAttempts().values()) {
+//      TaskAttemptId id = att.getID();
+//      String attid = MRApps.toString(id);
+//      Boolean found = false;
+//
+//      for (int i = 0; i < arr.length(); i++) {
+//        JSONObject info = arr.getJSONObject(i);
+//        if (attid.matches(info.getString("id"))) {
+//          found = true;
+//          verifyAMTaskAttempt(info, att, task.getType());
+//        }
+//      }
+//      assertTrue("task attempt with id: " + attid
+//          + " not in web service output", found);
+//    }
+//  }
+//
+//  public void verifyAMTaskAttemptsXML(NodeList nodes, Task task) {
+//    assertEquals("incorrect number of elements", 1, nodes.getLength());
+//
+//    for (TaskAttempt att : task.getAttempts().values()) {
+//      TaskAttemptId id = att.getID();
+//      String attid = MRApps.toString(id);
+//      Boolean found = false;
+//      for (int i = 0; i < nodes.getLength(); i++) {
+//        Element element = (Element) nodes.item(i);
+//
+//        if (attid.matches(WebServicesTestUtils.getXmlString(element, "id"))) {
+//          found = true;
+//          verifyAMTaskAttemptXML(element, att, task.getType());
+//        }
+//      }
+//      assertTrue("task with id: " + attid + " not in web service output", found);
+//    }
+//  }
+//
+//  public void verifyTaskAttemptGeneric(TaskAttempt ta, TaskType ttype,
+//      String id, String state, String type, String rack,
+//      String nodeHttpAddress, String diagnostics, String assignedContainerId,
+//      long startTime, long finishTime, long elapsedTime, float progress) {
+//
+//    TaskAttemptId attid = ta.getID();
+//    String attemptId = MRApps.toString(attid);
+//
+//    WebServicesTestUtils.checkStringMatch("id", attemptId, id);
+//    WebServicesTestUtils.checkStringMatch("type", ttype.toString(), type);
+//    WebServicesTestUtils.checkStringMatch("state", ta.getState().toString(),
+//        state);
+//    WebServicesTestUtils.checkStringMatch("rack", ta.getNodeRackName(), rack);
+//    WebServicesTestUtils.checkStringMatch("nodeHttpAddress",
+//        ta.getNodeHttpAddress(), nodeHttpAddress);
+//
+//    String expectDiag = "";
+//    List<String> diagnosticsList = ta.getDiagnostics();
+//    if (diagnosticsList != null && !diagnostics.isEmpty()) {
+//      StringBuffer b = new StringBuffer();
+//      for (String diag : diagnosticsList) {
+//        b.append(diag);
+//      }
+//      expectDiag = b.toString();
+//    }
+//    WebServicesTestUtils.checkStringMatch("diagnostics", expectDiag,
+//        diagnostics);
+//    WebServicesTestUtils.checkStringMatch("assignedContainerId",
+//        ConverterUtils.toString(ta.getAssignedContainerID()),
+//        assignedContainerId);
+//
+//    assertEquals("startTime wrong", ta.getLaunchTime(), startTime);
+//    assertEquals("finishTime wrong", ta.getFinishTime(), finishTime);
+//    assertEquals("elapsedTime wrong", finishTime - startTime, elapsedTime);
+//    assertEquals("progress wrong", ta.getProgress() * 100, progress, 1e-3f);
+//  }
+//
+//  public void verifyReduceTaskAttemptGeneric(TaskAttempt ta,
+//      long shuffleFinishTime, long mergeFinishTime, long elapsedShuffleTime,
+//      long elapsedMergeTime, long elapsedReduceTime) {
+//
+//    assertEquals("shuffleFinishTime wrong", ta.getShuffleFinishTime(),
+//        shuffleFinishTime);
+//    assertEquals("mergeFinishTime wrong", ta.getSortFinishTime(),
+//        mergeFinishTime);
+//    assertEquals("elapsedShuffleTime wrong",
+//        ta.getLaunchTime() - ta.getShuffleFinishTime(), elapsedShuffleTime);
+//    assertEquals("elapsedMergeTime wrong",
+//        ta.getShuffleFinishTime() - ta.getSortFinishTime(), elapsedMergeTime);
+//    assertEquals("elapsedReduceTime wrong",
+//        ta.getSortFinishTime() - ta.getFinishTime(), elapsedReduceTime);
+//  }
+//
+//  @Test
+//  public void testTaskAttemptIdCounters() throws JSONException, Exception {
+//    WebResource r = resource();
+//    Map<JobId, Job> jobsMap = appContext.getAllJobs();
+//
+//    for (JobId id : jobsMap.keySet()) {
+//      String jobId = MRApps.toString(id);
+//
+//      for (Task task : jobsMap.get(id).getTasks().values()) {
+//        String tid = MRApps.toString(task.getID());
+//
+//        for (TaskAttempt att : task.getAttempts().values()) {
+//          TaskAttemptId attemptid = att.getID();
+//          String attid = MRApps.toString(attemptid);
+//
+//          ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//              .path("jobs").path(jobId).path("tasks").path(tid)
+//              .path("attempts").path(attid).path("counters")
+//              .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
+//          assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//          JSONObject json = response.getEntity(JSONObject.class);
+//          assertEquals("incorrect number of elements", 1, json.length());
+//          JSONObject info = json.getJSONObject("jobTaskAttemptCounters");
+//          verifyAMJobTaskAttemptCounters(info, att);
+//        }
+//      }
+//    }
+//  }
+//
+//  @Test
+//  public void testTaskAttemptIdXMLCounters() throws JSONException, Exception {
+//    WebResource r = resource();
+//    Map<JobId, Job> jobsMap = appContext.getAllJobs();
+//    for (JobId id : jobsMap.keySet()) {
+//      String jobId = MRApps.toString(id);
+//      for (Task task : jobsMap.get(id).getTasks().values()) {
+//
+//        String tid = MRApps.toString(task.getID());
+//        for (TaskAttempt att : task.getAttempts().values()) {
+//          TaskAttemptId attemptid = att.getID();
+//          String attid = MRApps.toString(attemptid);
+//
+//          ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//              .path("jobs").path(jobId).path("tasks").path(tid)
+//              .path("attempts").path(attid).path("counters")
+//              .accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
+//
+//          assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
+//          String xml = response.getEntity(String.class);
+//          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+//          DocumentBuilder db = dbf.newDocumentBuilder();
+//          InputSource is = new InputSource();
+//          is.setCharacterStream(new StringReader(xml));
+//          Document dom = db.parse(is);
+//          NodeList nodes = dom.getElementsByTagName("jobTaskAttemptCounters");
+//
+//          verifyAMTaskCountersXML(nodes, att);
+//        }
+//      }
+//    }
+//  }
+//
+//  public void verifyAMJobTaskAttemptCounters(JSONObject info, TaskAttempt att)
+//      throws JSONException {
+//
+//    assertEquals("incorrect number of elements", 2, info.length());
+//
+//    WebServicesTestUtils.checkStringMatch("id", MRApps.toString(att.getID()),
+//        info.getString("id"));
+//
+//    // just do simple verification of fields - not data is correct
+//    // in the fields
+//    JSONArray counterGroups = info.getJSONArray("taskAttemptCounterGroup");
+//    for (int i = 0; i < counterGroups.length(); i++) {
+//      JSONObject counterGroup = counterGroups.getJSONObject(i);
+//      String name = counterGroup.getString("counterGroupName");
+//      assertTrue("name not set", (name != null && !name.isEmpty()));
+//      JSONArray counters = counterGroup.getJSONArray("counter");
+//      for (int j = 0; j < counters.length(); j++) {
+//        JSONObject counter = counters.getJSONObject(j);
+//        String counterName = counter.getString("name");
+//        assertTrue("name not set",
+//            (counterName != null && !counterName.isEmpty()));
+//        long value = counter.getLong("value");
+//        assertTrue("value  >= 0", value >= 0);
+//      }
+//    }
+//  }
+//
+//  public void verifyAMTaskCountersXML(NodeList nodes, TaskAttempt att) {
+//
+//    for (int i = 0; i < nodes.getLength(); i++) {
+//
+//      Element element = (Element) nodes.item(i);
+//      WebServicesTestUtils.checkStringMatch("id", MRApps.toString(att.getID()),
+//          WebServicesTestUtils.getXmlString(element, "id"));
+//      // just do simple verification of fields - not data is correct
+//      // in the fields
+//      NodeList groups = element.getElementsByTagName("taskAttemptCounterGroup");
+//
+//      for (int j = 0; j < groups.getLength(); j++) {
+//        Element counters = (Element) groups.item(j);
+//        assertNotNull("should have counters in the web service info", counters);
+//        String name = WebServicesTestUtils.getXmlString(counters,
+//            "counterGroupName");
+//        assertTrue("name not set", (name != null && !name.isEmpty()));
+//        NodeList counterArr = counters.getElementsByTagName("counter");
+//        for (int z = 0; z < counterArr.getLength(); z++) {
+//          Element counter = (Element) counterArr.item(z);
+//          String counterName = WebServicesTestUtils.getXmlString(counter,
+//              "name");
+//          assertTrue("counter name not set",
+//              (counterName != null && !counterName.isEmpty()));
+//
+//          long value = WebServicesTestUtils.getXmlLong(counter, "value");
+//          assertTrue("value not >= 0", value >= 0);
+//
+//        }
+//      }
+//    }
+//  }
+//
+//}

Added: hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebServicesJobConf.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebServicesJobConf.java?rev=1376283&view=auto
==============================================================================
--- hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebServicesJobConf.java (added)
+++ hadoop/common/branches/MR-3902/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-app2/src/test/java/org/apache/hadoop/mapreduce/v2/app2/webapp/TestAMWebServicesJobConf.java Wed Aug 22 22:11:39 2012
@@ -0,0 +1,342 @@
+///**
+// * 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.hadoop.mapreduce.v2.app2.webapp;
+//
+//import static org.junit.Assert.assertEquals;
+//import static org.junit.Assert.assertNotNull;
+//import static org.junit.Assert.assertTrue;
+//import static org.junit.Assert.fail;
+//
+//import java.io.File;
+//import java.io.IOException;
+//import java.io.OutputStream;
+//import java.io.StringReader;
+//import java.util.Map;
+//
+//import javax.ws.rs.core.MediaType;
+//import javax.xml.parsers.DocumentBuilder;
+//import javax.xml.parsers.DocumentBuilderFactory;
+//
+//import org.apache.hadoop.conf.Configuration;
+//import org.apache.hadoop.fs.FileSystem;
+//import org.apache.hadoop.fs.FileUtil;
+//import org.apache.hadoop.fs.Path;
+//import org.apache.hadoop.mapreduce.MRJobConfig;
+//import org.apache.hadoop.mapreduce.v2.api.records.JobId;
+//import org.apache.hadoop.mapreduce.v2.app2.AppContext;
+//import org.apache.hadoop.mapreduce.v2.app2.MockJobs;
+//import org.apache.hadoop.mapreduce.v2.app2.job.Job;
+//import org.apache.hadoop.mapreduce.v2.util.MRApps;
+//import org.apache.hadoop.yarn.Clock;
+//import org.apache.hadoop.yarn.ClusterInfo;
+//import org.apache.hadoop.yarn.api.records.ApplicationAttemptId;
+//import org.apache.hadoop.yarn.api.records.ApplicationId;
+//import org.apache.hadoop.yarn.event.EventHandler;
+//import org.apache.hadoop.yarn.webapp.GenericExceptionHandler;
+//import org.apache.hadoop.yarn.webapp.WebServicesTestUtils;
+//import org.codehaus.jettison.json.JSONArray;
+//import org.codehaus.jettison.json.JSONException;
+//import org.codehaus.jettison.json.JSONObject;
+//import org.junit.AfterClass;
+//import org.junit.Before;
+//import org.junit.Test;
+//import org.w3c.dom.Document;
+//import org.w3c.dom.Element;
+//import org.w3c.dom.NodeList;
+//import org.xml.sax.InputSource;
+//
+//import com.google.common.collect.Maps;
+//import com.google.inject.Guice;
+//import com.google.inject.Injector;
+//import com.google.inject.servlet.GuiceServletContextListener;
+//import com.google.inject.servlet.ServletModule;
+//import com.sun.jersey.api.client.ClientResponse;
+//import com.sun.jersey.api.client.WebResource;
+//import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
+//import com.sun.jersey.test.framework.JerseyTest;
+//import com.sun.jersey.test.framework.WebAppDescriptor;
+//
+///**
+// * Test the app master web service Rest API for getting the job conf. This
+// * requires created a temporary configuration file.
+// *
+// *   /ws/v1/mapreduce/job/{jobid}/conf
+// */
+//public class TestAMWebServicesJobConf extends JerseyTest {
+//
+//  private static Configuration conf = new Configuration();
+//  private static TestAppContext appContext;
+//
+//  private static File testConfDir = new File("target",
+//      TestAMWebServicesJobConf.class.getSimpleName() + "confDir");
+//
+//  static class TestAppContext implements AppContext {
+//    final ApplicationAttemptId appAttemptID;
+//    final ApplicationId appID;
+//    final String user = MockJobs.newUserName();
+//    final Map<JobId, Job> jobs;
+//    final long startTime = System.currentTimeMillis();
+//
+//    TestAppContext(int appid, int numTasks, int numAttempts, Path confPath) {
+//      appID = MockJobs.newAppID(appid);
+//      appAttemptID = MockJobs.newAppAttemptID(appID, 0);
+//      Map<JobId, Job> map = Maps.newHashMap();
+//      Job job = MockJobs.newJob(appID, 0, numTasks, numAttempts, confPath);
+//      map.put(job.getID(), job);
+//      jobs = map;
+//    }
+//
+//    @Override
+//    public ApplicationAttemptId getApplicationAttemptId() {
+//      return appAttemptID;
+//    }
+//
+//    @Override
+//    public ApplicationId getApplicationID() {
+//      return appID;
+//    }
+//
+//    @Override
+//    public CharSequence getUser() {
+//      return user;
+//    }
+//
+//    @Override
+//    public Job getJob(JobId jobID) {
+//      return jobs.get(jobID);
+//    }
+//
+//    @Override
+//    public Map<JobId, Job> getAllJobs() {
+//      return jobs; // OK
+//    }
+//
+//    @SuppressWarnings("rawtypes")
+//    @Override
+//    public EventHandler getEventHandler() {
+//      return null;
+//    }
+//
+//    @Override
+//    public Clock getClock() {
+//      return null;
+//    }
+//
+//    @Override
+//    public String getApplicationName() {
+//      return "TestApp";
+//    }
+//
+//    @Override
+//    public long getStartTime() {
+//      return startTime;
+//    }
+//
+//    @Override
+//    public ClusterInfo getClusterInfo() {
+//      return null;
+//    }
+//  }
+//
+//  private Injector injector = Guice.createInjector(new ServletModule() {
+//    @Override
+//    protected void configureServlets() {
+//
+//      Path confPath = new Path(testConfDir.toString(),
+//          MRJobConfig.JOB_CONF_FILE);
+//      Configuration config = new Configuration();
+//
+//      FileSystem localFs;
+//      try {
+//        localFs = FileSystem.getLocal(config);
+//        confPath = localFs.makeQualified(confPath);
+//
+//        OutputStream out = localFs.create(confPath);
+//        try {
+//          conf.writeXml(out);
+//        } finally {
+//          out.close();
+//        }
+//        if (!localFs.exists(confPath)) {
+//          fail("error creating config file: " + confPath);
+//        }
+//
+//      } catch (IOException e) {
+//        fail("error creating config file: " + e.getMessage());
+//      }
+//
+//      appContext = new TestAppContext(0, 2, 1, confPath);
+//
+//      bind(JAXBContextResolver.class);
+//      bind(AMWebServices.class);
+//      bind(GenericExceptionHandler.class);
+//      bind(AppContext.class).toInstance(appContext);
+//      bind(Configuration.class).toInstance(conf);
+//
+//      serve("/*").with(GuiceContainer.class);
+//    }
+//  });
+//
+//  public class GuiceServletConfig extends GuiceServletContextListener {
+//
+//    @Override
+//    protected Injector getInjector() {
+//      return injector;
+//    }
+//  }
+//
+//  @Before
+//  @Override
+//  public void setUp() throws Exception {
+//    super.setUp();
+//    testConfDir.mkdir();
+//
+//  }
+//
+//  @AfterClass
+//  static public void stop() {
+//    FileUtil.fullyDelete(testConfDir);
+//  }
+//
+//  public TestAMWebServicesJobConf() {
+//    super(new WebAppDescriptor.Builder(
+//        "org.apache.hadoop.mapreduce.v2.app2.webapp")
+//        .contextListenerClass(GuiceServletConfig.class)
+//        .filterClass(com.google.inject.servlet.GuiceFilter.class)
+//        .contextPath("jersey-guice-filter").servletPath("/").build());
+//  }
+//
+//  @Test
+//  public void testJobConf() throws JSONException, Exception {
+//    WebResource r = resource();
+//    Map<JobId, Job> jobsMap = appContext.getAllJobs();
+//    for (JobId id : jobsMap.keySet()) {
+//      String jobId = MRApps.toString(id);
+//
+//      ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//          .path("jobs").path(jobId).path("conf")
+//          .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
+//      assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//      JSONObject json = response.getEntity(JSONObject.class);
+//      assertEquals("incorrect number of elements", 1, json.length());
+//      JSONObject info = json.getJSONObject("conf");
+//      verifyAMJobConf(info, jobsMap.get(id));
+//    }
+//  }
+//
+//  @Test
+//  public void testJobConfSlash() throws JSONException, Exception {
+//    WebResource r = resource();
+//    Map<JobId, Job> jobsMap = appContext.getAllJobs();
+//    for (JobId id : jobsMap.keySet()) {
+//      String jobId = MRApps.toString(id);
+//
+//      ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//          .path("jobs").path(jobId).path("conf/")
+//          .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
+//      assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//      JSONObject json = response.getEntity(JSONObject.class);
+//      assertEquals("incorrect number of elements", 1, json.length());
+//      JSONObject info = json.getJSONObject("conf");
+//      verifyAMJobConf(info, jobsMap.get(id));
+//    }
+//  }
+//
+//  @Test
+//  public void testJobConfDefault() throws JSONException, Exception {
+//    WebResource r = resource();
+//    Map<JobId, Job> jobsMap = appContext.getAllJobs();
+//    for (JobId id : jobsMap.keySet()) {
+//      String jobId = MRApps.toString(id);
+//
+//      ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//          .path("jobs").path(jobId).path("conf").get(ClientResponse.class);
+//      assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
+//      JSONObject json = response.getEntity(JSONObject.class);
+//      assertEquals("incorrect number of elements", 1, json.length());
+//      JSONObject info = json.getJSONObject("conf");
+//      verifyAMJobConf(info, jobsMap.get(id));
+//    }
+//  }
+//
+//  @Test
+//  public void testJobConfXML() throws JSONException, Exception {
+//    WebResource r = resource();
+//    Map<JobId, Job> jobsMap = appContext.getAllJobs();
+//    for (JobId id : jobsMap.keySet()) {
+//      String jobId = MRApps.toString(id);
+//
+//      ClientResponse response = r.path("ws").path("v1").path("mapreduce")
+//          .path("jobs").path(jobId).path("conf")
+//          .accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
+//      assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
+//      String xml = response.getEntity(String.class);
+//      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+//      DocumentBuilder db = dbf.newDocumentBuilder();
+//      InputSource is = new InputSource();
+//      is.setCharacterStream(new StringReader(xml));
+//      Document dom = db.parse(is);
+//      NodeList info = dom.getElementsByTagName("conf");
+//      verifyAMJobConfXML(info, jobsMap.get(id));
+//    }
+//  }
+//
+//  public void verifyAMJobConf(JSONObject info, Job job) throws JSONException {
+//
+//    assertEquals("incorrect number of elements", 2, info.length());
+//
+//    WebServicesTestUtils.checkStringMatch("path", job.getConfFile().toString(),
+//        info.getString("path"));
+//    // just do simple verification of fields - not data is correct
+//    // in the fields
+//    JSONArray properties = info.getJSONArray("property");
+//    for (int i = 0; i < properties.length(); i++) {
+//      JSONObject prop = properties.getJSONObject(i);
+//      String name = prop.getString("name");
+//      String value = prop.getString("value");
+//      assertTrue("name not set", (name != null && !name.isEmpty()));
+//      assertTrue("value not set", (value != null && !value.isEmpty()));
+//    }
+//  }
+//
+//  public void verifyAMJobConfXML(NodeList nodes, Job job) {
+//
+//    assertEquals("incorrect number of elements", 1, nodes.getLength());
+//
+//    for (int i = 0; i < nodes.getLength(); i++) {
+//      Element element = (Element) nodes.item(i);
+//      WebServicesTestUtils.checkStringMatch("path", job.getConfFile()
+//          .toString(), WebServicesTestUtils.getXmlString(element, "path"));
+//
+//      // just do simple verification of fields - not data is correct
+//      // in the fields
+//      NodeList properties = element.getElementsByTagName("property");
+//
+//      for (int j = 0; j < properties.getLength(); j++) {
+//        Element property = (Element) properties.item(j);
+//        assertNotNull("should have counters in the web service info", property);
+//        String name = WebServicesTestUtils.getXmlString(property, "name");
+//        String value = WebServicesTestUtils.getXmlString(property, "value");
+//        assertTrue("name not set", (name != null && !name.isEmpty()));
+//        assertTrue("name not set", (value != null && !value.isEmpty()));
+//      }
+//    }
+//  }
+//
+//}



Mime
View raw message