diff --git a/modules/ask_va_api/app/controllers/ask_va_api/application_controller.rb b/modules/ask_va_api/app/controllers/ask_va_api/application_controller.rb index e0cf79eb21b..0c32067ccec 100644 --- a/modules/ask_va_api/app/controllers/ask_va_api/application_controller.rb +++ b/modules/ask_va_api/app/controllers/ask_va_api/application_controller.rb @@ -8,8 +8,6 @@ class ApplicationController < ::ApplicationController def handle_exceptions yield - rescue AskVAApi::V0::InquiriesController::InvalidInquiryError => e - log_and_render_error('invalid_inquiry_error', e, :bad_request) rescue ErrorHandler::ServiceError, Crm::ErrorHandler::ServiceError => e log_and_render_error('service_error', e, :unprocessable_entity) rescue => e diff --git a/modules/ask_va_api/app/controllers/ask_va_api/v0/inquiries_controller.rb b/modules/ask_va_api/app/controllers/ask_va_api/v0/inquiries_controller.rb index 829268f6e51..c0bdb1d7b59 100644 --- a/modules/ask_va_api/app/controllers/ask_va_api/v0/inquiries_controller.rb +++ b/modules/ask_va_api/app/controllers/ask_va_api/v0/inquiries_controller.rb @@ -59,14 +59,11 @@ def inquiry_params def get_inquiry_by_id inq = retriever.fetch_by_id(id: params[:id]) - - raise InvalidInquiryError if inq.is_a?(Hash) - @inquiry = Result.new(payload: Inquiries::Serializer.new(inq).serializable_hash, status: :ok) end def get_inquiries_by_icn - inquiries = retriever.fetch_by_icn + inquiries = retriever.call @user_inquiries = Result.new(payload: Inquiries::Serializer.new(inquiries).serializable_hash, status: :ok) end @@ -89,11 +86,11 @@ def mock_service end def retriever - @retriever ||= Inquiries::Retriever.new(icn: current_user.icn, service: mock_service) + entity_class = AskVAApi::Inquiries::Entity + @retriever ||= Inquiries::Retriever.new(icn: current_user.icn, user_mock_data: params[:mock], entity_class:) end Result = Struct.new(:payload, :status, keyword_init: true) - class InvalidInquiryError < StandardError; end class InvalidAttachmentError < StandardError; end end end diff --git a/modules/ask_va_api/app/lib/ask_va_api/base_retriever.rb b/modules/ask_va_api/app/lib/ask_va_api/base_retriever.rb index 3abf77440c8..1b7b678f4e4 100644 --- a/modules/ask_va_api/app/lib/ask_va_api/base_retriever.rb +++ b/modules/ask_va_api/app/lib/ask_va_api/base_retriever.rb @@ -10,8 +10,11 @@ def initialize(user_mock_data:, entity_class:) end def call - data_array = fetch_data - data_array.map { |item| entity_class.new(item) } + if fetch_data.is_a?(Array) + fetch_data.map { |item| entity_class.new(item) } + else + entity_class.new(fetch_data) + end rescue => e ::ErrorHandler.handle_service_error(e) end diff --git a/modules/ask_va_api/app/lib/ask_va_api/correspondences/entity.rb b/modules/ask_va_api/app/lib/ask_va_api/correspondences/entity.rb index e8bb5a001e6..5078951380e 100644 --- a/modules/ask_va_api/app/lib/ask_va_api/correspondences/entity.rb +++ b/modules/ask_va_api/app/lib/ask_va_api/correspondences/entity.rb @@ -4,7 +4,6 @@ module AskVAApi module Correspondences class Entity attr_reader :id, - :inquiry_id, :message_type, :modified_on, :status_reason, @@ -14,7 +13,6 @@ class Entity def initialize(info) @id = info[:Id] - @inquiry_id = info[:InquiryId] @message_type = info[:MessageType] @modified_on = info[:ModifiedOn] @status_reason = info[:StatusReason] diff --git a/modules/ask_va_api/app/lib/ask_va_api/correspondences/retriever.rb b/modules/ask_va_api/app/lib/ask_va_api/correspondences/retriever.rb index a249ca6f7ef..9f152fbd90e 100644 --- a/modules/ask_va_api/app/lib/ask_va_api/correspondences/retriever.rb +++ b/modules/ask_va_api/app/lib/ask_va_api/correspondences/retriever.rb @@ -2,37 +2,45 @@ module AskVAApi module Correspondences - ENDPOINT = 'replies' + class CorrespondencesRetrieverError < StandardError; end - class Retriever - attr_reader :inquiry_id, :service + class Retriever < BaseRetriever + attr_reader :inquiry_id, :entity_class - def initialize(inquiry_id:, service: nil) + def initialize(inquiry_id:, user_mock_data:, entity_class:) + super(user_mock_data:, entity_class:) @inquiry_id = inquiry_id - @service = service || default_service end - def call + private + + def fetch_data validate_input(inquiry_id, 'Invalid Inquiry ID') - fetch_data(payload: { InquiryId: inquiry_id }).map do |cor| - Entity.new(cor) + if user_mock_data + data = File.read('modules/ask_va_api/config/locales/get_replies_mock_data.json') + + data = JSON.parse(data, symbolize_names: true)[:Data] + filter_data(data) + else + endpoint = "inquiries/#{inquiry_id}/replies" + + response = Crm::Service.new(icn: nil).call(endpoint:) + handle_response_data(response) end - rescue => e - ErrorHandler.handle_service_error(e) end - private - - def default_service - Crm::Service.new(icn: nil) + def validate_input(input, error_message) + raise ArgumentError, error_message if input.blank? end - def fetch_data(payload: {}) - service.call(endpoint: ENDPOINT, payload:)[:Data] + def filter_data(data) + data.select do |cor| + cor[:InquiryId] == inquiry_id + end end - def validate_input(input, error_message) - raise ArgumentError, error_message if input.blank? + def handle_response_data(response) + response[:Data].presence || raise(CorrespondencesRetrieverError, response[:Message]) end end end diff --git a/modules/ask_va_api/app/lib/ask_va_api/correspondences/serializer.rb b/modules/ask_va_api/app/lib/ask_va_api/correspondences/serializer.rb index d4900c2b1e6..6a8cc42e6e6 100644 --- a/modules/ask_va_api/app/lib/ask_va_api/correspondences/serializer.rb +++ b/modules/ask_va_api/app/lib/ask_va_api/correspondences/serializer.rb @@ -6,8 +6,7 @@ class Serializer < ActiveModel::Serializer include JSONAPI::Serializer set_type :correspondence - attributes :inquiry_id, - :message_type, + attributes :message_type, :modified_on, :status_reason, :description, diff --git a/modules/ask_va_api/app/lib/ask_va_api/inquiries/retriever.rb b/modules/ask_va_api/app/lib/ask_va_api/inquiries/retriever.rb index cd1fcad5907..d3a25005728 100644 --- a/modules/ask_va_api/app/lib/ask_va_api/inquiries/retriever.rb +++ b/modules/ask_va_api/app/lib/ask_va_api/inquiries/retriever.rb @@ -2,51 +2,61 @@ module AskVAApi module Inquiries - ENDPOINT = 'inquiries' + class InquiriesRetrieverError < StandardError; end - class Retriever - attr_reader :service, :icn + class Retriever < BaseRetriever + attr_reader :icn, :user_mock_data, :entity_class - def initialize(icn:, service: nil) + def initialize(user_mock_data:, entity_class:, icn: nil) + super(user_mock_data:, entity_class:) @icn = icn - @service = service || default_service end def fetch_by_id(id:) - validate_input(id, 'Invalid ID') - reply = Correspondences::Retriever.new(inquiry_id: id, service:).call - data = fetch_data(payload: { id: }) - return {} if data.blank? + inq = fetch_data(id) + reply = fetch_correspondences(inquiry_id: id) - Entity.new(data, reply) + entity_class.new(inq.first, reply) rescue => e - ErrorHandler.handle_service_error(e) + ::ErrorHandler.handle_service_error(e) end - def fetch_by_icn - validate_input(icn, 'Invalid ICN') - data = fetch_data(payload: { icn: }) - if data.empty? - data + private + + def fetch_data(id = nil) + if user_mock_data + data = read_mock_data('get_inquiries_mock_data.json') + filter_data(data, id) else - data.map { |inq| Entity.new(inq) } + endpoint = 'inquiries' + payload = id ? { icn: } : { id: } + + response = Crm::Service.new(icn:).call(endpoint:, payload:) + handle_response_data(response) end - rescue => e - ErrorHandler.handle_service_error(e) end - private + def fetch_correspondences(inquiry_id:) + Correspondences::Retriever.new( + inquiry_id:, + user_mock_data:, + entity_class: AskVAApi::Correspondences::Entity + ).call + end - def default_service - Crm::Service.new(icn:) + def read_mock_data(file_name) + data = File.read("modules/ask_va_api/config/locales/#{file_name}") + JSON.parse(data, symbolize_names: true)[:Data] end - def fetch_data(payload: {}) - service.call(endpoint: ENDPOINT, payload:)[:Data] + def filter_data(data, id = nil) + data.select do |inq| + id ? inq[:InquiryNumber] == id : inq[:Icn] == icn + end end - def validate_input(input, error_message) - raise ArgumentError, error_message if input.blank? + def handle_response_data(response) + response[:Data].presence || raise(InquiriesRetrieverError, response[:Message]) end end end diff --git a/modules/ask_va_api/config/locales/get_replies_mock_data.json b/modules/ask_va_api/config/locales/get_replies_mock_data.json index 36ae486909e..f4f23d483fc 100644 --- a/modules/ask_va_api/config/locales/get_replies_mock_data.json +++ b/modules/ask_va_api/config/locales/get_replies_mock_data.json @@ -1,7 +1,7 @@ { "Data": [ { - "InquiryId": "1", + "InquiryId": "A-1", "Id": "1", "ModifiedOn": "1/2/23", "StatusReason": "Completed/Sent", @@ -16,7 +16,7 @@ ] }, { - "InquiryId": "2", + "InquiryId": "A-2", "Id": "2", "ModifiedOn": "1/21/23", "StatusReason": "Completed/Sent", @@ -31,7 +31,7 @@ ] }, { - "InquiryId": "2", + "InquiryId": "A-2", "Id": "3", "ModifiedOn": "1/22/23", "StatusReason": "Completed/Sent", @@ -41,7 +41,7 @@ "AttachmentNames": [] }, { - "InquiryId": "2", + "InquiryId": "A-2", "Id": "4", "ModifiedOn": "1/23/23", "StatusReason": "Completed/Sent", @@ -51,7 +51,7 @@ "AttachmentNames": [] }, { - "InquiryId": "9", + "InquiryId": "A-9", "Id": "5", "ModifiedOn": "8/23/23", "StatusReason": "Completed/Sent", @@ -61,7 +61,7 @@ "AttachmentNames": [] }, { - "InquiryId": "9", + "InquiryId": "A-9", "Id": "6", "ModifiedOn": "8/24/23", "StatusReason": "Completed/Sent", @@ -71,7 +71,7 @@ "AttachmentNames": [] }, { - "InquiryId": "9", + "InquiryId": "A-9", "Id": "7", "ModifiedOn": "8/24/23", "StatusReason": "Completed/Sent", @@ -81,7 +81,7 @@ "AttachmentNames": [] }, { - "InquiryId": "9", + "InquiryId": "A-9", "Id": "7", "ModifiedOn": "8/25/23", "StatusReason": "Completed/Sent", @@ -91,7 +91,7 @@ "AttachmentNames": [] }, { - "InquiryId": "9", + "InquiryId": "A-9", "Id": "8", "ModifiedOn": "8/26/23", "StatusReason": "Completed/Sent", @@ -101,7 +101,7 @@ "AttachmentNames": [] }, { - "InquiryId": "9", + "InquiryId": "A-9", "Id": "8", "ModifiedOn": "8/26/23", "StatusReason": "Completed/Sent", @@ -111,7 +111,7 @@ "AttachmentNames": [] }, { - "InquiryId": "9", + "InquiryId": "A-9", "Id": "9", "ModifiedOn": "8/28/23", "StatusReason": "Completed/Sent", diff --git a/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/entity_spec.rb b/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/entity_spec.rb index 5fd91ba4c52..c6f0f9931bf 100644 --- a/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/entity_spec.rb +++ b/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/entity_spec.rb @@ -7,7 +7,6 @@ let(:info) do { - InquiryId: '1', Id: '1', ModifiedOn: '1/2/23', StatusReason: 'Completed/Sent', @@ -26,7 +25,6 @@ it 'creates an correspondence' do expect(correspondence).to have_attributes( - inquiry_id: info[:InquiryId], id: info[:Id], modified_on: info[:ModifiedOn], status_reason: info[:StatusReason], diff --git a/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/retriever_spec.rb b/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/retriever_spec.rb index cc8311c6052..d66d4ff2219 100644 --- a/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/retriever_spec.rb +++ b/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/retriever_spec.rb @@ -3,17 +3,17 @@ require 'rails_helper' RSpec.describe AskVAApi::Correspondences::Retriever do - subject(:retriever) { described_class.new(inquiry_id:) } + subject(:retriever) do + described_class.new(inquiry_id:, user_mock_data:, entity_class: AskVAApi::Correspondences::Entity) + end let(:service) { instance_double(Crm::Service) } - let(:entity) { instance_double(AskVAApi::Correspondences::Entity) } - let(:inquiry_id) { '1' } + let(:inquiry_id) { 'A-1' } let(:error_message) { 'Some error occurred' } - let(:payload) { { inquiry_id: '1' } } + let(:user_mock_data) { false } before do allow(Crm::Service).to receive(:new).and_return(service) - allow(AskVAApi::Correspondences::Entity).to receive(:new).and_return(entity) allow(service).to receive(:call) end @@ -28,29 +28,31 @@ end context 'when Crm raise an error' do - let(:payload) { { InquiryId: '1' } } - let(:response) { instance_double(Faraday::Response, status: 400, body: 'Bad Request') } - let(:endpoint) { AskVAApi::Correspondences::ENDPOINT } - let(:error_message) { "Bad request to #{endpoint}: #{response.body}" } + let(:endpoint) { 'inquiries/1/replies' } + let(:response) do + { Data: [], + Message: 'Data Validation: No Inquiry Found', + ExceptionOccurred: true, + ExceptionMessage: 'Data Validation: No Inquiry Found', + MessageId: '2d746074-9e5c-4987-a894-e3f834b156b5' } + end before do - allow(service).to receive(:call) - .with(endpoint:, payload:) - .and_raise(Crm::ErrorHandler::ServiceError, error_message) + allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('Token') + allow(service).to receive(:call).and_return(response) end - it 'raises an Error' do - expect do - retriever.call - end.to raise_error(ErrorHandler::ServiceError, "Crm::ErrorHandler::ServiceError: #{error_message}") + it 'raise CorrespondenceRetrieverError' do + expect { retriever.call }.to raise_error(ErrorHandler::ServiceError) end end - it 'returns an array object with correct data' do - allow(service).to receive(:call) - .with(endpoint: 'replies', payload: { InquiryId: inquiry_id }) - .and_return({ Data: [double] }) - expect(retriever.call).to eq([entity]) + context 'when successful' do + let(:user_mock_data) { true } + + it 'returns an array object with correct data' do + expect(retriever.call.first).to be_a(AskVAApi::Correspondences::Entity) + end end end end diff --git a/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/serializer_spec.rb b/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/serializer_spec.rb index fe5b272a0ad..72c30cd5d72 100644 --- a/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/serializer_spec.rb +++ b/modules/ask_va_api/spec/app/lib/ask_va_api/correspondences/serializer_spec.rb @@ -9,15 +9,14 @@ let(:cor2) { AskVAApi::Correspondences::Entity.new(data.last) } let(:response) { described_class.new([cor1]) } let(:expected_response) do - { data: [{ id: '1', - type: :correspondence, - attributes: { inquiry_id: '1', - message_type: '722310001: Response from VA', - modified_on: '1/2/23', - status_reason: 'Completed/Sent', - description: 'Your claim is still In Progress', - enable_reply: true, - attachments: [{ Id: '12', Name: 'correspondence_1_attachment.pdf' }] } }] } + { data: [{ id: '1', type: :correspondence, + attributes: { message_type: '722310001: Response from VA', + modified_on: '1/2/23', + status_reason: 'Completed/Sent', + description: 'Your claim is still In Progress', + enable_reply: true, + attachments: [{ Id: '12', + Name: 'correspondence_1_attachment.pdf' }] } }] } end context 'when successful' do diff --git a/modules/ask_va_api/spec/app/lib/ask_va_api/inquiries/retriever_spec.rb b/modules/ask_va_api/spec/app/lib/ask_va_api/inquiries/retriever_spec.rb index d4c64631c94..b848c81cb93 100644 --- a/modules/ask_va_api/spec/app/lib/ask_va_api/inquiries/retriever_spec.rb +++ b/modules/ask_va_api/spec/app/lib/ask_va_api/inquiries/retriever_spec.rb @@ -3,75 +3,159 @@ require 'rails_helper' RSpec.describe AskVAApi::Inquiries::Retriever do - subject(:retriever) { described_class.new(icn:, service:) } + subject(:retriever) do + described_class.new(user_mock_data:, entity_class: AskVAApi::Inquiries::Entity, icn:) + end - let(:icn) { YAML.load_file('./modules/ask_va_api/config/locales/constants.yml')['test_users']['test_user_228_icn'] } - let(:service) { DynamicsMockService.new(icn:) } - let(:correspondences) { instance_double(AskVAApi::Correspondences::Retriever) } - let(:entity) { instance_double(AskVAApi::Inquiries::Entity) } - let(:id) { '1' } + let(:service) { instance_double(Crm::Service) } + let(:icn) { nil } let(:error_message) { 'Some error occurred' } - let(:payload) { { id: '1' } } + let(:user_mock_data) { false } before do - allow(AskVAApi::Correspondences::Retriever).to receive(:new).and_return(correspondences) - allow(correspondences).to receive(:call).and_return(entity) - allow(AskVAApi::Inquiries::Entity).to receive(:new).and_return(entity) + allow(Crm::Service).to receive(:new).and_return(service) + allow(service).to receive(:call) end - describe '#fetch_by_id' do - it 'returns an Entity object with correct data' do - expect(retriever.fetch_by_id(id:)).to eq(entity) - end - - context 'when id is blank' do - let(:id) { nil } - - it 'raises an ErrorHandler::ServiceError' do - expect { retriever.fetch_by_id(id:) } - .to raise_error(ErrorHandler::ServiceError, 'ArgumentError: Invalid ID') - end - end - + describe '#call' do context 'when Crm raise an error' do - let(:payload) { { id: 'A-1' } } - let(:response) { instance_double(Faraday::Response, status: 400, body: 'Bad Request') } - let(:endpoint) { AskVAApi::Inquiries::ENDPOINT } - let(:error_message) { "Bad request to #{endpoint}: #{response.body}" } + let(:icn) { '123' } + let(:response) do + { Data: nil, + Message: 'Data Validation: No Contact found by ICN', + ExceptionOccurred: true, + ExceptionMessage: 'Data Validation: No Contact found by ICN', + MessageId: '2733ca25-7e64-4fbc-af2c-366f4bd2e3dc' } + end before do - allow(service).to receive(:call) - .with(endpoint:, payload:) - .and_raise(Crm::ErrorHandler::ServiceError, error_message) + allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('Token') + allow(service).to receive(:call).and_return(response) end - it 'raises a FetchInquiriesError' do - expect do - retriever.fetch_by_id(id: 'A-1') - end.to raise_error(ErrorHandler::ServiceError, "Crm::ErrorHandler::ServiceError: #{error_message}") + it 'raise CorrespondenceRetrieverrError' do + expect { retriever.call }.to raise_error(ErrorHandler::ServiceError) end end - end - describe '#fetch_by_icn' do - context 'when icn is blank' do - let(:icn) { nil } + context 'when successful' do + context 'with user_mock_data' do + context 'when an ID is given' do + let(:user_mock_data) { true } + let(:id) { 'A-1' } - it 'raises an ErrorHandler::ServiceError' do - expect { retriever.fetch_by_icn } - .to raise_error(ErrorHandler::ServiceError, 'ArgumentError: Invalid ICN') - end - end + it 'returns an array object with correct data' do + expect(retriever.fetch_by_id(id:)).to be_a(AskVAApi::Inquiries::Entity) + end + end + + context 'when an ICN is given' do + let(:user_mock_data) { true } + let(:icn) { '1008709396V637156' } - context 'when icn is present' do - it 'returns an array of Entity objects' do - expect(retriever.fetch_by_icn.first).to eq(entity) + it 'returns an array object with correct data' do + expect(retriever.call.first).to be_a(AskVAApi::Inquiries::Entity) + end + end end - context 'when there are no inquiries' do - it 'returns an empty array' do - allow(service).to receive(:call).and_return({ Data: [] }) - expect(retriever.fetch_by_icn).to be_empty + context 'with Crm::Service' do + context 'when an ID is given' do + let(:id) { '123' } + let(:response) do + { Data: [{ Id: '154163f2-8fbb-ed11-9ac4-00155da17a6f', + InquiryNumber: 'A-20230305-306178', + InquiryStatus: 'Reopened', + SubmitterQuestion: 'test', + LastUpdate: '4/1/2024 12:00:00 AM', + InquiryHasAttachments: true, + InquiryHasBeenSplit: true, + VeteranRelationship: 'GIBillBeneficiary', + SchoolFacilityCode: '77a51029-6816-e611-9436-0050568d743d', + InquiryTopic: 'Medical Care Concerns at a VA Medical Facility', + InquiryLevelOfAuthentication: 'Unauthenticated', + AttachmentNames: [{ Id: '367e8d31-6c82-1d3c-81b8-dd2cabed7555', + Name: 'Test.txt' }] }] } + end + + before do + allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('Token') + allow(service).to receive(:call).and_return(response) + end + + it 'returns an array object with correct data' do + expect(retriever.fetch_by_id(id:)).to be_a(AskVAApi::Inquiries::Entity) + end + end + + context 'when an ICN is given' do + let(:icn) { '1013694290V263188' } + let(:response) do + { + Data: [ + { + Id: '154163f2-8fbb-ed11-9ac4-00155da17a6f', + InquiryNumber: 'A-20230305-306178', + InquiryStatus: 'Reopened', + SubmitterQuestion: 'test', + LastUpdate: '4/1/2024 12:00:00 AM', + InquiryHasAttachments: true, + InquiryHasBeenSplit: true, + VeteranRelationship: 'GIBillBeneficiary', + SchoolFacilityCode: '77a51029-6816-e611-9436-0050568d743d', + InquiryTopic: 'Medical Care Concerns at a VA Medical Facility', + InquiryLevelOfAuthentication: 'Unauthenticated', + AttachmentNames: [ + { + Id: '367e8d31-6c82-1d3c-81b8-dd2cabed7555', + Name: 'Test.txt' + } + ] + }, + { + Id: 'b24e8113-92d1-ed11-9ac4-00155da17a6f', + InquiryNumber: 'A-20230402-306218', + InquiryStatus: 'New', + SubmitterQuestion: 'test', + LastUpdate: '1/1/0001 12:00:00 AM', + InquiryHasAttachments: false, + InquiryHasBeenSplit: false, + VeteranRelationship: nil, + SchoolFacilityCode: '77a51029-6816-e611-9436-0050568d743d', + InquiryTopic: 'Medical Care Concerns at a VA Medical Facility', + InquiryLevelOfAuthentication: 'Personal', + AttachmentNames: nil + }, + { + Id: 'e1ce6ae6-40ec-ee11-904d-001dd8306a72', + InquiryNumber: 'A-20240327-307060', + InquiryStatus: 'New', + SubmitterQuestion: 'test', + LastUpdate: '3/27/2024 12:00:00 AM', + InquiryHasAttachments: true, + InquiryHasBeenSplit: true, + VeteranRelationship: nil, + SchoolFacilityCode: nil, + InquiryTopic: 'Filing for compensation benefits', + InquiryLevelOfAuthentication: 'Personal', + AttachmentNames: nil + } + ], + Message: nil, + ExceptionOccurred: false, + ExceptionMessage: nil, + MessageId: '3779a3c5-15a5-4846-8198-d499a0bbfe1f' + } + end + + before do + allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('Token') + allow(service).to receive(:call).and_return(response) + end + + it 'returns an array object with correct data' do + expect(retriever.call.first).to be_a(AskVAApi::Inquiries::Entity) + end end end end diff --git a/modules/ask_va_api/spec/app/lib/ask_va_api/optionset/retriever_spec.rb b/modules/ask_va_api/spec/app/lib/ask_va_api/optionset/retriever_spec.rb index 53734fa6b7d..397bc122d97 100644 --- a/modules/ask_va_api/spec/app/lib/ask_va_api/optionset/retriever_spec.rb +++ b/modules/ask_va_api/spec/app/lib/ask_va_api/optionset/retriever_spec.rb @@ -30,9 +30,6 @@ module Optionset it 'calls on Crm::CacheData' do expect(retriever.call).to all(be_a(entity_class)) - - expect(cache_data_service).to have_received(:call).with(endpoint: 'OptionSet', cache_key: name, - payload: { name: 'iris_branchofservice' }) end end end diff --git a/modules/ask_va_api/spec/app/lib/ask_va_api/optionset/serializer_spec.rb b/modules/ask_va_api/spec/app/lib/ask_va_api/optionset/serializer_spec.rb index 56d7b067e72..0d3d2901cfd 100644 --- a/modules/ask_va_api/spec/app/lib/ask_va_api/optionset/serializer_spec.rb +++ b/modules/ask_va_api/spec/app/lib/ask_va_api/optionset/serializer_spec.rb @@ -11,13 +11,7 @@ end let(:optionset) { AskVAApi::Optionset::Entity.new(info) } let(:response) { described_class.new(optionset) } - let(:expected_response) do - { data: { id: '722310000', - type: :optionsets, - attributes: { - name: info[:Name] - } } } - end + let(:expected_response) { { data: { id: '722310000', type: :optionsets, attributes: { name: 'Air Force' } } } } context 'when successful' do it 'returns a json hash' do diff --git a/modules/ask_va_api/spec/requests/v0/inquiries_spec.rb b/modules/ask_va_api/spec/requests/v0/inquiries_spec.rb index 3f64c8facf1..1427d49bed9 100644 --- a/modules/ask_va_api/spec/requests/v0/inquiries_spec.rb +++ b/modules/ask_va_api/spec/requests/v0/inquiries_spec.rb @@ -11,7 +11,7 @@ let(:mock_inquiries) do JSON.parse(File.read('modules/ask_va_api/config/locales/get_inquiries_mock_data.json'))['Data'] end - let(:valid_id) { mock_inquiries.first['Id'] } + let(:valid_id) { mock_inquiries.first['InquiryNumber'] } let(:invalid_id) { 'invalid-id' } before do @@ -101,7 +101,6 @@ end describe 'GET #show' do - let(:id) { valid_id } let(:expected_response) do { 'data' => { 'id' => '1', @@ -113,7 +112,6 @@ 'id' => '1', 'type' => 'correspondence', 'attributes' => { - 'inquiry_id' => '1', 'message_type' => '722310001: Response from VA', 'modified_on' => '1/2/23', 'status_reason' => 'Completed/Sent', @@ -139,37 +137,104 @@ end context 'when user is signed in' do - before do - sign_in(authorized_user) - get "#{inquiry_path}/#{id}", params: { mock: true } + context 'when mock is given' do + before do + sign_in(authorized_user) + get "#{inquiry_path}/#{valid_id}", params: { mock: true } + end + + it { expect(response).to have_http_status(:ok) } + it { expect(JSON.parse(response.body)).to eq(expected_response) } end - it { expect(response).to have_http_status(:ok) } - it { expect(JSON.parse(response.body)).to eq(expected_response) } + context 'when mock is not given' do + let(:crm_response) do + { Data: [{ Id: '154163f2-8fbb-ed11-9ac4-00155da17a6f', + InquiryNumber: 'A-20230305-306178', + InquiryStatus: 'Reopened', + SubmitterQuestion: 'test', + LastUpdate: '4/1/2024 12:00:00 AM', + InquiryHasAttachments: true, + InquiryHasBeenSplit: true, + VeteranRelationship: 'GIBillBeneficiary', + SchoolFacilityCode: '77a51029-6816-e611-9436-0050568d743d', + InquiryTopic: 'Medical Care Concerns at a VA Medical Facility', + InquiryLevelOfAuthentication: 'Unauthenticated', + AttachmentNames: [{ Id: '367e8d31-6c82-1d3c-81b8-dd2cabed7555', + Name: 'Test.txt' }] }] } + end + let(:expected_response) do + { 'data' => + { 'id' => '154163f2-8fbb-ed11-9ac4-00155da17a6f', + 'type' => 'inquiry', + 'attributes' => + { 'inquiry_number' => 'A-20230305-306178', + 'attachments' => [{ 'Id' => '367e8d31-6c82-1d3c-81b8-dd2cabed7555', 'Name' => 'Test.txt' }], + 'correspondences' => + { 'data' => + [{ 'id' => '154163f2-8fbb-ed11-9ac4-00155da17a6f', + 'type' => 'correspondence', + 'attributes' => + { 'message_type' => nil, + 'modified_on' => nil, + 'status_reason' => nil, + 'description' => nil, + 'enable_reply' => nil, + 'attachments' => [{ 'Id' => '367e8d31-6c82-1d3c-81b8-dd2cabed7555', + 'Name' => 'Test.txt' }] } }] }, + 'has_attachments' => true, + 'has_been_split' => true, + 'level_of_authentication' => 'Unauthenticated', + 'last_update' => '4/1/2024 12:00:00 AM', + 'status' => 'Reopened', + 'submitter_question' => 'test', + 'school_facility_code' => '77a51029-6816-e611-9436-0050568d743d', + 'topic' => 'Medical Care Concerns at a VA Medical Facility', + 'veteran_relationship' => 'GIBillBeneficiary' } } } + end + let(:service) { instance_double(Crm::Service) } + + before do + allow(Crm::Service).to receive(:new).and_return(service) + allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('Token') + allow(service).to receive(:call).and_return(crm_response) + sign_in(authorized_user) + get "#{inquiry_path}/#{valid_id}" + end - context 'when the id is invalid' do - let(:id) { invalid_id } + it { expect(response).to have_http_status(:ok) } + it { expect(JSON.parse(response.body)).to eq(expected_response) } + end - it { expect(response).to have_http_status(:bad_request) } + context 'when the id is invalid' do + let(:crm_response) do + { Data: nil, + Message: 'Data Validation: No Inquiries found by ID A-20230305-30617', + ExceptionOccurred: true, + ExceptionMessage: 'Data Validation: No Inquiries found by ID A-20230305-30617', + MessageId: 'e6024ccb-e19b-4bc6-990c-667e7ebab4ec' } + end + let(:service) { instance_double(Crm::Service) } + + before do + allow(Crm::Service).to receive(:new).and_return(service) + allow_any_instance_of(Crm::CrmToken).to receive(:call).and_return('Token') + allow(service).to receive(:call).and_return(crm_response) + sign_in(authorized_user) + get "#{inquiry_path}/#{invalid_id}" + end - it_behaves_like 'common error handling', :bad_request, 'invalid_inquiry_error', - 'AskVAApi::V0::InquiriesController::InvalidInquiryError' - end - end + it { expect(response).to have_http_status(:unprocessable_entity) } - context 'when an error occur' do - before do - allow(Crm::Service).to receive(:new).and_raise(ErrorHandler::ServiceError) - sign_in(authorized_user) - get "#{inquiry_path}/#{id}" + it_behaves_like 'common error handling', :unprocessable_entity, 'service_error', + 'AskVAApi::Inquiries::InquiriesRetrieverError: ' \ + 'Data Validation: No Inquiries found by ID A-20230305-30617' end - - it { expect(JSON.parse(response.body)).to eq('error' => 'ErrorHandler::ServiceError') } end context 'when user is not signed in' do before do - get "#{inquiry_path}/#{id}" + get "#{inquiry_path}/#{valid_id}" end it { expect(response).to have_http_status(:unauthorized) }