/*
 *  Copyright (c) 2012-2013 Malhar, Inc.
 *  All Rights Reserved.
 */
package com.datatorrent.flume.integration;


import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.flume.Event;
import org.apache.hadoop.conf.Configuration;

import com.datatorrent.api.*;
import com.datatorrent.api.Context.OperatorContext;
import com.datatorrent.api.DAG.Locality;

import com.datatorrent.common.util.Slice;
import com.datatorrent.flume.operator.AbstractFlumeInputOperator;

/**
 *
 * @author Chetan Narsude <chetan@datatorrent.com>
 */
@Ignore
public class ApplicationTest implements StreamingApplication
{
  public static class FlumeInputOperator extends AbstractFlumeInputOperator<Slice>
  {
    @Override
    public Slice convert(Event event)
    {
      return new Slice(event.getBody(), 0, event.getBody().length);
    }

  }

  public static class Counter implements Operator
  {
    private int count;
    private transient Slice slice;
    public final transient DefaultInputPort<Slice> input = new DefaultInputPort<Slice>()
    {
      @Override
      public void process(Slice tuple)
      {
        count++;
        slice = tuple;
      }

    };

    @Override
    public void beginWindow(long windowId)
    {
    }

    @Override
    public void endWindow()
    {
      logger.debug("total count = {}, tuple = {}", count, slice);
    }

    @Override
    public void setup(OperatorContext context)
    {
    }

    @Override
    public void teardown()
    {
    }

    private static final Logger logger = LoggerFactory.getLogger(Counter.class);
  }

  @Override
  public void populateDAG(DAG dag, Configuration conf)
  {
    dag.setAttribute(DAGContext.STREAMING_WINDOW_SIZE_MILLIS, 1000);
    FlumeInputOperator flume = dag.addOperator("FlumeOperator", new FlumeInputOperator());
    flume.setConnectAddresses(new String[]{"127.0.0.1:8080"});
    Counter counter = dag.addOperator("Counter", new Counter());

    dag.addStream("Slices", flume.output, counter.input).setLocality(Locality.CONTAINER_LOCAL);
  }

  @Test
  public void test()
  {
    try {
      LocalMode.runApp(this, Integer.MAX_VALUE);
    }
    catch (Exception ex) {
      logger.warn("The dag seems to be not testable yet, if it's - remove this exception handling", ex);
    }

  }

  private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
}

@@ -422,50 +422,8 @@
 y;%0A%0A
-import com.datatorrent.common.util.Slice;%0A
 impo
@@ -694,21 +694,21 @@
 perator%3C
-Slice
+Event
 %3E%0A  %7B%0A  
@@ -730,21 +730,21 @@
  public 
-Slice
+Event
  convert
@@ -780,69 +780,21 @@
 urn 
-new Slice(event.getBody(), 0, event.getBody().length)
+event
 ;%0A    %7D%0A
 %0A  %7D
@@ -781,33 +781,32 @@
 rn event;%0A    %7D%0A
-%0A
   %7D%0A%0A  public st
@@ -897,19 +897,19 @@
 ent 
-Slice slice
+Event event
 ;%0A  
@@ -950,21 +950,21 @@
 putPort%3C
-Slice
+Event
 %3E input 
@@ -986,21 +986,21 @@
 putPort%3C
-Slice
+Event
 %3E()%0A    
@@ -1043,21 +1043,21 @@
 process(
-Slice
+Event
  tuple)%0A
@@ -1089,21 +1089,21 @@
         
-slice
+event
  = tuple
@@ -1301,13 +1301,13 @@
 nt, 
-slice
+event
 );%0A 

Test application.