ClientTest.java 4.7 KB
Newer Older
1
/***********************************************************************************************************************
2
 * Copyright (C) 2010-2013 by the Stratosphere project (http://stratosphere.eu)
3 4 5 6 7 8 9 10 11 12
 *
 * 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.
 **********************************************************************************************************************/
S
StephanEwen 已提交
13
package eu.stratosphere.client.program;
14

M
Michael Hopstock 已提交
15 16 17 18 19 20 21
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.powermock.api.mockito.PowerMockito.whenNew;

22
import java.io.IOException;
23
import java.net.InetSocketAddress;
24 25 26 27 28 29 30 31

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

S
StephanEwen 已提交
32
import eu.stratosphere.api.common.Plan;
33 34 35 36 37
import eu.stratosphere.compiler.DataStatistics;
import eu.stratosphere.compiler.PactCompiler;
import eu.stratosphere.compiler.costs.CostEstimator;
import eu.stratosphere.compiler.plan.OptimizedPlan;
import eu.stratosphere.compiler.plantranslate.NepheleJobGraphGenerator;
38 39
import eu.stratosphere.configuration.ConfigConstants;
import eu.stratosphere.configuration.Configuration;
M
Michael Hopstock 已提交
40
import eu.stratosphere.nephele.client.AbstractJobResult.ReturnCode;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
import eu.stratosphere.nephele.client.JobClient;
import eu.stratosphere.nephele.client.JobSubmissionResult;
import eu.stratosphere.nephele.jobgraph.JobGraph;



/**
 * Simple and maybe stupid test to check the {@link Client} class.
 * However, the use of mocks can be copied copied easily from this example.
 *
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest(Client.class)
public class ClientTest {

	@Mock
	Configuration configMock;
58

59
	@Mock
60
	PackagedProgram program;
61
	@Mock
S
StephanEwen 已提交
62
	JobWithJars planWithJarsMock;
63
	@Mock
64
	Plan planMock;
65 66 67 68 69 70 71
	
	@Mock
	PactCompiler compilerMock;
	@Mock
	OptimizedPlan optimizedPlanMock;
	
	@Mock
72
	NepheleJobGraphGenerator generatorMock;
73 74 75
	@Mock
	JobGraph jobGraphMock;

76 77 78 79 80 81 82 83 84
	@Mock
	JobClient jobClientMock;
	@Mock
	JobSubmissionResult jobSubmissionResultMock;
	
	@Before
	public void setUp() throws Exception
	{
		initMocks(this);
85 86 87 88 89
		
		when(configMock.getString(ConfigConstants.JOB_MANAGER_IPC_ADDRESS_KEY, null)).thenReturn("localhost");
		when(configMock.getInteger(ConfigConstants.JOB_MANAGER_IPC_PORT_KEY, ConfigConstants.DEFAULT_JOB_MANAGER_IPC_PORT)).thenReturn(6123);
		
		when(planMock.getJobName()).thenReturn("MockPlan");
90
//		when(mockJarFile.getAbsolutePath()).thenReturn("mockFilePath");
91
		
92 93
		when(program.getPlanWithJars()).thenReturn(planWithJarsMock);
		when(planWithJarsMock.getPlan()).thenReturn(planMock);
94 95 96 97
		
		whenNew(PactCompiler.class).withArguments(any(DataStatistics.class), any(CostEstimator.class), any(InetSocketAddress.class)).thenReturn(this.compilerMock);
		when(compilerMock.compile(planMock)).thenReturn(optimizedPlanMock);
		
98
		whenNew(NepheleJobGraphGenerator.class).withNoArguments().thenReturn(generatorMock);
99 100
		when(generatorMock.compileJobGraph(optimizedPlanMock)).thenReturn(jobGraphMock);
		
101
		whenNew(JobClient.class).withArguments(any(JobGraph.class), any(Configuration.class)).thenReturn(this.jobClientMock);
102
		
103 104 105 106
		when(this.jobClientMock.submitJob()).thenReturn(jobSubmissionResultMock);
	}
	
	@Test
107
	public void shouldSubmitToJobClient() throws ProgramInvocationException, IOException
108 109
	{
		when(jobSubmissionResultMock.getReturnCode()).thenReturn(ReturnCode.SUCCESS);
110
		
111
		Client out = new Client(configMock);
112
		out.run(program.getPlanWithJars(), false);
113
		program.deleteExtractedLibraries();
114 115 116
		
		verify(this.compilerMock, times(1)).compile(planMock);
		verify(this.generatorMock, times(1)).compileJobGraph(optimizedPlanMock);
117 118 119 120 121 122 123 124 125 126
		verify(this.jobClientMock, times(1)).submitJob();
	}
	
	/**
	 * @throws Exception
	 */
	@Test(expected=ProgramInvocationException.class)
	public void shouldThrowException() throws Exception
	{
		when(jobSubmissionResultMock.getReturnCode()).thenReturn(ReturnCode.ERROR);
127
		
128
		Client out = new Client(configMock);
129
		out.run(program.getPlanWithJars(), false);
130 131
		program.deleteExtractedLibraries();
		
132 133 134
		verify(this.jobClientMock).submitJob();
	}
}