Module: DaVinciDTRTestKit::ValidationTest

Instance Method Summary collapse

Instance Method Details

#perform_profile_validation_test(resources, resource_type, profile_url) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/davinci_dtr_test_kit/validation_test.rb', line 28

def perform_profile_validation_test(
  resources,
  resource_type,
  profile_url
)
  skip_if(resources.nil?, "No `#{resource_type}` resources to validate, skipping test.")
  resources = JSON.parse(resources)
  resources = [resources] unless resources.is_a?(Array)
  resources.each do |resource|
    fhir_resource = FHIR.from_contents(resource.to_json)

    assert fhir_resource.present?, 'Resource does not contain a recognized FHIR object'
    assert_resource_type(resource_type, resource: fhir_resource)
    assert_valid_resource(resource: fhir_resource,
                          profile_url:)
  end
end

#perform_request_validation_test(resources, resource_type, profile_url, resource_url, using_manual_entry) ⇒ Object



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
# File 'lib/davinci_dtr_test_kit/validation_test.rb', line 46

def perform_request_validation_test(
  resources,
  resource_type,
  profile_url,
  resource_url,
  using_manual_entry
)
  omit_if resources.blank?,
          "No #{resource_type} resources provided so the #{profile_url} profile does not apply"
  resources = JSON.parse(resources) if using_manual_entry
  resources = [resources] unless resources.is_a?(Array)
  resources.each_with_index do |resource, index|
    if using_manual_entry
      fhir_resource = FHIR.from_contents(resource.to_json)
    else
      if resource.url != resource_url
        messages << { type: 'warning',
                      message: format_markdown(%(Request made to wrong URL: #{resource.request[:url]}.
                                                 Should instead be to #{resource_url})) }
      end
      fhir_resource = FHIR.from_contents(resource.request[:body])
    end
    validate_resource(fhir_resource, resource_type, profile_url, index)
  end
end

#perform_response_validation_test(resources, resource_type, profile_url) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/davinci_dtr_test_kit/validation_test.rb', line 72

def perform_response_validation_test(
  resources,
  resource_type,
  profile_url
)
  omit_if resources.blank?,
          "No #{resource_type} resources provided so the #{profile_url} profile does not apply"
  resources.each_with_index do |resource, index|
    validate_resource(resource, resource_type, profile_url, index)
  end
  return if tests_failed[profile_url].blank?

  raise tests_failed[profile_url][0]
end

#tests_failedObject



3
4
5
# File 'lib/davinci_dtr_test_kit/validation_test.rb', line 3

def tests_failed
  @tests_failed ||= {}
end

#validate_resource(fhir_resource, resource_type, profile_url, index) ⇒ Object

rubocop:disable Naming/PredicateMethod



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/davinci_dtr_test_kit/validation_test.rb', line 7

def validate_resource(fhir_resource, resource_type, profile_url, index) # rubocop:disable Naming/PredicateMethod
  begin
    assert fhir_resource.present?, 'Resource does not contain a recognized FHIR object'
    assert_resource_type(resource_type, resource: fhir_resource)
    assert_valid_resource(resource: fhir_resource,
                          profile_url:)
  rescue StandardError => e
    add_message('error', e.message)
    messages.each do |message|
      message[:message].prepend("[Resource #{index + 1}] ") unless message[:message].start_with? '[Resource'
    end
    if tests_failed[profile_url].blank?
      tests_failed[profile_url] = [e]
    else
      tests_failed[profile_url] << e
    end
    return false
  end
  true
end