package org.apache.helix.integration.task;

/*
 * 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.
 */
import org.apache.helix.HelixManagerFactory;
import org.apache.helix.InstanceType;
import org.apache.helix.TestHelper;
import org.apache.helix.manager.zk.ZkClient;
import org.apache.helix.task.JobConfig;
import org.apache.helix.task.JobContext;
import org.apache.helix.task.TaskDriver;
import org.apache.helix.task.Workflow;
import org.apache.helix.task.WorkflowConfig;
import org.apache.helix.task.WorkflowContext;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TestRetrieveWorkflows extends TaskTestBase {
  @Test
  public void testGetAllWorkflows() throws Exception {
    List<Workflow> workflowList = new ArrayList<Workflow>();
    for (int i = 0; i < 4; i++) {
      Workflow workflow = WorkflowGenerator.generateDefaultRepeatedJobWorkflowBuilder(TestHelper.getTestMethodName() + i).build();
      _driver.start(workflow);
      workflowList.add(workflow);
    }

    for (Workflow workflow : workflowList) {
      //TaskTestUtil.pollForWorkflowState(_driver, workflow.getName(), TaskState.COMPLETED);
    }

    _manager = HelixManagerFactory
        .getZKHelixManager("ESPRESSO_TEST_NUAGE", "Admin", InstanceType.ADMINISTRATOR, "zk-ei1-espresso.stg.linkedin.com:12913");
    _manager.connect();
    TaskDriver taskDriver = new TaskDriver(_manager);

    Map<String, WorkflowConfig> workflowConfigMap = taskDriver.getWorkflows();
    Assert.assertEquals(workflowConfigMap.size(), workflowList.size());

    for(Map.Entry<String, WorkflowConfig> workflow :  workflowConfigMap.entrySet()) {
      WorkflowConfig workflowConfig = workflow.getValue();
      WorkflowContext workflowContext = _driver.getWorkflowContext(workflow.getKey());
      Assert.assertNotNull(workflowContext);

      for(String job : workflowConfig.getJobDag().getAllNodes()) {
        JobConfig jobConfig = _driver.getJobConfig(job);
        JobContext jobContext = _driver.getJobContext(job);

        Assert.assertNotNull(jobConfig);
        Assert.assertNotNull(jobContext);
      }
    }
  }
}


@@ -873,146 +873,18 @@
 lix.
-HelixManagerFactory;%0Aimport org.apache.helix.InstanceType;%0Aimport org.apache.helix.TestHelper;%0Aimport org.apache.helix.manager.zk.ZkClient
+TestHelper
 ;%0Aim
@@ -995,22 +995,21 @@
 ask.Task
-Driver
+State
 ;%0Aimport
@@ -1334,18 +1334,16 @@
 %0A  @Test
-%0A 
  public 
@@ -1477,9 +1477,9 @@
 i %3C 
-4
+2
 ; i+
@@ -1526,16 +1526,27 @@
 enerator
+%0A          
 .generat
@@ -1748,18 +1748,16 @@
 %7B%0A      
-//
 TaskTest
@@ -1844,252 +1844,8 @@
  %7D%0A%0A
-    _manager = HelixManagerFactory%0A        .getZKHelixManager(%22ESPRESSO_TEST_NUAGE%22, %22Admin%22, InstanceType.ADMINISTRATOR, %22zk-ei1-espresso.stg.linkedin.com:12913%22);%0A    _manager.connect();%0A    TaskDriver taskDriver = new TaskDriver(_manager);%0A%0A
     
@@ -1896,13 +1896,10 @@
 p = 
-taskD
+_d
 rive
@@ -1996,16 +1996,17 @@
 %0A    for
+ 
 (Map.Ent
@@ -2042,17 +2042,16 @@
 rkflow :
- 
  workflo
@@ -2276,16 +2276,17 @@
      for
+ 
 (String 

Add unit tests to retrieve all workflows and job info from a cluster.