Module: Crucible::Tests::Assertions

Included in:
BaseTest
Defined in:
lib/tests/assertions.rb

Instance Method Summary collapse

Instance Method Details

#assert(test, message = "assertion failed, no message", data = "") ⇒ Object



5
6
7
8
9
# File 'lib/tests/assertions.rb', line 5

def assert(test, message="assertion failed, no message", data="")
  unless test
    raise AssertionException.new message, data
  end
end

#assert_bundle_entry_count(response, count) ⇒ Object



142
143
144
145
146
# File 'lib/tests/assertions.rb', line 142

def assert_bundle_entry_count(response, count)
  unless assertion_negated( response.resource.total == count.to_i )
    raise AssertionException.new "Expected FHIR Bundle with #{count} entries but found: #{response.resource.total} entries", response.body
  end
end

#assert_bundle_response(response) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/tests/assertions.rb', line 129

def assert_bundle_response(response)
  unless assertion_negated( response.resource.class == FHIR::Bundle )
    # check what this is...
    found = response.resource
    begin
      found = FHIR.from_contents(response.body)
    rescue
      found = nil
    end
    raise AssertionException.new "Expected FHIR Bundle but found: #{found.class.name.demodulize}", response.body
  end
end

#assert_bundle_transactions_okay(response) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/tests/assertions.rb', line 148

def assert_bundle_transactions_okay(response)
  response.resource.entry.each do |entry|
    unless assertion_negated( !entry.response.nil? )
      raise AssertionException.new "All Transaction/Batch Bundle.entry elements SHALL have a response."
    end
    status = entry.response.status
    unless assertion_negated( status && status.start_with?('200','201','204') )
      raise AssertionException.new "Expected all Bundle.entry.response.status to be 200, 201, or 204; but found: #{status}"
    end
  end
end

#assert_equal(expected, actual, message = "", data = "") ⇒ Object



11
12
13
14
15
16
# File 'lib/tests/assertions.rb', line 11

def assert_equal(expected, actual, message="", data="")
  unless assertion_negated( expected == actual )
    message += " Expected: #{expected}, but found: #{actual}."
    raise AssertionException.new message, data
  end
end

#assert_etag_present(client_reply) ⇒ Object



189
190
191
192
# File 'lib/tests/assertions.rb', line 189

def assert_etag_present(client_reply)
  header = client_reply.response[:headers]['etag']
  assert assertion_negated( !header.nil? ), 'ETag HTTP header is missing.'
end

#assert_last_modified_present(client_reply) ⇒ Object



194
195
196
197
# File 'lib/tests/assertions.rb', line 194

def assert_last_modified_present(client_reply)
  header = client_reply.response[:headers]['last-modified']
  assert assertion_negated( !header.nil? ), 'Last-modified HTTP header is missing.'
end

#assert_minimum(response, fixture) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/tests/assertions.rb', line 216

def assert_minimum(response, fixture)
  resource_xml = response.try(:resource).try(:to_xml) || response.try(:body)
  fixture_xml = fixture.try(:to_xml)

  resource_doc = Nokogiri::XML(resource_xml)
  raise "Could not retrieve Resource as XML from response" if resource_doc.root.nil?
  resource_doc.root.add_namespace_definition('fhir', 'http://hl7.org/fhir')

  fixture_doc = Nokogiri::XML(fixture_xml)
  raise "Could not retrieve Resource as XML from fixture" if fixture_doc.root.nil?
  fixture_doc.root.add_namespace_definition('fhir', 'http://hl7.org/fhir')

  # FIXME: This doesn't seem to work for a simple case...needs more work!
  # diffs = []
  # d1 = Nokogiri::XML('<b><p><a>1</a><b>2</b></p><p><a>2</a><b>3</b></p></b>')
  # d2 = Nokogiri::XML('<p><a>2</a><b>3</b></p>')
  # d2.diff(d1, :removed=>true){|change, node| diffs << node.to_xml}
  # diffs.empty? # this returns a list with d2 in it...

  diffs = []
  fixture_doc.diff(resource_doc, :removed => true){|change, node| diffs << node.to_xml}
  diffs.select!{|d| d.strip.length > 0}

  unless assertion_negated( diffs.empty? )
    raise AssertionException.new "Found #{diffs.length} difference(s) between minimum and actual resource.", diffs.to_s
  end
end


123
124
125
126
127
# File 'lib/tests/assertions.rb', line 123

def assert_navigation_links(bundle)
  unless assertion_negated( bundle.first_link && bundle.last_link && bundle.next_link )
    raise AssertionException.new "Expecting first, next and last link to be present"
  end
end

#assert_operator(operator, expected, actual, message = "", data = "") ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tests/assertions.rb', line 18

def assert_operator(operator, expected, actual, message="", data="")
  case operator
  when :equals
    unless assertion_negated( expected == actual )
      message += " Expected #{expected} but found #{actual}."
      raise AssertionException.new message, data
    end
  when :notEquals
    unless assertion_negated( expected != actual )
      message += " Did not expect #{expected} but found #{actual}."
      raise AssertionException.new message, data
    end
  when :in
    unless assertion_negated(expected.split(",").include?(actual))
      message += " Expected #{expected} but found #{actual}."
      raise AssertionException.new message, data
    end
  when :notIn
    unless assertion_negated(!expected.split(",").include?(actual))
      message += " Did not expect #{expected} but found #{actual}."
      raise AssertionException.new message, data
    end
  when :greaterThan
    unless assertion_negated(!actual.nil? && !expected.nil? && actual > expected)
      message += " Expected greater than #{expected} but found #{actual}."
      raise AssertionException.new message, data
    end
  when :lessThan
    unless assertion_negated(!actual.nil? && !expected.nil? && actual < expected)
      message += " Expected less than #{expected} but found #{actual}."
      raise AssertionException.new message, data
    end
  when :empty
    unless assertion_negated(actual.nil? || actual.length == 0)
      message += " Expected empty but found #{actual}."
      raise AssertionException.new message, data
    end
  when :notEmpty
    unless assertion_negated(!actual.nil? && actual.length > 0)
      message += " Expected not empty but found #{actual}."
      raise AssertionException.new message, data
    end
  when :contains
    unless assertion_negated(actual && actual.include?(expected))
      message += " Expected #{actual} to contain #{expected}."
      raise AssertionException.new message, data
    end
  when :notContains
    unless assertion_negated(actual.nil? || !actual.include?(expected))
      message += " Expected #{actual} to not contain #{expected}."
      raise AssertionException.new message, data
    end
  else
    message += " Invalid test; unknown operator: #{operator}."
    raise AssertionExection.new message, data
  end

end

#assert_resource_content_type(client_reply, content_type) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'lib/tests/assertions.rb', line 160

def assert_resource_content_type(client_reply, content_type)
  header = client_reply.response[:headers]['content-type']
  response_content_type = header
  response_content_type = header[0, header.index(';')] if !header.index(';').nil?

  unless assertion_negated( "application/fhir+#{content_type}" == response_content_type )
    raise AssertionException.new "Expected content-type application/fhir+#{content_type} but found #{response_content_type}", response_content_type
  end
end

#assert_resource_type(response, resource_type) ⇒ Object



210
211
212
213
214
# File 'lib/tests/assertions.rb', line 210

def assert_resource_type(response, resource_type)
  unless assertion_negated( !response.resource.nil? && response.resource.class == resource_type )
    raise AssertionException.new "Bad response type: expected #{resource_type}, but found #{response.resource.class}.", response.body
  end
end

#assert_response_bad(response) ⇒ Object



111
112
113
114
115
# File 'lib/tests/assertions.rb', line 111

def assert_response_bad(response)
  unless assertion_negated( [400].include?(response.code) )
    raise AssertionException.new "Bad response code: expected 400, but found #{response.code}", response.body
  end
end

#assert_response_code(response, code) ⇒ Object



204
205
206
207
208
# File 'lib/tests/assertions.rb', line 204

def assert_response_code(response, code)
  unless assertion_negated( code.to_s == response.code.to_s )
    raise AssertionException.new "Bad response code: expected #{code}, but found #{response.code}", response.body
  end
end

#assert_response_conflict(response) ⇒ Object



117
118
119
120
121
# File 'lib/tests/assertions.rb', line 117

def assert_response_conflict(response)
  unless assertion_negated( [409, 412].include?(response.code) )
    raise AssertionException.new "Bad response code: expected 409 or 412, but found #{response.code}", response.body
  end
end

#assert_response_created(response, error_message = "") ⇒ Object



93
94
95
96
97
# File 'lib/tests/assertions.rb', line 93

def assert_response_created(response, error_message="")
  unless assertion_negated( [201].include?(response.code) )
    raise AssertionException.new "Bad response code: expected 201, but found #{response.code}.#{" " + error_message}", response.body
  end
end

#assert_response_gone(response) ⇒ Object



99
100
101
102
103
# File 'lib/tests/assertions.rb', line 99

def assert_response_gone(response)
  unless assertion_negated( [410].include?(response.code) )
    raise AssertionException.new "Bad response code: expected 410, but found #{response.code}", response.body
  end
end

#assert_response_not_found(response) ⇒ Object



105
106
107
108
109
# File 'lib/tests/assertions.rb', line 105

def assert_response_not_found(response)
  unless assertion_negated( [404].include?(response.code) )
    raise AssertionException.new "Bad response code: expected 404, but found #{response.code}", response.body
  end
end

#assert_response_ok(response, error_message = "") ⇒ Object



87
88
89
90
91
# File 'lib/tests/assertions.rb', line 87

def assert_response_ok(response, error_message="")
  unless assertion_negated( [200, 201].include?(response.code) )
    raise AssertionException.new "Bad response code: expected 200, 201, but found #{response.code}.#{" " + error_message}", response.body
  end
end

#assert_valid_content_location_present(client_reply) ⇒ Object



199
200
201
202
# File 'lib/tests/assertions.rb', line 199

def assert_valid_content_location_present(client_reply)
  header = client_reply.response[:headers]['location']
  assert assertion_negated( !header.nil? ), 'Location HTTP header is missing.'
end

#assert_valid_profile(response, klass) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/tests/assertions.rb', line 77

def assert_valid_profile(response, klass)
  unless assertion_negated( response[:code].to_s == "200")

    raise AssertionException.new "Server created a #{klass.name.demodulize} with the ID `_validate` rather than validate the resource." if response[:code].to_s == "201"

    raise AssertionException.new "Response code #{response[:code]} with no OperationOutcome provided"
  end

end

#assert_valid_resource_content_type_present(client_reply) ⇒ Object

Based on MIME Types defined in hl7.org/fhir/2015May/http.html#2.1.0.6



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/tests/assertions.rb', line 172

def assert_valid_resource_content_type_present(client_reply)
  header = client_reply.response[:headers]['content-type']
  content_type = header
  charset = encoding = nil

  content_type = header[0, header.index(';')] if !header.index(';').nil?
  charset = header[header.index('charset=')+8..-1] if !header.index('charset=').nil?
  encoding = Encoding.find(charset) if !charset.nil?

  unless assertion_negated( encoding == Encoding::UTF_8 )
    raise AssertionException.new "Response content-type specifies encoding other than UTF-8: #{charset}", header
  end
  unless assertion_negated( (content_type == FHIR::Formats::ResourceFormat::RESOURCE_XML) || (content_type == FHIR::Formats::ResourceFormat::RESOURCE_JSON) )
    raise AssertionException.new "Invalid FHIR content-type: #{content_type}", header
  end
end

#assertion_negated(expression) ⇒ Object



244
245
246
# File 'lib/tests/assertions.rb', line 244

def assertion_negated(expression)
  if @negated then !expression else expression end
end

#skipObject

Raises:



248
249
250
# File 'lib/tests/assertions.rb', line 248

def skip
  raise SkipException.new
end