maestrano-connector-rails 2.0.2.pre.RC6 → 2.0.2.pre.RC7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +6 -0
  3. data/DEVELOPER.md +1 -4
  4. data/Gemfile +2 -32
  5. data/Rakefile +6 -30
  6. data/app/models/maestrano/connector/rails/concerns/entity.rb +12 -0
  7. data/app/models/maestrano/connector/rails/concerns/sub_entity_base.rb +3 -2
  8. data/app/models/maestrano/connector/rails/id_map.rb +1 -0
  9. data/app/models/maestrano/connector/rails/services/data_consolidator.rb +4 -4
  10. data/db/migrate/20170315032224_add_metadata_to_id_map.rb +5 -0
  11. data/lib/maestrano/connector/rails/version.rb +7 -0
  12. data/lib/maestrano_connector_rails/factories.rb +1 -0
  13. data/maestrano-connector-rails.gemspec +44 -308
  14. metadata +13 -151
  15. data/VERSION +0 -1
  16. data/spec/controllers/connec_controller_spec.rb +0 -179
  17. data/spec/controllers/dependancies_controller_spec.rb +0 -31
  18. data/spec/controllers/group_users_controller_spec.rb +0 -39
  19. data/spec/controllers/groups_controller_spec.rb +0 -36
  20. data/spec/controllers/synchronizations_controller_spec.rb +0 -211
  21. data/spec/controllers/version_controller_spec.rb +0 -17
  22. data/spec/dummy/README.md +0 -1
  23. data/spec/dummy/Rakefile +0 -6
  24. data/spec/dummy/app/assets/images/.keep +0 -0
  25. data/spec/dummy/app/assets/javascripts/application.js +0 -13
  26. data/spec/dummy/app/assets/stylesheets/application.css +0 -15
  27. data/spec/dummy/app/controllers/application_controller.rb +0 -8
  28. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  29. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  30. data/spec/dummy/app/mailers/.keep +0 -0
  31. data/spec/dummy/app/models/.keep +0 -0
  32. data/spec/dummy/app/models/concerns/.keep +0 -0
  33. data/spec/dummy/app/models/entities/.keep +0 -0
  34. data/spec/dummy/app/views/home/index.html.erb +0 -1
  35. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  36. data/spec/dummy/bin/bundle +0 -3
  37. data/spec/dummy/bin/rails +0 -4
  38. data/spec/dummy/bin/rake +0 -4
  39. data/spec/dummy/bin/setup +0 -29
  40. data/spec/dummy/config.ru +0 -4
  41. data/spec/dummy/config/application.rb +0 -26
  42. data/spec/dummy/config/boot.rb +0 -5
  43. data/spec/dummy/config/database.yml +0 -25
  44. data/spec/dummy/config/environment.rb +0 -5
  45. data/spec/dummy/config/environments/development.rb +0 -41
  46. data/spec/dummy/config/environments/production.rb +0 -79
  47. data/spec/dummy/config/environments/test.rb +0 -39
  48. data/spec/dummy/config/initializers/assets.rb +0 -11
  49. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  50. data/spec/dummy/config/initializers/cookies_serializer.rb +0 -3
  51. data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  52. data/spec/dummy/config/initializers/inflections.rb +0 -16
  53. data/spec/dummy/config/initializers/maestrano.rb +0 -2
  54. data/spec/dummy/config/initializers/mime_types.rb +0 -4
  55. data/spec/dummy/config/initializers/session_store.rb +0 -3
  56. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  57. data/spec/dummy/config/locales/en.yml +0 -23
  58. data/spec/dummy/config/routes.rb +0 -9
  59. data/spec/dummy/config/secrets.yml +0 -22
  60. data/spec/dummy/config/settings.yml +0 -2
  61. data/spec/dummy/config/settings/development.yml +0 -12
  62. data/spec/dummy/config/settings/production.yml +0 -1
  63. data/spec/dummy/config/settings/test.yml +0 -12
  64. data/spec/dummy/config/settings/uat.yml +0 -1
  65. data/spec/dummy/db/schema.rb +0 -102
  66. data/spec/dummy/lib/assets/.keep +0 -0
  67. data/spec/dummy/log/.keep +0 -0
  68. data/spec/dummy/public/404.html +0 -67
  69. data/spec/dummy/public/422.html +0 -67
  70. data/spec/dummy/public/500.html +0 -66
  71. data/spec/dummy/public/favicon.ico +0 -0
  72. data/spec/dummy/tmp/cache/.gitkeep +0 -0
  73. data/spec/integration/complex_id_references_spec.rb +0 -249
  74. data/spec/integration/complex_naming_spec.rb +0 -192
  75. data/spec/integration/complex_spec.rb +0 -578
  76. data/spec/integration/connec_to_external_spec.rb +0 -327
  77. data/spec/integration/external_to_connec_spec.rb +0 -159
  78. data/spec/integration/id_references_spec.rb +0 -584
  79. data/spec/integration/singleton_spec.rb +0 -297
  80. data/spec/jobs/all_synchronizations_job_spec.rb +0 -24
  81. data/spec/jobs/push_to_connec_job_spec.rb +0 -111
  82. data/spec/jobs/push_to_connec_worker_spec.rb +0 -37
  83. data/spec/jobs/synchronization_job_spec.rb +0 -216
  84. data/spec/models/complex_entity_spec.rb +0 -407
  85. data/spec/models/connec_helper_spec.rb +0 -588
  86. data/spec/models/connector_logger_spec.rb +0 -26
  87. data/spec/models/entity_base_spec.rb +0 -20
  88. data/spec/models/entity_spec.rb +0 -1078
  89. data/spec/models/external_spec.rb +0 -27
  90. data/spec/models/id_map_spec.rb +0 -12
  91. data/spec/models/organization_spec.rb +0 -243
  92. data/spec/models/sub_entity_base_spec.rb +0 -155
  93. data/spec/models/synchronization_spec.rb +0 -106
  94. data/spec/models/user_organization_rel_spec.rb +0 -14
  95. data/spec/models/user_spec.rb +0 -15
  96. data/spec/routing/connec_routing_spec.rb +0 -12
  97. data/spec/spec_helper.rb +0 -34
  98. data/template/Procfile +0 -2
  99. data/template/application-sample.yml +0 -13
  100. data/template/database.yml +0 -31
  101. data/template/gitignore +0 -23
  102. data/template/maestrano.rb +0 -2
  103. data/template/maestrano_connector_template.rb +0 -135
  104. data/template/routes.rb +0 -25
  105. data/template/rubocop.yml +0 -55
  106. data/template/settings/development.yml +0 -0
  107. data/template/settings/production.yml +0 -0
  108. data/template/settings/settings.yml +0 -2
  109. data/template/settings/test.yml +0 -0
  110. data/template/settings/uat.yml +0 -0
  111. data/template/sidekiq.rb +0 -9
  112. data/template/sidekiq.yml +0 -4
  113. data/template/spec_helper.rb +0 -24
@@ -1,192 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'complex entity subentities naming conflict' do
4
-
5
- class Entities::NamePayment < Maestrano::Connector::Rails::ComplexEntity
6
- def self.connec_entities_names
7
- {Payment: 'NamePayment'}
8
- end
9
- def self.external_entities_names
10
- {Payment: 'ExtPayment'}
11
- end
12
- def connec_model_to_external_model(connec_hash_of_entities)
13
- {'Payment' => {'Payment' => connec_hash_of_entities['Payment']}}
14
- end
15
- def external_model_to_connec_model(external_hash_of_entities)
16
- {'Payment' => {'Payment' => external_hash_of_entities['Payment']}}
17
- end
18
- end
19
-
20
- module Entities::SubEntities
21
- end
22
- class Entities::SubEntities::NamePayment < Maestrano::Connector::Rails::SubEntityBase
23
- def self.external?
24
- false
25
- end
26
- def self.entity_name
27
- 'Payment'
28
- end
29
- def self.mapper_classes
30
- {
31
- 'Payment' => ::PaymentMapper
32
- }
33
- end
34
- def self.object_name_from_connec_entity_hash(entity)
35
- entity['title']
36
- end
37
- def self.references
38
- {'Payment' => {record_references: %w(organization_id), id_references: %w(lines/id)}}
39
- end
40
- def self.id_from_external_entity_hash(entity)
41
- entity['ID']
42
- end
43
- end
44
-
45
- class Entities::SubEntities::ExtPayment < Maestrano::Connector::Rails::SubEntityBase
46
- def self.external?
47
- true
48
- end
49
- def self.entity_name
50
- 'Payment'
51
- end
52
- def self.mapper_classes
53
- {'Payment' => ::PaymentMapper}
54
- end
55
- def self.id_from_external_entity_hash(entity)
56
- entity['ID']
57
- end
58
- def self.references
59
- {'Payment' => {record_references: %w(organization_id), id_references: %w(lines/id)}}
60
- end
61
- end
62
-
63
- class LineMapper
64
- extend HashMapper
65
- map from('id'), to('ID')
66
- map from('amount'), to('Price')
67
- end
68
-
69
- class PaymentMapper
70
- extend HashMapper
71
- map from('title'), to('Title')
72
- map from('organization_id'), to('AccountId')
73
- map from('lines'), to('Line'), using: LineMapper
74
- end
75
-
76
- let(:provider) { 'provider' }
77
- let(:oauth_uid) { 'oauth uid' }
78
- let!(:organization) { create(:organization, oauth_provider: provider, oauth_uid: oauth_uid) }
79
- let(:connec_client) { Maestrano::Connector::Rails::ConnecHelper.get_client(organization) }
80
- let(:external_client) { Object.new }
81
- let(:connec_payment_id) { '1205c5e0-e18e-0133-890f-07d4de9f9781' }
82
- let(:connec_line_id1) { '3405c5e0-e18e-0133-890f-07d4de9f9781' }
83
- let(:ext_payment_id) { 'ext payment id' }
84
- let(:ext_org_id) { 'ext org id' }
85
- let(:ext_line_id1) { 'ext line id1' }
86
- let(:payment_title) { 'This is a payment' }
87
-
88
- before {
89
- allow(Maestrano::Connector::Rails::External).to receive(:entities_list).and_return(%w(name_payment))
90
- organization.reset_synchronized_entities(true)
91
- }
92
-
93
- subject { Maestrano::Connector::Rails::SynchronizationJob.new.sync_entity('name_payment', organization, connec_client, external_client, organization.last_synchronization_date, {}) }
94
-
95
- describe 'fetching entities from external' do
96
- before {
97
- allow_any_instance_of(Entities::SubEntities::NamePayment).to receive(:get_connec_entities).and_return([])
98
- }
99
-
100
- it 'does a fetch with the right name' do
101
- expect_any_instance_of(Entities::SubEntities::ExtPayment).to receive(:get_external_entities).with('Payment', organization.last_synchronization_date).and_return([])
102
- subject
103
- end
104
- end
105
-
106
- describe 'sending entities to external' do
107
- before {
108
- allow(connec_client).to receive(:get).and_return(ActionDispatch::Response.new(200, {}, {payments: [connec_payment]}.to_json, {}))
109
- allow(connec_client).to receive(:batch).and_return(ActionDispatch::Response.new(200, {}, {results: [{status: 200, body: {payments: {}}}]}.to_json, {}))
110
-
111
- allow_any_instance_of(Entities::SubEntities::ExtPayment).to receive(:get_external_entities).and_return([])
112
- }
113
-
114
- let(:connec_payment) {
115
- {
116
- 'id' => [
117
- {
118
- 'realm' => 'org-fg4a',
119
- 'provider' => 'connec',
120
- 'id' => connec_payment_id
121
- }
122
- ],
123
- 'title' => payment_title,
124
- 'organization_id' => [
125
- {
126
- 'realm' => 'org-fg4a',
127
- 'provider' => 'connec',
128
- 'id' => '2305c5e0-e18e-0133-890f-07d4de9f9781'
129
- },
130
- {
131
- 'realm' => oauth_uid,
132
- 'provider' => provider,
133
- 'id' => ext_org_id
134
- }
135
- ],
136
- 'lines' => lines
137
- }
138
- }
139
-
140
- let(:lines) {
141
- [
142
- {
143
- 'id' => [
144
- {
145
- 'realm' => 'org-fg4a',
146
- 'provider' => 'connec',
147
- 'id' => connec_line_id1
148
- }
149
- ],
150
- 'amount' => 123
151
- }
152
- ]
153
- }
154
-
155
- let(:mapped_payment) {
156
- {
157
- "Title"=>"This is a payment",
158
- "AccountId"=>"ext org id",
159
- "Line"=>[{"Price"=>123}]}
160
- }
161
-
162
- before {
163
- allow_any_instance_of(Entities::SubEntities::NamePayment).to receive(:create_external_entity).and_return({'ID' => ext_payment_id})
164
- }
165
-
166
- it 'handles the idmap correctly' do
167
- expect{
168
- subject
169
- }.to change{ Maestrano::Connector::Rails::IdMap.count }.by(1)
170
- idmap = Maestrano::Connector::Rails::IdMap.last
171
- expect(idmap.name).to eql(payment_title)
172
- expect(idmap.connec_entity).to eql('payment')
173
- expect(idmap.external_entity).to eql('payment')
174
- expect(idmap.message).to be_nil
175
- expect(idmap.external_id).to eql(ext_payment_id)
176
- expect(idmap.connec_id).to eql(connec_payment_id)
177
- end
178
-
179
- it 'handles the mapping correctly' do
180
- payment_idmap = Entities::SubEntities::NamePayment.create_idmap(organization_id: organization.id, external_id: ext_payment_id, connec_entity: 'payment')
181
- allow(Maestrano::Connector::Rails::IdMap).to receive(:create).and_return(payment_idmap)
182
- expect_any_instance_of(Entities::NamePayment).to receive(:push_entities_to_external).with({'Payment' => {'Payment' => [{entity: mapped_payment.with_indifferent_access, idmap: payment_idmap, id_refs_only_connec_entity: {'lines' => lines.map { |l| l.delete('amount'); l }}}]}})
183
- subject
184
- end
185
-
186
- it 'handles the sending with the correct names' do
187
- expect_any_instance_of(Entities::SubEntities::NamePayment).to receive(:create_external_entity).once.with(mapped_payment, 'Payment').and_return({})
188
- expect(connec_client).to receive(:batch).once.and_return(ActionDispatch::Response.new(201, {}, {results: []}.to_json, {}))
189
- subject
190
- end
191
- end
192
- end
@@ -1,578 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'complex entities workflow' do
4
- class Entities::CustomerAndSupplier < Maestrano::Connector::Rails::ComplexEntity
5
- def self.connec_entities_names
6
- %w(CompOrganization)
7
- end
8
- def self.external_entities_names
9
- %w(CompCustomer CompSupplier)
10
- end
11
- def connec_model_to_external_model(connec_hash_of_entities)
12
- organizations = connec_hash_of_entities['CompOrganization']
13
- modelled_connec_entities = {'CompOrganization' => { 'CompSupplier' => [], 'CompCustomer' => [] }}
14
-
15
- organizations.each do |organization|
16
- if organization['is_supplier']
17
- modelled_connec_entities['CompOrganization']['CompSupplier'] << organization
18
- else
19
- modelled_connec_entities['CompOrganization']['CompCustomer'] << organization
20
- end
21
- end
22
- return modelled_connec_entities
23
- end
24
- def external_model_to_connec_model(external_hash_of_entities)
25
- return {'CompCustomer' => {'CompOrganization' => external_hash_of_entities['CompCustomer']}, 'CompSupplier' => {'CompOrganization' => external_hash_of_entities['CompSupplier']}}
26
- end
27
- end
28
-
29
- module Entities::SubEntities
30
- end
31
- class Entities::SubEntities::CompOrganization < Maestrano::Connector::Rails::SubEntityBase
32
- def self.external?
33
- false
34
- end
35
- def self.entity_name
36
- 'CompOrganization'
37
- end
38
- def self.mapper_classes
39
- {
40
- 'CompCustomer' => ::CompMapper,
41
- 'CompSupplier' => ::CompMapper
42
- }
43
- end
44
- def self.object_name_from_connec_entity_hash(entity)
45
- entity['name']
46
- end
47
- def self.references
48
- {'CompCustomer' => ['ref_id'], 'CompSupplier' => ['ref_id']}
49
- end
50
- def self.id_from_external_entity_hash(entity)
51
- entity['id']
52
- end
53
- end
54
-
55
- class Entities::SubEntities::CompCustomer < Maestrano::Connector::Rails::SubEntityBase
56
- def self.external?
57
- true
58
- end
59
- def self.entity_name
60
- 'CompCustomer'
61
- end
62
- def self.mapper_classes
63
- {'CompOrganization' => ::CompMapper}
64
- end
65
- def self.id_from_external_entity_hash(entity)
66
- entity['id']
67
- end
68
- def self.object_name_from_external_entity_hash(entity)
69
- entity['name']
70
- end
71
- def self.references
72
- {'CompOrganization' => ['ref_id']}
73
- end
74
- def map_to(name, entity, first_time_mapped = nil)
75
- super.merge(is_supplier: false)
76
- end
77
- end
78
- class Entities::SubEntities::CompSupplier < Maestrano::Connector::Rails::SubEntityBase
79
- def self.external?
80
- true
81
- end
82
- def self.entity_name
83
- 'CompSupplier'
84
- end
85
- def self.mapper_classes
86
- {'CompOrganization' => ::CompMapper}
87
- end
88
- def self.id_from_external_entity_hash(entity)
89
- entity['id']
90
- end
91
- def self.object_name_from_external_entity_hash(entity)
92
- entity['name']
93
- end
94
- def self.references
95
- {'CompOrganization' => ['ref_id']}
96
- end
97
- def map_to(name, entity, first_time_mapped = nil)
98
- super.merge(is_supplier: true)
99
- end
100
- end
101
-
102
- class CompMapper
103
- extend HashMapper
104
- map from('id'), to('id')
105
- map from('ref_id'), to('ref_id')
106
- map from('name'), to('name')
107
- end
108
-
109
- let(:provider) { 'provider' }
110
- let(:oauth_uid) { 'oauth uid' }
111
- let!(:organization) { create(:organization, oauth_provider: provider, oauth_uid: oauth_uid, uid: 'uid') }
112
- let(:connec_client) { Maestrano::Connec::Client[organization.tenant].new(organization.uid) }
113
- let(:external_client) { Object.new }
114
-
115
-
116
- let(:connec_org1_id) { 'connec_org1_id' }
117
- let(:connec_org2_id) { 'connec_org2_id' }
118
- let(:connec_org1_ext_id) { 'connec_org1_ext_id' }
119
- let(:connec_org2_ext_id) { 'connec_org2_ext_id' }
120
- let(:connec_org1_name) { 'connec_org1_name' }
121
- let(:connec_org2_name) { 'connec_org2_name' }
122
- let(:connec_org1_ext_ref_id) { 'connec_org1_ext_ref_id' }
123
- let(:connec_org2_ext_ref_id) { 'connec_org2_ext_ref_id' }
124
- let(:connec_org1) {
125
- {
126
- 'id' => [{'provider' => 'connec', 'id' => connec_org1_id, 'realm' => organization.uid}],
127
- 'name' => connec_org1_name,
128
- 'is_supplier' => true,
129
- 'ref_id' => [{'provider' => provider, 'id' => connec_org1_ext_ref_id, 'realm' => oauth_uid}]
130
- }
131
- }
132
- let(:mapped_connec_org1) {
133
- {
134
- ref_id: connec_org1_ext_ref_id,
135
- name: connec_org1_name,
136
- }
137
- }
138
- let(:connec_org2) {
139
- {
140
- 'id' => [{'provider' => provider, 'id' => connec_org2_ext_id, 'realm' => oauth_uid}, {'id' => connec_org2_id, 'provider' => 'connec', 'realm' => organization.uid}],
141
- 'name' => connec_org2_name,
142
- 'is_supplier' => false,
143
- 'ref_id' => [{'provider' => provider, 'id' => connec_org2_ext_ref_id, 'realm' => oauth_uid}]
144
- }
145
- }
146
- let(:mapped_connec_org2) {
147
- {
148
- ref_id: connec_org2_ext_ref_id,
149
- name: connec_org2_name,
150
- id: connec_org2_ext_id
151
- }
152
- }
153
- let(:connec_orgs) { [connec_org1, connec_org2] }
154
-
155
- let(:ext_customer_id) { 'ext_customer_id' }
156
- let(:ext_supplier_id) { 'ext_supplier_id' }
157
- let(:ext_customer_name) { 'ext_customer_name' }
158
- let(:ext_supplier_name) { 'ext_supplier_name' }
159
- let(:ext_ref_id) { 'ext_ref_id' }
160
- let(:ext_customer) {
161
- {
162
- 'id' => ext_customer_id,
163
- 'name' => ext_customer_name,
164
- 'ref_id' => ext_ref_id
165
- }
166
- }
167
- let(:mapped_ext_customer) {
168
- {
169
- :id => [
170
- {
171
- :id => ext_customer_id,
172
- :provider => provider,
173
- :realm => oauth_uid
174
- }
175
- ],
176
- :ref_id => [
177
- {
178
- :id => ext_ref_id,
179
- :provider => provider,
180
- :realm => oauth_uid
181
- }
182
- ],
183
- :name => ext_customer_name,
184
- :is_supplier => false
185
- }
186
- }
187
- let(:ext_supplier) {
188
- {
189
- 'id' => ext_supplier_id,
190
- 'name' => ext_supplier_name,
191
- 'ref_id' => ext_ref_id
192
- }
193
- }
194
- let(:mapped_ext_supplier) {
195
- {
196
- :id => [
197
- {
198
- :id => ext_supplier_id,
199
- :provider => provider,
200
- :realm => oauth_uid
201
- }
202
- ],
203
- :ref_id => [
204
- {
205
- :id => ext_ref_id,
206
- :provider => provider,
207
- :realm => oauth_uid
208
- }
209
- ],
210
- :name => ext_supplier_name,
211
- :is_supplier => true
212
- }
213
- }
214
- let!(:supplier_idmap) { Entities::SubEntities::CompSupplier.create_idmap(organization_id: organization.id, external_id: ext_supplier_id, connec_entity: 'comporganization') }
215
- let(:entity_name) { 'customer_and_supplier' }
216
-
217
- before do
218
- allow(connec_client).to receive(:get).and_return(ActionDispatch::Response.new(200, {}, {comporganizations: connec_orgs}.to_json, {}))
219
- allow_any_instance_of(Entities::SubEntities::CompCustomer).to receive(:get_external_entities).and_return([ext_customer])
220
- allow_any_instance_of(Entities::SubEntities::CompSupplier).to receive(:get_external_entities).and_return([ext_supplier])
221
- allow(Maestrano::Connector::Rails::External).to receive(:entities_list).and_return([entity_name])
222
- organization.reset_synchronized_entities(true)
223
- end
224
-
225
- subject { Maestrano::Connector::Rails::SynchronizationJob.new.sync_entity(entity_name, organization, connec_client, external_client, nil, {}) }
226
-
227
- it 'handles the fetching correctly' do
228
- expect_any_instance_of(Entities::CustomerAndSupplier).to receive(:consolidate_and_map_data).with({'CompOrganization' => connec_orgs}, {'CompCustomer' => [ext_customer], 'CompSupplier' => [ext_supplier]}).and_return({connec_entities: [], external_entities: []})
229
- subject
230
- end
231
-
232
- it 'handles the idmap correctly' do
233
- allow(connec_client).to receive(:batch).and_return(ActionDispatch::Response.new(201, {}, {results: []}.to_json, {}))
234
- allow_any_instance_of(Entities::SubEntities::CompOrganization).to receive(:create_external_entity).and_return({'id' => connec_org1_ext_id})
235
- allow_any_instance_of(Entities::SubEntities::CompOrganization).to receive(:update_external_entity).and_return(nil)
236
- expect{
237
- subject
238
- }.to change{ Maestrano::Connector::Rails::IdMap.count }.by(3)
239
-
240
- expect(supplier_idmap.reload.name).to eql(ext_supplier_name)
241
- expect(supplier_idmap.reload.connec_entity).to eql('comporganization')
242
- expect(supplier_idmap.reload.external_entity).to eql('compsupplier')
243
- expect(supplier_idmap.reload.message).to be_nil
244
- expect(supplier_idmap.reload.external_id).to eql(ext_supplier_id)
245
-
246
- customer_idmap = Entities::SubEntities::CompCustomer.find_idmap({external_id: ext_customer_id})
247
- expect(customer_idmap).to_not be_nil
248
- expect(customer_idmap.name).to eql(ext_customer_name)
249
- expect(customer_idmap.connec_entity).to eql('comporganization')
250
- expect(customer_idmap.external_entity).to eql('compcustomer')
251
- expect(customer_idmap.message).to be_nil
252
-
253
- org1_idmap = Entities::SubEntities::CompOrganization.find_idmap({external_id: connec_org1_ext_id})
254
- expect(org1_idmap).to_not be_nil
255
- expect(org1_idmap.name).to eql(connec_org1_name)
256
- expect(org1_idmap.connec_entity).to eql('comporganization')
257
- expect(org1_idmap.external_entity).to eql('compsupplier')
258
- expect(org1_idmap.message).to be_nil
259
-
260
- org2_idmap = Entities::SubEntities::CompOrganization.find_idmap({external_id: connec_org2_ext_id})
261
- expect(org2_idmap).to_not be_nil
262
- expect(org2_idmap.name).to eql(connec_org2_name)
263
- expect(org2_idmap.connec_entity).to eql('comporganization')
264
- expect(org2_idmap.external_entity).to eql('compcustomer')
265
- expect(org2_idmap.message).to be_nil
266
- end
267
-
268
- it 'handles the mapping correctly' do
269
- cust_idmap = Entities::SubEntities::CompCustomer.create_idmap(organization_id: organization.id, external_id: ext_customer_id, connec_entity: 'comporganization')
270
- org1_idmap = Entities::SubEntities::CompOrganization.create_idmap(organization_id: organization.id, external_id: connec_org1_ext_id, external_entity: 'compsupplier')
271
- org2_idmap = Entities::SubEntities::CompOrganization.create_idmap(organization_id: organization.id, external_id: connec_org2_ext_id, external_entity: 'compcustomer')
272
- allow(Maestrano::Connector::Rails::IdMap).to receive(:create).and_return(org1_idmap, org2_idmap, cust_idmap)
273
- expect_any_instance_of(Entities::CustomerAndSupplier).to receive(:push_entities_to_external).with({'CompOrganization' => {'CompSupplier' => [{entity: mapped_connec_org1.with_indifferent_access, idmap: org1_idmap, id_refs_only_connec_entity: {}}], 'CompCustomer' => [{entity: mapped_connec_org2.with_indifferent_access, idmap: org2_idmap, id_refs_only_connec_entity: {}}]}})
274
- expect_any_instance_of(Entities::CustomerAndSupplier).to receive(:push_entities_to_connec).with({'CompCustomer' => {'CompOrganization' => [{entity: mapped_ext_customer.with_indifferent_access, idmap: cust_idmap}]}, 'CompSupplier' => {'CompOrganization' => [{entity: mapped_ext_supplier.with_indifferent_access, idmap: supplier_idmap}]}})
275
- subject
276
- end
277
-
278
- it 'sends two objects to connec, two objects to external and send back one id to connec' do
279
- expect_any_instance_of(Entities::SubEntities::CompOrganization).to receive(:create_external_entity).once.with(mapped_connec_org1, 'CompSupplier').and_return({})
280
- expect_any_instance_of(Entities::SubEntities::CompOrganization).to receive(:update_external_entity).once.with(mapped_connec_org2, connec_org2_ext_id, 'CompCustomer')
281
- expect(connec_client).to receive(:batch).exactly(3).times.and_return(ActionDispatch::Response.new(201, {}, {results: []}.to_json, {}), ActionDispatch::Response.new(200, {}, {results: []}.to_json, {}))
282
- subject
283
- end
284
-
285
- describe "when using creation_mapper_classes" do
286
-
287
- before do
288
- allow(connec_client).to receive(:get).and_return(ActionDispatch::Response.new(200, {}, {comporganizationmissingfields: connec_orgs}.to_json, {}))
289
- allow_any_instance_of(Entities::SubEntities::CompCustomerMissingField).to receive(:get_external_entities).and_return([ext_customer])
290
- allow_any_instance_of(Entities::SubEntities::CompSupplierMissingField).to receive(:get_external_entities).and_return([ext_supplier])
291
- end
292
-
293
- let(:subject_missing_field) { Maestrano::Connector::Rails::SynchronizationJob.new.sync_entity('customer_and_supplier_missing_field', organization, connec_client, external_client, nil, {}) }
294
-
295
- class Entities::CustomerAndSupplierMissingField < Maestrano::Connector::Rails::ComplexEntity
296
- def self.connec_entities_names
297
- %w(CompOrganizationMissingField)
298
- end
299
- def self.external_entities_names
300
- %w(CompCustomerMissingField CompSupplierMissingField)
301
- end
302
- def connec_model_to_external_model(connec_hash_of_entities)
303
- organizations_missing_field = connec_hash_of_entities['CompOrganizationMissingField']
304
- modelled_connec_entities = { 'CompOrganizationMissingField' => { 'CompSupplierMissingField' => [], 'CompCustomerMissingField' => [] } }
305
-
306
- organizations_missing_field.each do |organization|
307
- if organization['is_supplier']
308
- modelled_connec_entities['CompOrganizationMissingField']['CompSupplierMissingField'] << organization
309
- else
310
- modelled_connec_entities['CompOrganizationMissingField']['CompCustomerMissingField'] << organization
311
- end
312
- end
313
-
314
- return modelled_connec_entities
315
- end
316
-
317
- def external_model_to_connec_model(external_hash_of_entities)
318
- return {'CompCustomerMissingField' => {'CompOrganizationMissingField' => external_hash_of_entities['CompCustomerMissingField']}, 'CompSupplierMissingField' => {'CompOrganizationMissingField' => external_hash_of_entities['CompSupplierMissingField']}}
319
- end
320
- end
321
-
322
- class Entities::SubEntities::CompOrganizationMissingField < Entities::SubEntities::CompOrganization
323
-
324
- def self.entity_name
325
- 'CompOrganizationMissingField'
326
- end
327
-
328
- def self.mapper_classes
329
- {
330
- 'CompCustomerMissingField' => ::CompMapper,
331
- 'CompSupplierMissingField' => ::CompMapper
332
- }
333
- end
334
-
335
- def self.creation_mapper_classes
336
- {
337
- 'CompCustomerMissingField' => ::CreationCompMapper,
338
- 'CompSupplierMissingField' => ::CreationCompMapper
339
- }
340
- end
341
-
342
- def self.references
343
- {'CompCustomerMissingField' => ['ref_id'], 'CompSupplierMissingField' => ['ref_id']}
344
- end
345
- end
346
-
347
- class Entities::SubEntities::CompCustomerMissingField < Maestrano::Connector::Rails::SubEntityBase
348
- def self.external?
349
- true
350
- end
351
-
352
- def self.entity_name
353
- 'CompCustomerMissingField'
354
- end
355
-
356
- def self.creation_mapper_classes
357
- {'CompOrganizationMissingField' => ::CreationCompMapper}
358
- end
359
-
360
- def self.id_from_external_entity_hash(entity)
361
- entity['id']
362
- end
363
-
364
- def self.object_name_from_external_entity_hash(entity)
365
- entity['name']
366
- end
367
-
368
- def self.references
369
- {'CompOrganizationMissingField' => ['ref_id']}
370
- end
371
-
372
- def map_to(name, entity, first_time_mapped = nil)
373
- super.merge(is_supplier: false)
374
- end
375
- end
376
-
377
- class Entities::SubEntities::CompSupplierMissingField < Maestrano::Connector::Rails::SubEntityBase
378
- def self.external?
379
- true
380
- end
381
-
382
- def self.entity_name
383
- 'CompSupplierMissingField'
384
- end
385
-
386
- def self.mapper_classes
387
- {'CompOrganizationMissingField' => ::CompMapper}
388
- end
389
-
390
- def self.id_from_external_entity_hash(entity)
391
- entity['id']
392
- end
393
-
394
- def self.object_name_from_external_entity_hash(entity)
395
- entity['name']
396
- end
397
-
398
- def self.references
399
- {'CompOrganizationMissingField' => ['ref_id']}
400
- end
401
-
402
- def map_to(name, entity, first_time_mapped = nil)
403
- super.merge(is_supplier: true)
404
- end
405
- end
406
-
407
- class CreationCompMapper < CompMapper
408
- after_normalize do |input, output|
409
- output[:missing_field] = "Default"
410
- output
411
- end
412
-
413
- after_denormalize do |input, output|
414
- output[:missing_field] = "Default"
415
- output
416
- end
417
- end
418
-
419
- let(:mapped_connec_org1) {
420
- {
421
- ref_id: connec_org1_ext_ref_id,
422
- name: connec_org1_name,
423
- missing_field: "Default"
424
- }
425
- }
426
-
427
- let(:mapped_connec_org2) {
428
- {
429
- ref_id: connec_org2_ext_ref_id,
430
- name: connec_org2_name,
431
- id: connec_org2_ext_id,
432
- missing_field: "Default"
433
- }
434
- }
435
-
436
- let(:mapped_ext_customer) {
437
- {
438
- :id => [
439
- {
440
- :id => ext_customer_id,
441
- :provider => provider,
442
- :realm => oauth_uid
443
- }
444
- ],
445
- :ref_id => [
446
- {
447
- :id => ext_ref_id,
448
- :provider => provider,
449
- :realm => oauth_uid
450
- }
451
- ],
452
- :name => ext_customer_name,
453
- :is_supplier => false,
454
- :missing_field => "Default"
455
- }
456
- }
457
-
458
- let(:mapped_ext_supplier) {
459
- {
460
- :id => [
461
- {
462
- :id => ext_supplier_id,
463
- :provider => provider,
464
- :realm => oauth_uid
465
- }
466
- ],
467
- :ref_id => [
468
- {
469
- :id => ext_ref_id,
470
- :provider => provider,
471
- :realm => oauth_uid
472
- }
473
- ],
474
- :name => ext_supplier_name,
475
- :is_supplier => true
476
- }
477
- }
478
-
479
- let!(:supplier_missing_field_idmap) { Entities::SubEntities::CompSupplierMissingField.create_idmap(organization_id: organization.id, external_id: ext_supplier_id, connec_entity: 'comporganizationmissingfield') }
480
-
481
- it 'handles the mapping correctly' do
482
- cust_idmap = Entities::SubEntities::CompCustomerMissingField.create_idmap(organization_id: organization.id, external_id: ext_customer_id, connec_entity: 'comporganizationmissingfield')
483
- org1_idmap = Entities::SubEntities::CompOrganizationMissingField.create_idmap(organization_id: organization.id, external_id: connec_org1_ext_id, external_entity: 'compsuppliermissingfield')
484
- org2_idmap = Entities::SubEntities::CompOrganizationMissingField.create_idmap(organization_id: organization.id, external_id: connec_org2_ext_id, external_entity: 'compcustomermissingfield')
485
- allow(Maestrano::Connector::Rails::IdMap).to receive(:create).and_return(org1_idmap, org2_idmap, cust_idmap)
486
- expect_any_instance_of(Entities::CustomerAndSupplierMissingField).to receive(:push_entities_to_external).with({'CompOrganizationMissingField' => {'CompSupplierMissingField' => [{entity: mapped_connec_org1.with_indifferent_access, idmap: org1_idmap, id_refs_only_connec_entity: {}}], 'CompCustomerMissingField' => [{entity: mapped_connec_org2.with_indifferent_access, idmap: org2_idmap, id_refs_only_connec_entity: {}}]}})
487
- expect_any_instance_of(Entities::CustomerAndSupplierMissingField).to receive(:push_entities_to_connec).with({'CompCustomerMissingField' => {'CompOrganizationMissingField' => [{entity: mapped_ext_customer.with_indifferent_access, idmap: cust_idmap}]}, 'CompSupplierMissingField' => {'CompOrganizationMissingField' => [{entity: mapped_ext_supplier.with_indifferent_access, idmap: supplier_missing_field_idmap}]}})
488
- subject_missing_field
489
- end
490
-
491
- context "with idmap and last_push_to_external field present it does not overwrite the missing field" do
492
-
493
- let(:subject_already_pushed_entities) { Maestrano::Connector::Rails::SynchronizationJob.new.sync_entity('customer_and_supplier_missing_field', organization, connec_client, external_client, nil, {}) }
494
-
495
- before do
496
- allow(connec_client).to receive(:get).and_return(ActionDispatch::Response.new(200, {}, {comporganizationmissingfields: connec_orgs_already_pushed}.to_json, {}))
497
- allow_any_instance_of(Entities::SubEntities::CompCustomerMissingField).to receive(:get_external_entities).and_return([ext_customer])
498
- allow_any_instance_of(Entities::SubEntities::CompSupplierMissingField).to receive(:get_external_entities).and_return([ext_supplier])
499
- end
500
-
501
- let(:connec_org1_already_pushed) { connec_org1.merge!(updated_at: "2015-08-10T14:42:38Z", 'id' => connec_org1['id'].push({'provider' => provider, 'id' => connec_org1_ext_id, 'realm' => oauth_uid})) }
502
- let(:connec_org2_already_pushed) { connec_org2.merge!(updated_at: "2015-08-10T14:42:38Z", 'id' => connec_org2['id'].push({'provider' => provider, 'id' => connec_org2_ext_id, 'realm' => oauth_uid})) }
503
-
504
- let(:connec_orgs_already_pushed) { [connec_org1_already_pushed, connec_org2_already_pushed] }
505
-
506
- let(:mapped_connec_org1) {
507
- {
508
- id: connec_org1_ext_id,
509
- ref_id: connec_org1_ext_ref_id,
510
- name: connec_org1_name
511
- }
512
- }
513
-
514
- let(:mapped_connec_org2) {
515
- {
516
- id: connec_org2_ext_id,
517
- ref_id: connec_org2_ext_ref_id,
518
- name: connec_org2_name
519
-
520
- }
521
- }
522
-
523
- let(:mapped_ext_customer) {
524
- {
525
- :id => [
526
- {
527
- :id => ext_customer_id,
528
- :provider => provider,
529
- :realm => oauth_uid
530
- }
531
- ],
532
- :ref_id => [
533
- {
534
- :id => ext_ref_id,
535
- :provider => provider,
536
- :realm => oauth_uid
537
- }
538
- ],
539
- :name => ext_customer_name,
540
- :is_supplier => false,
541
- :missing_field => "Default"
542
- }
543
- }
544
-
545
- let(:mapped_ext_supplier) {
546
- {
547
- :id => [
548
- {
549
- :id => ext_supplier_id,
550
- :provider => provider,
551
- :realm => oauth_uid
552
- }
553
- ],
554
- :ref_id => [
555
- {
556
- :id => ext_ref_id,
557
- :provider => provider,
558
- :realm => oauth_uid
559
- }
560
- ],
561
- :name => ext_supplier_name,
562
- :is_supplier => true
563
- }
564
- }
565
-
566
- let!(:supplier_missing_field_idmap) { Entities::SubEntities::CompSupplierMissingField.create_idmap(organization_id: organization.id, external_id: ext_supplier_id, connec_entity: 'comporganizationmissingfield', last_push_to_external: "2014-08-10T14:42:38Z") }
567
- let!(:customer_missing_field_idmap) { Entities::SubEntities::CompCustomerMissingField.create_idmap(organization_id: organization.id, external_id: ext_customer_id, connec_entity: 'comporganizationmissingfield', last_push_to_external: "2014-08-10T14:42:38Z") }
568
- let!(:org1_missing_field_idmap) { Entities::SubEntities::CompOrganizationMissingField.create_idmap(organization_id: organization.id, external_id: connec_org1_ext_id, external_entity: 'compsuppliermissingfield', connec_id: connec_org1_id, name: connec_org1_name, last_push_to_external: "2014-08-10T14:42:38Z") }
569
- let!(:org2_missing_field_idmap) { Entities::SubEntities::CompOrganizationMissingField.create_idmap(organization_id: organization.id, external_id: connec_org2_ext_id, external_entity: 'compcustomermissingfield', connec_id: connec_org2_id, name: connec_org2_name, last_push_to_external: "2014-08-10T14:42:38Z") }
570
- #update idmap with last_push_to_external and check that the :missing_field is not updated
571
- it 'does not overwrite the default field in the subsequent mapping' do
572
- expect_any_instance_of(Entities::CustomerAndSupplierMissingField).to receive(:push_entities_to_external).with({'CompOrganizationMissingField' => {'CompSupplierMissingField' => [{entity: mapped_connec_org1.with_indifferent_access, idmap: org1_missing_field_idmap, id_refs_only_connec_entity: {}}], 'CompCustomerMissingField' => [{entity: mapped_connec_org2.with_indifferent_access, idmap: org2_missing_field_idmap, id_refs_only_connec_entity: {}}]}})
573
- expect_any_instance_of(Entities::CustomerAndSupplierMissingField).to receive(:push_entities_to_connec).with({'CompCustomerMissingField' => {'CompOrganizationMissingField' => [{entity: mapped_ext_customer.with_indifferent_access, idmap: customer_missing_field_idmap}]}, 'CompSupplierMissingField' => {'CompOrganizationMissingField' => [{entity: mapped_ext_supplier.with_indifferent_access, idmap: supplier_missing_field_idmap}]}})
574
- subject_already_pushed_entities
575
- end
576
- end
577
- end
578
- end