/* Copyright (c) 2014, 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 org.junit.Test;

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.impl.email.Email;
import com.effektif.workflow.impl.email.EmailStore;
import com.effektif.workflow.impl.email.EmailTrigger;
import com.effektif.workflow.impl.util.Lists;
import com.effektif.workflow.test.WorkflowTest;

/**
 * @author Peter Hilton
 */
public class EmailTriggerTest extends WorkflowTest {
  
  @Test
  public void testEmailTrigger() {
    Workflow workflow = new Workflow()
      .variable("triggerEmail", new EmailIdType())
      .trigger(new EmailTrigger()
        .emailIdVariableId("triggerEmail"));
    
    deploy(workflow);
    
    Email email = new Email()
      .from("me")
      .to(Lists.of("you"))
      .subject("hi");
    
    workflowEngine.start(new TriggerInstance()
      .workflowId(workflow.getId())
      .data(EmailTrigger.EMAIL_KEY, email));
    
    // TODO check if the email shows up in the variable "triggerEmail"
  }
}

@@ -17,9 +17,9 @@
  201
-4
+5
 , Ef
@@ -634,32 +634,8 @@
 i;%0A%0A
-import org.junit.Test;%0A%0A
 impo
@@ -723,25 +723,25 @@
 api.
-types.EmailIdType
+workflow.Workflow
 ;%0Aim
@@ -783,64 +783,33 @@
 flow
-.Workflow;%0Aimport com.effektif.workflow.impl.email.Email
+instance.WorkflowInstance
 ;%0Aim
@@ -855,13 +855,8 @@
 mail
-Store
 ;%0Aim
@@ -1000,16 +1000,147 @@
 lowTest;
+%0Aimport org.junit.Test;%0A%0Aimport static junit.framework.TestCase.assertEquals;%0Aimport static junit.framework.TestCase.assertNotNull;
 %0A%0A/**%0A *
@@ -1314,135 +1314,37 @@
    .
-variable(%22triggerEmail%22, new EmailIdType())%0A      .trigger(new EmailTrigger()%0A        .emailIdVariableId(%22triggerEmail%22));%0A    
+trigger(new EmailTrigger());%0A
 %0A   
@@ -1464,24 +1464,56 @@
 (%22hi%22);%0A
+%0A
     
-%0A   
+WorkflowInstance workflowInstance =
  workflo
@@ -1526,16 +1526,29 @@
 e.start(
+%0A            
 new Trig
@@ -1560,23 +1560,16 @@
 stance()
-%0A      
 .workflo
@@ -1593,129 +1593,577 @@
 d())
-%0A      .data(EmailTrigger.EMAIL_KEY, email));%0A    %0A    // TODO check if the email shows up in the variable %22triggerEmail%22
+.data(EmailTrigger.EMAIL_KEY, email));%0A%0A    Object emailVariable = workflowInstance.getVariableValue(EmailTrigger.EMAIL_KEY);%0A    assertNotNull(%22Email not null%22, emailVariable);%0A    assertEquals(%22Email has correct type%22, Email.class, emailVariable.getClass());%0A%0A    Email storedEmail = (Email) emailVariable;%0A    assertEquals(%22Email from%22, %22me%22, storedEmail.getFrom());%0A    assertEquals(%22Email subject%22, %22hi%22, storedEmail.getSubject());%0A    assertEquals(%22Email to length%22, 1, storedEmail.getTo().size());%0A    assertEquals(%22Email to%22, %22you%22, storedEmail.getTo().get(0));
 %0A  %7D

Add assertions