/**
 * 
 */
package org.camunda.bpm.engine.test.jobexecutor;

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

import org.camunda.bpm.engine.impl.cmd.DeleteJobsCmd;
import org.camunda.bpm.engine.impl.interceptor.Command;
import org.camunda.bpm.engine.impl.interceptor.CommandContext;
import org.camunda.bpm.engine.impl.interceptor.CommandExecutor;
import org.camunda.bpm.engine.impl.persistence.entity.MessageEntity;
import org.camunda.bpm.engine.impl.test.PluggableProcessEngineTestCase;
import org.camunda.bpm.engine.runtime.Job;

/**
 * @author Tom Baeyens
 */
public class JobExecutorCmdExceptionTest extends PluggableProcessEngineTestCase {

  protected TweetExceptionHandler tweetExceptionHandler = new TweetExceptionHandler();

  private CommandExecutor commandExecutor;

  public void setUp() throws Exception {
    processEngineConfiguration.getJobHandlers().put(tweetExceptionHandler.getType(), tweetExceptionHandler);
    this.commandExecutor = processEngineConfiguration.getCommandExecutorTxRequired();
  }

  public void tearDown() throws Exception {
    processEngineConfiguration.getJobHandlers().remove(tweetExceptionHandler.getType());
  }

  public void testJobCommandsWith2Exceptions() {
    commandExecutor.execute(new Command<String>() {

      public String execute(CommandContext commandContext) {
        MessageEntity message = createTweetExceptionMessage();
        commandContext.getJobManager().send(message);
        return message.getId();
      }
    });

    waitForJobExecutorToProcessAllJobs(15000L, 50L);
  }

  public void testJobCommandsWith3Exceptions() {
    tweetExceptionHandler.setExceptionsRemaining(3);

    String jobId = commandExecutor.execute(new Command<String>() {

      public String execute(CommandContext commandContext) {
        MessageEntity message = createTweetExceptionMessage();
        commandContext.getJobManager().send(message);
        return message.getId();
      }
    });

    waitForJobExecutorToProcessAllJobs(15000L, 50L);

    // TODO check if there is a failed job in the DLQ

    commandExecutor.execute(new DeleteJobsCmd(jobId));
  }
  
  public void testMixedFailingAndSucceedingJobs() {
    tweetExceptionHandler.setExceptionsRemaining(62);
    
    List<String> createdJobs = new ArrayList<String>();

    // create 40 jobs: 20 of these fail and 20 pass.
    for(int i = 0; i < 40; i++) {
      createdJobs.add(commandExecutor.execute(new Command<String>() {
  
        public String execute(CommandContext commandContext) {
          MessageEntity message = createTweetExceptionMessage();
          commandContext.getJobManager().send(message);
          return message.getId();
        }
      }));
    }

    waitForJobExecutorToProcessAllJobs(15000L, 50L);
    
    // now there are 20 jobs with retries = 0:
    List<Job> jobList = managementService.createJobQuery().list();    
    assertEquals(20, jobList.size());
    
    for (Job job : jobList) {
      // all jobs have retries exhausted
      assertEquals(0, job.getRetries());      
    }

    for (String jobId : createdJobs) {
      commandExecutor.execute(new DeleteJobsCmd(jobId));            
    }
    
  }

  protected MessageEntity createTweetExceptionMessage() {
    MessageEntity message = new MessageEntity();
    message.setJobHandlerType("tweet-exception");
    return message;
  }
}

@@ -2133,29 +2133,19 @@
 estM
-ixedFailingAndSucceed
+ultipleFail
 ingJ
@@ -2202,17 +2202,18 @@
 aining(6
-2
+00
 );%0A    %0A
@@ -2265,24 +2265,24 @@
 String%3E();%0A%0A
+
     // creat
@@ -2294,39 +2294,8 @@
 jobs
-: 20 of these fail and 20 pass.
 %0A   
@@ -2723,17 +2723,17 @@
 ere are 
-2
+4
 0 jobs w
@@ -2837,17 +2837,17 @@
 tEquals(
-2
+4
 0, jobLi

fix testcase