Class: State

Inherits:
Object
  • Object
show all
Defined in:
lib/State.rb

Overview

The State class is a container for both global and per-test state. The test framework keeps one global State object (in $GLOBAL_STATE). A per-test State object is created for each test. The per-test object inherits all the global state but can override values if needed. Values set in the per-test class are not persisted beyond the lifetime of a single test.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(baseobj = nil) ⇒ State


Constructor. If baseobj is nil, a top-level State object is created. Otherwise, a child (per-test) State object is created.



17
18
19
20
21
22
23
24
25
26
# File 'lib/State.rb', line 17

def initialize(baseobj = nil)
  @vars = Hash.new()
  @validations = Hash.new()

  @headers = Hash.new
  @headers['Content-Type'] = 'application/json'
  @headers['User-Agent'] = 'restest'

  @baseobj = baseobj
end

Instance Attribute Details

#varsObject

Returns the value of attribute vars.



11
12
13
# File 'lib/State.rb', line 11

def vars
  @vars
end

Instance Method Details

#get(name) ⇒ Object


Get a named value. Value is returned from either self, if present, or baseobj, if applicable.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/State.rb', line 31

def get(name)
  if (@vars[name] != nil)
    return @vars[name]
  end

  if (@baseobj != nil)
    return @baseobj.get(name)
  end

  return nil
end

#headers(mix = nil) ⇒ Object


Return HTTP headers for request. A minimal default set of headers is returned.

If a mix Hash is provided, its elements are merged to the default headers (headers in ‘mix’ override the defaults if both are present). This allows per-test header customization.



83
84
85
86
87
88
89
# File 'lib/State.rb', line 83

def headers(mix = nil)
  if (mix != nil)
    return @headers.merge(mix)
  else
    return @headers
  end
end

#set(name, value) ⇒ Object


Set a persistent value. For per-test State objects, this means the value is set in the parent (global) State object so it will persist beyond one test.



47
48
49
50
51
52
53
# File 'lib/State.rb', line 47

def set(name, value)
  if (@baseobj != nil)
    @baseobj.set(name, value)
  else
    @vars[name] = value
  end
end

#set_in_test(name, value) ⇒ Object


Set a non-persistent value in a per-test State object. Calling this method on the global State is not allowed.



69
70
71
72
73
74
75
# File 'lib/State.rb', line 69

def set_in_test(name, value)
  if (@baseobj != nil)
    @vars[name] = value
  else
    die("cannot set_in_test for top-level State (name=#{name}, value=#{value})")
  end
end

#set_validate(name, value) ⇒ Object


Record a per-test validation (loaded from test file). Calling this method on the global State is not allowed.



94
95
96
97
98
99
100
# File 'lib/State.rb', line 94

def set_validate(name, value)
  if (@baseobj != nil)
    @validations[name] =value
  else
    die("cannot set_validate for top-level State (name=#{name}, value=#{value})")
  end
end

#to_sObject


To string…



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/State.rb', line 166

def to_s
  if (@baseobj == nil)
    s = "Base State: "
    @vars.each_key { |key|
      s += "#{key}='#{@vars[key]}' "
    }
  else
    s = "Test State: "
    @vars.each_key { |key|
      s += "#{key}='#{@vars[key]}' "
    }
    s += "(" + @baseobj.to_s + ")"
  end
  return s
end

#unset(name) ⇒ Object


Remove a persistent value.



58
59
60
61
62
63
64
# File 'lib/State.rb', line 58

def unset(name)
  if (@baseobj != nil)
    @baseobj.unset(name)
  else
    @vars.delete(name)
  end
end

#validate(result) ⇒ Object


Validates all the ‘validate’ conditions for the current test. Calling this method on the global State is not allowed. Validating verifies that all entries set with set_validate have a matching value in test state. If any one or more fail, the result is marked as erroneous.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/State.rb', line 108

def validate(result)
  @validations.each_key { |key|
    failed = false
    val = get(key)
    wanted = @validations[key]
    wanted =~ /(\S+) (.*)/
    op = $1
    target = $2
    out(1, "validating: [#{key}]: want: [#{op} #{target}], got [#{val}]")
    if (op == "=")
      failed = true if (target != val.to_s)

    elsif (op == "<")
      failed = true if (val.to_i >= target.to_i)

    elsif (op == ">")
      failed = true if (val.to_i <= target.to_i)

    elsif (op == "is")
      if (target == "set")
        failed = true if (val == nil || val.length == 0)
      elsif (target == "unset")
        failed = true if (val != nil && val.length > 0)
      else
        die("unknown directive for operation 'is': #{target}")
      end

    else
      die("unknown validate operation #{op}")
    end

    if (failed)
      result.error("For [#{key}] expected [#{op} #{target}] but got [#{val}].")
      $VALIDATIONS_FAIL += 1
    else
      $VALIDATIONS_OK += 1
    end
  }

  # This is Proofpoint specific and should not be in this class. There's not a good place to put it right
  # now that doesn't involve copying multiple times, so put it here for now.
  if (result.code == "500")
    if (result.body != nil)
      begin
        json = JSON.parse(result.body)
        if (json != nil && json['host'] != nil && json['message'] != nil)
          result.add_message("Server: [#{json['host']}] said: [#{json['message']}])")
        end
      rescue
      end
    end
  end

end