account-lookup-service 15.5.0-iso.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (209) hide show
  1. package/.circleci/config.yml +11 -0
  2. package/.ncurc.yaml +6 -0
  3. package/.nvmrc +1 -0
  4. package/.nycrc.yml +20 -0
  5. package/.versionrc +15 -0
  6. package/CHANGELOG.md +330 -0
  7. package/CODEOWNERS +38 -0
  8. package/Dockerfile +45 -0
  9. package/LICENSE.md +10 -0
  10. package/README.md +252 -0
  11. package/audit-ci.jsonc +32 -0
  12. package/audit-resolve.json +161 -0
  13. package/config/default.json +109 -0
  14. package/config/knexfile.js +21 -0
  15. package/docker/account-lookup-service/default.json +106 -0
  16. package/docker/account-lookup-service/make-default-json.sh +5 -0
  17. package/docker/account-lookup-service/override.json +15 -0
  18. package/docker/central-ledger/default.json +458 -0
  19. package/docker/config-modifier/account-lookup-service.js +31 -0
  20. package/docker/kafka/consumer.properties +26 -0
  21. package/docker/kafka/producer.properties +45 -0
  22. package/docker/kafka/server.properties +143 -0
  23. package/docker/kafka/tools-log4j.properties +21 -0
  24. package/docker/mock-proxy/Dockerfile +15 -0
  25. package/docker/mock-proxy/package-lock.json +4986 -0
  26. package/docker/mock-proxy/package.json +24 -0
  27. package/docker/mock-proxy/src/config.ts +14 -0
  28. package/docker/mock-proxy/src/server.ts +94 -0
  29. package/docker/mock-proxy/src/utils.ts +29 -0
  30. package/docker/mock-proxy/tsconfig.json +24 -0
  31. package/docker/sql-init/01_permissions.sql +2 -0
  32. package/docker/sql-init-central-ledger/01_permissions.sql +2 -0
  33. package/docker/wait-for/wait-for-account-lookup-service.sh +10 -0
  34. package/docker/wait-for/wait-for-central-ledger.sh +11 -0
  35. package/docker/wait-for/wait-for-kafka.sh +7 -0
  36. package/docker/wait-for/wait-for-ml-api-adapter.sh +9 -0
  37. package/docker/wait-for/wait-for-mockserver.sh +20 -0
  38. package/docker/wait-for/wait-for-mysql-als.sh +14 -0
  39. package/docker/wait-for/wait-for-mysql-central-ledger.sh +11 -0
  40. package/docker/wait-for/wait-for-mysql.sh +11 -0
  41. package/docker/wait-for/wait-for-objstore.sh +12 -0
  42. package/docker/wait-for/wait-for.env +18 -0
  43. package/docker/wait-for/wait-for.sh +81 -0
  44. package/docker-compose.integration.yml +29 -0
  45. package/docker-compose.yml +243 -0
  46. package/jest-int.config.js +8 -0
  47. package/jest.config.js +16 -0
  48. package/jsdoc.json +38 -0
  49. package/migrations/01_currency.js +42 -0
  50. package/migrations/02_endpointType.js +43 -0
  51. package/migrations/03_endpointType-indexes.js +37 -0
  52. package/migrations/04_partyIdType.js +43 -0
  53. package/migrations/05_partyIdType-indexes.js +38 -0
  54. package/migrations/08_oracleEndpoint.js +51 -0
  55. package/migrations/09_oracleEndpoint-indexes.js +41 -0
  56. package/migrations/10_oracleEndpoint-remove-constraints.js +38 -0
  57. package/package.json +180 -0
  58. package/scripts/_wait4_all.js +143 -0
  59. package/scripts/test-functional.sh +76 -0
  60. package/secrets/jwsSigningKey.key +27 -0
  61. package/seeds/currency.js +765 -0
  62. package/seeds/endpointType.js +65 -0
  63. package/seeds/partyIdType.js +79 -0
  64. package/src/api/endpointcache.js +67 -0
  65. package/src/api/health.js +66 -0
  66. package/src/api/index.js +85 -0
  67. package/src/api/oracles/{ID}.js +100 -0
  68. package/src/api/oracles.js +96 -0
  69. package/src/api/participants/{ID}/error.js +44 -0
  70. package/src/api/participants/{ID}.js +44 -0
  71. package/src/api/participants/{Type}/{ID}/error.js +74 -0
  72. package/src/api/participants/{Type}/{ID}/{SubId}/error.js +68 -0
  73. package/src/api/participants/{Type}/{ID}/{SubId}.js +113 -0
  74. package/src/api/participants/{Type}/{ID}.js +133 -0
  75. package/src/api/participants.js +63 -0
  76. package/src/api/parties/{Type}/{ID}/error.js +66 -0
  77. package/src/api/parties/{Type}/{ID}/{SubId}/error.js +56 -0
  78. package/src/api/parties/{Type}/{ID}/{SubId}.js +77 -0
  79. package/src/api/parties/{Type}/{ID}.js +98 -0
  80. package/src/api/routes.js +294 -0
  81. package/src/constants.js +16 -0
  82. package/src/domain/oracle/index.js +33 -0
  83. package/src/domain/oracle/oracle.js +234 -0
  84. package/src/domain/participants/index.js +35 -0
  85. package/src/domain/participants/participants.js +560 -0
  86. package/src/domain/parties/getPartiesByTypeAndID.js +239 -0
  87. package/src/domain/parties/index.js +32 -0
  88. package/src/domain/parties/parties.js +215 -0
  89. package/src/domain/parties/utils.js +84 -0
  90. package/src/domain/timeout/dto.js +48 -0
  91. package/src/domain/timeout/index.js +104 -0
  92. package/src/handlers/TimeoutHandler.js +94 -0
  93. package/src/handlers/index.js +70 -0
  94. package/src/handlers/monitoring/index.js +51 -0
  95. package/src/handlers/monitoring/plugins/health.js +61 -0
  96. package/src/handlers/monitoring/plugins/metrics.js +48 -0
  97. package/src/handlers/register.js +102 -0
  98. package/src/index.js +66 -0
  99. package/src/interface/admin-swagger.yaml +804 -0
  100. package/src/interface/admin_swagger.json +959 -0
  101. package/src/interface/api-swagger-iso20022-parties.yaml +1734 -0
  102. package/src/interface/api-swagger.yaml +1733 -0
  103. package/src/interface/api_swagger.json +3046 -0
  104. package/src/interface/fspiop-rest-v2.0-ISO20022_parties.yaml +2256 -0
  105. package/src/interface/thirdparty/admin-swagger.yaml +808 -0
  106. package/src/interface/thirdparty/admin_swagger.json +961 -0
  107. package/src/interface/thirdparty/api-swagger.yaml +1739 -0
  108. package/src/interface/thirdparty/api_swagger.json +3142 -0
  109. package/src/lib/argv.js +39 -0
  110. package/src/lib/cache.js +126 -0
  111. package/src/lib/config.js +183 -0
  112. package/src/lib/db.js +26 -0
  113. package/src/lib/headers.js +53 -0
  114. package/src/lib/healthCheck/subServiceHealth.js +84 -0
  115. package/src/lib/index.js +11 -0
  116. package/src/lib/migrator.js +17 -0
  117. package/src/lib/requestLogger.js +54 -0
  118. package/src/lib/util.js +66 -0
  119. package/src/metrics/handler.js +33 -0
  120. package/src/metrics/plugin.js +52 -0
  121. package/src/metrics/routes.js +43 -0
  122. package/src/models/currency/currency.js +48 -0
  123. package/src/models/currency/index.js +32 -0
  124. package/src/models/endpointType/endpointType.js +48 -0
  125. package/src/models/endpointType/index.js +32 -0
  126. package/src/models/misc/migrationLock.js +49 -0
  127. package/src/models/oracle/facade.js +341 -0
  128. package/src/models/oracle/index.js +41 -0
  129. package/src/models/oracle/oracleEndpoint.js +192 -0
  130. package/src/models/oracle/oracleEndpointCached.js +108 -0
  131. package/src/models/participantEndpoint/facade.js +238 -0
  132. package/src/models/partyIdType/index.js +32 -0
  133. package/src/models/partyIdType/partyIdType.js +41 -0
  134. package/src/plugins.js +139 -0
  135. package/src/server.js +199 -0
  136. package/test/fixtures/index.js +131 -0
  137. package/test/fixtures/iso.js +110 -0
  138. package/test/integration/.env +8 -0
  139. package/test/integration/api/parties.test.js +137 -0
  140. package/test/integration/constants.js +20 -0
  141. package/test/integration/domain/oracle/index.test.js +324 -0
  142. package/test/integration/domain/timeout/index.test.js +75 -0
  143. package/test/integration/env.sh +15 -0
  144. package/test/integration/example.test.js +12 -0
  145. package/test/integration/models/currency/currency.test.js +68 -0
  146. package/test/integration/plugins.test.js +62 -0
  147. package/test/integration/prepareTestParticipants.js +30 -0
  148. package/test/integration/setup.js +5 -0
  149. package/test/integration-config.json +81 -0
  150. package/test/integration-runner.sh +108 -0
  151. package/test/unit/api/health.test.js +142 -0
  152. package/test/unit/api/oracles/{ID}.test.js +264 -0
  153. package/test/unit/api/oracles.test.js +173 -0
  154. package/test/unit/api/participants/participants.test.js +117 -0
  155. package/test/unit/api/participants/{Type}/{ID}/error.test.js +155 -0
  156. package/test/unit/api/participants/{Type}/{ID}/{SubId}/error.test.js +131 -0
  157. package/test/unit/api/participants/{Type}/{ID}/{SubId}.test.js +377 -0
  158. package/test/unit/api/participants/{Type}/{ID}.test.js +383 -0
  159. package/test/unit/api/participants.test.js +108 -0
  160. package/test/unit/api/parties/endpointcache.test.js +83 -0
  161. package/test/unit/api/parties/parties.test.js +102 -0
  162. package/test/unit/api/parties/{Type}/{ID}/error.test.js +145 -0
  163. package/test/unit/api/parties/{Type}/{ID}/{SubId}/error.test.js +141 -0
  164. package/test/unit/api/parties/{Type}/{ID}/{SubId}.test.js +241 -0
  165. package/test/unit/api/parties/{Type}/{ID}.test.js +240 -0
  166. package/test/unit/domain/oracle/oracle.test.js +505 -0
  167. package/test/unit/domain/participants/participants.test.js +1724 -0
  168. package/test/unit/domain/parties/parties.test.js +940 -0
  169. package/test/unit/domain/timeout/dto.test.js +28 -0
  170. package/test/unit/domain/timeout/index.test.js +81 -0
  171. package/test/unit/handlers/TimeoutHandler.test.js +125 -0
  172. package/test/unit/handlers/index.test.js +56 -0
  173. package/test/unit/handlers/register.test.js +90 -0
  174. package/test/unit/index.test.js +139 -0
  175. package/test/unit/iso20022/partiesValidation.test.js +129 -0
  176. package/test/unit/lib/TransformFacades.test.js +18 -0
  177. package/test/unit/lib/argv.test.js +40 -0
  178. package/test/unit/lib/cache.test.js +172 -0
  179. package/test/unit/lib/config.test.js +108 -0
  180. package/test/unit/lib/healthCheck/subServiceHealth.test.js +89 -0
  181. package/test/unit/lib/migrator.test.js +52 -0
  182. package/test/unit/lib/requestLogger.test.js +115 -0
  183. package/test/unit/lib/util.test.js +68 -0
  184. package/test/unit/mocks.js +66 -0
  185. package/test/unit/models/currency/currency.test.js +91 -0
  186. package/test/unit/models/endpointType/endpointType.test.js +69 -0
  187. package/test/unit/models/misc/migrationLock.test.js +96 -0
  188. package/test/unit/models/oracle/facade.test.js +546 -0
  189. package/test/unit/models/oracle/oracleEndpoint.test.js +409 -0
  190. package/test/unit/models/oracle/oracleEndpointCached.test.js +153 -0
  191. package/test/unit/models/participantEndpoint/facade.test.js +295 -0
  192. package/test/unit/models/partyIdType/partyIdType.test.js +88 -0
  193. package/test/unit/plugins.test.js +89 -0
  194. package/test/unit/setup.js +7 -0
  195. package/test/util/apiClients/AlsApiClient.js +44 -0
  196. package/test/util/apiClients/BasicApiClient.js +34 -0
  197. package/test/util/apiClients/ProxyApiClient.js +25 -0
  198. package/test/util/apiClients/index.js +7 -0
  199. package/test/util/helper.js +332 -0
  200. package/test/util/index.js +11 -0
  201. package/test/util/mockgen.js +43 -0
  202. package/test/util/onboarding.js +132 -0
  203. package/test/util/scripts/addAlsDb.sh +33 -0
  204. package/test/util/scripts/configureMockServer.sh +35 -0
  205. package/test/util/scripts/env.sh +19 -0
  206. package/test/util/scripts/populateTestData.sh +62 -0
  207. package/test/util/scripts/startMockCentralServer.sh +45 -0
  208. package/test/util/scripts/startMockOracleServer.sh +45 -0
  209. package/test/util/testConfig.js +44 -0
@@ -0,0 +1,409 @@
1
+ /*****
2
+ License
3
+ --------------
4
+ Copyright © 2017 Bill & Melinda Gates Foundation
5
+ The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
6
+ http://www.apache.org/licenses/LICENSE-2.0
7
+ Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
8
+ Contributors
9
+ --------------
10
+ This is the official list of the Mojaloop project contributors for this file.
11
+ Names of the original copyright holders (individuals or organizations)
12
+ should be listed with a '*' in the first column. People who have
13
+ contributed from an organization can be listed under the organization
14
+ that actually holds the copyright for their contributions (see the
15
+ Gates Foundation organization for an example). Those individuals should have
16
+ their names indented and be marked with a '-'. Email address can be added
17
+ optionally within square brackets <email>.
18
+ * Gates Foundation
19
+ - Name Surname <name.surname@gatesfoundation.com>
20
+
21
+ * Crosslake
22
+ - Lewis Daly <lewisd@crosslaketech.com>
23
+
24
+ --------------
25
+ ******/
26
+
27
+ 'use strict'
28
+
29
+ const Sinon = require('sinon')
30
+
31
+ const Db = require('../../../../src/lib/db')
32
+ const oracleEndpoint = require('../../../../src/models/oracle/oracleEndpoint')
33
+ const Logger = require('@mojaloop/central-services-logger')
34
+
35
+ Logger.isDebugEnabled = jest.fn(() => true)
36
+ Logger.isErrorEnabled = jest.fn(() => true)
37
+ Logger.isInfoEnabled = jest.fn(() => true)
38
+ let sandbox
39
+
40
+ const getOracleDatabaseResponse = [{
41
+ oracleEndpointId: 1,
42
+ endpointType: 'URL',
43
+ value: 'http://localhost:8444',
44
+ idType: 'MSISDN',
45
+ currency: 'USD',
46
+ isDefault: true
47
+ }]
48
+
49
+ const createOracleModel = {
50
+ oracleEndpointId: 1,
51
+ endpointType: 'URL',
52
+ value: 'http://localhost:8444',
53
+ idType: 'MSISDN',
54
+ currency: 'USD'
55
+ }
56
+
57
+ describe('oracleEndpoint', () => {
58
+ beforeEach(() => {
59
+ sandbox = Sinon.createSandbox()
60
+ sandbox.stub(Db, 'connect').returns(Promise.resolve({}))
61
+ })
62
+
63
+ afterEach(() => {
64
+ sandbox.restore()
65
+ })
66
+
67
+ describe('getOracleEndpointByType', () => {
68
+ let queryStub
69
+
70
+ beforeEach(() => {
71
+ queryStub = sandbox.stub()
72
+ Db.oracleEndpoint = {
73
+ query: queryStub
74
+ }
75
+ Db.from = (table) => {
76
+ return Db[table]
77
+ }
78
+ })
79
+
80
+ it('gets an oracleEndpoint by type', async () => {
81
+ // Arrange
82
+ queryStub.resolves(getOracleDatabaseResponse)
83
+
84
+ // Act
85
+ const result = await oracleEndpoint.getOracleEndpointByType('URL')
86
+
87
+ // Assert
88
+ expect(queryStub.calledOnce).toBe(true)
89
+ expect(result).toStrictEqual(getOracleDatabaseResponse)
90
+ })
91
+
92
+ it('gets an oracleEndpoint by type with builder', async () => {
93
+ // Arrange
94
+ const builderStub = sandbox.stub()
95
+ builderStub.innerJoin = sandbox.stub().returns({
96
+ innerJoin: sandbox.stub().returns({
97
+ where: sandbox.stub().returns({
98
+ select: sandbox.stub().resolves(getOracleDatabaseResponse)
99
+ })
100
+ })
101
+ })
102
+ Db.oracleEndpoint.query.callsArgWith(0, builderStub)
103
+
104
+ // Act
105
+ const result = await oracleEndpoint.getOracleEndpointByType('URL')
106
+
107
+ // Assert
108
+ expect(result).toStrictEqual(getOracleDatabaseResponse)
109
+ })
110
+
111
+ it('fails to get an oracleEndpoint', async () => {
112
+ // Arrange
113
+ queryStub.throws(new Error('failed to get oracleEndpoint'))
114
+
115
+ // Act
116
+ const action = async () => oracleEndpoint.getOracleEndpointByType('123')
117
+
118
+ // Assert
119
+ await expect(action()).rejects.toThrow()
120
+ })
121
+ })
122
+
123
+ describe('getOracleEndpointByTypeAndCurrency', () => {
124
+ let queryStub
125
+
126
+ beforeEach(() => {
127
+ queryStub = sandbox.stub()
128
+ Db.oracleEndpoint = {
129
+ query: queryStub
130
+ }
131
+ })
132
+
133
+ it('gets an oracleEndpoint by type and currency', async () => {
134
+ // Arrange
135
+ const builderStub = sandbox.stub()
136
+ builderStub.innerJoin = sandbox.stub().returns({
137
+ innerJoin: sandbox.stub().returns({
138
+ innerJoin: sandbox.stub().returns({
139
+ where: sandbox.stub().returns({
140
+ select: sandbox.stub().resolves(getOracleDatabaseResponse)
141
+ })
142
+ })
143
+ })
144
+ })
145
+ Db.oracleEndpoint.query.callsArgWith(0, builderStub)
146
+
147
+ // Act
148
+ const result = await oracleEndpoint.getOracleEndpointByTypeAndCurrency('URL', 'USD')
149
+
150
+ // Assert
151
+ expect(queryStub.calledOnce).toBe(true)
152
+ expect(result).toStrictEqual(getOracleDatabaseResponse)
153
+ })
154
+
155
+ it('fails to get an oracleEndpoint by type and currency', async () => {
156
+ // Arrange
157
+ queryStub.throws(new Error('failed to get oracleEndpoint'))
158
+
159
+ // Act
160
+ const action = async () => oracleEndpoint.getOracleEndpointByTypeAndCurrency('URL', 'USD')
161
+
162
+ // Assert
163
+ await expect(action()).rejects.toThrow()
164
+ })
165
+ })
166
+
167
+ describe('getOracleEndpointByCurrency', () => {
168
+ let queryStub
169
+
170
+ beforeEach(() => {
171
+ queryStub = sandbox.stub()
172
+ Db.oracleEndpoint = {
173
+ query: queryStub
174
+ }
175
+ })
176
+
177
+ it('gets an oracleEndpoint by currency', async () => {
178
+ // Arrange
179
+ const builderStub = sandbox.stub()
180
+ builderStub.innerJoin = sandbox.stub().returns({
181
+ innerJoin: sandbox.stub().returns({
182
+ innerJoin: sandbox.stub().returns({
183
+ where: sandbox.stub().returns({
184
+ select: sandbox.stub().resolves(getOracleDatabaseResponse)
185
+ })
186
+ })
187
+ })
188
+ })
189
+ Db.oracleEndpoint.query.callsArgWith(0, builderStub)
190
+
191
+ // Act
192
+ const result = await oracleEndpoint.getOracleEndpointByCurrency('USD')
193
+
194
+ // Assert
195
+ expect(queryStub.calledOnce).toBe(true)
196
+ expect(result).toStrictEqual(getOracleDatabaseResponse)
197
+ })
198
+
199
+ it('fails to get an oracleEndpoint by currency', async () => {
200
+ // Arrange
201
+ queryStub.throws(new Error('failed to get oracleEndpoint'))
202
+
203
+ // Act
204
+ const action = async () => oracleEndpoint.getOracleEndpointByCurrency('USD')
205
+
206
+ // Assert
207
+ await expect(action()).rejects.toThrow()
208
+ })
209
+ })
210
+
211
+ describe('getOracleEndpointById', () => {
212
+ let queryStub
213
+
214
+ beforeEach(() => {
215
+ queryStub = sandbox.stub()
216
+ Db.oracleEndpoint = {
217
+ query: queryStub
218
+ }
219
+ })
220
+
221
+ it('gets an oracleEndpoint by Id', async () => {
222
+ // Arrange
223
+ const builderStub = sandbox.stub()
224
+ builderStub.innerJoin = sandbox.stub().returns({
225
+ innerJoin: sandbox.stub().returns({
226
+ innerJoin: sandbox.stub().returns({
227
+ where: sandbox.stub().returns({
228
+ select: sandbox.stub().resolves(getOracleDatabaseResponse)
229
+ })
230
+ })
231
+ })
232
+ })
233
+ Db.oracleEndpoint.query.callsArgWith(0, builderStub)
234
+
235
+ // Act
236
+ const result = await oracleEndpoint.getOracleEndpointById('1')
237
+
238
+ // Assert
239
+ expect(queryStub.calledOnce).toBe(true)
240
+ expect(result).toStrictEqual(getOracleDatabaseResponse)
241
+ })
242
+
243
+ it('fails to get an oracleEndpoint by Id', async () => {
244
+ // Arrange
245
+ queryStub.throws(new Error('failed to get oracleEndpoint'))
246
+
247
+ // Act
248
+ const action = async () => oracleEndpoint.getOracleEndpointById('1')
249
+
250
+ // Assert
251
+ await expect(action()).rejects.toThrow()
252
+ })
253
+ })
254
+
255
+ describe('getAllOracleEndpoint', () => {
256
+ let queryStub
257
+
258
+ beforeEach(() => {
259
+ queryStub = sandbox.stub()
260
+ Db.oracleEndpoint = {
261
+ query: queryStub
262
+ }
263
+ })
264
+
265
+ it('gets all oracle endpoints', async () => {
266
+ // Arrange
267
+ const builderStub = sandbox.stub()
268
+ builderStub.innerJoin = sandbox.stub().returns({
269
+ innerJoin: sandbox.stub().returns({
270
+ where: sandbox.stub().returns({
271
+ select: sandbox.stub().resolves(getOracleDatabaseResponse)
272
+ })
273
+ })
274
+ })
275
+ Db.oracleEndpoint.query.callsArgWith(0, builderStub)
276
+
277
+ // Act
278
+ const result = await oracleEndpoint.getAllOracleEndpoint('1')
279
+
280
+ // Assert
281
+ expect(queryStub.calledOnce).toBe(true)
282
+ expect(result).toStrictEqual(getOracleDatabaseResponse)
283
+ })
284
+ })
285
+
286
+ describe('createOracleEndpoint', () => {
287
+ let insertStub
288
+
289
+ beforeEach(() => {
290
+ insertStub = sandbox.stub()
291
+ Db.oracleEndpoint = {
292
+ insert: insertStub
293
+ }
294
+ })
295
+
296
+ it('creates an oracleEndpoint by Id', async () => {
297
+ // Arrange
298
+ insertStub.resolves(true)
299
+
300
+ // Act
301
+ const result = await oracleEndpoint.createOracleEndpoint(createOracleModel)
302
+
303
+ // Assert
304
+ expect(insertStub.calledOnce).toBe(true)
305
+ expect(result).toBe(true)
306
+ })
307
+
308
+ it('fails to create an oracleEndpoint by Id', async () => {
309
+ // Arrange
310
+ insertStub.throws(new Error('failed to create oracle endpoint'))
311
+
312
+ // Act
313
+ const action = async () => oracleEndpoint.createOracleEndpoint(createOracleModel)
314
+
315
+ // Assert
316
+ await expect(action()).rejects.toThrow()
317
+ })
318
+ })
319
+
320
+ describe('updateOracleEndpointById', () => {
321
+ let updateStub
322
+
323
+ beforeEach(() => {
324
+ updateStub = sandbox.stub()
325
+ Db.oracleEndpoint = {
326
+ update: updateStub
327
+ }
328
+ })
329
+
330
+ it('fails to update an oracleEndpoint by Id', async () => {
331
+ // Arrange
332
+ updateStub.throws(new Error('failed to create oracle endpoint'))
333
+
334
+ // Act
335
+ const action = async () => oracleEndpoint.updateOracleEndpointById(createOracleModel)
336
+
337
+ // Assert
338
+ await expect(action()).rejects.toThrow()
339
+ })
340
+ })
341
+
342
+ describe('setIsActiveOracleEndpoint', () => {
343
+ let updateStub
344
+
345
+ beforeEach(() => {
346
+ updateStub = sandbox.stub()
347
+ Db.oracleEndpoint = {
348
+ update: updateStub
349
+ }
350
+ })
351
+
352
+ it('sets the active oracleEndpoint', async () => {
353
+ // Arrange
354
+ updateStub.resolves(true)
355
+
356
+ // Act
357
+ const result = await oracleEndpoint.setIsActiveOracleEndpoint('USD', true)
358
+
359
+ // Assert
360
+ expect(updateStub.calledOnce).toBe(true)
361
+ expect(result).toBe(true)
362
+ })
363
+
364
+ it('fails to set the active oracleEndpoint', async () => {
365
+ // Arrange
366
+ updateStub.throws(new Error('failed to set active oracle endpoint'))
367
+
368
+ // Act
369
+ const action = async () => oracleEndpoint.setIsActiveOracleEndpoint('USD', true)
370
+
371
+ // Assert
372
+ await expect(action()).rejects.toThrow()
373
+ })
374
+ })
375
+
376
+ describe('destroyOracleEndpointById', () => {
377
+ let updateStub
378
+
379
+ beforeEach(() => {
380
+ updateStub = sandbox.stub()
381
+ Db.oracleEndpoint = {
382
+ update: updateStub
383
+ }
384
+ })
385
+
386
+ it('destroys the oracleEndpoint by Id', async () => {
387
+ // Arrange
388
+ updateStub.resolves(true)
389
+
390
+ // Act
391
+ const result = await oracleEndpoint.destroyOracleEndpointById('1')
392
+
393
+ // Assert
394
+ expect(updateStub.calledOnce).toBe(true)
395
+ expect(result).toBe(true)
396
+ })
397
+
398
+ it('fails to destroy the oracleEndpoint', async () => {
399
+ // Arrange
400
+ updateStub.throws(new Error('failed to set active oracle endpoint'))
401
+
402
+ // Act
403
+ const action = async () => oracleEndpoint.destroyOracleEndpointById('1')
404
+
405
+ // Assert
406
+ await expect(action()).rejects.toThrow()
407
+ })
408
+ })
409
+ })
@@ -0,0 +1,153 @@
1
+ /*****
2
+ License
3
+ --------------
4
+ Copyright © 2017 Bill & Melinda Gates Foundation
5
+ The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
6
+ http://www.apache.org/licenses/LICENSE-2.0
7
+ Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
8
+ Contributors
9
+ --------------
10
+ This is the official list of the Mojaloop project contributors for this file.
11
+ Names of the original copyright holders (individuals or organizations)
12
+ should be listed with a '*' in the first column. People who have
13
+ contributed from an organization can be listed under the organization
14
+ that actually holds the copyright for their contributions (see the
15
+ Gates Foundation organization for an example). Those individuals should have
16
+ their names indented and be marked with a '-'. Email address can be added
17
+ optionally within square brackets <email>.
18
+ * Gates Foundation
19
+ * Name Surname <name.surname@gatesfoundation.com>
20
+
21
+ * Kevin Leyow <kevin.leyow@infitx.com>
22
+
23
+ --------------
24
+ ******/
25
+
26
+ 'use strict'
27
+
28
+ const Sinon = require('sinon')
29
+ const OracleEndpointUncached = require('../../../../src/models/oracle/oracleEndpoint')
30
+ const Cache = require('../../../../src/lib/cache')
31
+ const Model = require('../../../../src/models/oracle/oracleEndpointCached')
32
+ const Db = require('../../../../src/lib/db')
33
+ const Logger = require('@mojaloop/central-services-logger')
34
+
35
+ Logger.isDebugEnabled = jest.fn(() => true)
36
+ Logger.isErrorEnabled = jest.fn(() => true)
37
+ Logger.isInfoEnabled = jest.fn(() => true)
38
+ let sandbox
39
+
40
+ describe('ParticipantCurrency cached model', () => {
41
+ const oracleEndpoints = [
42
+ {
43
+ oracleEndpointId: 1,
44
+ partyIdType: 1, // MSISDN
45
+ endpointTypeId: 1, // URL
46
+ currencyId: 'USD',
47
+ value: 'http://endpoint',
48
+ isDefault: 1,
49
+ isActive: 1,
50
+ createdDate: '2023-11-16 14:57:55',
51
+ createdBy: 'Admin'
52
+ }
53
+ ]
54
+ beforeEach(() => {
55
+ sandbox = Sinon.createSandbox()
56
+ sandbox.stub(Cache)
57
+ sandbox.stub(Db, 'connect').returns(Promise.resolve({}))
58
+ sandbox.stub(OracleEndpointUncached, 'getOracleEndpointByTypeAndCurrency').returns(oracleEndpoints)
59
+ sandbox.stub(OracleEndpointUncached, 'getOracleEndpointByCurrency').returns(oracleEndpoints)
60
+ sandbox.stub(OracleEndpointUncached, 'getOracleEndpointByType').returns(oracleEndpoints)
61
+ })
62
+
63
+ afterEach(() => {
64
+ sandbox.restore()
65
+ })
66
+
67
+ it('initializes cache correctly', async () => {
68
+ const cacheClient = {
69
+ createKey: sandbox.stub().returns({})
70
+ }
71
+ Cache.registerCacheClient.returns(cacheClient)
72
+
73
+ // initialize calls registerCacheClient and createKey
74
+ expect(Cache.registerCacheClient.calledOnce).toBeFalsy()
75
+ expect(cacheClient.createKey.calledOnce).toBeFalsy()
76
+ await Model.initialize()
77
+ expect(Cache.registerCacheClient.calledOnce).toBeTruthy()
78
+ })
79
+
80
+ it('getOracleEndpointByTypeAndCurrency calls correct uncached function', async () => {
81
+ let cache = null
82
+ const cacheClient = {
83
+ createKey: sandbox.stub().returns({}),
84
+ get: () => cache,
85
+ set: (key, x) => {
86
+ cache = { item: x } // the cache returns {item: <data>} structure
87
+ }
88
+ }
89
+ Cache.registerCacheClient.returns(cacheClient)
90
+ await Model.initialize()
91
+
92
+ await Model.getOracleEndpointByTypeAndCurrency('MSISDN', 'USD')
93
+ expect(OracleEndpointUncached.getOracleEndpointByTypeAndCurrency.calledOnce).toBeTruthy()
94
+ })
95
+
96
+ it('getOracleEndpointByType calls correct uncached function', async () => {
97
+ let cache = null
98
+ const cacheClient = {
99
+ createKey: sandbox.stub().returns({}),
100
+ get: () => cache,
101
+ set: (key, x) => {
102
+ cache = { item: x } // the cache returns {item: <data>} structure
103
+ }
104
+ }
105
+ Cache.registerCacheClient.returns(cacheClient)
106
+ await Model.initialize()
107
+ await Model.getOracleEndpointByType('MSISDN')
108
+ expect(OracleEndpointUncached.getOracleEndpointByType.calledOnce).toBeTruthy()
109
+ })
110
+
111
+ it('getOracleEndpointByCurrency calls correct uncached function', async () => {
112
+ let cache = null
113
+ const cacheClient = {
114
+ createKey: sandbox.stub().returns({}),
115
+ get: () => cache,
116
+ set: (key, x) => {
117
+ cache = { item: x } // the cache returns {item: <data>} structure
118
+ }
119
+ }
120
+ Cache.registerCacheClient.returns(cacheClient)
121
+ await Model.initialize()
122
+
123
+ await Model.getOracleEndpointByCurrency('USD')
124
+ expect(OracleEndpointUncached.getOracleEndpointByCurrency.calledOnce).toBeTruthy()
125
+ })
126
+
127
+ it('queries call set on null cache', async () => {
128
+ const cache = null
129
+ const cacheClient = {
130
+ createKey: sandbox.stub().returns({}),
131
+ get: () => cache,
132
+ set: sandbox.stub().returns({})
133
+ }
134
+ Cache.registerCacheClient.returns(cacheClient)
135
+ await Model.initialize()
136
+
137
+ await Model.getOracleEndpointByTypeAndCurrency('MSISDN', 'USD')
138
+ expect(cacheClient.set.called).toBeTruthy()
139
+ })
140
+
141
+ it('queries hit cache when item is found', async () => {
142
+ const cacheClient = {
143
+ createKey: sandbox.stub().returns({}),
144
+ get: sandbox.stub().returns({ item: oracleEndpoints }),
145
+ set: sandbox.stub().returns({})
146
+ }
147
+ Cache.registerCacheClient.returns(cacheClient)
148
+ await Model.initialize()
149
+
150
+ await Model.getOracleEndpointByTypeAndCurrency('MSISDN', 'USD')
151
+ expect(cacheClient.get.called).toBeTruthy()
152
+ })
153
+ })