final boolean STEP_1_RESULT = true;
final boolean HANDLER_RESULT = true;
final NodesSelector nodeset = SelectorUtils.singleNode(testFramework.getFrameworkNodeName());
final ArrayList<StepExecutionItem> commands = new ArrayList<StepExecutionItem>();
final StepExecutionItem testHandlerItem = new ScriptFileCommandBase() {
@Override
public String getScript() {
return "failure handler script";
}
@Override
public String[] getArgs() {
return new String[]{"failure","script","args"};
}
@Override
public String toString() {
return "testHandlerItem";
}
};
final StepExecutionItem testWorkflowCmdItem = new ExecCommandBase() {
@Override
public String[] getCommand() {
return new String[]{"a", "2", "command"};
}
@Override
public StepExecutionItem getFailureHandler() {
return testHandlerItem;
}
@Override
public String toString() {
return "testWorkflowCmdItem";
}
};
commands.add(testWorkflowCmdItem);
final StepExecutionItem testWorkflowCmdItem2 = new ExecCommandBase() {
@Override
public String[] getCommand() {
return new String[]{"a", "3", "command"};
}
@Override
public StepExecutionItem getFailureHandler() {
return testHandlerItem;
}
@Override
public String toString() {
return "testWorkflowCmdItem2";
}
};
commands.add(testWorkflowCmdItem2);
final WorkflowImpl workflow = new WorkflowImpl(commands, 1, false,
WorkflowStrategy.STEP_FIRST);
workflow.setKeepgoing(KEEPGOING_TEST);
final WorkflowExecutionItemImpl executionItem = new WorkflowExecutionItemImpl(workflow);
final StepFirstWorkflowStrategy strategy = new StepFirstWorkflowStrategy(testFramework);
final StepExecutionContext context =
new ExecutionContextImpl.Builder()
.frameworkProject(TEST_PROJECT)
.user("user1")
.nodeSelector(nodeset)
.executionListener(new testListener())
.framework(testFramework)
.nodes(testFramework.filterNodeSet(nodeset, TEST_PROJECT, null))
.build();
//setup testInterpreter for all command types
final NodeStepExecutionService interpreterService = NodeStepExecutionService.getInstanceForFramework(
testFramework);
testInterpreter interpreterMock = new testInterpreter();
testInterpreter handlerInterpreterMock = new testInterpreter();
testInterpreter failMock = new testInterpreter();
failMock.shouldThrowException = true;
// interpreterService.registerInstance(JobExecutionItem.COMMAND_TYPE, interpreterMock);
interpreterService.registerInstance("exec", interpreterMock);
interpreterService.registerInstance("script", handlerInterpreterMock);
interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_NODE_FIRST, failMock);
interpreterService.registerInstance(WorkflowExecutionItem.COMMAND_TYPE_STEP_FIRST, failMock);
//set resturn results
interpreterMock.resultList.add(new testResult(STEP_0_RESULT, 0));
interpreterMock.resultList.add(new testResult(STEP_1_RESULT, 1));
handlerInterpreterMock.resultList.add(new testResult(HANDLER_RESULT, 0));
final WorkflowExecutionResult result = strategy.executeWorkflow(context, executionItem);
assertNotNull(result);
if (!result.isSuccess() && null != result.getException()) {
result.getException().printStackTrace(System.err);
}
assertTrue(result.isSuccess());
assertNull("threw exception: " + result.getException(), result.getException());
assertNotNull(result.getResultSet());
final List<StepExecutionResult> test1 = result.getResultSet();
System.err.println("results: "+test1);
assertEquals(2, test1.size());
assertEquals(2, interpreterMock.executionItemList.size());
assertEquals(2, interpreterMock.executionContextList.size());
//check handler item was executed
assertEquals(1, handlerInterpreterMock.executionItemList.size());
assertEquals(1, handlerInterpreterMock.executionContextList.size());
int resultIndex =0;
int stepNum=0;
{
//failure handler result
final StepExecutionResult interpreterResult = test1.get(resultIndex);
final DispatcherResult dr = NodeDispatchStepExecutor.extractDispatcherResult(interpreterResult);
assertEquals(1, dr.getResults().size());
final NodeStepResult nrs = dr.getResults().values().iterator().next();
assertTrue("unexpected class: " + nrs.getClass(),
nrs instanceof testResult);
testResult val = (testResult) nrs;
assertEquals(0, val.flag);
assertTrue(val.isSuccess());
final StepExecutionItem executionItemX = handlerInterpreterMock.executionItemList.get(stepNum);
assertTrue("wrong class: " + executionItemX.getClass().getName(),
executionItemX instanceof ScriptFileCommandExecutionItem);
ScriptFileCommandExecutionItem execItemX = (ScriptFileCommandExecutionItem) executionItemX;
assertNotNull(execItemX.getScript());
assertNotNull(execItemX.getArgs());
assertEquals("failure handler script", execItemX.getScript());
assertEquals(3, execItemX.getArgs().length);
assertEquals("failure", execItemX.getArgs()[0]);
assertEquals("script", execItemX.getArgs()[1]);
assertEquals("args", execItemX.getArgs()[2]);
final ExecutionContext executionContextX = handlerInterpreterMock.executionContextList.get(stepNum);
assertEquals(TEST_PROJECT, executionContextX.getFrameworkProject());
assertNotNull(executionContextX.getDataContext());
assertNotNull(executionContextX.getDataContext().get("node"));
assertEquals(0, executionContextX.getLoglevel());
assertEquals("user1", executionContextX.getUser());
assertEquals(nodeset, executionContextX.getNodeSelector());
}
resultIndex=1;
stepNum = 1;
{
//second step result
final StepExecutionResult interpreterResult = test1.get(resultIndex);
final DispatcherResult dr = NodeDispatchStepExecutor.extractDispatcherResult(interpreterResult);
assertEquals(1, dr.getResults().size());
final NodeStepResult nrs = dr.getResults().values().iterator().next();
assertTrue("unexpected class: " + nrs.getClass(),
nrs instanceof testResult);
testResult val = (testResult) nrs;
assertEquals(1, val.flag);
assertTrue(val.isSuccess());
final StepExecutionItem executionItem1 = interpreterMock.executionItemList.get(stepNum);
assertTrue("wrong class: " + executionItem1.getClass().getName(),
executionItem1 instanceof ExecCommandExecutionItem);
ExecCommandExecutionItem execItem = (ExecCommandExecutionItem) executionItem1;
assertNotNull(execItem.getCommand());
assertEquals(3, execItem.getCommand().length);
assertEquals("a", execItem.getCommand()[0]);