# The Grinder 3.2
# HTTP script recorded by TCPProxy at May 6, 2009 5:32:52 PM

#from net.grinder.script import Test
from net.grinder.script import Test as StandardTest

def Test(number, description):
    # Adjust to the appropriate offset.
    TEST_NUMBER_OFFSET = 80000
    return StandardTest(number + TEST_NUMBER_OFFSET, description)

from net.grinder.script.Grinder import grinder
from net.grinder.plugin.http import HTTPPluginControl, HTTPRequest
from HTTPClient import NVPair
connectionDefaults = HTTPPluginControl.getConnectionDefaults()
httpUtilities = HTTPPluginControl.getHTTPUtilities()

import grinder_common as gc

# To use a proxy server, uncomment the next line and set the host and port.
# connectionDefaults.setProxyServer("localhost", 8001)

# These definitions at the top level of the file are evaluated once,
# when the worker process is started.

connectionDefaults.defaultHeaders = \
  ( NVPair('Accept-Language', 'en-us,en;q=0.5'),
    NVPair('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7'),
    NVPair('Accept-Encoding', 'gzip,deflate'),
    NVPair('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'), )

headers0= \
  ( NVPair('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'), )

headers1= \
  ( NVPair('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
    NVPair('Referer', 'http://localhost:8080/sustain/login.jsp'), )

headers2= \
  ( NVPair('Accept', '*/*'),
    NVPair('Referer', 'http://localhost:8080/sustain/cms/dashboard'), )

headers3= \
  ( NVPair('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
    NVPair('Referer', 'http://localhost:8080/sustain/cms/dashboard'), )

headers4= \
  ( NVPair('Accept', '*/*'),
    NVPair('Referer', 'http://localhost:8080/sustain/cms/financial/cr/front?dispatch=begin'), )

headers5= \
  ( NVPair('Accept', 'image/png,image/*;q=0.8,*/*;q=0.5'),
    NVPair('Referer', 'http://localhost:8080/sustain/menus/tree.css'), )

headers6= \
  ( NVPair('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
    NVPair('Referer', 'http://localhost:8080/sustain/cms/financial/cr/front?dispatch=begin'), )

headers7= \
  ( NVPair('Accept', '*/*'),
    NVPair('Referer', 'http://localhost:8080/sustain/cms/financial/cr/front?dispatch=closeTill'), )

url0 = 'http://localhost:8080'

# Create an HTTPRequest for each request, then replace the
# reference to the HTTPRequest with an instrumented version.
# You can access the unadorned instance using request101.__target__.
# go to home page
request101 = HTTPRequest(url=url0, headers=headers0)
request101 = Test(101, 'GET /').wrap(request101)

request102 = HTTPRequest(url=url0, headers=headers0)
request102 = Test(102, 'GET dashboard').wrap(request102)

request103 = HTTPRequest(url=url0, headers=headers0)
request103 = Test(103, 'GET login.jsp').wrap(request103)

# login
request201 = HTTPRequest(url=url0, headers=headers1)
request201 = Test(201, 'GET j_security_check').wrap(request201)

request202 = HTTPRequest(url=url0, headers=headers1)
request202 = Test(202, 'GET dashboard').wrap(request202)

request203 = HTTPRequest(url=url0, headers=headers2)
request203 = Test(203, 'GET engine.js').wrap(request203)

request204 = HTTPRequest(url=url0, headers=headers2)
request204 = Test(204, 'GET userNoteManagerAjax.js').wrap(request204)

# go to front desk
request301 = HTTPRequest(url=url0, headers=headers3)
request301 = Test(301, 'GET front').wrap(request301)

request302 = HTTPRequest(url=url0, headers=headers4)
request302 = Test(302, 'GET engine.js').wrap(request302)

request303 = HTTPRequest(url=url0, headers=headers4)
request303 = Test(303, 'GET userNoteManagerAjax.js').wrap(request303)

# close till and open new till
request401 = HTTPRequest(url=url0, headers=headers6)
request401 = Test(401, 'GET front').wrap(request401)

request402 = HTTPRequest(url=url0, headers=headers7)
request402 = Test(402, 'GET engine.js').wrap(request402)

request403 = HTTPRequest(url=url0, headers=headers7)
request403 = Test(403, 'GET userNoteManagerAjax.js').wrap(request403)

request501 = HTTPRequest(url=url0, headers=headers7)
request501 = Test(501, 'GET citystateupdate_js.jsp').wrap(request501)

headersLogout= \
  ( NVPair('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
    NVPair('Referer', 'http://localhost:8080/sustain/cms/dashboard'), )

requestLogout = HTTPRequest(url=url0, headers=headersLogout)
requestLogout = Test(1, 'GET j_sustain_logout').wrap(requestLogout)


class TestRunner:
  """A TestRunner instance is created for each worker thread."""

  # A method for each recorded page.
  def page1(self):
    """GET / (requests 101-103)."""
    
    # Expecting 302 'Moved Temporarily'
    result = request101.GET('/sustain/')

    
    # Expecting 302 'Moved Temporarily'
    request102.GET('/sustain/cms/dashboard')

    request103.GET('/sustain/login.jsp')

    return result

  def page2(self):
    """GET j_security_check (requests 201-204)."""
    self.token_j_username = \
      self.user_data['UserName'] # 'admin'
    self.token_j_password = \
      self.user_data['Password'] # 'pass'
    self.token_btnSubmit = \
      '++Login++'
    
    # Expecting 302 'Moved Temporarily'
    result = request201.GET('/sustain/j_security_check' +
      '?j_username=' +
      self.token_j_username +
      '&j_password=' +
      self.token_j_password +
      '&btnSubmit=' +
      self.token_btnSubmit)

    grinder.sleep(10)
    request202.GET('/sustain/cms/dashboard')
    self.token_dispatch = \
      httpUtilities.valueFromBodyURI('dispatch') # 'onView'

    grinder.sleep(10)
    request203.GET('/sustain/dwr/engine.js', None,
      ( NVPair('If-Modified-Since', 'Thu, 30 Apr 2009 00:40:13 GMT'),
        NVPair('If-None-Match', '\"1241052013000\"'), ))

    grinder.sleep(10)
    request204.GET('/sustain/dwr/interface/userNoteManagerAjax.js')

    return result

  def page3(self):
    """GET front (requests 301-304)."""
    self.token_dispatch = \
      'begin'
    result = request301.GET('/sustain/cms/financial/cr/front' +
      '?dispatch=' +
      self.token_dispatch)
    self.token_dispatch = \
      httpUtilities.valueFromBodyURI('dispatch') # 'onView'

    grinder.sleep(10)
    request302.GET('/sustain/dwr/engine.js', None,
      ( NVPair('If-Modified-Since', 'Thu, 30 Apr 2009 00:40:13 GMT'),
        NVPair('If-None-Match', '\"1241052013000\"'), ))

    grinder.sleep(10)
    request303.GET('/sustain/dwr/interface/userNoteManagerAjax.js')

    return result

  def page4(self):
    """GET front (requests 401-404)."""
    self.token_dispatch = \
      'closeTill'
    result = request401.GET('/sustain/cms/financial/cr/front' +
      '?dispatch=' +
      self.token_dispatch)
    self.token_dispatch = \
      httpUtilities.valueFromBodyURI('dispatch') # 'onView'
    self.token_casemap = \
      httpUtilities.valueFromHiddenInput('casemap') # ''
    self.token_docDefWidgetid = \
      httpUtilities.valueFromHiddenInput('docDefWidget.id') # ''
    self.token_payPlanView = \
      httpUtilities.valueFromHiddenInput('payPlanView') # ''

    grinder.sleep(10)
    request402.GET('/sustain/dwr/engine.js', None,
      ( NVPair('If-Modified-Since', 'Thu, 30 Apr 2009 00:40:13 GMT'),
        NVPair('If-None-Match', '\"1241052013000\"'), ))

    grinder.sleep(10)
    request403.GET('/sustain/dwr/interface/userNoteManagerAjax.js')

    return result

  def page5(self):
    """GET citystateupdate_js.jsp (request 501)."""
    result = request501.GET('/sustain/system/js/citystateupdate_js.jsp')

    return result

  def pageLogout(self):
    """GET j_sustain_logout (request 1)."""
    
    # Expecting 302 'Moved Temporarily'
    result = requestLogout.GET('/sustain/j_sustain_logout')
    requestLogout.GET('/sustain/login.jsp')

    return result

  def __call__(self):
    """This method is called for every run performed by the worker thread."""
    gc.setup_thread()
    self.user_data = gc.thread_user_data()
    #self.user_data = {
    #  'UserName' : 'user10', # id 28
    #  'Password' : 'pass',
    #  }

    self.page1()      # GET / (requests 101-103)

    grinder.sleep(10)
    self.page2()      # GET j_security_check (requests 201-204)

    grinder.sleep(10)
    self.page3()      # GET front (requests 301-304)

    grinder.sleep(10)
    self.page4()      # GET front (requests 401-404)

    grinder.sleep(10)
    self.page5()      # GET citystateupdate_js.jsp (request 501)

    grinder.sleep(10)
    self.pageLogout()


def instrumentMethod(test, method_name, c=TestRunner):
  """Instrument a method with the given Test."""
  unadorned = getattr(c, method_name)
  import new
  method = new.instancemethod(test.wrap(unadorned), None, c)
  setattr(c, method_name, method)

# Replace each method with an instrumented version.
# You can call the unadorned method using self.page1.__target__().
instrumentMethod(Test(100, 'Page 1'), 'page1')
instrumentMethod(Test(200, 'Page 2'), 'page2')
instrumentMethod(Test(300, 'Page 3'), 'page3')
instrumentMethod(Test(400, 'Page 4'), 'page4')
instrumentMethod(Test(500, 'Page 5'), 'page5')
instrumentMethod(Test(9999, 'Page Logout'), 'pageLogout')
