/*
 Copyright 2013 Edward Capriolo, Matt Landolf, Lodwin Cueto

Licensed 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 io.teknek.driver;

import java.io.File;
import java.net.MalformedURLException;

import io.teknek.collector.Collector;
import io.teknek.model.ITuple;
import io.teknek.model.Operator;
import io.teknek.model.Tuple;
import io.teknek.plan.OperatorDesc;
import io.teknek.plan.TestPlan;

import org.junit.Assert;
import org.junit.Test;

public class TestDriverFactory {

  @Test
  public void aTest() throws InterruptedException{
    Driver driver = DriverFactory.createDriver(TestDriver.getPart(), TestPlan.getPlan());
    Assert.assertEquals(1, driver.getDriverNode().getChildren().size());
    DriverNode minus1Driver = driver.getDriverNode();
    Assert.assertTrue(minus1Driver.getOperator() instanceof Minus1Operator );
    DriverNode times2Driver = driver.getDriverNode().getChildren().get(0);
    Assert.assertTrue(times2Driver.getOperator()+"", times2Driver.getOperator() instanceof Times2Operator);
    Assert.assertEquals(0, times2Driver.getChildren().size() );
    
    driver.initialize();
    Thread t = new Thread(driver);
    t.start();
    t.join(5000);
    //TODO we should attach a collector to this plan and assert results
    /*
    Assert.assertEquals(times2Driver.getCollectorProcessor().getCollector().peek(), 5);
    List<Tuple> expected = new ArrayList<Tuple>();
    for (int i = 0; i < 9; i++) {
      Tuple tup = new Tuple();
      tup.setField("x", (i - 1) * 2);
      expected.add(tup);
    }
    TestDriver.assertExpectedPairs(times2Driver, expected);
    */
  }
   
  public static OperatorDesc buildGroovyOperatorDesc(){
    OperatorDesc o = new OperatorDesc();
    o.setSpec("groovy");
    o.setTheClass("ATry");
    o.setScript("import io.teknek.driver.Minus1Operator\n"+"public class ATry extends Minus1Operator { \n }");
    return o;
  }
  
  @Test
  public void operatorTest(){
    Operator operator = DriverFactory.buildOperator(buildGroovyOperatorDesc());
    Assert.assertNotNull(operator);
    Assert.assertEquals ("ATry", operator.getClass().getName());
  }
  
  @Test
  public void groovyClosureTest() throws InterruptedException{
    OperatorDesc o = new OperatorDesc();
    o.setSpec("groovyclosure");
    o.setTheClass("");
    o.setScript("{ tuple, collector ->  collector.emit(tuple) }");
    Operator operator = DriverFactory.buildOperator(o);
    operator.setCollector(new Collector());
    Assert.assertNotNull(operator);
    Assert.assertEquals ("io.teknek.model.GroovyOperator", operator.getClass().getName());
    ITuple t = new Tuple();
    t.setField("x", 5);
    operator.handleTuple(t);
    Assert.assertEquals(5, ((Collector) operator.getCollector()).take().getField("x"));
  }
  
  @Test
  public void testUrlLoader() throws MalformedURLException {
    OperatorDesc o = new OperatorDesc();
    o.setTheClass("io.teknek.model.CopyOfIdentityOperator");
    o.setSpec("url");
    File f = new File("src/test/resources/id.jar");
    Assert.assertTrue(f.exists());
    o.setScript(f.toURL().toString());
    Operator oo = DriverFactory.buildOperator(o);
    Assert.assertEquals("io.teknek.plan.OperatorDesc", o.getClass().getName());
  }
}

@@ -3291,16 +3291,77 @@
 ption %7B%0A
+    String cname = %22io.teknek.model.CopyOfIdentityOperator%22;%0A
     Oper
@@ -3415,48 +3415,13 @@
 ass(
-%22io.teknek.model.CopyOfIdentityOperator%22
+cname
 );%0A 
@@ -3645,39 +3645,16 @@
 als(
-%22io.teknek.plan.OperatorDesc%22
+cname
 , 
+o
 o.ge

Test did not actually test what it was supposed to