pvdgm_services 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (256) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +29 -0
  3. data/Rakefile +40 -0
  4. data/app/contexts/services/accept_hl7_message_context.rb +32 -0
  5. data/app/contexts/services/application_mds_context.rb +21 -0
  6. data/app/contexts/services/complete_sltc_registration_process_context.rb +23 -0
  7. data/app/contexts/services/create_account_mapping_context.rb +23 -0
  8. data/app/contexts/services/create_configured_account_context.rb +26 -0
  9. data/app/contexts/services/create_configured_facility_context.rb +26 -0
  10. data/app/contexts/services/create_credential_context.rb +23 -0
  11. data/app/contexts/services/create_facility_mapping_context.rb +23 -0
  12. data/app/contexts/services/create_public_key_context.rb +22 -0
  13. data/app/contexts/services/create_service_context.rb +22 -0
  14. data/app/contexts/services/create_service_definition_context.rb +26 -0
  15. data/app/contexts/services/create_sltc_registration_context.rb +24 -0
  16. data/app/contexts/services/create_third_party_context.rb +22 -0
  17. data/app/contexts/services/hl7_message_trimming_context.rb +19 -0
  18. data/app/contexts/services/invoke_service_context.rb +21 -0
  19. data/app/contexts/services/list_sltc_providers_context.rb +22 -0
  20. data/app/contexts/services/mds_file_processing_context.rb +27 -0
  21. data/app/contexts/services/mds_file_upload_context.rb +25 -0
  22. data/app/contexts/services/mds_pull_context.rb +21 -0
  23. data/app/contexts/services/monthly_service_table_cleanup_context.rb +19 -0
  24. data/app/contexts/services/notify_new_registration_context.rb +22 -0
  25. data/app/contexts/services/notify_sltc_provider_change_context.rb +19 -0
  26. data/app/contexts/services/request_sltc_baseline_context.rb +23 -0
  27. data/app/contexts/services/update_account_mapping_context.rb +23 -0
  28. data/app/contexts/services/update_configured_account_context.rb +26 -0
  29. data/app/contexts/services/update_configured_facility_context.rb +26 -0
  30. data/app/contexts/services/update_credential_context.rb +23 -0
  31. data/app/contexts/services/update_facility_mapping_context.rb +23 -0
  32. data/app/contexts/services/update_public_key_context.rb +23 -0
  33. data/app/contexts/services/update_service_context.rb +23 -0
  34. data/app/contexts/services/update_service_definition_context.rb +26 -0
  35. data/app/contexts/services/update_third_party_context.rb +23 -0
  36. data/app/controllers/services/account_mappings_controller.rb +54 -0
  37. data/app/controllers/services/adts_controller.rb +35 -0
  38. data/app/controllers/services/application_controller.rb +17 -0
  39. data/app/controllers/services/assessment_requests_controller.rb +48 -0
  40. data/app/controllers/services/available_files_controller.rb +19 -0
  41. data/app/controllers/services/configured_account_base_controller.rb +17 -0
  42. data/app/controllers/services/configured_accounts_controller.rb +59 -0
  43. data/app/controllers/services/configured_facilities_controller.rb +65 -0
  44. data/app/controllers/services/facility_mappings_controller.rb +58 -0
  45. data/app/controllers/services/mds_files_controller.rb +75 -0
  46. data/app/controllers/services/mds_pull_accounts_controller.rb +57 -0
  47. data/app/controllers/services/public_keys_controller.rb +50 -0
  48. data/app/controllers/services/service_base_controller.rb +18 -0
  49. data/app/controllers/services/service_definition_base_controller.rb +31 -0
  50. data/app/controllers/services/service_definitions_controller.rb +59 -0
  51. data/app/controllers/services/services_controller.rb +56 -0
  52. data/app/controllers/services/sltc_providers_controller.rb +15 -0
  53. data/app/controllers/services/sltc_registrations_controller.rb +74 -0
  54. data/app/controllers/services/status_masking.rb +17 -0
  55. data/app/controllers/services/third_parties_controller.rb +47 -0
  56. data/app/controllers/services/third_party_base_controller.rb +17 -0
  57. data/app/controllers/services/validation_controller.rb +17 -0
  58. data/app/helpers/services/application_helper.rb +8 -0
  59. data/app/helpers/services/assessment_request_helper.rb +19 -0
  60. data/app/helpers/services/available_files_helper.rb +27 -0
  61. data/app/helpers/services/mds_pull_accounts_helper.rb +27 -0
  62. data/app/helpers/services/sltc_registrations_helper.rb +15 -0
  63. data/app/mailers/services_mailer.rb +57 -0
  64. data/app/models/services/abaqis_mds_push.rb +51 -0
  65. data/app/models/services/account_mapping.rb +10 -0
  66. data/app/models/services/application_api.rb +19 -0
  67. data/app/models/services/assessment_request.rb +18 -0
  68. data/app/models/services/available_file.rb +73 -0
  69. data/app/models/services/configured_account.rb +26 -0
  70. data/app/models/services/configured_facility.rb +16 -0
  71. data/app/models/services/credential.rb +9 -0
  72. data/app/models/services/facility_mapping.rb +12 -0
  73. data/app/models/services/ftp_server.rb +69 -0
  74. data/app/models/services/hl7_inbound_service.rb +47 -0
  75. data/app/models/services/hl7_message.rb +27 -0
  76. data/app/models/services/isc_code.rb +15 -0
  77. data/app/models/services/isc_code_lookup.rb +75 -0
  78. data/app/models/services/mds_assessment.rb +371 -0
  79. data/app/models/services/mds_content.rb +55 -0
  80. data/app/models/services/mds_pull.rb +41 -0
  81. data/app/models/services/mds_pull_account.rb +192 -0
  82. data/app/models/services/mds_push.rb +24 -0
  83. data/app/models/services/mds_upload.rb +64 -0
  84. data/app/models/services/mds_upload_content.rb +148 -0
  85. data/app/models/services/mds_ws_response.rb +21 -0
  86. data/app/models/services/mds_ws_response_handler.rb +31 -0
  87. data/app/models/services/pcc_mds_pull.rb +77 -0
  88. data/app/models/services/provider_change.rb +26 -0
  89. data/app/models/services/public_key.rb +11 -0
  90. data/app/models/services/service.rb +20 -0
  91. data/app/models/services/service_definition.rb +37 -0
  92. data/app/models/services/service_implementation.rb +29 -0
  93. data/app/models/services/sltc_api.rb +179 -0
  94. data/app/models/services/sltc_api_exception.rb +54 -0
  95. data/app/models/services/sltc_mds_pull.rb +230 -0
  96. data/app/models/services/sltc_registration.rb +23 -0
  97. data/app/models/services/third_party.rb +18 -0
  98. data/app/roles/services/account_mapping_creator.rb +13 -0
  99. data/app/roles/services/account_mapping_updator.rb +12 -0
  100. data/app/roles/services/configured_account_creator.rb +13 -0
  101. data/app/roles/services/configured_account_updater.rb +12 -0
  102. data/app/roles/services/configured_facility_creator.rb +13 -0
  103. data/app/roles/services/configured_facility_updater.rb +12 -0
  104. data/app/roles/services/credential_creator.rb +11 -0
  105. data/app/roles/services/credential_updater.rb +20 -0
  106. data/app/roles/services/facility_mapping_creator.rb +13 -0
  107. data/app/roles/services/facility_mapping_updater.rb +11 -0
  108. data/app/roles/services/hl7_adt_message_saver.rb +15 -0
  109. data/app/roles/services/hl7_message_trimmer.rb +14 -0
  110. data/app/roles/services/mds_assessment_categorizer.rb +71 -0
  111. data/app/roles/services/mds_file_processor.rb +86 -0
  112. data/app/roles/services/mds_notifications.rb +44 -0
  113. data/app/roles/services/monthly_service_table_cleaner.rb +19 -0
  114. data/app/roles/services/new_registration_notifier.rb +11 -0
  115. data/app/roles/services/public_key_creator.rb +14 -0
  116. data/app/roles/services/public_key_updater.rb +12 -0
  117. data/app/roles/services/service_creator.rb +13 -0
  118. data/app/roles/services/service_definition_creator.rb +13 -0
  119. data/app/roles/services/service_definition_updater.rb +12 -0
  120. data/app/roles/services/service_invoker.rb +25 -0
  121. data/app/roles/services/service_updater.rb +11 -0
  122. data/app/roles/services/sltc_baseline_requestor.rb +19 -0
  123. data/app/roles/services/sltc_provider_change_notifier.rb +25 -0
  124. data/app/roles/services/sltc_provider_lister.rb +13 -0
  125. data/app/roles/services/sltc_registration_completer.rb +81 -0
  126. data/app/roles/services/sltc_registration_saver.rb +13 -0
  127. data/app/roles/services/submit_mds_file_for_processing.rb +28 -0
  128. data/app/roles/services/third_party_creator.rb +13 -0
  129. data/app/roles/services/third_party_updater.rb +12 -0
  130. data/app/utils/services/file_upload_handler.rb +33 -0
  131. data/app/utils/services/mds_upload_filters.rb +26 -0
  132. data/app/utils/services/mds_xml_file_parser.rb +104 -0
  133. data/app/utils/services/upload_file.rb +13 -0
  134. data/app/validators/isc_code_validator.rb +42 -0
  135. data/app/validators/mds_birthdate_validator.rb +37 -0
  136. data/app/validators/mds_date_validator.rb +8 -0
  137. data/app/validators/mds_integer_validator.rb +15 -0
  138. data/app/validators/mds_version_validator.rb +16 -0
  139. data/app/validators/state_code_validator.rb +9 -0
  140. data/app/views/layouts/services/application.html.erb +14 -0
  141. data/app/views/services/account_mappings/index.json.jbuilder +9 -0
  142. data/app/views/services/account_mappings/show.json.jbuilder +7 -0
  143. data/app/views/services/assessment_requests/index.json.jbuilder +11 -0
  144. data/app/views/services/available_files/index.json.jbuilder +13 -0
  145. data/app/views/services/configured_accounts/index.json.jbuilder +13 -0
  146. data/app/views/services/configured_accounts/show.json.jbuilder +11 -0
  147. data/app/views/services/configured_facilities/index.json.jbuilder +13 -0
  148. data/app/views/services/configured_facilities/show.json.jbuilder +11 -0
  149. data/app/views/services/facility_mappings/index.json.jbuilder +10 -0
  150. data/app/views/services/facility_mappings/show.json.jbuilder +8 -0
  151. data/app/views/services/mds_files/create.builder +10 -0
  152. data/app/views/services/mds_pull_accounts/index.json.jbuilder +11 -0
  153. data/app/views/services/public_keys/index.json.jbuilder +8 -0
  154. data/app/views/services/public_keys/show.json.jbuilder +6 -0
  155. data/app/views/services/service_definitions/index.json.jbuilder +17 -0
  156. data/app/views/services/service_definitions/show.json.jbuilder +16 -0
  157. data/app/views/services/services/index.json.jbuilder +6 -0
  158. data/app/views/services/services/show.json.jbuilder +4 -0
  159. data/app/views/services/sltc_providers/index.json.jbuilder +10 -0
  160. data/app/views/services/sltc_registrations/index.json.jbuilder +10 -0
  161. data/app/views/services/third_parties/index.json.jbuilder +7 -0
  162. data/app/views/services/third_parties/show.json.jbuilder +5 -0
  163. data/app/views/services_mailer/burying_job.html.erb +20 -0
  164. data/app/views/services_mailer/delaying_job.html.erb +20 -0
  165. data/app/views/services_mailer/notify_sltc_client_registration.html.erb +39 -0
  166. data/app/views/services_mailer/notify_sltc_client_registration_invalid_request_type.html.erb +8 -0
  167. data/app/views/services_mailer/notify_sltc_client_registration_request_invalid.html.erb +10 -0
  168. data/app/views/services_mailer/notify_sltc_client_registration_validation_errors.html.erb +13 -0
  169. data/app/views/services_mailer/notify_sltc_provider_changes.html.erb +26 -0
  170. data/app/views/services_mailer/notify_support_about_no_mds_pull.html.erb +6 -0
  171. data/config/routes.rb +48 -0
  172. data/db/migrate/20140102000000_create_services_engine_tables.rb +131 -0
  173. data/db/migrate/20140517184450_new_services_columns.rb +24 -0
  174. data/db/migrate/20140525142842_new_configured_provider_table.rb +18 -0
  175. data/db/migrate/20140714172442_add_error_column_to_hl7_messages.rb +5 -0
  176. data/db/migrate/20140730164152_mds_upload_tables.rb +78 -0
  177. data/db/sql_data/service_data_setup.sql +24 -0
  178. data/lib/services/engine.rb +40 -0
  179. data/lib/services/version.rb +3 -0
  180. data/lib/services.rb +4 -0
  181. data/lib/tasks/services_tasks.rake +40 -0
  182. data/spec/dummy/README.rdoc +261 -0
  183. data/spec/dummy/Rakefile +7 -0
  184. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  185. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  186. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  187. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  188. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  189. data/spec/dummy/config/application.rb +60 -0
  190. data/spec/dummy/config/boot.rb +10 -0
  191. data/spec/dummy/config/database.yml +29 -0
  192. data/spec/dummy/config/environment.rb +5 -0
  193. data/spec/dummy/config/environments/development.rb +35 -0
  194. data/spec/dummy/config/environments/production.rb +68 -0
  195. data/spec/dummy/config/environments/test.rb +32 -0
  196. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  197. data/spec/dummy/config/initializers/email.rb +1 -0
  198. data/spec/dummy/config/initializers/inflections.rb +15 -0
  199. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  200. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  201. data/spec/dummy/config/initializers/session_store.rb +8 -0
  202. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  203. data/spec/dummy/config/locales/en.yml +5 -0
  204. data/spec/dummy/config/routes.rb +4 -0
  205. data/spec/dummy/config.ru +4 -0
  206. data/spec/dummy/db/schema.rb +237 -0
  207. data/spec/dummy/db/sql_data/services_isc_codes.sql +878 -0
  208. data/spec/dummy/log/test.log +3498 -0
  209. data/spec/dummy/public/404.html +26 -0
  210. data/spec/dummy/public/422.html +26 -0
  211. data/spec/dummy/public/500.html +25 -0
  212. data/spec/dummy/public/favicon.ico +0 -0
  213. data/spec/dummy/script/rails +6 -0
  214. data/spec/models/abaqis_mds_push_spec.rb +120 -0
  215. data/spec/models/available_file_spec.rb +234 -0
  216. data/spec/models/configured_account_spec.rb +39 -0
  217. data/spec/models/ftp_server_spec.rb +221 -0
  218. data/spec/models/isc_code_lookup_spec.rb +125 -0
  219. data/spec/models/isc_code_spec.rb +5 -0
  220. data/spec/models/mds_assessment_spec.rb +1070 -0
  221. data/spec/models/mds_pull_account_spec.rb +468 -0
  222. data/spec/models/mds_pull_spec.rb +48 -0
  223. data/spec/models/mds_push_spec.rb +43 -0
  224. data/spec/models/mds_ws_response_spec.rb +54 -0
  225. data/spec/models/pcc_mds_pull_spec.rb +273 -0
  226. data/spec/models/service_implementation_spec.rb +88 -0
  227. data/spec/models/sltc_api_exception_spec.rb +136 -0
  228. data/spec/models/sltc_api_spec.rb +192 -0
  229. data/spec/models/sltc_mds_pull_spec.rb +776 -0
  230. data/spec/roles/account_mapping_creator_spec.rb +40 -0
  231. data/spec/roles/account_mapping_updator_spec.rb +16 -0
  232. data/spec/roles/configured_account_creator_spec.rb +40 -0
  233. data/spec/roles/configured_account_updater_spec.rb +16 -0
  234. data/spec/roles/configured_facility_creator_spec.rb +40 -0
  235. data/spec/roles/configured_facility_updater_spec.rb +16 -0
  236. data/spec/roles/credential_creator_spec.rb +23 -0
  237. data/spec/roles/credential_updater_spec.rb +38 -0
  238. data/spec/roles/facility_mapping_creator_spec.rb +40 -0
  239. data/spec/roles/facility_mapping_updater_spec.rb +16 -0
  240. data/spec/roles/hl7_adt_message_saver_spec.rb +35 -0
  241. data/spec/roles/hl7_message_trimmer_spec.rb +31 -0
  242. data/spec/roles/monthly_service_table_cleaner_spec.rb +27 -0
  243. data/spec/roles/new_registration_notifier_spec.rb +18 -0
  244. data/spec/roles/service_ceator_spec.rb +34 -0
  245. data/spec/roles/service_definition_creator_spec.rb +40 -0
  246. data/spec/roles/service_definition_updater_spec.rb +16 -0
  247. data/spec/roles/service_invoker_spec.rb +22 -0
  248. data/spec/roles/service_updater_spec.rb +17 -0
  249. data/spec/roles/sltc_baseline_requestor_spec.rb +30 -0
  250. data/spec/roles/sltc_provider_lister_spec.rb +27 -0
  251. data/spec/roles/sltc_registration_completer_spec.rb +187 -0
  252. data/spec/roles/sltc_registration_saver_spec.rb +34 -0
  253. data/spec/roles/third_party_creator_spec.rb +34 -0
  254. data/spec/roles/third_party_updater_spec.rb +17 -0
  255. data/spec/spec_helper.rb +72 -0
  256. metadata +581 -0
@@ -0,0 +1,776 @@
1
+ require 'spec_helper'
2
+
3
+ describe Services::SltcMdsPull do
4
+
5
+ context 'Instance Methods' do
6
+
7
+ before(:each) do
8
+ @mock_sd = mock_model(Services::ServiceDefinition)
9
+
10
+ @sltc_mds_pull = Services::SltcMdsPull.new(@mock_sd)
11
+ end
12
+
13
+ context '#providers_for_account' do
14
+ let(:service) { Services::Service.create!(name: 'Service1') }
15
+ let(:third_party) { tp = Services::ThirdParty.new(name: "TP1", contact_email: "me@mail.com"); tp.id = 3; tp.save!; tp }
16
+ let(:service_definition) { Services::ServiceDefinition.create!(service: service, third_party: third_party, service_class: 'SltcMdsPull') }
17
+ let(:configured_account) { Services::ConfiguredAccount.create!(service_definition: service_definition, account_id: 2) }
18
+ let(:smp) { Services::SltcMdsPull.new(service_definition) }
19
+
20
+ before :each do
21
+ Services::ApplicationApi.should_receive(:api_impl).and_return do
22
+ double("Impl").tap do | impl |
23
+ impl.should_receive(:enabled_facilities_for_account_id).with(configured_account.account_id).and_return do
24
+ double("Facilities").tap do | facilities |
25
+ facilities.should_receive(:pluck).with(:id).and_return([ 1, 2 ])
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ it 'returns an empty array if there are no providers configured for the account' do
33
+ expect(smp.providers_for_account(configured_account)).to eq([])
34
+ end
35
+
36
+ it "returns the facility codes for only the facilities associated with this account" do
37
+ # Set up the facilities
38
+ Services::FacilityMapping.create!(third_party: third_party, facility_id: 1, facility_code: 'P1')
39
+ Services::FacilityMapping.create!(third_party: third_party, facility_id: 2, facility_code: 'P2')
40
+ Services::FacilityMapping.create!(third_party: third_party, facility_id: 3, facility_code: 'P3')
41
+ Services::FacilityMapping.create!(third_party: third_party, facility_id: 4, facility_code: 'P4')
42
+
43
+ Services::ConfiguredFacility.create!(service_definition: service_definition, facility_id: 1, enabled: 1)
44
+ Services::ConfiguredFacility.create!(service_definition: service_definition, facility_id: 2, enabled: 1)
45
+ Services::ConfiguredFacility.create!(service_definition: service_definition, facility_id: 3, enabled: 1)
46
+ Services::ConfiguredFacility.create!(service_definition: service_definition, facility_id: 4, enabled: 1)
47
+
48
+ expect(smp.providers_for_account(configured_account)).to eq([ 'P1', 'P2' ])
49
+ end
50
+
51
+ it "returns the facility codes for only the facilities associated with this account and enabled" do
52
+ # Set up the facilities
53
+ Services::FacilityMapping.create!(third_party: third_party, facility_id: 1, facility_code: 'P1')
54
+ Services::FacilityMapping.create!(third_party: third_party, facility_id: 2, facility_code: 'P2')
55
+ Services::FacilityMapping.create!(third_party: third_party, facility_id: 3, facility_code: 'P3')
56
+ Services::FacilityMapping.create!(third_party: third_party, facility_id: 4, facility_code: 'P4')
57
+
58
+ Services::ConfiguredFacility.create!(service_definition: service_definition, facility_id: 1, enabled: 0)
59
+ Services::ConfiguredFacility.create!(service_definition: service_definition, facility_id: 2, enabled: 1)
60
+ Services::ConfiguredFacility.create!(service_definition: service_definition, facility_id: 3, enabled: 1)
61
+ Services::ConfiguredFacility.create!(service_definition: service_definition, facility_id: 4, enabled: 1)
62
+
63
+ expect(smp.providers_for_account(configured_account)).to eq([ 'P2' ])
64
+ end
65
+ end
66
+
67
+ context '#identify_providers for real' do
68
+ let(:service) { Services::Service.create!(name: 'Service1') }
69
+ let(:third_party) { tp = Services::ThirdParty.new(name: "TP1", contact_email: "me@mail.com"); tp.id = 3; tp.save!; tp }
70
+ let(:service_definition) { Services::ServiceDefinition.create!(service: service, third_party: third_party, service_class: 'SltcMdsPull') }
71
+ let(:configured_account) { Services::ConfiguredAccount.create!(service_definition: service_definition, account_id: 2) }
72
+ let(:smp) { Services::SltcMdsPull.new(service_definition) }
73
+
74
+ before :each do
75
+ @facility1 = double("Facility")
76
+ @facility1.stub(:id).and_return(1)
77
+ @facility1.stub(:govid).and_return("ccn1")
78
+ @facility1.stub(:state).and_return('CO')
79
+ @facility1.stub(:fac_id).and_return('fac1')
80
+
81
+ @facility2 = double("facility")
82
+ @facility2.stub(:id).and_return(2)
83
+ @facility2.stub(:govid).and_return("ccn2")
84
+ @facility2.stub(:state).and_return('CO')
85
+ @facility2.stub(:fac_id).and_return('fac2')
86
+
87
+ @m_api_impl = double("Impl")
88
+ @m_api_impl.stub(:enabled_facilities_for_account_id).with(configured_account.account_id).and_return do
89
+ double("Facilities").tap do | facilities |
90
+ facilities.should_receive(:pluck).with(:id).and_return([ 1, 2 ])
91
+ end
92
+ end
93
+ @m_api_impl.stub(:facilities_with_ids).and_return([])
94
+
95
+ Services::ApplicationApi.stub(:api_impl).and_return(@m_api_impl)
96
+ # Services::ApplicationApi.stub(:api_impl).and_return do
97
+ # double("Impl").tap do | impl |
98
+ # impl.stub(:enabled_facilities_for_account_id).with(configured_account.account_id).and_return do
99
+ # double("Facilities").tap do | facilities |
100
+ # facilities.should_receive(:pluck).with(:id).and_return([ 1, 2 ])
101
+ # end
102
+ # end
103
+ # impl.stub(:enabled_facilities_with_govids).with([ 'ccn1', 'ccn2' ]).and_return([ @facility1, @facility2 ])
104
+ # impl.stub(:facilities_with_ids).and_return([])
105
+ # end
106
+ # end
107
+
108
+ end
109
+
110
+ it "adds providers to the configuration if they're not already there" do
111
+ @m_api_impl.stub(:enabled_facilities_with_govids).with([ 'ccn1', 'ccn2' ]).and_return([ @facility1, @facility2 ])
112
+ m_api = double("ApiImplementation")
113
+ m_api.should_receive(:list_providers).and_return([{ "id" => "P1",
114
+ "name" => "Provider1",
115
+ "ccn" => 'ccn1',
116
+ "state" => 'CO',
117
+ "mdsFacilityId" => 'fac1' },
118
+ { "id" => "P2",
119
+ "name" => "Provider2",
120
+ "ccn" => "ccn2",
121
+ "state" => 'CO',
122
+ "mdsFacilityId" => 'fac2'}])
123
+
124
+ smp.should_receive(:api_impl).with(configured_account).and_return(m_api)
125
+
126
+ smp.identify_providers(configured_account)
127
+
128
+ expect(Services::ConfiguredFacility.count).to eq(2)
129
+ expect(Services::FacilityMapping.count).to eq(2)
130
+ end
131
+
132
+ it "does not add providers to the configuration if they're already there" do
133
+ @m_api_impl.stub(:enabled_facilities_with_govids).with([ ]).and_return([ ])
134
+
135
+ # Set up the facilities
136
+ Services::FacilityMapping.create!(third_party: third_party, facility_id: 1, facility_code: 'P1')
137
+ Services::FacilityMapping.create!(third_party: third_party, facility_id: 2, facility_code: 'P2')
138
+
139
+ Services::ConfiguredFacility.create!(service_definition: service_definition, facility_id: 1, enabled: 1)
140
+ Services::ConfiguredFacility.create!(service_definition: service_definition, facility_id: 2, enabled: 1)
141
+
142
+ m_api = double("ApiImplementation")
143
+ m_api.should_receive(:list_providers).and_return([{ "id" => "P1",
144
+ "name" => "Provider1",
145
+ "ccn" => 'ccn1',
146
+ "state" => 'CO',
147
+ "mdsFacilityId" => 'fac1' },
148
+ { "id" => "P2",
149
+ "name" => "Provider2",
150
+ "ccn" => "ccn2",
151
+ "state" => 'CO',
152
+ "mdsFacilityId" => 'fac2'}])
153
+
154
+ smp.should_receive(:api_impl).with(configured_account).and_return(m_api)
155
+
156
+ ServicesMailer.should_not_receive(:notify_sltc_facility_changes)
157
+
158
+ configured_account.should_not_receive(:account)
159
+
160
+ smp.identify_providers(configured_account)
161
+
162
+ expect(Services::ConfiguredFacility.count).to eq(2)
163
+ expect(Services::FacilityMapping.count).to eq(2)
164
+ end
165
+
166
+ it "does not add or remove providers to the configuration if they're already there and may belong to other accounts" do
167
+ @m_api_impl.stub(:enabled_facilities_with_govids).with([ ]).and_return([ ])
168
+
169
+ # Set up the facilities
170
+ Services::FacilityMapping.create!(third_party: third_party, facility_id: 1, facility_code: 'P1')
171
+ Services::FacilityMapping.create!(third_party: third_party, facility_id: 2, facility_code: 'P2')
172
+ Services::FacilityMapping.create!(third_party: third_party, facility_id: 3, facility_code: 'P3')
173
+ Services::FacilityMapping.create!(third_party: third_party, facility_id: 4, facility_code: 'P4')
174
+
175
+ Services::ConfiguredFacility.create!(service_definition: service_definition, facility_id: 1, enabled: 1)
176
+ Services::ConfiguredFacility.create!(service_definition: service_definition, facility_id: 2, enabled: 1)
177
+ Services::ConfiguredFacility.create!(service_definition: service_definition, facility_id: 3, enabled: 1)
178
+ Services::ConfiguredFacility.create!(service_definition: service_definition, facility_id: 4, enabled: 1)
179
+
180
+ m_api = double("ApiImplementation")
181
+ m_api.should_receive(:list_providers).and_return([{ "id" => "P1",
182
+ "name" => "Provider1",
183
+ "ccn" => 'ccn1',
184
+ "state" => 'CO',
185
+ "mdsFacilityId" => 'fac1' },
186
+ { "id" => "P2",
187
+ "name" => "Provider2",
188
+ "ccn" => "ccn2",
189
+ "state" => 'CO',
190
+ "mdsFacilityId" => 'fac2'}])
191
+
192
+ smp.should_receive(:api_impl).with(configured_account).and_return(m_api)
193
+
194
+ ServicesMailer.should_not_receive(:notify_sltc_facility_changes)
195
+
196
+ configured_account.should_not_receive(:account)
197
+
198
+ smp.identify_providers(configured_account)
199
+
200
+ expect(Services::ConfiguredFacility.count).to eq(4)
201
+ expect(Services::FacilityMapping.count).to eq(4)
202
+ end
203
+
204
+ end
205
+
206
+ context '#list_available_files' do
207
+
208
+ it "should return an array of the file information if there are no batches or baselines" do
209
+ mock_api = double("SltcApi")
210
+
211
+ mock_ca = mock_model(Services::ConfiguredAccount)
212
+
213
+ @sltc_mds_pull.should_receive(:api_impl).with(mock_ca).and_return(mock_api)
214
+ @sltc_mds_pull.
215
+ should_receive(:list_available_assessment_requests).
216
+ with(mock_api, mock_ca).
217
+ and_return([])
218
+
219
+ expect(@sltc_mds_pull.list_available_files(mock_ca)).to eq([])
220
+ end
221
+
222
+ it "should return an array of the file information if there assessment requests reqdy to be downloaded" do
223
+ mock_api = double("SltcApi")
224
+
225
+ mock_ca = mock_model(Services::ConfiguredAccount)
226
+
227
+ @sltc_mds_pull.should_receive(:api_impl).with(mock_ca).and_return(mock_api)
228
+
229
+ @sltc_mds_pull.
230
+ should_receive(:list_available_assessment_requests).
231
+ with(mock_api, mock_ca).
232
+ and_return([ [ 23, 'file_1' ], [ 82, 'file_2' ] ])
233
+
234
+ expect(@sltc_mds_pull.list_available_files(mock_ca)).to eq([ [ 23, 'file_1' ], [ 82, 'file_2' ] ])
235
+ end
236
+
237
+ end
238
+
239
+ context '#list_available_assessment_requests' do
240
+
241
+ it "should return an empty array if there are no requested baselines" do
242
+ mock_api = double("SltcApi")
243
+
244
+ m_ca = mock_model(Services::ConfiguredAccount)
245
+
246
+ Services::AssessmentRequest.should_receive(:available).and_return do
247
+ double("Available").tap do | avail |
248
+ avail.should_receive(:where).with(facility_code: [ 'P1' ]).and_return([])
249
+ end
250
+ end
251
+
252
+ @sltc_mds_pull.should_receive(:providers_for_account).with(m_ca).and_return([ 'P1' ])
253
+
254
+ expect(@sltc_mds_pull.list_available_assessment_requests(mock_api, m_ca)).to eq([])
255
+ end
256
+
257
+ it "should return an empty array if there are requested baselines, but none are ready to process" do
258
+ m_ca = mock_model(Services::ConfiguredAccount)
259
+
260
+ mock_api = double("SltcApi")
261
+ mock_api.should_receive(:assessment_request_status).with('b_request_1').and_return( { 'requestId' => 'b_request_1', 'status' => 'Processing' } )
262
+
263
+ mock_rb = mock_model(Services::AssessmentRequest)
264
+ mock_rb.should_receive(:request_id).and_return('b_request_1')
265
+
266
+ Services::AssessmentRequest.should_receive(:available).and_return do
267
+ double("Available").tap do | avail |
268
+ avail.should_receive(:where).with(facility_code: [ 'P1' ]).and_return([ mock_rb ])
269
+ end
270
+ end
271
+
272
+ @sltc_mds_pull.should_receive(:providers_for_account).with(m_ca).and_return([ 'P1' ])
273
+
274
+ expect(@sltc_mds_pull.list_available_assessment_requests(mock_api, m_ca)).to eq([])
275
+ end
276
+
277
+ it "should return an array of baseline info if there are requested baselines, and at least one is ready to process" do
278
+ m_ca = mock_model(Services::ConfiguredAccount)
279
+
280
+ mock_api = double("SltcApi")
281
+ mock_api.should_receive(:assessment_request_status).with('b_request_1').and_return( { 'requestId' => 'b_request_1', 'status' => 'Complete' } )
282
+
283
+ mock_rb = mock_model(Services::AssessmentRequest)
284
+ mock_rb.stub(:request_id).and_return('b_request_1')
285
+ mock_rb.should_receive(:update_attribute).with(:status, Services::AssessmentRequest::STATUS_COMPLETED)
286
+
287
+ Services::AssessmentRequest.should_receive(:available).and_return do
288
+ double("Available").tap do | avail |
289
+ avail.should_receive(:where).with(facility_code: [ 'P1' ]).and_return([ mock_rb ])
290
+ end
291
+ end
292
+
293
+ @sltc_mds_pull.should_receive(:providers_for_account).with(m_ca).and_return([ 'P1' ])
294
+
295
+ expect(@sltc_mds_pull.list_available_assessment_requests(mock_api, m_ca)).to eq([ [ 'b_request_1', 'b_request_1.zip' ] ])
296
+ end
297
+
298
+ end
299
+
300
+ context '#perform_file_downloads' do
301
+
302
+ it "should return an empty array if no files to download are specified" do
303
+ mock_ca = double("ConfiguredAccount")
304
+
305
+ mock_api = double("SltcApi")
306
+
307
+ @sltc_mds_pull.should_receive(:api_impl).with(mock_ca).and_return(mock_api)
308
+
309
+ expect(@sltc_mds_pull.perform_file_downloads(mock_ca, [])).to eq([])
310
+ end
311
+
312
+ it "should return an empty array if no files were successfully downloaded" do
313
+ mock_ca = double("ConfiguredAccount")
314
+ mock_af = mock_model(Services::AvailableFile)
315
+
316
+ mock_api = double("SltcApi")
317
+
318
+ @sltc_mds_pull.should_receive(:api_impl).with(mock_ca).and_return(mock_api)
319
+ @sltc_mds_pull.should_receive(:download_file).with(mock_api, mock_af).and_return(nil)
320
+
321
+ expect(@sltc_mds_pull.perform_file_downloads(mock_ca, [ mock_af ])).to eq([])
322
+ end
323
+
324
+ it "should return an array with the available file ids if successfully downloaded" do
325
+ mock_ca = double("ConfiguredAccount")
326
+ mock_af = mock_model(Services::AvailableFile)
327
+
328
+ mock_api = double("SltcApi")
329
+
330
+ @sltc_mds_pull.should_receive(:api_impl).with(mock_ca).and_return(mock_api)
331
+ @sltc_mds_pull.should_receive(:download_file).with(mock_api, mock_af).and_return(mock_af.id)
332
+
333
+ expect(@sltc_mds_pull.perform_file_downloads(mock_ca, [ mock_af ])).to eq([ mock_af.id ])
334
+ end
335
+
336
+ end
337
+
338
+ context '#perform_cleanup_on_remote_server' do
339
+
340
+ it "should return an empty array if there are no deleted available files" do
341
+ mock_ca = mock_model(Services::ConfiguredAccount)
342
+
343
+ mock_ca.should_receive(:available_files).and_return do
344
+ double("AvailableFiles").tap do | available_files |
345
+ available_files.should_receive(:deleted).and_return do
346
+ double("DeletedFiles").tap do | deleted_files |
347
+ deleted_files.
348
+ should_receive(:pluck).
349
+ with(:id).
350
+ and_return([])
351
+ end
352
+ end
353
+ end
354
+ end
355
+
356
+ expect(@sltc_mds_pull.perform_cleanup_on_remote_server(mock_ca)).to eq([])
357
+ end
358
+
359
+ it "should return an array of the ids of all the deleted files" do
360
+ mock_ca = mock_model(Services::ConfiguredAccount)
361
+
362
+ mock_af_1 = mock_model(Services::AvailableFile)
363
+ mock_af_2 = mock_model(Services::AvailableFile)
364
+
365
+ mock_ca.should_receive(:available_files).and_return do
366
+ double("AvailableFiles").tap do | available_files |
367
+ available_files.should_receive(:deleted).and_return do
368
+ double("DeletedFiles").tap do | deleted_files |
369
+ deleted_files.
370
+ should_receive(:pluck).
371
+ with(:id).
372
+ and_return([ mock_af_1.id, mock_af_2.id ])
373
+ end
374
+ end
375
+ end
376
+ end
377
+
378
+ expect(@sltc_mds_pull.perform_cleanup_on_remote_server(mock_ca)).to eq([mock_af_1.id, mock_af_2.id])
379
+ end
380
+
381
+ end
382
+
383
+ context '#download_file' do
384
+
385
+ it "should download the specified baseline file and return the id of the available file" do
386
+ mock_api = double("SltcApi")
387
+ mock_api.
388
+ should_receive(:download_assessment_request).
389
+ with("abc123").
390
+ and_return("the file contents")
391
+
392
+ mock_af = mock_model(Services::AvailableFile)
393
+ mock_af.stub(:download_path).and_return('/tmp')
394
+ mock_af.stub(:downloaded_file_path).and_return('/tmp/file.zip')
395
+ mock_af.stub(:request_id).and_return("abc123")
396
+ mock_af.stub(:baseline).and_return(true)
397
+
398
+ mock_file = double("File")
399
+ mock_file.should_receive(:write).with("the file contents")
400
+
401
+ File.
402
+ should_receive(:open).
403
+ with('/tmp/file.zip', 'wb').
404
+ and_yield(mock_file)
405
+
406
+ @sltc_mds_pull.should_receive(:mkdir_p).with('/tmp')
407
+
408
+ expect(@sltc_mds_pull.download_file(mock_api, mock_af)).to eq(mock_af.id)
409
+ end
410
+
411
+ it "should download the specified batch file and return the id of the available file" do
412
+ mock_api = double("SltcApi")
413
+ mock_api.
414
+ should_receive(:download_assessment_request).
415
+ with("abc123").
416
+ and_return("the file contents")
417
+
418
+ mock_af = mock_model(Services::AvailableFile)
419
+ mock_af.stub(:download_path).and_return('/tmp')
420
+ mock_af.stub(:downloaded_file_path).and_return('/tmp/file.zip')
421
+ mock_af.stub(:request_id).and_return("abc123")
422
+ mock_af.stub(:baseline).and_return(false)
423
+
424
+ mock_file = double("File")
425
+ mock_file.should_receive(:write).with("the file contents")
426
+
427
+ File.
428
+ should_receive(:open).
429
+ with('/tmp/file.zip', 'wb').
430
+ and_yield(mock_file)
431
+
432
+ @sltc_mds_pull.should_receive(:mkdir_p).with('/tmp')
433
+
434
+ expect(@sltc_mds_pull.download_file(mock_api, mock_af)).to eq(mock_af.id)
435
+ end
436
+
437
+ end
438
+
439
+ context '#api_impl' do
440
+
441
+ it "should return an instance of the SltcApi configured and ready to go" do
442
+ mock_ca = mock_model(Services::ConfiguredAccount)
443
+
444
+ mock_api = double("SltcApi")
445
+
446
+ Services::SltcApi.
447
+ should_receive(:for_configured_account).
448
+ with(mock_ca).
449
+ and_return(mock_api)
450
+
451
+ expect(@sltc_mds_pull.api_impl(mock_ca)).to be(mock_api)
452
+
453
+ end
454
+
455
+ end
456
+
457
+ context '#list_available_assessment_requests' do
458
+
459
+ it "should return an empty array if there are no assessment request ready for download" do
460
+ m_ca = mock_model(Services::ConfiguredAccount)
461
+ mock_api = double("SltcApi")
462
+
463
+ Services::AssessmentRequest.
464
+ should_receive(:available).
465
+ and_return do
466
+ double("Available").tap do | avail |
467
+ avail.should_receive(:where).with(facility_code: [ 'P1' ]).and_return([])
468
+ end
469
+ end
470
+
471
+ @sltc_mds_pull.should_receive(:providers_for_account).with(m_ca).and_return([ 'P1' ])
472
+
473
+ expect(@sltc_mds_pull.list_available_assessment_requests(mock_api, m_ca)).to eq([])
474
+ end
475
+
476
+ it "should return an array of the assesment requests completed by SLTC" do
477
+ mock_api = double("SltcApi")
478
+ mock_api.
479
+ should_receive(:assessment_request_status).
480
+ with('Request1').
481
+ and_return('status' => 'Complete')
482
+ mock_api.
483
+ should_receive(:assessment_request_status).
484
+ with('Request2').
485
+ and_return('status' => 'Processing')
486
+
487
+ m_completed_ar = mock_model(Services::AssessmentRequest)
488
+ m_completed_ar.stub(:request_id).and_return("Request1")
489
+ m_completed_ar.
490
+ should_receive(:update_attribute).
491
+ with(:status, Services::AssessmentRequest::STATUS_COMPLETED)
492
+
493
+ m_incompleted_ar = mock_model(Services::AssessmentRequest)
494
+ m_incompleted_ar.stub(:request_id).and_return("Request2")
495
+
496
+ m_ca = mock_model(Services::ConfiguredAccount)
497
+
498
+ Services::AssessmentRequest.
499
+ should_receive(:available).
500
+ and_return do
501
+ double("Available").tap do | avail |
502
+ avail.should_receive(:where).with(facility_code: [ 'P1' ]).and_return([ m_completed_ar, m_incompleted_ar ])
503
+ end
504
+ end
505
+
506
+ @sltc_mds_pull.should_receive(:providers_for_account).with(m_ca).and_return([ 'P1' ])
507
+
508
+ expect(@sltc_mds_pull.list_available_assessment_requests(mock_api, m_ca)).
509
+ to eq([ [ 'Request1', 'Request1.zip' ] ])
510
+ end
511
+
512
+ end
513
+
514
+ context '#identify_providers' do
515
+
516
+ it "should process the list of providers returned by the SLTC api" do
517
+ m_ca = mock_model(Services::ConfiguredAccount)
518
+
519
+ m_api = double("SltcApi")
520
+ m_api.
521
+ should_receive(:list_providers).
522
+ and_return([{ 'id' => '1', 'name' => 'joe' }])
523
+
524
+ @sltc_mds_pull.should_receive(:api_impl).with(m_ca).and_return(m_api)
525
+ @sltc_mds_pull.
526
+ should_receive(:process_provider_list).
527
+ with(m_ca, [{ 'id' => '1', 'name' => 'joe' }])
528
+
529
+ @sltc_mds_pull.identify_providers(m_ca)
530
+
531
+ end
532
+
533
+ end
534
+
535
+ context '#request_assessments' do
536
+
537
+ it "should not request assessments if no providers are enabled for this account" do
538
+ m_ca = mock_model(Services::ConfiguredAccount)
539
+ m_ca.stub(:account_id).and_return(2)
540
+
541
+ Services::ApplicationApi.
542
+ should_receive(:api_impl).and_return do
543
+ double("ApiImpl").tap do | api_impl |
544
+ api_impl.
545
+ should_receive(:enabled_facilities_for_account_id).
546
+ with(2).
547
+ and_return do
548
+ double("EnabledFacilities").tap do | enabled_facilities |
549
+ enabled_facilities.
550
+ should_receive(:pluck).
551
+ with(:id).
552
+ and_return([])
553
+ end
554
+ end
555
+ end
556
+ end
557
+
558
+ Services::FacilityMapping.
559
+ should_receive(:joins).
560
+ with('LEFT OUTER JOIN services_configured_facilities ON services_configured_facilities.facility_id = services_facility_mappings.facility_id').
561
+ and_return do
562
+ double("FMJoins").tap do | fm_joins |
563
+ fm_joins.
564
+ should_receive(:where).
565
+ with('services_facility_mappings.facility_id IN (?) AND services_configured_facilities.enabled = 1', []).
566
+ and_return do
567
+ double("FmWhere").tap do | fm_where |
568
+ fm_where.
569
+ should_receive(:pluck).
570
+ with(:facility_code).
571
+ and_return([])
572
+ end
573
+ end
574
+ end
575
+ end
576
+
577
+ @sltc_mds_pull.should_not_receive(:request_assessments_to_pull)
578
+
579
+ @sltc_mds_pull.request_assessments(m_ca)
580
+ end
581
+
582
+ it "should request assessments if there is at least one provider enabled for this account" do
583
+ m_ca = mock_model(Services::ConfiguredAccount)
584
+ m_ca.stub(:account_id).and_return(2)
585
+
586
+ Services::ApplicationApi.
587
+ should_receive(:api_impl).and_return do
588
+ double("ApiImpl").tap do | api_impl |
589
+ api_impl.
590
+ should_receive(:enabled_facilities_for_account_id).
591
+ with(2).
592
+ and_return do
593
+ double("EnabledFacilities").tap do | enabled_facilities |
594
+ enabled_facilities.
595
+ should_receive(:pluck).
596
+ with(:id).
597
+ and_return([2, 3, 4])
598
+ end
599
+ end
600
+ end
601
+ end
602
+
603
+ Services::FacilityMapping.
604
+ should_receive(:joins).
605
+ with('LEFT OUTER JOIN services_configured_facilities ON services_configured_facilities.facility_id = services_facility_mappings.facility_id').
606
+ and_return do
607
+ double("FMJoins").tap do | fm_joins |
608
+ fm_joins.
609
+ should_receive(:where).
610
+ with('services_facility_mappings.facility_id IN (?) AND services_configured_facilities.enabled = 1', [2, 3, 4]).
611
+ and_return do
612
+ double("FmWhere").tap do | fm_where |
613
+ fm_where.
614
+ should_receive(:pluck).
615
+ with(:facility_code).
616
+ and_return([ 18 ])
617
+ end
618
+ end
619
+ end
620
+ end
621
+
622
+ @sltc_mds_pull.
623
+ should_receive(:request_assessments_to_pull).
624
+ with(m_ca, 18)
625
+
626
+ @sltc_mds_pull.request_assessments(m_ca)
627
+ end
628
+ end
629
+
630
+ context '#process_provider_list' do
631
+
632
+ it "should clean up the configuration of configured facilities" do
633
+
634
+ m_ca = mock_model(Services::ConfiguredAccount)
635
+
636
+ @sltc_mds_pull.should_receive(:providers_for_account).with(m_ca).and_return([ 'P2', 'P3' ])
637
+ @sltc_mds_pull.should_receive(:register_provider_deltas).with(m_ca, [{"id"=>"P1", "name"=>"Provider 1"}], Services::ProviderChange::VERB_ADD)
638
+ @sltc_mds_pull.should_receive(:register_provider_deltas).with(m_ca, [{"id"=>"P3", "name"=>"Provider 3", "state" => "TX", "npi" => "npi123", "ccn" => "ccn123", "mdsFacilityId" => "fac123" }], Services::ProviderChange::VERB_DELETE)
639
+
640
+ Services::ProviderChange.
641
+ should_receive(:added_providers).
642
+ with(m_ca.id, [ 'P1' ]).and_return do
643
+ double("AddedProviders").tap do | added_providers |
644
+ added_providers.should_receive(:pluck).with(:facility_code).and_return([])
645
+ end
646
+ end
647
+
648
+ Services::ProviderChange.
649
+ should_receive(:deleted_providers).
650
+ with(m_ca.id, [ 'P3' ]).and_return do
651
+ double("DeletedProviders").tap do | added_providers |
652
+ added_providers.should_receive(:pluck).with(:facility_code).and_return([])
653
+ end
654
+ end
655
+
656
+ m_facility = double("Facility")
657
+ m_facility.stub(:name).and_return("Provider 3")
658
+ m_facility.stub(:state).and_return("TX")
659
+ m_facility.stub(:npi).and_return("npi123")
660
+ m_facility.stub(:govid).and_return("ccn123")
661
+ m_facility.stub(:fac_id).and_return("fac123")
662
+
663
+ m_fm = double("FacilityMapping")
664
+ m_fm.stub(:facility).and_return(m_facility)
665
+ m_fm.stub(:facility_code).and_return('P3')
666
+
667
+ Services::FacilityMapping.
668
+ should_receive(:sltc_provider_mappings).
669
+ with([ 'P3' ]).
670
+ and_return([ m_fm ])
671
+
672
+ @sltc_mds_pull.
673
+ should_receive(:disable_removed_facilities).
674
+ with([ 'P3' ])
675
+
676
+ @sltc_mds_pull.
677
+ should_receive(:configure_added_facilities).
678
+ with([ { 'id' => 'P1', 'name' => 'Provider 1' } ])
679
+
680
+ @sltc_mds_pull.process_provider_list(m_ca, [ { 'id' => 'P1', 'name' => 'Provider 1' },
681
+ { 'id' => 'P2', 'name' => 'Provider 2' } ])
682
+ end
683
+
684
+ end
685
+
686
+ context '#disable_removed_facilities' do
687
+
688
+ it "should disable any removed facilities" do
689
+
690
+ fac_mappings = double("FacilityMappings")
691
+ fac_mappings.should_receive(:pluck).with(:facility_id).and_return([ 1001, 1002 ])
692
+ fac_mappings.should_receive(:pluck).with(:id).and_return([10001, 10002])
693
+
694
+ Services::FacilityMapping.
695
+ should_receive(:sltc_provider_mappings).with([ 'P1', 'P2' ]).
696
+ and_return(fac_mappings)
697
+
698
+ Services::ConfiguredFacility.
699
+ should_receive(:where).
700
+ with(facility_id: [ 1001, 1002 ]).
701
+ and_return do
702
+ double("Wheres").tap do | wheres |
703
+ wheres.should_receive(:destroy_all)
704
+ end
705
+ end
706
+
707
+ Services::FacilityMapping.
708
+ should_receive(:where).
709
+ with(id: [ 10001, 10002 ]).
710
+ and_return do
711
+ double("Wheres").tap do | wheres |
712
+ wheres.should_receive(:destroy_all)
713
+ end
714
+ end
715
+
716
+ @sltc_mds_pull.disable_removed_facilities([ 'P1', 'P2' ])
717
+ end
718
+
719
+ end
720
+
721
+ context '#request_assessments_to_pull' do
722
+
723
+ it "should generate the necessary assessment requests if no previous pull" do
724
+ m_ca = double(Services::ConfiguredAccount)
725
+
726
+ dt = Time.now
727
+ yesterday = dt - 24.hours
728
+
729
+ m_api = double("SltcApi")
730
+ m_api.
731
+ should_receive(:assessment_request).
732
+ with('P1', yesterday, dt, 'Accepted').
733
+ and_return({ 'requestId' => 'TheRequestId', 'status' => 'Processing' })
734
+
735
+ @sltc_mds_pull.
736
+ should_receive(:api_impl).
737
+ with(m_ca).
738
+ and_return(m_api)
739
+
740
+ Services::AssessmentRequest.
741
+ should_receive(:where).
742
+ with(facility_code: 'P1').
743
+ and_return do
744
+ double("Wheres").tap do | wheres |
745
+ wheres.
746
+ should_receive(:order).
747
+ with('pull_to DESC').
748
+ and_return do
749
+ double("Orders").tap do | orders |
750
+ orders.
751
+ should_receive(:limit).
752
+ with(1).
753
+ and_return([])
754
+ end
755
+ end
756
+ end
757
+ end
758
+
759
+ Time.stub(:now).and_return(dt)
760
+
761
+ Services::AssessmentRequest.
762
+ should_receive(:create).
763
+ with(facility_code: 'P1',
764
+ pull_from: yesterday,
765
+ request_id: 'TheRequestId',
766
+ pull_to: dt,
767
+ status: Services::AssessmentRequest::STATUS_AVAILABLE)
768
+
769
+ @sltc_mds_pull.request_assessments_to_pull(m_ca, 'P1')
770
+ end
771
+
772
+ end
773
+
774
+ end
775
+
776
+ end