

package org.apache.flink.test.javaApiOperators.lambdas;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.test.util.JavaProgramTestBase;

public class FilterITCase extends JavaProgramTestBase {

	private static final String EXPECTED_RESULT = "3,2,Hello world\n" +
			"4,3,Hello world, how are you?\n";

	public static DataSet<Tuple3<Integer, Long, String>> get3TupleDataSet(ExecutionEnvironment env) {

		List<Tuple3<Integer, Long, String>> data = new ArrayList<Tuple3<Integer, Long, String>>();
		data.add(new Tuple3<Integer, Long, String>(1,1l,"Hi"));
		data.add(new Tuple3<Integer, Long, String>(2,2l,"Hello"));
		data.add(new Tuple3<Integer, Long, String>(3,2l,"Hello world"));
		data.add(new Tuple3<Integer, Long, String>(4,3l,"Hello world, how are you?"));
		data.add(new Tuple3<Integer, Long, String>(5,3l,"I am fine."));
		data.add(new Tuple3<Integer, Long, String>(6,3l,"Luke Skywalker"));
		data.add(new Tuple3<Integer, Long, String>(7,4l,"Comment#1"));
		data.add(new Tuple3<Integer, Long, String>(8,4l,"Comment#2"));
		data.add(new Tuple3<Integer, Long, String>(9,4l,"Comment#3"));
		data.add(new Tuple3<Integer, Long, String>(10,4l,"Comment#4"));
		data.add(new Tuple3<Integer, Long, String>(11,5l,"Comment#5"));
		data.add(new Tuple3<Integer, Long, String>(12,5l,"Comment#6"));
		data.add(new Tuple3<Integer, Long, String>(13,5l,"Comment#7"));
		data.add(new Tuple3<Integer, Long, String>(14,5l,"Comment#8"));
		data.add(new Tuple3<Integer, Long, String>(15,5l,"Comment#9"));
		data.add(new Tuple3<Integer, Long, String>(16,6l,"Comment#10"));
		data.add(new Tuple3<Integer, Long, String>(17,6l,"Comment#11"));
		data.add(new Tuple3<Integer, Long, String>(18,6l,"Comment#12"));
		data.add(new Tuple3<Integer, Long, String>(19,6l,"Comment#13"));
		data.add(new Tuple3<Integer, Long, String>(20,6l,"Comment#14"));
		data.add(new Tuple3<Integer, Long, String>(21,6l,"Comment#15"));

		Collections.shuffle(data);

		return env.fromCollection(data);
	}

	private String resultPath;

	@Override
	protected void preSubmit() throws Exception {
		resultPath = getTempDirPath("result");
	}

	@Override
	protected void testProgram() throws Exception {
		final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

		DataSet<Tuple3<Integer, Long, String>> ds = get3TupleDataSet(env);
		DataSet<Tuple3<Integer, Long, String>> filterDs = ds.
				filter(value -> value.f2.contains("world"));
		filterDs.writeAsCsv(resultPath);
		env.execute();
	}

	@Override
	protected void postSubmit() throws Exception {
		compareResultsByLinesInMemory(EXPECTED_RESULT, resultPath);
	}
}

