Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FI-2015 GET requests added to fhir_operation #380

Merged
merged 22 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
3acc04b
GET requests added to fhir_operation
360dgries Aug 4, 2023
43eac91
Updated function contract
360dgries Aug 4, 2023
82aed41
linting
360dgries Aug 4, 2023
a07f634
added tests for helper function
360dgries Aug 4, 2023
83d2ada
deferred affectsState detection to test author
360dgries Aug 29, 2023
354674c
linting
360dgries Aug 29, 2023
53276a3
package-lock from main
360dgries Aug 29, 2023
fc94781
adjusted body_to_path to take one argument
360dgries Aug 29, 2023
e7dfb4d
Merge branch 'main' into FI-2015-enable-get-request-fhir_operation
360dgries Aug 30, 2023
aa9d5e8
Repeated parameter support, contract updating, let changes in spec te…
360dgries Aug 31, 2023
c999f43
Linting and Cyclomatic complexity handling
360dgries Aug 31, 2023
5044914
Merge branch 'FI-2015-enable-get-request-fhir_operation' of https://g…
360dgries Aug 31, 2023
d4dc3c8
Reverted Package-lock.json to original state
360dgries Sep 11, 2023
ac8a754
bundle update
360dgries Oct 2, 2023
d77c0bf
reorganization of spec tests and changes to checking parameter for pr…
360dgries Oct 6, 2023
268a585
Merge branch 'main' into FI-2015-enable-get-request-fhir_operation
360dgries Oct 6, 2023
94600a2
Merge branch 'main' into FI-2015-enable-get-request-fhir_operation
360dgries Oct 6, 2023
4e7d126
conflict resolution
360dgries Oct 6, 2023
8ac86f9
Taking package and gemfile from Main Branch
360dgries Oct 9, 2023
ac83e98
Linting spec tests
360dgries Oct 9, 2023
25da3d6
added rspec test for unhandled REST methods
360dgries Oct 10, 2023
cd511f2
linting of rspec file
360dgries Oct 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 28 additions & 4 deletions lib/inferno/dsl/fhir_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,50 @@ def fhir_clients
@fhir_clients ||= {}
end

# Move parameters from body to the path for GET operations
360dgries marked this conversation as resolved.
Show resolved Hide resolved
#
# @param path [String]
# @param body [FHIR::Parameters] Must all be primitive if making GET request
# @private
def body_to_path(path, body)
body.parameter.reduce("#{path}?") do |new_path, x|
360dgries marked this conversation as resolved.
Show resolved Hide resolved
valid = x.valid? && x.part.empty? && x.resource.nil? # Parameter is valid
param_val = x.to_hash.except('name') # should contain only one value if is a valid parameter, checked above
if valid && !param_val.empty? && FHIR.primitive?(datatype: param_val.keys[0][5..], value: param_val.values[0])
"#{new_path}#{x.name}=#{param_val.values[0]}&"
360dgries marked this conversation as resolved.
Show resolved Hide resolved
else
# Handle the case of nonprimitive
Inferno::Application[:logger].error "Cannot use GET request with non-primitive datatype #{x.name}"
raise ArgumentError, "Cannot use GET request with non-primitive datatype #{x.name}"
end
end
end

# Perform a FHIR operation
#
# @note This is a placeholder method until the FHIR::Client supports
# general operations
#
# @param path [String]
# @param body [FHIR::Parameters]
# @param body [FHIR::Parameters] Must all be primitive if making GET request
# @param client [Symbol]
# @param name [Symbol] Name for this request to allow it to be used by
# other tests
# @param headers [Hash] custom headers for this operation
# @param affects_state [Bool] indicates whether operation affects server (true implies POST)
360dgries marked this conversation as resolved.
Show resolved Hide resolved
# @return [Inferno::Entities::Request]
def fhir_operation(path, body: nil, client: :default, name: nil, headers: {})
def fhir_operation(path, body: nil, client: :default, name: nil, headers: {}, affects_state: true)
store_request_and_refresh_token(fhir_client(client), name) do
tcp_exception_handler do
operation_headers = fhir_client(client).fhir_headers
operation_headers.merge!('Content-Type' => 'application/fhir+json') if body.present?
operation_headers.merge!(headers) if headers.present?

fhir_client(client).send(:post, path, body, operation_headers)
if affects_state
fhir_client(client).send(:post, path, body, operation_headers)
else
path = body_to_path(path, body) if body.present?
fhir_client(client).send(:get, path, operation_headers)
end
end
end
end
Expand Down
104 changes: 100 additions & 4 deletions spec/inferno/dsl/fhir_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,78 @@ def test_session_id
end
end

describe '#body_to_path' do
p1 = FHIR::Parameters::Parameter.new
360dgries marked this conversation as resolved.
Show resolved Hide resolved
p1.name = 'Param1'
p1.valueBoolean = true

p2 = FHIR::Parameters::Parameter.new
p2.name = 'Param2'
p2.valueString = 'zyx'

p3 = FHIR::Parameters::Parameter.new
p3.name = 'non_prim_param'

p4 = FHIR::Parameters::Parameter.new
p4.name = 'resource_param'
p4.resource = FHIR::Patient.new

ratio = FHIR::Ratio.new
ratio.numerator = FHIR::Quantity.new
ratio.denominator = FHIR::Quantity.new
p3.valueRatio = ratio

it 'converts primitives into a path query' do
b = FHIR::Parameters.new
b.parameter = [p1, p2]
expect(group.body_to_path('', b)).to eq('?Param1=true&Param2=zyx&')
end

it 'raises error if non-primitive parameter found' do
b = FHIR::Parameters.new
b.parameter = [p1, p3]
expect do
group.body_to_path('', b)
end.to raise_error(ArgumentError, 'Cannot use GET request with non-primitive datatype non_prim_param')
end

it 'raises error if parameter is resource' do
b = FHIR::Parameters.new
b.parameter = [p1, p4]
expect do
group.body_to_path('', b)
end.to raise_error(ArgumentError, 'Cannot use GET request with non-primitive datatype resource_param')
end
end

describe '#fhir_operation' do
let(:path) { 'abc' }
let(:stub_operation_request) do
stub_request(:post, "#{base_url}/#{path}")
.to_return(status: 200, body: resource.to_json)
end
let(:stub_operation_get_request) do
stub_request(:get, "#{base_url}/#{path}")
.to_return(status: 200, body: resource.to_json)
end

before do
stub_operation_request
stub_operation_get_request
end

it 'performs a get' do
it 'performs a post' do
group.fhir_operation(path)

expect(stub_operation_request).to have_been_made.once
end

it 'performs a get' do
group.fhir_operation(path, affects_state: false)

expect(stub_operation_get_request).to have_been_made.once
end

it 'returns an Inferno::Entities::Request' do
result = group.fhir_operation(path)

Expand All @@ -105,6 +160,47 @@ def test_session_id
expect(group.request).to eq(result)
end

context 'with a body of parameters' do
p1 = FHIR::Parameters::Parameter.new
p1.name = 'Param1'
p1.valueBoolean = true

p2 = FHIR::Parameters::Parameter.new
p2.name = 'Param2'
p2.valueString = 'zyx'

p3 = FHIR::Parameters::Parameter.new
p3.name = 'non_prim_param'

ratio = FHIR::Ratio.new
ratio.numerator = FHIR::Quantity.new
ratio.denominator = FHIR::Quantity.new
p3.valueRatio = ratio

it 'uses get when all parameters are primitive' do
b = FHIR::Parameters.new
b.parameter = [p1, p2]

query_string = '?Param1=true&Param2=zyx'

get_with_body_request_stub =
stub_request(:get, "#{base_url}/#{path + query_string}")
360dgries marked this conversation as resolved.
Show resolved Hide resolved
.to_return(status: 200, body: resource.to_json)

group.fhir_operation(path, body: b, affects_state: false)

expect(get_with_body_request_stub).to have_been_made.once
end

it 'prevents get when parameters are non-primitive' do
b = FHIR::Parameters.new
b.parameter = [p1, p3]
expect do
group.fhir_operation(path, body: b, affects_state: false)
end.to raise_error(ArgumentError, 'Cannot use GET request with non-primitive datatype non_prim_param')
end
end

context 'with the client parameter' do
it 'uses that client' do
other_url = 'http://www.example.com/fhir/r4'
Expand Down Expand Up @@ -149,7 +245,7 @@ def test_session_id
.to_return(status: 200, body: resource.to_json)
end

it 'as custom only, performs a get' do
it 'as custom only, performs a post' do
operation_request =
stub_request(:post, "#{base_url}/#{path}")
.with(headers: { 'CustomHeader' => 'CustomTest' })
Expand All @@ -160,7 +256,7 @@ def test_session_id
expect(operation_request).to have_been_made.once
end

it 'as default only, performs a get' do
it 'as default only, performs a post' do
operation_request =
stub_request(:post, "#{base_url}/#{path}")
.with(headers: { 'DefaultHeader' => 'ClientHeader' })
Expand All @@ -172,7 +268,7 @@ def test_session_id
expect(operation_request).to have_been_made.once
end

it 'as both default and custom, performs a get' do
it 'as both default and custom, performs a post' do
operation_request =
stub_request(:post, "#{base_url}/#{path}")
.with(headers: { 'DefaultHeader' => 'ClientHeader', 'CustomHeader' => 'CustomTest' })
Expand Down