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,273 @@
1
+ require 'spec_helper'
2
+
3
+ describe Services::PccMdsPull do
4
+
5
+ context '#Instance Methods' do
6
+
7
+ before(:each) do
8
+ @mock_sd = mock_model(Services::ServiceDefinition)
9
+ @mock_sd.stub(:hostname).and_return('the_server')
10
+ @mock_sd.stub(:username).and_return('the_user')
11
+ @mock_sd.stub(:base_uri).and_return('/home')
12
+
13
+ mock_credentials = mock_model(Services::Credential)
14
+ mock_credentials.stub(:password).and_return('the_password')
15
+
16
+ @mock_account_map = mock_model(Services::AccountMapping)
17
+ @mock_account_map.stub(:account_code).and_return('kind')
18
+
19
+ @pcc_mds_pull = Services::PccMdsPull.new(@mock_sd)
20
+ @pcc_mds_pull.stub(:server_credentials).and_return(mock_credentials)
21
+ @pcc_mds_pull.stub(:account_map).and_return(@mock_account_map)
22
+ end
23
+
24
+ context '#ftp_server' do
25
+
26
+ it "should return a reference to the ftp server to use for file access" do
27
+ mock_ftp_server = double("FtpServer")
28
+ Services::FtpServer.
29
+ should_receive(:new).
30
+ with('the_server',
31
+ 'the_user',
32
+ 'the_password',
33
+ '/home').
34
+ and_return(mock_ftp_server)
35
+
36
+ expect(@pcc_mds_pull.ftp_server).to be(mock_ftp_server)
37
+
38
+ # Call it again; but it shouldn't construct a new instance
39
+ # (see expectations)
40
+ expect(@pcc_mds_pull.ftp_server).to be(mock_ftp_server)
41
+ end
42
+
43
+ end
44
+
45
+ context '#list_available_files' do
46
+
47
+ before(:each) do
48
+ @mock_ftp_server = double("FtpServer")
49
+ @mock_ftp_server.
50
+ stub(:connect).
51
+ and_yield(@mock_ftp_server)
52
+
53
+ @mock_configured_account = mock_model(Services::ConfiguredAccount)
54
+ @mock_configured_account.stub(:account_id).and_return(3)
55
+
56
+ @pcc_mds_pull.stub(:ftp_server).and_return(@mock_ftp_server)
57
+ end
58
+
59
+ it "should return an empty array if no files are available" do
60
+ @mock_ftp_server.
61
+ should_receive(:file_list).
62
+ with('kind', '*.zip').
63
+ and_return([])
64
+
65
+ expect(@pcc_mds_pull.list_available_files(@mock_configured_account)).to eq([])
66
+ end
67
+
68
+ it "should return an array of tuples when files are available" do
69
+ @mock_ftp_server.
70
+ should_receive(:file_list).
71
+ with('kind', '*.zip').
72
+ and_return([ 'file_1.zip', 'file_2.zip' ])
73
+
74
+ expect(@pcc_mds_pull.list_available_files(@mock_configured_account)).to eq([ [ 'file_1.zip', 'file_1.zip' ], [ 'file_2.zip', 'file_2.zip' ] ])
75
+ end
76
+
77
+ end
78
+
79
+ context '#perform_file_downloads' do
80
+
81
+ before(:each) do
82
+ @mock_ftp_server = double("FtpServer")
83
+ @mock_ftp_server.
84
+ stub(:connect).
85
+ and_yield(@mock_ftp_server)
86
+
87
+ @mock_configured_account = mock_model(Services::ConfiguredAccount)
88
+ @mock_configured_account.stub(:account_id).and_return(3)
89
+
90
+ @pcc_mds_pull.stub(:ftp_server).and_return(@mock_ftp_server)
91
+ end
92
+
93
+ it "should return an empty array of available file ids if no files are specified for download" do
94
+ @pcc_mds_pull.
95
+ should_not_receive(:download_file)
96
+
97
+ expect(@pcc_mds_pull.perform_file_downloads(@mock_configured_account, [])).to eq([])
98
+ end
99
+
100
+ it "should return an array of the available file ids that were actually downloaded" do
101
+ mock_af_1 = mock_model(Services::AvailableFile)
102
+ mock_af_2 = mock_model(Services::AvailableFile)
103
+ mock_af_3 = mock_model(Services::AvailableFile)
104
+
105
+ @pcc_mds_pull.
106
+ should_receive(:download_file).
107
+ with(@mock_ftp_server, mock_af_1, 'kind').
108
+ and_return(mock_af_1.id)
109
+ @pcc_mds_pull.
110
+ should_receive(:download_file).
111
+ with(@mock_ftp_server, mock_af_2, 'kind').
112
+ and_return(nil) # File not actually downloaded successfully
113
+ @pcc_mds_pull.
114
+ should_receive(:download_file).
115
+ with(@mock_ftp_server, mock_af_3, 'kind').
116
+ and_return(mock_af_3.id)
117
+
118
+ expect(@pcc_mds_pull.perform_file_downloads(@mock_configured_account, [mock_af_1, mock_af_2, mock_af_3])).to eq([ mock_af_1.id, mock_af_3.id ])
119
+ end
120
+
121
+ end
122
+
123
+ context '#perform_cleanup_on_remote_server' do
124
+
125
+ before(:each) do
126
+ @mock_ftp_server = double("FtpServer")
127
+ @mock_ftp_server.
128
+ stub(:connect).
129
+ and_yield(@mock_ftp_server)
130
+
131
+ @mock_configured_account = mock_model(Services::ConfiguredAccount)
132
+ @mock_configured_account.stub(:account_id).and_return(3)
133
+
134
+ @pcc_mds_pull.stub(:ftp_server).and_return(@mock_ftp_server)
135
+ end
136
+
137
+ it "should return an empty array if no files needed to be cleaned up" do
138
+ @mock_configured_account.should_receive(:available_files).and_return do
139
+ double("AvailableFiles").tap do | available_files |
140
+ available_files.should_receive(:deleted).and_return([])
141
+ end
142
+ end
143
+
144
+ @pcc_mds_pull.should_not_receive(:remove_file)
145
+
146
+ expect(@pcc_mds_pull.perform_cleanup_on_remote_server(@mock_configured_account)).to eq([])
147
+ end
148
+
149
+ it "should return an empty array if no files were successfully cleaned up" do
150
+ mock_af_1 = mock_model(Services::AvailableFile)
151
+ mock_af_2 = mock_model(Services::AvailableFile)
152
+ mock_af_3 = mock_model(Services::AvailableFile)
153
+
154
+ @mock_configured_account.should_receive(:available_files).and_return do
155
+ double("AvailableFiles").tap do | available_files |
156
+ available_files.should_receive(:deleted).and_return([ mock_af_1, mock_af_2, mock_af_3 ])
157
+ end
158
+ end
159
+
160
+ @pcc_mds_pull.
161
+ should_receive(:remove_file).
162
+ with(@mock_ftp_server, mock_af_1, 'kind').
163
+ and_return(nil)
164
+
165
+ @pcc_mds_pull.
166
+ should_receive(:remove_file).
167
+ with(@mock_ftp_server, mock_af_2, 'kind').
168
+ and_return(nil)
169
+
170
+ @pcc_mds_pull.
171
+ should_receive(:remove_file).
172
+ with(@mock_ftp_server, mock_af_3, 'kind').
173
+ and_return(nil)
174
+
175
+ expect(@pcc_mds_pull.perform_cleanup_on_remote_server(@mock_configured_account)).to eq([])
176
+ end
177
+
178
+ it "should return an array of the successfully removed files" do
179
+ mock_af_1 = mock_model(Services::AvailableFile)
180
+ mock_af_2 = mock_model(Services::AvailableFile)
181
+ mock_af_3 = mock_model(Services::AvailableFile)
182
+
183
+ @mock_configured_account.should_receive(:available_files).and_return do
184
+ double("AvailableFiles").tap do | available_files |
185
+ available_files.should_receive(:deleted).and_return([ mock_af_1, mock_af_2, mock_af_3 ])
186
+ end
187
+ end
188
+
189
+ @pcc_mds_pull.
190
+ should_receive(:remove_file).
191
+ with(@mock_ftp_server, mock_af_1, 'kind').
192
+ and_return(23)
193
+
194
+ @pcc_mds_pull.
195
+ should_receive(:remove_file).
196
+ with(@mock_ftp_server, mock_af_2, 'kind').
197
+ and_return(nil)
198
+
199
+ @pcc_mds_pull.
200
+ should_receive(:remove_file).
201
+ with(@mock_ftp_server, mock_af_3, 'kind').
202
+ and_return(82)
203
+
204
+ expect(@pcc_mds_pull.perform_cleanup_on_remote_server(@mock_configured_account)).to eq([ 23, 82 ])
205
+ end
206
+
207
+ end
208
+
209
+ context '#download_file' do
210
+
211
+ it "should return the id of the file if successfully downloaded" do
212
+ mock_af = mock_model(Services::AvailableFile)
213
+ mock_af.should_receive(:request_id).and_return('file_1')
214
+ mock_af.should_receive(:download_path).and_return('/tmp')
215
+
216
+ mock_server = double("FtpServer")
217
+ mock_server.
218
+ should_receive(:download_file).
219
+ with('kind', 'file_1', '/tmp')
220
+
221
+ expect(@pcc_mds_pull.download_file(mock_server, mock_af, 'kind')).to eq(mock_af.id)
222
+ end
223
+
224
+ it "should return nil if file was not successfully downloaded" do
225
+ mock_af = mock_model(Services::AvailableFile)
226
+ mock_af.should_receive(:request_id).and_return('file_1')
227
+ mock_af.should_receive(:download_path).and_return('/tmp')
228
+ mock_af.should_receive(:to_rrepr).and_return("")
229
+
230
+ mock_server = double("FtpServer")
231
+ mock_server.
232
+ should_receive(:download_file).
233
+ with('kind', 'file_1', '/tmp').
234
+ and_raise(Exception)
235
+
236
+ expect(@pcc_mds_pull.download_file(mock_server, mock_af, 'kind')).to be_nil
237
+ end
238
+
239
+ end
240
+
241
+ context '#remove_file' do
242
+
243
+ it "should return the id of the file if successfully removed" do
244
+ mock_af = mock_model(Services::AvailableFile)
245
+ mock_af.should_receive(:request_id).and_return('file_1')
246
+
247
+ mock_server = double("FtpServer")
248
+ mock_server.
249
+ should_receive(:remove_file).
250
+ with('kind', 'file_1')
251
+
252
+ expect(@pcc_mds_pull.remove_file(mock_server, mock_af, 'kind')).to eq(mock_af.id)
253
+ end
254
+
255
+ it "should return nil if the file is not successfully removed" do
256
+ mock_af = mock_model(Services::AvailableFile)
257
+ mock_af.should_receive(:request_id).and_return('file_1')
258
+ mock_af.should_receive(:to_rrepr).and_return("")
259
+
260
+ mock_server = double("FtpServer")
261
+ mock_server.
262
+ should_receive(:remove_file).
263
+ with('kind', 'file_1').
264
+ and_raise(Exception)
265
+
266
+ expect(@pcc_mds_pull.remove_file(mock_server, mock_af, 'kind')).to be_nil
267
+ end
268
+
269
+ end
270
+
271
+ end
272
+
273
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe Services::ServiceImplementation do
4
+
5
+ before(:each) do
6
+ @mock_sd = double("ServiceDefinition")
7
+ end
8
+
9
+ context '#initialize' do
10
+
11
+ it "should hold onto the specified service definition" do
12
+ si = Services::ServiceImplementation.new @mock_sd
13
+
14
+ expect(si.service_definition).to be(@mock_sd)
15
+ end
16
+
17
+ end
18
+
19
+ context '#server_credentials' do
20
+
21
+ it "should return the first set of credentials associated with the service definition" do
22
+ mock_credentials = double("Credentials")
23
+
24
+ si = Services::ServiceImplementation.new @mock_sd
25
+
26
+ @mock_sd.should_receive(:credentials).and_return([ mock_credentials ])
27
+
28
+ expect(si.server_credentials).to be(mock_credentials)
29
+ end
30
+
31
+ it "should cache the credentials after the first call" do
32
+ mock_credentials = double("Credentials")
33
+
34
+ si = Services::ServiceImplementation.new @mock_sd
35
+
36
+ @mock_sd.should_receive(:credentials).exactly(1).times.and_return([ mock_credentials ])
37
+
38
+ expect(si.server_credentials).to be(mock_credentials)
39
+
40
+ expect(si.server_credentials).to be(mock_credentials)
41
+ end
42
+
43
+ it "should raise an exception if there are no credentials associated with the server" do
44
+ si = Services::ServiceImplementation.new @mock_sd
45
+
46
+ @mock_sd.should_receive(:credentials).exactly(1).times.and_return([ ])
47
+ @mock_sd.should_receive(:to_rrepr).and_return("The Error")
48
+
49
+ expect { si.server_credentials }.to raise_error
50
+ end
51
+
52
+ end
53
+
54
+ context '#account_map' do
55
+
56
+ it "should look up the account mapping for the specified account id" do
57
+ @mock_sd.should_receive(:third_party_id).and_return(9)
58
+
59
+ si = Services::ServiceImplementation.new @mock_sd
60
+
61
+ mock_account_map = double("AccountMapping")
62
+
63
+ Services::AccountMapping.
64
+ should_receive(:where).
65
+ with(account_id: 2, third_party_id: 9).
66
+ and_return([ mock_account_map ])
67
+
68
+ expect(si.account_map(2)).to be(mock_account_map)
69
+ end
70
+
71
+ it "should raise an exception if the account mapping doesn't exist" do
72
+ @mock_sd.should_receive(:third_party_id).and_return(9)
73
+
74
+ @mock_sd.should_receive(:to_rrepr).and_return("The Error")
75
+
76
+ si = Services::ServiceImplementation.new @mock_sd
77
+
78
+ Services::AccountMapping.
79
+ should_receive(:where).
80
+ with(account_id: 2, third_party_id: 9).
81
+ and_return([ ])
82
+
83
+ expect { si.account_map(2) }.to raise_error
84
+ end
85
+
86
+ end
87
+
88
+ end
@@ -0,0 +1,136 @@
1
+ require 'spec_helper'
2
+
3
+ describe Services::SltcApiException do
4
+
5
+ context '#initialize' do
6
+
7
+ it "should have a message that is whatever is returned from :format_exception_message" do
8
+ inner_exception = Exception.new
9
+
10
+ ex = Services::SltcApiException.new(inner_exception)
11
+ expect(ex.message).to eq("Unknown SimpleLTC API Error (no http_code associated with inner exception)")
12
+ expect(ex.inner_exception).to eq(inner_exception)
13
+ end
14
+
15
+ end
16
+
17
+ context 'Public Instance Methods' do
18
+
19
+ context '#http_code' do
20
+
21
+ it "should return the value of the http_code attribute of the inner exception" do
22
+ inner_exception = Exception.new("Error Message")
23
+ inner_exception.stub(:http_code).and_return(500)
24
+
25
+ ex = Services::SltcApiException.new(inner_exception)
26
+ expect(ex.http_code).to eq(500)
27
+ end
28
+
29
+ it "should return nil if the inner exception doesn't have an http_code attribute" do
30
+ inner_exception = Exception.new("Error Message")
31
+
32
+ ex = Services::SltcApiException.new(inner_exception)
33
+ expect(ex.http_code).to be_nil
34
+ end
35
+
36
+ end
37
+
38
+ context '#http_message' do
39
+
40
+ it "should return the message string from the inner exception" do
41
+ inner_exception = Exception.new("Error Message")
42
+
43
+ ex = Services::SltcApiException.new(inner_exception)
44
+ expect(ex.http_message).to eq("Error Message")
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ context 'Private Instance Methods' do
52
+
53
+ context '#basic_message_fmt' do
54
+
55
+ it "should provide a message that displays the http_code and message when an http_code is present" do
56
+ mock_exception = double("Exception")
57
+ mock_exception.stub(:http_code).and_return(500)
58
+ mock_exception.stub(:message).and_return("General Error")
59
+
60
+ ex = Services::SltcApiException.new(mock_exception)
61
+ expect(ex.send(:basic_message_fmt)).to eq("500: General Error")
62
+ end
63
+
64
+ it "should return a general message if no http_code is found on the inner exception" do
65
+ inner_exception = Exception.new
66
+ ex = Services::SltcApiException.new(inner_exception)
67
+ expect(ex.send(:basic_message_fmt)).to eq("no http_code associated with inner exception")
68
+ end
69
+
70
+ end
71
+
72
+ context '#extract_sltc_error_information' do
73
+
74
+ it "should return an error message displaying the SLTC error code/message when provided in the response" do
75
+ inner_exception = Exception.new("Error Message")
76
+ inner_exception.stub(:http_code).and_return(500)
77
+ inner_exception.stub(:response).and_return('{ "ErrorCode": 12, "Message": "Sltc Error Message" }')
78
+
79
+ ex = Services::SltcApiException.new(inner_exception)
80
+ expect(ex.send(:extract_sltc_error_information)).to eq("SimpleLTC error response: (12) Sltc Error Message (500: Error Message)")
81
+ end
82
+
83
+ it "should return the raw json as part of the message if no 'Message' key is found in response" do
84
+ inner_exception = Exception.new("Error Message")
85
+ inner_exception.stub(:http_code).and_return(500)
86
+ inner_exception.stub(:response).and_return('{ "ErrorCode": 12 }')
87
+
88
+ ex = Services::SltcApiException.new(inner_exception)
89
+ expect(ex.send(:extract_sltc_error_information)).to eq("No error message in SimpleLTC error response ({ \"ErrorCode\": 12 }) (500: Error Message)")
90
+ end
91
+
92
+ it "should return the raw json as part of the message if the response json does not parse to a hash" do
93
+ inner_exception = Exception.new("Error Message")
94
+ inner_exception.stub(:http_code).and_return(500)
95
+ inner_exception.stub(:response).and_return('[ 1 ]')
96
+
97
+ ex = Services::SltcApiException.new(inner_exception)
98
+ expect(ex.send(:extract_sltc_error_information)).to eq("No error message in SimpleLTC error response ([ 1 ]) (500: Error Message)")
99
+ end
100
+
101
+ it "should format the exception returned when there was an error parsing the json response" do
102
+ inner_exception = Exception.new("Error Message")
103
+ inner_exception.stub(:http_code).and_return(500)
104
+ inner_exception.stub(:response).and_return('{ "ErrorCode":')
105
+
106
+ ex = Services::SltcApiException.new(inner_exception)
107
+ expect(ex.send(:extract_sltc_error_information)).to eq("Error parsing SimpleLTC API Error JSON ({ \"ErrorCode\":): 757: unexpected token at '{ \"ErrorCode\":' (500: Error Message)")
108
+ end
109
+
110
+ end
111
+
112
+ context '#format_exception_message' do
113
+
114
+ it "should return a generic error message if there is no raw json" do
115
+ inner_exception = Exception.new("Error Message")
116
+ inner_exception.stub(:http_code).and_return(500)
117
+ inner_exception.stub(:response).and_return(nil)
118
+
119
+ ex = Services::SltcApiException.new(inner_exception)
120
+ expect(ex.send(:format_exception_message)).to eq("Unknown SimpleLTC API Error (500: Error Message)")
121
+ end
122
+
123
+ it "should return the value returned by extract_sltc_error_information if the json response is not nil" do
124
+ inner_exception = Exception.new("Error Message")
125
+ inner_exception.stub(:http_code).and_return(500)
126
+ inner_exception.stub(:response).and_return('{ "ErrorCode": 12, "Message": "Sltc Error Message" }')
127
+
128
+ ex = Services::SltcApiException.new(inner_exception)
129
+ ex.should_receive(:extract_sltc_error_information).and_return("Message returned from extract_sltc_error_information")
130
+ expect(ex.send(:format_exception_message)).to eq("Message returned from extract_sltc_error_information")
131
+ end
132
+
133
+ end
134
+ end
135
+
136
+ end
@@ -0,0 +1,192 @@
1
+ require 'spec_helper'
2
+
3
+ describe Services::SltcApi do
4
+
5
+ context '#initialize' do
6
+
7
+ it "should faithfully reproduce the parameters in the accessors" do
8
+
9
+ api = Services::SltcApi.new("https://api-test.simpleltc.com/v1",
10
+ "the_token",
11
+ "the_account",
12
+ "the_username",
13
+ "the_password")
14
+
15
+ expect(api.base_uri).to eq('https://api-test.simpleltc.com/v1')
16
+ expect(api.api_key).to eq('the_token')
17
+ expect(api.account).to eq('the_account')
18
+ expect(api.username).to eq('the_username')
19
+ expect(api.password).to eq('the_password')
20
+
21
+ end
22
+
23
+ end
24
+
25
+ context 'Instance Methods' do
26
+
27
+ before(:each) do
28
+ @api = Services::SltcApi.new("https://api-test.simpleltc.com/v1",
29
+ "the_token",
30
+ "the_account",
31
+ "the_username",
32
+ "the_password")
33
+
34
+ end
35
+
36
+ context '#get_response' do
37
+
38
+ it "should return a hash converted from JSON when using the default no-parameter" do
39
+ response = @api.send(:get_response) do
40
+ '{ "Message" : "The Message" }'
41
+ end
42
+
43
+ expect(response).to be_a(Hash)
44
+ end
45
+
46
+ it "should return the raw body of the block if the 'binary_response' parameter is true" do
47
+ response = @api.send(:get_response, true) do
48
+ '{ "Message" : "The Message" }'
49
+ end
50
+
51
+ expect(response).to eq('{ "Message" : "The Message" }')
52
+ end
53
+
54
+ it "should raise an SltcApiException if a RestClient::Exception is raised in the block" do
55
+ expect { @api.send(:get_response) { raise RestClient::Exception } }.to raise_error(Services::SltcApiException)
56
+ end
57
+
58
+ it "should allow a non-RestClient::Exception to be raised out of the method" do
59
+ ex = Exception.new
60
+ expect { @api.send(:get_response) { raise ex } }.to raise_error(ex)
61
+ end
62
+
63
+ end
64
+
65
+ context '#authentication_headers' do
66
+
67
+ it "should return a hash of the headers to be sent for authentication" do
68
+ expected_hash = {
69
+ 'SLTC-Api-Key' => 'the_token',
70
+ "Host"=>"api-test.simpleltc.com"
71
+ }
72
+
73
+ expect(@api.send(:authentication_headers)).to eq(expected_hash)
74
+ end
75
+ end
76
+
77
+ context '#list_providers' do
78
+
79
+ it "should make the web service call and return the response as a hash" do
80
+ json = '[ { "id" : 3993, "name" : "Rouster Health" }, { "id" : 3994, "name" : "Blueth Health" }, { "id" : 4003, "name" : "Orange Health" } ]'
81
+ providers = [
82
+ { 'id' => 3993, 'name' => "Rouster Health" },
83
+ { 'id' => 3994, 'name' => "Blueth Health" },
84
+ { 'id' => 4003, 'name' => "Orange Health" }
85
+ ]
86
+
87
+ @api.should_receive(:build_url).and_return('the_url')
88
+ @api.should_receive(:authentication_headers).and_return( { header: 'the_headers' } )
89
+
90
+ RestClient::Request.
91
+ should_receive(:execute).
92
+ with({ method: :get,
93
+ url: 'the_url',
94
+ user: 'the_username@the_account',
95
+ password: 'the_password',
96
+ headers: { header: 'the_headers', 'Accept' => 'application/json' }}).
97
+ and_return(json)
98
+
99
+ expect(@api.list_providers).to eq(providers)
100
+ end
101
+
102
+ end
103
+
104
+ context '#list_batches' do
105
+
106
+ it "should make the web service call and return the response as a hash" do
107
+ json = '[ { "id" : 1, "provider" : { "id" : 12, "name" : "Health" }, "filename" : "the_filename", "status" : "Verified", "statusDate" : "2013-10-01T00:00Z", "uploadDate" : "2013-10-01T01:00Z", "totalRecords" : 12 } ]'
108
+ batches = [
109
+ { "id" => 1, "provider" => { "id" => 12, "name" => "Health" }, "filename" => 'the_filename', "status" => "Verified", "statusDate" => "2013-10-01T00:00Z", "uploadDate" => "2013-10-01T01:00Z", "totalRecords" => 12 }
110
+ ]
111
+
112
+ @api.should_receive(:build_url).and_return('the_url')
113
+ @api.should_receive(:authentication_headers).and_return( { header: 'the_headers' } )
114
+ @api.should_receive(:account).and_return('the_account')
115
+ @api.should_receive(:password).and_return('the_password')
116
+
117
+ # RestClient.should_receive(:get).with('the_url', { header: 'the_headers', 'Accept' => 'application/json' }).and_return(json)
118
+ RestClient::Request.
119
+ should_receive(:execute).
120
+ with({ method: :get,
121
+ url: 'the_url',
122
+ user: "the_username@the_account",
123
+ password: "the_password",
124
+ headers: { header: 'the_headers', 'Accept' => 'application/json' } }).
125
+ and_return(json)
126
+
127
+ expect(@api.list_providers).to eq(batches)
128
+ end
129
+
130
+ end
131
+
132
+ end
133
+
134
+ context 'Private Methods' do
135
+ before(:each) do
136
+ @api = Services::SltcApi.new("https://api-test.simpleltc.com/v1",
137
+ "the_token",
138
+ "the_account",
139
+ "the_username",
140
+ "the_password")
141
+
142
+ end
143
+
144
+ context '#authentication_headers' do
145
+
146
+ it "should incorporate all the necessary headers for authentication" do
147
+ expect(@api.send(:authentication_headers)).to eq({
148
+ 'SLTC-Api-Key' => 'the_token',
149
+ 'Host' => 'api-test.simpleltc.com'
150
+ })
151
+ end
152
+ end
153
+
154
+ context '#build_url' do
155
+
156
+ it "should return the url that has no parameters" do
157
+ expect(@api.send(:build_url, :MdsBatches)).
158
+ to eq("https://api-test.simpleltc.com/v1/MdsBatches")
159
+ end
160
+
161
+ it "should return the url that has a string parameter" do
162
+ expect(@api.send(:build_url, :MdsBatches, 'value')).
163
+ to eq("https://api-test.simpleltc.com/v1/MdsBatches/value")
164
+ end
165
+
166
+ it "should return the url that has multiple query parameters" do
167
+ expect(@api.send(:build_url, :MdsBatches, { a: 'aval', b: 'bval' })).
168
+ to eq("https://api-test.simpleltc.com/v1/MdsBatches?a=aval&b=bval")
169
+ end
170
+
171
+ it "should return the url that has a single query parameter" do
172
+ expect(@api.send(:build_url, :MdsBatches, { a: 'aval' })).
173
+ to eq("https://api-test.simpleltc.com/v1/MdsBatches?a=aval")
174
+ end
175
+
176
+ it "should return the url that has an empty hash of query parameters" do
177
+ expect(@api.send(:build_url, :MdsBatches, {})).
178
+ to eq("https://api-test.simpleltc.com/v1/MdsBatches")
179
+ end
180
+
181
+ it "should return the url that has a query parameter value that is a time" do
182
+ d = Time.now
183
+ dv = d.strftime("%Y-%m-%dT%H:%MZ")
184
+ expect(@api.send(:build_url, :MdsBatches, {d: d})).
185
+ to eq("https://api-test.simpleltc.com/v1/MdsBatches?d=#{dv}")
186
+
187
+ end
188
+ end
189
+
190
+ end
191
+
192
+ end