public void testInitialize_ExceptionInSetStatus() throws Exception {

    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    MultiplyBean bean = new MultiplyBean();
    actor.setWrappedBean(bean);
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();

    actor.configure();
    assertNull(bean.actorStatus);
    assertFalse(bean.statusSet);
    MultiplyBean.throwExceptionInStatusSetter = true;

    Exception exception = null;
    try {
      actor.initialize();
    } catch (RestFlowException e) {
      exception = e;
    }
    assertNotNull(exception);
    assertEquals(
        "org.restflow.exceptions.WorkflowRuntimeException: "
            + "Exception in setStatus() method of actor MultiplyActor",
        exception.toString());
    assertNotNull(exception.getCause());
    assertEquals(
        "java.lang.Exception: Exception in status setter", exception.getCause().toString());
  }
  public void testConfigure_ExceptionConstructingBean() throws Exception {

    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");

    assertNull(actor.getWrappedBean());
    assertNull(actor.getWrappedBeanClass());

    actor.setBeanClassName("org.restflow.actors.TestJavaActor$MultiplyBean");
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();

    MultiplyBean.throwExceptionInConstructor = true;

    Exception exception = null;
    try {
      actor.configure();
    } catch (WorkflowRuntimeException e) {
      exception = e;
    }

    assertEquals(
        "org.restflow.exceptions.WorkflowRuntimeException: "
            + "Error instantiating instance of bean class "
            + "class org.restflow.actors.TestJavaActor$MultiplyBean "
            + "for actor MultiplyActor",
        exception.toString());
    assertNotNull(exception.getCause());
    assertEquals("java.lang.Exception: Exception in constructor", exception.getCause().toString());

    assertNull(actor.getWrappedBean());
  }
  public void testConfigure_BeanClassNameSet_NotFound() throws Exception {

    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");

    actor.setBeanClassName("org.restflow.actors.TestJavaActor$MissingBean");
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();
    assertNull(actor.getWrappedBean());
    assertNull(actor.getWrappedBeanClass());

    Exception exception = null;
    try {
      actor.configure();
    } catch (ActorDeclarationException e) {
      exception = e;
    }

    assertEquals(
        "org.restflow.exceptions.ActorDeclarationException: "
            + "Bean class org.restflow.actors.TestJavaActor$MissingBean "
            + "not found for actor MultiplyActor",
        exception.toString());
    assertNotNull(exception.getCause());
    assertEquals(
        "java.lang.ClassNotFoundException: " + "org.restflow.actors.TestJavaActor$MissingBean",
        exception.getCause().toString());

    assertNull(actor.getWrappedBean());
    assertNull(actor.getWrappedBeanClass());
  }
  public void testConfigure_NoBeanSet_NoClassNameSet() throws Exception {

    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();

    assertNull(actor.getWrappedBean());
    assertNull(actor.getWrappedBeanClass());

    Exception exception = null;
    try {
      actor.configure();
    } catch (ActorDeclarationException e) {
      exception = e;
    }

    assertEquals(
        "org.restflow.exceptions.ActorDeclarationException: "
            + "No bean class provided for actor MultiplyActor",
        exception.toString());
    assertNull(exception.getCause());
    assertNull(actor.getWrappedBean());
    assertNull(actor.getWrappedBeanClass());
  }
  public void testSetInputValue_NewValueForSetting() throws Exception {

    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    MultiplyBean bean = new MultiplyBean();
    actor.setWrappedBean(bean);

    Yaml yaml = new Yaml();
    actor.setInputs((Map<String, Object>) yaml.load("originalValue:" + EOL));
    actor.setOutputs((Map<String, Object>) yaml.load("multipliedValue:" + EOL));
    actor.setSettings((Map<String, Object>) yaml.load("factor: 3" + EOL));
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();
    actor.configure();
    actor.initialize();

    Exception exception = null;
    try {
      actor.setInputValue("factor", 4);
    } catch (Exception e) {
      exception = e;
    }
    assertNotNull(exception);
    assertEquals(
        "org.restflow.exceptions.RestFlowException: "
            + "Attempt to reassign value of setting 'factor' on actor MultiplyActor",
        exception.toString());
  }
  public void testClone_ExceptionInBeanClone() throws Exception {
    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    MultiplyBean bean = new MultiplyBean();
    actor.setWrappedBean(bean);
    Yaml yaml = new Yaml();
    actor.setInputs((Map<String, Object>) yaml.load("originalValue:" + EOL));
    actor.setOutputs((Map<String, Object>) yaml.load("multipliedValue:" + EOL));
    actor.setSettings((Map<String, Object>) yaml.load("factor: 4" + EOL));
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();
    actor.configure();
    actor.initialize();
    MultiplyBean.throwExceptionInClone = true;

    Exception exception = null;
    try {
      actor.clone();
    } catch (CloneNotSupportedException e) {
      exception = e;
    }
    assertEquals(
        "java.lang.CloneNotSupportedException: "
            + "Exception cloning class org.restflow.actors.TestJavaActor$MultiplyBean "
            + "wrapped by actor MultiplyActor",
        exception.toString());
  }
  public void testStep_SetAndUpdateStateVariablesWithNulls() throws Exception {

    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    MultiplyBean bean = new MultiplyBean();
    actor.setWrappedBean(bean);

    Yaml yaml = new Yaml();
    actor.setInputs((Map<String, Object>) yaml.load("originalValue:" + EOL));
    actor.setOutputs((Map<String, Object>) yaml.load("multipliedValue:" + EOL));
    actor.setSettings((Map<String, Object>) yaml.load("factor: 3" + EOL));
    actor.setState((Map<String, Object>) yaml.load("accumulator: 0" + EOL + "count: 0" + EOL));
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();
    actor.configure();
    actor.initialize();

    actor.setInputValue("originalValue", 4);
    assertEquals(0, bean.getAccumulator());
    assertEquals((Integer) 0, bean.getCount());
    MultiplyBean.setCountToNullInStep = true;
    actor.step();
    assertEquals(12, actor.getOutputValue("multipliedValue"));
    assertNull(bean.getCount());
    assertEquals(12, bean.getAccumulator());

    actor.setInputValue("originalValue", 6);
    bean.setCount(17);
    actor.step();
    assertEquals(18, actor.getOutputValue("multipliedValue"));
    assertNull(bean.getCount());
    assertEquals(30, bean.getAccumulator());
  }
  public void testConfigure_CustomWrapupMissing() throws Exception {

    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    MultiplyBean bean = new MultiplyBean();
    actor.setWrappedBean(bean);
    actor.setWrapupMethod("customWrapupMissing");
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();

    Exception exception = null;
    try {
      actor.configure();
    } catch (ActorDeclarationException e) {
      exception = e;
    }
    assertNotNull(exception);
    assertEquals(
        "org.restflow.exceptions.ActorDeclarationException: "
            + "Error finding declared method customWrapupMissing "
            + "on bean class org.restflow.actors.TestJavaActor$MultiplyBean "
            + "for actor MultiplyActor",
        exception.toString());
    Throwable cause = exception.getCause();
    assertNotNull(cause);
    assertEquals(
        "java.lang.NoSuchMethodException: "
            + "org.restflow.actors.TestJavaActor$MultiplyBean.customWrapupMissing()",
        cause.toString());
  }
  public void testStep_SetInputValue_NullValue_NotNullable() throws Exception {
    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    MultiplyBean bean = new MultiplyBean();
    actor.setWrappedBean(bean);
    Yaml yaml = new Yaml();
    actor.setInputs((Map<String, Object>) yaml.load("originalValue:" + EOL));
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();
    actor.configure();
    actor.initialize();

    Exception exception = null;
    try {
      actor.setInputValue("originalValue", null);
    } catch (NullInputException e) {
      exception = e;
    }
    assertNotNull(exception);
    assertEquals(
        "org.restflow.exceptions.NullInputException: "
            + "Null data received on non-nullable input 'originalValue' "
            + "of actor 'MultiplyActor'",
        exception.toString());
  }
 public static JavaActor getConfiguredActor() throws Exception {
   JavaActor actor = new JavaActor();
   actor.setName("BadWorkflowProcessingActor");
   actor.setWrappedBean(new BadWorkflowProcessingBean());
   Yaml yaml = new Yaml();
   actor.setInputs((Map<String, Object>) yaml.load("workflow:" + EOL));
   actor.setApplicationContext(new WorkflowContextBuilder().build());
   actor.afterPropertiesSet();
   actor.elaborate();
   actor.configure();
   return actor;
 }
  public void testConfigure_NoDefaultMethodsOnBean() throws Exception {

    JavaActor actor = new JavaActor();
    EmptyBean bean = new EmptyBean();
    actor.setWrappedBean(bean);
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();
    actor.configure();
    actor.initialize();
    actor.step();
    actor.wrapup();
  }
 public static JavaActor getConfiguredActor() throws Exception {
   JavaActor actor = new JavaActor();
   actor.setName("MultiplyActor");
   actor.setWrappedBean(new MultiplyBean());
   Yaml yaml = new Yaml();
   actor.setInputs((Map<String, Object>) yaml.load("originalValue:" + EOL));
   actor.setOutputs((Map<String, Object>) yaml.load("multipliedValue:" + EOL));
   actor.setSettings((Map<String, Object>) yaml.load("factor: 2" + EOL));
   actor.setApplicationContext(new WorkflowContextBuilder().build());
   actor.afterPropertiesSet();
   actor.elaborate();
   actor.configure();
   return actor;
 }
 public void testStep_SetInputValue_NonNullValue() throws Exception {
   JavaActor actor = new JavaActor();
   actor.setName("MultiplyActor");
   MultiplyBean bean = new MultiplyBean();
   actor.setWrappedBean(bean);
   Yaml yaml = new Yaml();
   actor.setInputs((Map<String, Object>) yaml.load("originalValue:" + EOL));
   actor.setApplicationContext(_context);
   actor.afterPropertiesSet();
   actor.elaborate();
   actor.configure();
   actor.initialize();
   actor.setInputValue("originalValue", 72);
   actor.step();
   assertEquals(new Integer(72), bean.getOriginalValue());
 }
  public void testConfigure_DefaultWrapup() throws Exception {

    JavaActor actor = new JavaActor();
    MultiplyBean bean = new MultiplyBean();
    actor.setWrappedBean(bean);
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();

    actor.configure();
    actor.initialize();
    assertFalse(bean.wrappedUp);
    actor.wrapup();
    assertTrue(bean.wrappedUp);
    assertFalse(bean.customWrapup);
  }
  public void testConfigure_BeanClassNameSet() throws Exception {

    JavaActor actor = new JavaActor();

    actor.setBeanClassName("org.restflow.actors.TestJavaActor$MultiplyBean");
    assertNull(actor.getWrappedBean());
    assertNull(actor.getWrappedBeanClass());

    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();
    actor.configure();
    assertEquals(MultiplyBean.class, actor.getWrappedBeanClass());

    Object bean = actor.getWrappedBean();
    assertSame(MultiplyBean.class, bean.getClass());
  }
  public void testConfigure_CustomInitialize() throws Exception {

    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    MultiplyBean bean = new MultiplyBean();
    actor.setWrappedBean(bean);
    actor.setInitializeMethod("customInitialize");
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();

    actor.configure();
    assertFalse(bean.initialized);

    actor.initialize();
    assertTrue(bean.initialized);
    assertTrue(bean.customInitialization);
  }
  public void testConfigure_BeanSet() throws Exception {

    JavaActor actor = new JavaActor();
    MultiplyBean bean = new MultiplyBean();

    assertNull(actor.getWrappedBean());
    assertNull(actor.getWrappedBeanClass());

    actor.setWrappedBean(bean);
    assertNull(actor.getWrappedBeanClass());
    assertSame(bean, actor.getWrappedBean());

    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();
    actor.configure();
    assertEquals(MultiplyBean.class, actor.getWrappedBeanClass());
    assertSame(bean, actor.getWrappedBean());
  }
  public void testInitialize_DefaultSetStatus() throws Exception {

    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    MultiplyBean bean = new MultiplyBean();
    actor.setWrappedBean(bean);
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();

    actor.configure();
    assertNull(bean.actorStatus);
    assertFalse(bean.statusSet);

    actor.initialize();
    assertNotNull(bean.actorStatus);
    assertTrue(bean.statusSet);
    assertFalse(bean.customSetStatus);
  }
  public void testConfigure_CustomStep() throws Exception {

    JavaActor actor = new JavaActor();
    MultiplyBean bean = new MultiplyBean();
    actor.setStepMethod("customStep");
    actor.setWrappedBean(bean);
    Yaml yaml = new Yaml();
    actor.setInputs((Map<String, Object>) yaml.load("originalValue:" + EOL));
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();

    actor.configure();
    actor.initialize();
    actor.setInputValue("originalValue", 1);

    assertFalse(bean.stepped);
    actor.step();
    assertTrue(bean.stepped);
    assertTrue(bean.customStep);
  }
  public void testStep_SetInputValue_NullValue_Nullable() throws Exception {
    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    MultiplyBean bean = new MultiplyBean();
    actor.setWrappedBean(bean);
    Yaml yaml = new Yaml();
    actor.setInputs(
        (Map<String, Object>) yaml.load("originalValue:" + EOL + "  nullable: true" + EOL));
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();
    actor.configure();
    actor.initialize();

    actor.setInputValue("originalValue", null);
    try {
      actor.step();
    } catch (Exception e) {
    }
    assertNull(bean.getOriginalValue());
  }
  public void testClone_UncloneableBean() throws Exception {
    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    UncloneableMultiplyBean bean = new UncloneableMultiplyBean();
    actor.setWrappedBean(bean);
    Yaml yaml = new Yaml();
    actor.setInputs((Map<String, Object>) yaml.load("originalValue:" + EOL));
    actor.setOutputs((Map<String, Object>) yaml.load("multipliedValue:" + EOL));
    actor.setSettings((Map<String, Object>) yaml.load("factor: 4" + EOL));
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();
    actor.configure();
    actor.initialize();

    JavaActor clone = (JavaActor) actor.clone();
    assertNotSame(bean, clone.getWrappedBean());

    clone.setInputValue("originalValue", 5);
    clone.step();
    assertEquals(20, clone.getOutputValue("multipliedValue"));
  }
  public void testInitialize_setStateVariablesInitialValues_Nulls() throws Exception {

    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    MultiplyBean bean = new MultiplyBean();
    actor.setWrappedBean(bean);

    Yaml yaml = new Yaml();
    actor.setInputs((Map<String, Object>) yaml.load("originalValue:" + EOL));
    actor.setOutputs((Map<String, Object>) yaml.load("multipliedValue:" + EOL));
    actor.setSettings((Map<String, Object>) yaml.load("factor: 3" + EOL));
    actor.setState((Map<String, Object>) yaml.load("accumulator:" + EOL + "count:" + EOL));

    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();
    actor.configure();

    actor.initialize();
    assertEquals(0, bean.getAccumulator());
    assertEquals(new Integer(0), bean.getCount());
  }
  public void testConfigure_BothBeanAndClassNameSet() throws Exception {

    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    actor.setWrappedBean(new MultiplyBean());
    actor.setBeanClassName("org.restflow.actors.TestJavaActor$MultiplyBean");
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();

    Exception exception = null;
    try {
      actor.configure();
    } catch (ActorDeclarationException e) {
      exception = e;
    }
    assertNotNull(exception);
    assertEquals(
        "org.restflow.exceptions.ActorDeclarationException: "
            + "Actor MultiplyActor declared both a bean instance and a bean class.",
        exception.toString());
  }
  public void testInitialize_setSettings() throws Exception {

    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    actor.setWrappedBean(new MultiplyBean());

    Yaml yaml = new Yaml();
    actor.setInputs((Map<String, Object>) yaml.load("originalValue:" + EOL));
    actor.setOutputs((Map<String, Object>) yaml.load("multipliedValue:" + EOL));

    actor.setSettings((Map<String, Object>) yaml.load("factor: 3" + EOL));
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();
    actor.configure();

    actor.initialize();

    actor.setInputValue("originalValue", 4);
    actor.step();
    assertEquals(12, actor.getOutputValue("multipliedValue"));
  }
  public void testStep_SetStatus() throws Exception {

    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    MultiplyBean bean = new MultiplyBean();
    actor.setWrappedBean(bean);
    Yaml yaml = new Yaml();
    actor.setInputs((Map<String, Object>) yaml.load("originalValue:" + EOL));
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();
    actor.configure();
    actor.initialize();

    actor.setInputValue("originalValue", 4);
    assertEquals(0, bean.getAccumulator());
    assertEquals((Integer) 0, bean.getCount());
    bean.statusSet = false;
    bean.actorStatus = null;
    actor.step();
    assertNotNull(bean.actorStatus);
    assertTrue(bean.statusSet);
  }
  public void testStep_SetStatusException() throws Exception {

    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");
    MultiplyBean bean = new MultiplyBean();
    actor.setWrappedBean(bean);
    Yaml yaml = new Yaml();
    actor.setInputs((Map<String, Object>) yaml.load("originalValue:" + EOL));
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();
    actor.configure();
    actor.initialize();

    actor.setInputValue("originalValue", 4);
    assertEquals(0, bean.getAccumulator());
    assertEquals((Integer) 0, bean.getCount());
    bean.statusSet = false;
    bean.actorStatus = null;
    MultiplyBean.throwExceptionInStatusSetter = true;

    Exception exception = null;
    try {
      actor.step();
    } catch (RestFlowException e) {
      exception = e;
    }
    assertNotNull(exception);
    assertEquals(
        "org.restflow.exceptions.WorkflowRuntimeException: "
            + "Exception in setStatus() method of actor MultiplyActor",
        exception.toString());
    assertNotNull(exception.getCause());
    assertEquals(
        "java.lang.Exception: Exception in status setter", exception.getCause().toString());
  }
  public void testConfigure_ExceptionPrivateBeanConstructor() throws Exception {

    JavaActor actor = new JavaActor();
    actor.setName("MultiplyActor");

    assertNull(actor.getWrappedBean());
    assertNull(actor.getWrappedBeanClass());

    actor.setBeanClassName("org.restflow.actors.TestJavaActor$BeanWithPrivateConstructor");
    actor.setApplicationContext(_context);
    actor.afterPropertiesSet();
    actor.elaborate();

    Exception exception = null;
    try {
      actor.configure();
    } catch (WorkflowRuntimeException e) {
      exception = e;
    }

    assertEquals(
        "org.restflow.exceptions.WorkflowRuntimeException: "
            + "Error instantiating instance of bean class "
            + "class org.restflow.actors.TestJavaActor$BeanWithPrivateConstructor "
            + "for actor MultiplyActor",
        exception.toString());
    assertNotNull(exception.getCause());
    assertEquals(
        "java.lang.IllegalAccessException: "
            + "Class org.restflow.actors.JavaActor can not access a member of class "
            + "org.restflow.actors.TestJavaActor$BeanWithPrivateConstructor "
            + "with modifiers \"private\"",
        exception.getCause().toString());

    assertNull(actor.getWrappedBean());
  }