/* Copyright (c) 2015, Effektif GmbH.
 * 
 * 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. */
package com.effektif.workflow.test.api;

import static org.junit.Assert.*;

import org.junit.Test;

import com.effektif.workflow.api.model.EmailId;
import com.effektif.workflow.api.model.TriggerInstance;
import com.effektif.workflow.api.types.EmailIdType;
import com.effektif.workflow.api.workflow.Workflow;
import com.effektif.workflow.api.workflowinstance.WorkflowInstance;
import com.effektif.workflow.impl.email.Email;
import com.effektif.workflow.impl.email.EmailTrigger;
import com.effektif.workflow.test.WorkflowTest;

/**
 * @author Peter Hilton
 */
public class EmailTriggerTest extends WorkflowTest {
  
  @Test
  public void testEmailTrigger() {
    Workflow workflow = new Workflow()
      .trigger(new EmailTrigger());

    deploy(workflow);
    
    Email email = new Email();

    WorkflowInstance workflowInstance = workflowEngine.start(
      new TriggerInstance().workflowId(workflow.getId()).data(EmailTrigger.EMAIL_KEY, email));

    Object emailVariable = workflowInstance.getVariableValue(EmailTrigger.EMAIL_ID_KEY);
    assertNotNull("Email ID not null", emailVariable);
    assertEquals("Email ID has correct type", EmailId.class, emailVariable.getClass());
  }

  /**
   * Tests specifying a custom variable ID for the {@link com.effektif.workflow.api.model.EmailId} workflow variable
   * in the trigger.
   */
  @Test
  public void testEmailTriggerVariableId() {
    Workflow workflow = new Workflow()
      .variable("triggerEmail", new EmailIdType())
      .trigger(new EmailTrigger().emailIdVariableId("triggerEmail"));

    deploy(workflow);

    Email email = new Email();

    WorkflowInstance workflowInstance = workflowEngine.start(
      new TriggerInstance().workflowId(workflow.getId()).data(EmailTrigger.EMAIL_KEY, email));

    Object emailVariable = workflowInstance.getVariableValue("triggerEmail");
    assertNotNull("Email ID not null", emailVariable);
    assertEquals("Email ID has correct type", EmailId.class, emailVariable.getClass());
  }

  /**
   * Repeats the test in {@link #testEmailTriggerVariableId} but does not declare the workflow variable, to test that
   * the variable’s type is automatically set correctly to {@link EmailIdType}.
   */
  @Test
  public void testEmailTriggerVariableIdDetectType() {
    Workflow workflow = new Workflow()
      .trigger(new EmailTrigger().emailIdVariableId("triggerEmail"));

    deploy(workflow);

    Email email = new Email();

    WorkflowInstance workflowInstance = workflowEngine.start(
      new TriggerInstance().workflowId(workflow.getId()).data(EmailTrigger.EMAIL_KEY, email));

    Object emailVariable = workflowInstance.getVariableValue("triggerEmail");
    assertNotNull("Email ID not null", emailVariable);
    assertEquals("Email ID has correct type", EmailId.class, emailVariable.getClass());
  }
}

@@ -665,16 +665,168 @@
 ert.*;%0A%0A
+import com.effektif.workflow.impl.email.EmailService;%0Aimport com.effektif.workflow.impl.email.EmailStore;%0Aimport com.effektif.workflow.impl.util.Lists;%0A
 import o
@@ -840,16 +840,16 @@
 t.Test;%0A
-
 %0Aimport 
@@ -1271,16 +1271,68 @@
 t;%0A%0A/**%0A
+ * Tests starting workflows with email triggers.%0A *%0A
  * @auth
@@ -1404,18 +1404,135 @@
 wTest %7B%0A
+%0A
   
+/**%0A   * Tests starting a workflow with an email trigger, and retrieving the email from the workflow instance.%0A   */
 %0A  @Test
@@ -1691,32 +1691,142 @@
 il = new Email()
+%0A      .subject(%22Software release%22)%0A      .from(%22dev@example.com%22)%0A      .to(Lists.of(%22releases@example.com%22))
 ;%0A%0A    WorkflowI
@@ -2106,32 +2106,32 @@
 emailVariable);%0A
-
     assertEquals
@@ -2193,32 +2193,481 @@
 ble.getClass());
+%0A%0A    Email storedEmail = configuration.get(EmailStore.class).findEmailById((EmailId) emailVariable);%0A    assertNotNull(%22Email not null%22, storedEmail);%0A    assertEquals(%22Email subject%22, %22Software release%22, storedEmail.getSubject());%0A    assertEquals(%22Email from%22, %22dev@example.com%22, storedEmail.getFrom());%0A    assertEquals(%22Email to%22, 1, storedEmail.getTo().size());%0A    assertEquals(%22Email to%22, %22releases@example.com%22, storedEmail.getTo().get(0));
 %0A  %7D%0A%0A  /**%0A   *

Complete EmailTriggerTest