#
# Testing OpenWFE
#
#

require 'openwfe/def'

require 'flowtestbase'
require 'openwfe/def'
require 'openwfe/extras/participants/activeparticipants'
require 'extras/active_connection'
require 'openwfe/participants/participant'

class SpotParticipant #< Extras::ActiveStoreParticipant
       include OpenWFE::LocalParticipant

  def consume( workitem)

      begin
         fe    = get_flow_expression(workitem)
         activity  = fe.attributes['activity'] if fe
         
        puts("**** SPOT Participant Request: #{activity}")
        
        case activity
          when "GetFeasibilities"
            # store workitem in store
            new_wi = workitem.dup
            new_wi["threat"]      = 69
            
            awi                   = OpenWFE::Extras::Workitem.from_owfe_workitem(new_wi)
            awi.store_name        = 'spot'
            awi.save!
           
          else
            puts "can do something else here"
        end
      rescue  Exception => e
        puts e.message + " " + e.backtrace.join("\n")
      end
  end
  
end

class FlowTest67 < Test::Unit::TestCase
    include FlowTestBase

    #
    # TEST 0

    class Test0 < ProcessDefinition
       sequence do
          SPOT "activity"=>"GetFeasibilities"
          participant ref="DONE"
      end
    end

    def test_0
    
        log_level_to_debug
    
        OpenWFE::Extras::Workitem.delete(:all)
        
        @engine.register_participant "SPOT", SpotParticipant  #.new("spot")

        # simple participant to check when we go to next participant
        @engine.register_participant "DONE" do |workitem|
          puts "DONE!"
        end
    
        wfid = @engine.launch(Test0)

        sleep 0.100
        
        # emulation of message processing here
        # get workitem out of the store
        wi = OpenWFE::Extras::Workitem.find_by_participant_name("spot")
        
        # let's try to proceeed it as if we are a workitem_controller
        # and see if the SpotParticipant gets it
        @engine.proceed( wi )
        
      
    end
end