/*
 * Copyright (c) 2013, Effektif GmbH.  All rights reserved.
 */
package com.effektif.script.test;

import com.effektif.script.ScriptTask;
import com.effektif.workflow.api.model.TriggerInstance;
import com.effektif.workflow.api.types.TextType;
import com.effektif.workflow.api.workflow.Workflow;
import com.effektif.workflow.api.workflowinstance.WorkflowInstance;
import com.effektif.workflow.impl.deprecated.identity.IdentityService;
import com.effektif.workflow.impl.deprecated.identity.User;
import com.effektif.workflow.test.WorkflowTest;
import org.junit.Test;

import static org.junit.Assert.assertEquals;


/**
 * @author Tom Baeyens
 */
public class ScriptTaskTest extends WorkflowTest {
  
  @Test
  public void testScript() {
    Workflow workflow = new Workflow()
      .variable("n", new TextType())
      .variable("m", new TextType())
      .activity("s", new ScriptTask()
        .script("message = 'Hello ' + name;")
        .scriptMapping("name", "n")
        .scriptMapping("message", "m"));

    deploy(workflow);
    
    WorkflowInstance workflowInstance = workflowEngine.start(new TriggerInstance()
      .workflowId(workflow.getId())
      .data("n", "World"));

    assertEquals("Hello World", workflowInstance.getVariableValue("m"));
  }

  @Test
  public void testScriptDereferencing() {
    User johndoe = new User()
      .id(JOHN_ID)
      .fullName("John Doe")
      .email("johndoe@localhost");

    configuration.get(IdentityService.class)
      .createUser(johndoe);

    Workflow workflow = new Workflow()
      .variable("user", new TextType())
      .variable("name", new TextType())
      .activity("s", new ScriptTask()
        .script("name = user.fullName;"));

    deploy(workflow);
    
    WorkflowInstance workflowInstance = workflowEngine.start(new TriggerInstance()
      .workflowId(workflow.getId())
      .data("user", JOHN_ID));

    assertEquals("John Doe", workflowInstance.getVariableValue("name"));
  }
}

@@ -351,32 +351,34 @@
 rkflowInstance;%0A
+//
 import com.effek
@@ -428,24 +428,26 @@
 ityService;%0A
+//
 import com.e
@@ -1309,24 +1309,26 @@
 rencing() %7B%0A
+//
     User joh
@@ -1345,16 +1345,18 @@
  User()%0A
+//
       .i
@@ -1366,16 +1366,18 @@
 OHN_ID)%0A
+//
       .f
@@ -1396,16 +1396,18 @@
 n Doe%22)%0A
+//
       .e
@@ -1433,17 +1433,21 @@
 host%22);%0A
-%0A
+//%0A//
     conf
@@ -1483,16 +1483,18 @@
 .class)%0A
+//
       .c

Remove dependency on deprecated identity package