require 'spec_helper' describe LifenFhir::CommunicationRequest do describe ':send' do let(:medium1) { LifenFhir::Medium.new(uuid: "valid-medium-uuid-1") } let(:medium2) { LifenFhir::Medium.new(uuid: "valid-medium-uuid-2") } let(:sender) { LifenFhir::Practitioner.new(uuid: "valid-sender-uuid") } let(:recipient1) { LifenFhir::Practitioner.new(uuid: "valid-recipient-uuid-1") } let(:recipient2) { LifenFhir::Practitioner.new(uuid: "valid-recipient-uuid-2") } let(:patient) { LifenFhir::Patient.new(uuid: "valid-patient-id", first_name: "Jean", last_name: "Dupond", birth_date: Date.new(2000,1,1)) } context 'with a binary' do let(:binary) { LifenFhir::Binary.new(uuid: "valid-binary-uuid") } context 'to multi-recipients' do let(:communication_request) { LifenFhir::CommunicationRequest.new(sender: sender, recipients: [recipient1, recipient2], medium: [medium1, medium2], binary: binary, patient: patient) } it 'works' do VCR.use_cassette "communication_request/send/valid_attributes_binary" do communication_request.send end expect(communication_request.uuid).to_not be_nil expect(communication_request.number_communications).to eq(2) end context 'with unknown recipient' do let(:recipient) { LifenFhir::Practitioner.new(uuid: "invalid-recipient-uuid") } let(:communication_request) { LifenFhir::CommunicationRequest.new(sender: sender, recipients: [recipient], medium: [medium1], binary: binary) } it 'fails nicely' do expect{ VCR.use_cassette "communication_request/send/unknown_recipient" do communication_request.send end }.to raise_error LifenFhir::Error end end end context 'to a patient' do let(:communication_request) { LifenFhir::CommunicationRequest.new(sender: sender, recipients: [patient], binary: binary) } it 'works' do VCR.use_cassette "communication_request/send/patient/valid_attributes_binary" do communication_request.send end expect(communication_request.uuid).to_not be_nil expect(communication_request.number_communications).to eq(1) end context 'with unknown patient' do let(:patient) { LifenFhir::Patient.new(uuid: "invalid-patient-id", first_name: "Jean", last_name: "Dupond", birth_date: Date.new(2000,1,1)) } it 'fails nicely' do VCR.use_cassette "communication_request/send/patient/invalid_attributes_binary" do expect{communication_request.send}.to raise_error LifenFhir::Error end end end end context 'to a patient and a practitioner' do let(:communication_request) { LifenFhir::CommunicationRequest.new(sender: sender, recipients: [recipient1, patient], binary: binary, medium: [medium1]) } it 'works' do VCR.use_cassette("communication_request/send/to_patient_also") do communication_request.send end expect(communication_request.uuid).to_not be_nil expect(communication_request.number_communications).to eq(2) end end context 'with an invalid medium' do let(:invalid_medium) { LifenFhir::Medium.new(uuid: "invalid-medium-uuid") } let(:communication_request) { LifenFhir::CommunicationRequest.new(sender: sender, recipients: [recipient1, recipient2], medium: [invalid_medium, medium2], binary: binary, patient: patient) } it 'fails nicely' do VCR.use_cassette "communication_request/send/invalid_medium" do expect{ communication_request.send }.to raise_error LifenFhir::Error end end end context 'with an unknown binary' do let(:binary) { LifenFhir::Binary.new(uuid: "invalid-binary-uuid") } let(:communication_request) { LifenFhir::CommunicationRequest.new(sender: sender, recipients: [recipient1], medium: [medium1], binary: binary) } it 'fails nicely' do expect{ VCR.use_cassette "communication_request/send/unknown_binary" do communication_request.send end }.to raise_error LifenFhir::Error end end end context 'with an attachment' do let(:attachment) { LifenFhir::Attachment.new(title: "Master Plan", path: File.dirname(__FILE__) + "/support/master_plan.pdf", content_type: "application/pdf") } context 'to multi-recipients' do let(:communication_request) { LifenFhir::CommunicationRequest.new(sender: sender, recipients: [recipient1, recipient2], medium: [medium1, medium2], attachment: attachment, patient: patient) } it 'works' do VCR.use_cassette "communication_request/send/valid_attributes" do communication_request.send end expect(communication_request.uuid).to_not be_nil expect(communication_request.number_communications).to eq(2) end end end end context ':find_by_uuid' do it 'works' do VCR.use_cassette "communication_request/find_by_uuid/valid_uuid_with_a_status" do @communication_request = LifenFhir::CommunicationRequest.find_by_uuid("2d217345-6ce8-4412-a03d-076a37c6661a") end expect(@communication_request.status).to eq "cancelled" end context ':when only status, id and binary' do it 'works' do VCR.use_cassette "communication_request/find_by_uuid/valid_uuid_with_status_and_binary_only" do @communication_request = LifenFhir::CommunicationRequest.find_by_uuid("f1924980-6685-40eb-b07d-51f317365ae5") end expect(@communication_request.status).to eq "cancelled" expect(@communication_request.uuid).to eq "f1924980-6685-40eb-b07d-51f317365ae5" expect(@communication_request.sender).to be_nil expect(@communication_request.recipients.count).to eq(0) expect(@communication_request.category.code).to eq("MEDICAL_REPORT") end end context ':when only status and communication_request id' do it 'works' do VCR.use_cassette "communication_request/find_by_uuid/valid_uuid_with_status_only" do @communication_request = LifenFhir::CommunicationRequest.find_by_uuid("f1924980-6685-40eb-b07d-51f317365ae5") end expect(@communication_request.status).to eq "cancelled" expect(@communication_request.binary).to be_nil end end context 'with no defined status' do it 'works' do VCR.use_cassette "communication_request/find_by_uuid/valid_uuid_with_no_status" do @communication_request = LifenFhir::CommunicationRequest.find_by_uuid("2d217345-6ce8-4412-a03d-076a37c6661a") end expect(@communication_request.status).to eq "unknown" expect(@communication_request.binary.uuid).to eq "11e7317b-d85e-5aa9-bdf4-0242ac110004" expect(@communication_request.category.code).to eq "MEDICATION_ORDER" expect(@communication_request.sender.uuid).to eq "11e5b841-4ff1-2dbb-9643-eabb809fa654" expect(@communication_request.recipients.first.uuid).to eq "11e5b841-4ff8-2dbb-9643-eabb809fa654" end end context 'with a patient' do pending "works" end context 'with invalid uuid' do it 'fails nicely' do expect{ VCR.use_cassette "communication_request/find_by_uuid/invalid_uuid" do @communication_request = LifenFhir::CommunicationRequest.find_by_uuid("invalid_uuid") end }.to raise_error LifenFhir::Error end end end end