/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.flink.test.misc;

import org.apache.flink.api.common.ExecutionConfig;
import org.apache.flink.api.common.functions.RichMapPartitionFunction;
import org.apache.flink.api.common.io.GenericInputFormat;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.io.LocalCollectionOutputFormat;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.core.io.GenericInputSplit;
import org.apache.flink.test.util.MiniClusterResource;
import org.apache.flink.util.Collector;
import org.apache.flink.util.TestLogger;

import org.junit.ClassRule;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * This test verifies that the auto parallelism is properly forwarded to the runtime.
 */
@SuppressWarnings("serial")
public class AutoParallelismITCase extends TestLogger {

	private static final int NUM_TM = 2;
	private static final int SLOTS_PER_TM = 7;
	private static final int PARALLELISM = NUM_TM * SLOTS_PER_TM;

	@ClassRule
	public static final MiniClusterResource MINI_CLUSTER_RESOURCE = new MiniClusterResource(
		new MiniClusterResource.MiniClusterResourceConfiguration(
			new Configuration(),
			2,
			7));

	@Test
	public void testProgramWithAutoParallelism() {
		try {
			ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
			env.setParallelism(ExecutionConfig.PARALLELISM_AUTO_MAX);
			env.getConfig().disableSysoutLogging();

			DataSet<Integer> result = env
					.createInput(new ParallelismDependentInputFormat())
					.rebalance()
					.mapPartition(new ParallelismDependentMapPartition());

			List<Integer> resultCollection = new ArrayList<Integer>();
			result.output(new LocalCollectionOutputFormat<Integer>(resultCollection));

			env.execute();

			assertEquals(PARALLELISM, resultCollection.size());
		}
		catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
	}

	private static class ParallelismDependentInputFormat extends GenericInputFormat<Integer> {

		private transient boolean emitted;

		@Override
		public GenericInputSplit[] createInputSplits(int numSplits) throws IOException {
			assertEquals(PARALLELISM, numSplits);
			return super.createInputSplits(numSplits);
		}

		@Override
		public boolean reachedEnd() {
			return emitted;
		}

		@Override
		public Integer nextRecord(Integer reuse) {
			if (emitted) {
				return null;
			}
			emitted = true;
			return 1;
		}
	}

	private static class ParallelismDependentMapPartition extends RichMapPartitionFunction<Integer, Integer> {

		@Override
		public void mapPartition(Iterable<Integer> values, Collector<Integer> out) {
			out.collect(getRuntimeContext().getIndexOfThisSubtask());
		}
	}
}

@@ -1093,35 +1093,45 @@
 pi.java.
-Ex
+io.LocalColl
 ec
-u
 tion
-Environmen
+OutputForma
 t;%0Aimpor
@@ -1153,47 +1153,37 @@
 ink.
-api.java.io.LocalCollectionOutputFormat
+configuration.ConfigConstants
 ;%0Aim
@@ -1312,37 +1312,100 @@
 ink.
-test.util.MiniClusterResource
+runtime.minicluster.LocalFlinkMiniCluster;%0Aimport org.apache.flink.test.util.TestEnvironment
 ;%0Aim
@@ -1501,25 +1501,56 @@
 g.junit.
+AfterClass;%0Aimport org.junit.Before
 Class
-Rule
 ;%0Aimport
@@ -2060,348 +2060,826 @@
 ;%0A%0A%09
-@ClassRule%0A%09public static final MiniClusterResource MINI_CLUSTER_RESOURCE = new MiniClusterResource(%0A%09%09new MiniClusterResource.MiniClusterResourceConfiguration(%0A%09%09%09new Configuration(),%0A%09%09%092,%0A%09%09%097));%0A%0A%09@Test%0A%09public void testProgramWithAutoParallelism() %7B%0A%09%09try %7B%0A%09%09%09ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
+private static LocalFlinkMiniCluster cluster;%0A%0A%09private static TestEnvironment env;%0A%0A%09@BeforeClass%0A%09public static void setupCluster() %7B%0A%09%09Configuration config = new Configuration();%0A%09%09config.setInteger(ConfigConstants.LOCAL_NUMBER_TASK_MANAGER, NUM_TM);%0A%09%09config.setInteger(ConfigConstants.TASK_MANAGER_NUM_TASK_SLOTS, SLOTS_PER_TM);%0A%09%09cluster = new LocalFlinkMiniCluster(config, false);%0A%0A%09%09cluster.start();%0A%0A%09%09env = new TestEnvironment(cluster, NUM_TM * SLOTS_PER_TM, false);%0A%09%7D%0A%0A%09@AfterClass%0A%09public static void teardownCluster() %7B%0A%09%09try %7B%0A%09%09%09cluster.stop();%0A%09%09%7D%0A%09%09catch (Throwable t) %7B%0A%09%09%09System.err.println(%22Error stopping cluster on shutdown%22);%0A%09%09%09t.printStackTrace();%0A%09%09%09fail(%22ClusterClient shutdown caused an exception: %22 + t.getMessage());%0A%09%09%7D%0A%09%7D%0A%0A%09@Test%0A%09public void testProgramWithAutoParallelism() %7B%0A%09%09try %7B
 %0A%09%09%09

Revert "[FLINK-8703][tests] Port AutoParallelismITCase to flip6"