package org.broadinstitute.compositetask;

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

import java.io.File;
import java.io.IOException;
import java.util.Set;

import org.broadinstitute.parser.SyntaxError;

import org.broadinstitute.compositetask.CompositeTask;

public class ApiTest
{
    private CompositeTask task;

    @BeforeClass
    private void createCompositeTaskObject() {
        try {
            File source = new File("test-files/api/0.wdl");
            String relative = new File(".").toURI().relativize(source.toURI()).getPath();
            this.task = new CompositeTask(source, relative);
        } catch(IOException error) {
            Assert.fail("IOException reading file: " + error);
        } catch(SyntaxError error) {
            Assert.fail("Not expecting syntax error: " + error);
        }
    }

    @Test
    public void testGetNodesMethodReturnsTheRightNumberOfNodes() {
        Set<CompositeTaskNode> nodes = this.task.getNodes();
        Assert.assertEquals(nodes.size(), 2, "Expecting 2 nodes, found " + nodes.size());
    }

    @Test
    public void testGetNodesMethodReturnsASingleTopLevelStepNode() {
        CompositeTaskStep step = this.task.getStep("atask");
        Assert.assertNotNull(step, "Expected one step with name 'atask'");
        Assert.assertEquals(step.getName(), "atask", "Expecting one step with name 'atask'");
    }

    @Test
    public void testGetNodesMethodReturnsASingleTopLevelForLoopNode() {
        Set<CompositeTaskNode> nodes = this.task.getNodes();
        CompositeTaskStep innerStep = this.task.getStep("btask");
        CompositeTaskForLoop loop = (CompositeTaskForLoop) innerStep.getParent();
        Assert.assertEquals(loop.getCollection().getName(), "foobar", "Expecting one loop with a collection called 'foobar'");
        Assert.assertEquals(loop.getVariable().getName(), "item", "Expecting one loop with a variable called 'item'");
    }
}

@@ -970,94 +970,48 @@
     
-Set%3CCompositeTaskNode%3E nodes = this.task.getNodes();%0A        Assert.assertEquals(nodes
+Assert.assertEquals(this.task.getNodes()
 .siz
@@ -1048,21 +1048,36 @@
 und %22 + 
-n
+this.task.getN
 odes
+()
 .size())
@@ -1110,37 +1110,36 @@
 lic void testGet
-Nodes
+Step
 MethodReturnsASi
@@ -1127,39 +1127,42 @@
 tepMethodReturns
-ASingle
+TheCorrect
 TopLevelStepNode
@@ -1157,20 +1157,16 @@
 evelStep
-Node
 () %7B%0A   
@@ -1400,299 +1400,2227 @@
     
-%7D%0A%0A    @Test%0A    public void testGetNodesMethodReturnsASingleTopLevelForLoopNode() %7B%0A        Set%3CCompositeTaskNode%3E nodes = this.task.getNodes();%0A        CompositeTaskStep innerStep = this.task.getStep(%22btask%22);%0A        CompositeTaskForLoop loop = (CompositeTaskForLoop) innerStep.getParent(
+    Assert.assertEquals(step.getParent(), this.task, %22Expecting the parent of 'atask' step to be the composite task%22);%0A    %7D%0A%0A    @Test%0A    public void testTopLevelStepHasCorrectOutputMappingsToVariableX() %7B%0A        CompositeTaskStep step = this.task.getStep(%22atask%22);%0A        boolean found = false;%0A        for ( CompositeTaskStepOutput output : step.getOutputs() ) %7B%0A            if ( output.getVariable().getName().equals(%22x%22) ) %7B%0A                found = true;%0A                Assert.assertEquals(output.getType(), %22File%22, %22Expected the variable 'x' to be a File mapping%22);%0A                Assert.assertEquals(output.getMethod(), %22assign%22, %22Expected the mapping method to be 'assign'%22);%0A                Assert.assertEquals(output.getPath(), %22foo.txt%22, %22Expected the output file path to be 'foo.txt'%22);%0A            %7D%0A        %7D%0A        if ( !found ) Assert.fail(%22Expected to find one output to 'atask' to variable 'x'%22);%0A    %7D%0A%0A    @Test%0A    public void testTopLevelStepHasCorrectOutputMappingsToVariableY() %7B%0A        CompositeTaskStep step = this.task.getStep(%22atask%22);%0A        boolean found = false;%0A        for ( CompositeTaskStepOutput output : step.getOutputs() ) %7B%0A            if ( output.getVariable().getName().equals(%22y%22) ) %7B%0A                found = true;%0A                Assert.assertEquals(output.getType(), %22FirstLine%22, %22Expected the variable 'y' to be a File mapping%22);%0A                Assert.assertEquals(output.getMethod(), %22assign%22, %22Expected the mapping method to be 'assign'%22);%0A                Assert.assertEquals(output.getPath(), %22bar.txt%22, %22Expected the output file path to be 'bar.txt'%22);%0A            %7D%0A        %7D%0A        if ( !found ) Assert.fail(%22Expected to find one output to 'atask' to variable 'y'%22);%0A    %7D%0A%0A    @Test%0A    public void testSingleTopLevelForLoopExistsWithSubTask() %7B%0A        Set%3CCompositeTaskNode%3E nodes = this.task.getNodes();%0A        CompositeTaskStep innerStep = this.task.getStep(%22btask%22);%0A        Assert.assertNotNull(innerStep, %22Expected a step with name 'btask' to exist%22);%0A        CompositeTaskForLoop loop = (CompositeTaskForLoop) innerStep.getParent();%0A        Assert.assertEquals(loop.getParent(), this.task, %22Expected the loop's parent node to be the composite task%22
 );%0A 

Add more tests, refactor existing tests