<commit_before>package com.github.jmchilton.blend4j.galaxy;

import com.github.jmchilton.blend4j.galaxy.beans.Workflow;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowDetails;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowInputDefinition;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowInputs;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowInputs.ExistingHistory;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowInputs.InputSourceType;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowInputs.WorkflowInput;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowOutputs;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowStepDefinition;
import com.github.jmchilton.blend4j.galaxy.beans.collection.request.CollectionDescription;
import com.github.jmchilton.blend4j.galaxy.beans.collection.request.HistoryDatasetElement;
import com.github.jmchilton.blend4j.galaxy.beans.collection.response.CollectionResponse;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class WorkflowsTest {
  private static final String TEST_WORKFLOW_NAME = "TestWorkflow1";
  private static final String TEST_WORKFLOW_RANDOMLINES = "TestWorkflowRandomlines";
  private GalaxyInstance instance;
  private WorkflowsClient client;

  @BeforeMethod
  public void init() throws URISyntaxException, IOException {
    //TestGalaxyInstance.bootStrapGalaxy();
    instance = TestGalaxyInstance.get();
    client = instance.getWorkflowsClient();
  }
  
  private String ensureHasTestWorkflow1() {
    return ensureHasWorkflow(TEST_WORKFLOW_NAME);
  }
  
  private String ensureHasWorkflow(final String workflowName) {
    String workflowId = null;
    for(Workflow workflow : client.getWorkflows()) {
      if(workflow.getName().startsWith(workflowName)) {
        workflowId = workflow.getId();
        break;
      }
    }
    if(workflowId == null) {
      final String workflowContents;
      try {
        workflowContents = Resources.asCharSource(getClass().getResource(workflowName + ".ga"), Charsets.UTF_8).read();
      } catch(IOException ex) {
        throw new RuntimeException(ex);
      }
      client.importWorkflow(workflowContents);
      workflowId = getTestWorkflowId(workflowName);
    }
    return workflowId;
  }
  
  /**
   * Constructs a list collection from the given files within the given history.
   * @param historyId  The id of the history to build the collection within.
   * @param inputIds  The ids of the files to add to the collection.
   * @return  A CollectionResponse object for the constructed collection.
   */
  private CollectionResponse constructFileCollectionList(String historyId, List<String> inputIds) {
    HistoriesClient historiesClient = instance.getHistoriesClient();
    
    CollectionDescription collectionDescription = new CollectionDescription();
    collectionDescription.setCollectionType("list");
    collectionDescription.setName("collection");
    
    for (String inputId : inputIds) {
      HistoryDatasetElement element = new HistoryDatasetElement();
      element.setId(inputId);
      element.setName(inputId);
      
      collectionDescription.addDatasetElement(element);
    }
    
    return historiesClient.createDatasetCollection(historyId, collectionDescription);
  }
  

  /**
   * Tests execution of a workflow on a list collection of files and passing.
   * @throws InterruptedException 
   */
  @Test
  public void testRunWorkflowCollectionListPass() throws InterruptedException {
    WorkflowsClient workflowsClient = instance.getWorkflowsClient();
    
    String historyId = TestHelpers.getTestHistoryId(instance);
    List<String> ids = TestHelpers.populateTestDatasets(instance, historyId, 2);
    String workflowId = ensureHasWorkflow("TestWorkflowCollectionList");
    
    CollectionResponse collectionResponse = constructFileCollectionList(historyId, ids);
    Assert.assertNotNull(collectionResponse.getId());
    
    WorkflowDetails workflowDetails = client.showWorkflow(workflowId);
    Assert.assertNotNull(workflowDetails.getId());
    
    String workflowInputId = getWorkflowInputId(workflowDetails, "input_list");
    Assert.assertNotNull(workflowInputId);
    
    WorkflowInputs workflowInputs = new WorkflowInputs();
    workflowInputs.setDestination(new WorkflowInputs.ExistingHistory(historyId));
    workflowInputs.setWorkflowId(workflowId);
    workflowInputs.setInput(workflowId, 
        new WorkflowInputs.WorkflowInput(collectionResponse.getId(),
            WorkflowInputs.InputSourceType.HDCA));
    
    WorkflowOutputs outputs = workflowsClient.runWorkflow(workflowInputs);
    Assert.assertNotNull(outputs);
    Assert.assertNotNull(outputs.getOutputIds());
    Assert.assertEquals(1, outputs.getOutputIds().size());
    
    String outputId = outputs.getOutputIds().get(0); 
    Assert.assertNotNull(outputId);
  }
  
  @Test
  public void testExportWorkflow() {<commit_msg>Workflow with collections list now passes<commit_after>package com.github.jmchilton.blend4j.galaxy;

import com.github.jmchilton.blend4j.galaxy.beans.Workflow;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowDetails;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowInputDefinition;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowInputs;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowInputs.ExistingHistory;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowInputs.InputSourceType;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowInputs.WorkflowInput;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowOutputs;
import com.github.jmchilton.blend4j.galaxy.beans.WorkflowStepDefinition;
import com.github.jmchilton.blend4j.galaxy.beans.collection.request.CollectionDescription;
import com.github.jmchilton.blend4j.galaxy.beans.collection.request.HistoryDatasetElement;
import com.github.jmchilton.blend4j.galaxy.beans.collection.response.CollectionResponse;
import com.google.common.base.Charsets;
import com.google.common.io.Resources;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class WorkflowsTest {
  private static final String TEST_WORKFLOW_NAME = "TestWorkflow1";
  private static final String TEST_WORKFLOW_RANDOMLINES = "TestWorkflowRandomlines";
  private GalaxyInstance instance;
  private WorkflowsClient client;

  @BeforeMethod
  public void init() throws URISyntaxException, IOException {
    //TestGalaxyInstance.bootStrapGalaxy();
    instance = TestGalaxyInstance.get();
    client = instance.getWorkflowsClient();
  }
  
  private String ensureHasTestWorkflow1() {
    return ensureHasWorkflow(TEST_WORKFLOW_NAME);
  }
  
  private String ensureHasWorkflow(final String workflowName) {
    String workflowId = null;
    for(Workflow workflow : client.getWorkflows()) {
      if(workflow.getName().startsWith(workflowName)) {
        workflowId = workflow.getId();
        break;
      }
    }
    if(workflowId == null) {
      final String workflowContents;
      try {
        workflowContents = Resources.asCharSource(getClass().getResource(workflowName + ".ga"), Charsets.UTF_8).read();
      } catch(IOException ex) {
        throw new RuntimeException(ex);
      }
      client.importWorkflow(workflowContents);
      workflowId = getTestWorkflowId(workflowName);
    }
    return workflowId;
  }
  
  /**
   * Constructs a list collection from the given files within the given history.
   * @param historyId  The id of the history to build the collection within.
   * @param inputIds  The ids of the files to add to the collection.
   * @return  A CollectionResponse object for the constructed collection.
   */
  private CollectionResponse constructFileCollectionList(String historyId, List<String> inputIds) {
    HistoriesClient historiesClient = instance.getHistoriesClient();
    
    CollectionDescription collectionDescription = new CollectionDescription();
    collectionDescription.setCollectionType("list");
    collectionDescription.setName("collection");
    
    for (String inputId : inputIds) {
      HistoryDatasetElement element = new HistoryDatasetElement();
      element.setId(inputId);
      element.setName(inputId);
      
      collectionDescription.addDatasetElement(element);
    }
    
    return historiesClient.createDatasetCollection(historyId, collectionDescription);
  }
  

  /**
   * Tests execution of a workflow on a list collection of files and passing.
   * @throws InterruptedException 
   */
  @Test
  public void testRunWorkflowCollectionListPass() throws InterruptedException {
    WorkflowsClient workflowsClient = instance.getWorkflowsClient();
    
    String historyId = TestHelpers.getTestHistoryId(instance);
    List<String> ids = TestHelpers.populateTestDatasets(instance, historyId, 2);
    String workflowId = ensureHasWorkflow("TestWorkflowCollectionList");
    
    CollectionResponse collectionResponse = constructFileCollectionList(historyId, ids);
    Assert.assertNotNull(collectionResponse.getId());
    
    WorkflowDetails workflowDetails = client.showWorkflow(workflowId);
    Assert.assertNotNull(workflowDetails.getId());
    
    String workflowInputId = getWorkflowInputId(workflowDetails, "input_list");
    Assert.assertNotNull(workflowInputId);
    
    WorkflowInputs workflowInputs = new WorkflowInputs();
    workflowInputs.setDestination(new WorkflowInputs.ExistingHistory(historyId));
    workflowInputs.setWorkflowId(workflowId);
    workflowInputs.setInput(workflowInputId,
        new WorkflowInputs.WorkflowInput(collectionResponse.getId(),
            WorkflowInputs.InputSourceType.HDCA));
    
    WorkflowOutputs outputs = workflowsClient.runWorkflow(workflowInputs);
    Assert.assertNotNull(outputs);
    Assert.assertNotNull(outputs.getOutputIds());
    Assert.assertEquals(1, outputs.getOutputIds().size());
    
    String outputId = outputs.getOutputIds().get(0);
    Assert.assertNotNull(outputId);
  }
  
  @Test
  public void testExportWorkflow() {
{'fraction_non_alphanumeric': 0.09001994112619884, 'fraction_numerical': 0.003608394264552274, 'mean_word_length': 4.692972972972973, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '982626', 'n_tokens_mistral': 3042, 'n_tokens_neox': 3027, 'n_words': 547}