/*
 * 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.client;

import org.apache.commons.cli.CommandLine;
import org.apache.flink.client.CliFrontendTestUtils.TestingCliFrontend;
import org.apache.flink.client.program.Client;
import org.apache.flink.client.program.PackagedProgram;
import org.apache.flink.client.program.ProgramInvocationException;
import org.apache.flink.compiler.CompilerException;
import org.apache.flink.configuration.Configuration;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;

import static org.junit.Assert.*;

public class CliFrontendInfoTest {
	
	@BeforeClass
	public static void init() {
		CliFrontendTestUtils.pipeSystemOutToNull();
		CliFrontendTestUtils.clearGlobalConfiguration();
	}
	
	@Test
	public void testErrorCases() {
		try {
			// test unrecognized option
			{
				String[] parameters = {"-v", "-l"};
				CliFrontend testFrontend = new CliFrontend();
				int retCode = testFrontend.cancel(parameters);
				assertTrue(retCode == 1);
			}
			
			// test missing options
			{
				String[] parameters = {};
				CliFrontend testFrontend = new CliFrontend();
				int retCode = testFrontend.cancel(parameters);
				assertTrue(retCode != 0);
			}
		}
		catch (Exception e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
			fail("Program caused an exception: " + e.getMessage());
		}
	}
	
	@Test
	public void testShowExecutionPlan() {
		try {
			String[] parameters = {"-e", CliFrontendTestUtils.getTestJarPath()};
			InfoTestCliFrontend testFrontend = new InfoTestCliFrontend(-1);
			testFrontend.info(parameters);
		}
		catch (Exception e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
			fail("Program caused an exception: " + e.getMessage());
		}
	}
	
	@Test
	public void testShowExecutionPlanWithParallelism() {
		try {
			String[] parameters = {"-e", "-p", "17", CliFrontendTestUtils.getTestJarPath()};
			InfoTestCliFrontend testFrontend = new InfoTestCliFrontend(17);
			testFrontend.info(parameters);
		}
		catch (Exception e) {
			System.err.println(e.getMessage());
			e.printStackTrace();
			fail("Program caused an exception: " + e.getMessage());
		}
	}
	
	// --------------------------------------------------------------------------------------------
	
	private static final class InfoTestCliFrontend extends TestingCliFrontend {
		
		private final int expectedDop;
		
		public InfoTestCliFrontend(int expectedDop) {
			this.expectedDop = expectedDop;
		}

		@Override
		protected Client getClient(CommandLine line, ClassLoader loader, String programName) throws IOException {
			try {
				return new TestClient(expectedDop);
			}
			catch (Exception e) {
				throw new IOException(e);
			}
		}
	}
	
	private static final class TestClient extends Client {
		
		private final int expectedDop;
		
		private TestClient(int expectedDop) throws Exception {
			super(new InetSocketAddress(InetAddress.getLocalHost(), 6176), new Configuration(), CliFrontendInfoTest.class.getClassLoader());
			
			this.expectedDop = expectedDop;
		}
		
		@Override
		public String getOptimizedPlanAsJson(PackagedProgram prog, int parallelism) throws CompilerException, ProgramInvocationException  {
			assertEquals(this.expectedDop, parallelism);
			return "";
		}
	}
}

@@ -2289,22 +2289,16 @@
 ters = %7B
-%22-e%22, 
 CliFront
@@ -2395,24 +2395,38 @@
 end(-1);%0A%09%09%09
+int retCode = 
 testFrontend
@@ -2436,32 +2436,61 @@
 fo(parameters);%0A
+%09%09%09assertTrue(retCode == 0);%0A
 %09%09%7D%0A%09%09catch (Exc
@@ -2731,14 +2731,8 @@
  = %7B
-%22-e%22, 
 %22-p%22
@@ -2849,16 +2849,30 @@
 17);%0A%09%09%09
+int retCode = 
 testFron
@@ -2894,16 +2894,45 @@
 eters);%0A
+%09%09%09assertTrue(retCode == 0);%0A
 %09%09%7D%0A%09%09ca

fix missing checks in CliFrontendInfoTest