@mojaloop/central-services-shared 18.4.0 → 18.5.0-snapshot.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mojaloop/central-services-shared",
3
- "version": "18.4.0",
3
+ "version": "18.5.0-snapshot.0",
4
4
  "description": "Shared code for mojaloop central services",
5
5
  "license": "Apache-2.0",
6
6
  "author": "ModusBox",
package/src/enums/http.js CHANGED
@@ -34,10 +34,6 @@
34
34
 
35
35
  const Headers = {
36
36
  FSPIOP: {
37
- SWITCH: {
38
- regex: /^switch$/i,
39
- value: 'switch'
40
- },
41
37
  SOURCE: 'fspiop-source',
42
38
  DESTINATION: 'fspiop-destination',
43
39
  HTTP_METHOD: 'fspiop-http-method',
@@ -127,7 +123,7 @@ const RestMethods = {
127
123
  const HeaderResources = {
128
124
  PARTICIPANTS: 'participants',
129
125
  ORACLE: 'oracle',
130
- SWITCH: 'switch',
126
+ // SWITCH: 'switch', // @note: hub/switch name should now be passed in via service config.
131
127
  TRANSFERS: 'transfers',
132
128
  FX_TRANSFERS: 'fxTransfers',
133
129
  QUOTES: 'quotes',
@@ -41,6 +41,8 @@ const Metrics = require('@mojaloop/central-services-metrics')
41
41
  let client
42
42
  let policy
43
43
  let switchEndpoint
44
+ let hubName
45
+ let hubNameRegex
44
46
 
45
47
  /**
46
48
  * @function fetchEndpoints
@@ -58,10 +60,22 @@ const fetchEndpoints = async (fsp) => {
58
60
  ).startTimer()
59
61
  try {
60
62
  Logger.isDebugEnabled && Logger.debug(`[fsp=${fsp}] ~ participantEndpointCache::fetchEndpoints := Refreshing the cache for FSP: ${fsp}`)
61
- const defaultHeaders = Http.SwitchDefaultHeaders(Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.PARTICIPANTS, Enum.Http.HeaderResources.SWITCH)
63
+ if (!hubName) {
64
+ throw Error('"hubName" is not initialized. Initialize the cache first.')
65
+ }
66
+ if (!hubNameRegex) {
67
+ throw Error('"hubNameRegex" is not initialized. Initialize the cache first.')
68
+ }
69
+ const defaultHeaders = Http.SwitchDefaultHeaders(hubName, Enum.Http.HeaderResources.PARTICIPANTS, hubName)
62
70
  const url = Mustache.render(switchEndpoint + Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET, { fsp })
63
71
  Logger.isDebugEnabled && Logger.debug(`[fsp=${fsp}] ~ participantEndpointCache::fetchEndpoints := URL for FSP: ${url}`)
64
- const response = await request.sendRequest(url, defaultHeaders, Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.SWITCH)
72
+ const response = await request.sendRequest({
73
+ url,
74
+ headers: defaultHeaders,
75
+ source: hubName,
76
+ destination: hubName,
77
+ hubNameRegex
78
+ })
65
79
  Logger.isDebugEnabled && Logger.debug(`[fsp=${fsp}] ~ Model::participantEndpoint::fetchEndpoints := successful with body: ${JSON.stringify(response.data)}`)
66
80
  const endpoints = response.data
67
81
  const endpointMap = {}
@@ -88,11 +102,11 @@ const fetchEndpoints = async (fsp) => {
88
102
  * @function initializeCache
89
103
  *
90
104
  * @description This initializes the cache for endpoints
91
- * @param {object} policyOptions The Endpoint_Cache_Config for the Cache being stored https://hapi.dev/module/catbox/api/?v=12.1.1#policy
92
-
105
+ * @param {object} policyOptions The Endpoint_Cache_Config for the Cache being stored https://hapi.dev/module/catbox/api/?v=12.1.1#policy
106
+ * @param {object} config The config object containing paramters used for the request function
93
107
  * @returns {boolean} Returns true on successful initialization of the cache, throws error on failures
94
108
  */
95
- exports.initializeCache = async (policyOptions) => {
109
+ exports.initializeCache = async (policyOptions, config) => {
96
110
  try {
97
111
  Logger.isDebugEnabled && Logger.debug(`participantEndpointCache::initializeCache::start::clientOptions - ${JSON.stringify(clientOptions)}`)
98
112
  client = new Catbox.Client(CatboxMemory, clientOptions)
@@ -101,6 +115,8 @@ exports.initializeCache = async (policyOptions) => {
101
115
  Logger.isDebugEnabled && Logger.debug(`participantEndpointCache::initializeCache::start::policyOptions - ${JSON.stringify(policyOptions)}`)
102
116
  policy = new Catbox.Policy(policyOptions, client, partition)
103
117
  Logger.isDebugEnabled && Logger.debug('participantEndpointCache::initializeCache::Cache initialized successfully')
118
+ hubName = config.hubName
119
+ hubNameRegex = config.hubNameRegex
104
120
  return true
105
121
  } catch (err) {
106
122
  Logger.isErrorEnabled && Logger.error(`participantEndpointCache::Cache error:: ERROR:'${err}'`)
@@ -153,7 +153,7 @@ const transformHeaders = (headers, config) => {
153
153
  case (ENUM.Headers.FSPIOP.HTTP_METHOD):
154
154
  // Check to see if we find a regex match the source header containing the switch name.
155
155
  // If so we include the signature otherwise we remove it.
156
- if (headers[normalizedKeys[ENUM.Headers.FSPIOP.SOURCE]].match(ENUM.Headers.FSPIOP.SWITCH.regex) === null) {
156
+ if (headers[normalizedKeys[ENUM.Headers.FSPIOP.SOURCE]].match(config.hubNameRegex) === null) {
157
157
  if (config.httpMethod.toLowerCase() === headerValue.toLowerCase()) {
158
158
  // HTTP Methods match, and thus no change is required
159
159
  normalizedHeaders[headerKey] = headerValue
@@ -178,7 +178,7 @@ const transformHeaders = (headers, config) => {
178
178
  normalizedHeaders[headerKey] = config.destinationFsp
179
179
  break
180
180
  case (ENUM.Headers.GENERAL.ACCEPT.value):
181
- if (!ENUM.Headers.FSPIOP.SWITCH.regex.test(config.sourceFsp)) {
181
+ if (!config.hubNameRegex.test(config.sourceFsp)) {
182
182
  normalizedHeaders[headerKey] = headerValue
183
183
  break
184
184
  }
@@ -188,7 +188,7 @@ const transformHeaders = (headers, config) => {
188
188
  normalizedHeaders[headerKey] = `application/vnd.interoperability.${resourceType}+json;version=${acceptVersion}`
189
189
  break
190
190
  case (ENUM.Headers.GENERAL.CONTENT_TYPE.value):
191
- if (!ENUM.Headers.FSPIOP.SWITCH.regex.test(config.sourceFsp)) {
191
+ if (!config.hubNameRegex.test(config.sourceFsp)) {
192
192
  normalizedHeaders[headerKey] = headerValue
193
193
  break
194
194
  }
@@ -202,7 +202,7 @@ const transformHeaders = (headers, config) => {
202
202
  }
203
203
  }
204
204
 
205
- if (normalizedHeaders[normalizedKeys[ENUM.Headers.FSPIOP.SOURCE]].match(ENUM.Headers.FSPIOP.SWITCH.regex) !== null) {
205
+ if (normalizedHeaders[normalizedKeys[ENUM.Headers.FSPIOP.SOURCE]].match(config.hubNameRegex) !== null) {
206
206
  // Check to see if we find a regex match the source header containing the switch name.
207
207
  // If so we remove the signature added by default.
208
208
  delete normalizedHeaders[normalizedKeys[ENUM.Headers.FSPIOP.SIGNATURE]]
@@ -305,7 +305,7 @@ const proceed = async (defaultKafkaConfig, params, opts) => {
305
305
  }
306
306
  if (fromSwitch) {
307
307
  message.value.to = message.value.from
308
- message.value.from = Enum.Http.Headers.FSPIOP.SWITCH.value
308
+ message.value.from = opts.hubName
309
309
  if (message.value.content.headers) {
310
310
  message.value.content.headers[Enum.Http.Headers.FSPIOP.SOURCE] = message.value.from
311
311
  message.value.content.headers[Enum.Http.Headers.FSPIOP.DESTINATION] = message.value.to
@@ -39,10 +39,14 @@ const Metrics = require('@mojaloop/central-services-metrics')
39
39
  let client
40
40
  let policy
41
41
  let switchEndpoint
42
+ let hubName
43
+ let hubNameRegex
42
44
 
43
45
  /**
44
46
  * @function fetchParticipant
45
47
  *
48
+ * @param {string} fsp The fsp id
49
+ * @param {object} options The options for the request function
46
50
  * @description This populates the cache of participants
47
51
  *
48
52
  * @returns {object} participant Returns the object containing the participants
@@ -55,10 +59,22 @@ const fetchParticipant = async (fsp) => {
55
59
  ).startTimer()
56
60
  try {
57
61
  Logger.isDebugEnabled && Logger.debug('participantCache::fetchParticipant := Refreshing participant cache')
58
- const defaultHeaders = Http.SwitchDefaultHeaders(Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.PARTICIPANTS, Enum.Http.HeaderResources.SWITCH)
62
+ if (!hubName) {
63
+ throw Error('"hubName" is not initialized. Initialize the cache first.')
64
+ }
65
+ if (!hubNameRegex) {
66
+ throw Error('"hubNameRegex" is not initialized. Initialize the cache first.')
67
+ }
68
+ const defaultHeaders = Http.SwitchDefaultHeaders(hubName, Enum.Http.HeaderResources.PARTICIPANTS, hubName)
59
69
  const url = Mustache.render(switchEndpoint + Enum.EndPoints.FspEndpointTemplates.PARTICIPANTS_GET, { fsp })
60
70
  Logger.isDebugEnabled && Logger.debug(`participantCache::fetchParticipant := URL: ${url}`)
61
- const response = await request.sendRequest(url, defaultHeaders, Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.SWITCH)
71
+ const response = await request.sendRequest({
72
+ url,
73
+ headers: defaultHeaders,
74
+ source: hubName,
75
+ destination: hubName,
76
+ hubNameRegex
77
+ })
62
78
  const participant = response.data
63
79
  histTimer({ success: true })
64
80
  return participant
@@ -72,11 +88,11 @@ const fetchParticipant = async (fsp) => {
72
88
  * @function initializeCache
73
89
  *
74
90
  * @description This initializes the cache for endpoints
75
- * @param {object} policyOptions The Endpoint_Cache_Config for the Cache being stored https://hapi.dev/module/catbox/api/?v=12.1.1#policy
76
-
91
+ * @param {object} policyOptions The Endpoint_Cache_Config for the Cache being stored https://hapi.dev/module/catbox/api/?v=12.1.1#policy
92
+ * @param {object} config The config object containing paramters used for the request function
77
93
  * @returns {boolean} Returns true on successful initialization of the cache, throws error on failures
78
94
  */
79
- exports.initializeCache = async (policyOptions) => {
95
+ exports.initializeCache = async (policyOptions, config) => {
80
96
  try {
81
97
  Logger.isDebugEnabled && Logger.debug(`participantCache::initializeCache::start::clientOptions - ${JSON.stringify(clientOptions)}`)
82
98
  client = new Catbox.Client(CatboxMemory, clientOptions)
@@ -85,6 +101,8 @@ exports.initializeCache = async (policyOptions) => {
85
101
  Logger.isDebugEnabled && Logger.debug(`participantCache::initializeCache::start::policyOptions - ${JSON.stringify(policyOptions)}`)
86
102
  policy = new Catbox.Policy(policyOptions, client, partition)
87
103
  Logger.isDebugEnabled && Logger.debug('participantCache::initializeCache::Cache initialized successfully')
104
+ hubName = config.hubName
105
+ hubNameRegex = config.hubNameRegex
88
106
  return true
89
107
  } catch (err) {
90
108
  Logger.isErrorEnabled && Logger.error(`participantCache::Cache error:: ERROR:'${err}'`)
@@ -68,7 +68,7 @@ request.defaults.httpAgent.toJSON = () => ({})
68
68
  *@return {Promise<any>} The response for the request being sent or error object with response included
69
69
  */
70
70
 
71
- const sendRequest = async (
71
+ const sendRequest = async ({
72
72
  url,
73
73
  headers,
74
74
  source,
@@ -79,8 +79,9 @@ const sendRequest = async (
79
79
  span = undefined,
80
80
  jwsSigner = undefined,
81
81
  protocolVersions = undefined,
82
- axiosRequestOptionsOverride = {}
83
- ) => {
82
+ axiosRequestOptionsOverride = {},
83
+ hubNameRegex
84
+ }) => {
84
85
  const histTimerEnd = Metrics.getHistogram(
85
86
  'sendRequest',
86
87
  `sending ${method} request to: ${url} from: ${source} to: ${destination}`,
@@ -100,7 +101,8 @@ const sendRequest = async (
100
101
  httpMethod: method,
101
102
  sourceFsp: source,
102
103
  destinationFsp: destination,
103
- protocolVersions
104
+ protocolVersions,
105
+ hubNameRegex
104
106
  })
105
107
  requestOptions = {
106
108
  url,
@@ -36,7 +36,8 @@ const Helper = require('../../util/helper')
36
36
  const headerConfigExample = {
37
37
  httpMethod: 'PUT',
38
38
  sourceFsp: 'switch',
39
- destinationFsp: 'FSPDest'
39
+ destinationFsp: 'FSPDest',
40
+ hubNameRegex: /^Hub$/i
40
41
  }
41
42
 
42
43
  const headerDataInputExample = {
@@ -110,7 +111,8 @@ Test('Transfer Transformer tests', TransformerTest => {
110
111
  const headerConfig = {
111
112
  httpMethod: 'PUT',
112
113
  sourceFsp: 'switch',
113
- destinationFsp: 'FSPDest'
114
+ destinationFsp: 'FSPDest',
115
+ hubNameRegex: /^Hub$/i
114
116
  }
115
117
 
116
118
  const headerData = Util.clone(headerDataInputExample)
@@ -131,12 +133,13 @@ Test('Transfer Transformer tests', TransformerTest => {
131
133
 
132
134
  const headerConfig = {
133
135
  httpMethod: 'PUT',
134
- sourceFsp: 'switch',
136
+ sourceFsp: 'Hub',
135
137
  destinationFsp: 'FSPDest',
136
138
  protocolVersions: {
137
139
  content: '1.1',
138
140
  accept: '1'
139
- }
141
+ },
142
+ hubNameRegex: /^Hub$/i
140
143
  }
141
144
 
142
145
  const headerData = Util.clone(headerDataInputExample)
@@ -13,13 +13,16 @@ const Config = require('../../util/config')
13
13
  const Http = require(`${src}/util`).Http
14
14
  const Enum = require(`${src}`).Enum
15
15
  const Helper = require('../../util/helper')
16
- const FSPIOP_CALLBACK_URL_TRANSFER_PUT = Enum.EndPoints.FspEndpointTypes.FSPIOP_CALLBACK_URL_TRANSFER_PUT
16
+ const FSPIOP_CALLBACK_URL_TRANSFER_PUT =
17
+ Enum.EndPoints.FspEndpointTypes.FSPIOP_CALLBACK_URL_TRANSFER_PUT
17
18
  const Metrics = require('@mojaloop/central-services-metrics')
18
19
 
19
- Test('Cache Test', cacheTest => {
20
+ Test('Cache Test', (cacheTest) => {
20
21
  let sandbox
22
+ const hubName = 'Hub'
23
+ const hubNameRegex = /^Hub$/i
21
24
 
22
- cacheTest.beforeEach(async test => {
25
+ cacheTest.beforeEach(async (test) => {
23
26
  Metrics.setup({
24
27
  INSTRUMENTATION: {
25
28
  METRICS: {
@@ -43,7 +46,7 @@ Test('Cache Test', cacheTest => {
43
46
  test.end()
44
47
  })
45
48
 
46
- cacheTest.afterEach(async test => {
49
+ cacheTest.afterEach(async (test) => {
47
50
  sandbox.restore()
48
51
  test.end()
49
52
  })
@@ -51,45 +54,38 @@ Test('Cache Test', cacheTest => {
51
54
  cacheTest.test('getEndpoint should', async (getEndpointTest) => {
52
55
  getEndpointTest.test('return the endpoint', async (test) => {
53
56
  const fsp = 'fsp'
54
- const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET, { fsp })
57
+ const url = Mustache.render(
58
+ Config.ENDPOINT_SOURCE_URL +
59
+ Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET,
60
+ { fsp }
61
+ )
55
62
  const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
56
- const expected = 'http://localhost:1080/transfers/97b01bd3-b223-415b-b37b-ab5bef9bdbed'
63
+ const expected =
64
+ 'http://localhost:1080/transfers/97b01bd3-b223-415b-b37b-ab5bef9bdbed'
57
65
 
58
- await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG)
59
- request.sendRequest.withArgs(url, Helper.defaultHeaders()).returns(Promise.resolve(Helper.getEndPointsResponse))
60
-
61
- try {
62
- const result = await Cache.getEndpoint(Config.ENDPOINT_SOURCE_URL, fsp, endpointType, { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' })
63
- test.equal(result, expected, 'The results match')
64
-
65
- const result2 = await Cache.getEndpoint(Config.ENDPOINT_SOURCE_URL, fsp, endpointType, { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' }, { path: '/additionalPath' })
66
- test.equal(result2, `${expected}/additionalPath`, 'The results match')
67
-
68
- await Cache.stopCache()
69
- test.end()
70
- } catch (err) {
71
- test.fail('Error thrown', err)
72
- test.end()
73
- }
74
- })
75
-
76
- getEndpointTest.test('return the endpoint if catbox returns decoratedValue object', async (test) => {
77
- const fsp = 'fsp'
78
- const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET, { fsp })
79
- const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
80
- const expected = 'http://localhost:1080/transfers/97b01bd3-b223-415b-b37b-ab5bef9bdbed'
81
-
82
- await Cache.initializeCache({
83
- ...Config.ENDPOINT_CACHE_CONFIG,
84
- getDecoratedValue: true
66
+ await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, {
67
+ hubName, hubNameRegex
85
68
  })
86
- request.sendRequest.withArgs(url, Helper.defaultHeaders()).returns(Promise.resolve(Helper.getEndPointsResponse))
69
+ request.sendRequest
70
+ .withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex })
71
+ .returns(Promise.resolve(Helper.getEndPointsResponse))
87
72
 
88
73
  try {
89
- const result = await Cache.getEndpoint(Config.ENDPOINT_SOURCE_URL, fsp, endpointType, { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' })
74
+ const result = await Cache.getEndpoint(
75
+ Config.ENDPOINT_SOURCE_URL,
76
+ fsp,
77
+ endpointType,
78
+ { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' }
79
+ )
90
80
  test.equal(result, expected, 'The results match')
91
81
 
92
- const result2 = await Cache.getEndpoint(Config.ENDPOINT_SOURCE_URL, fsp, endpointType, { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' }, { path: '/additionalPath' })
82
+ const result2 = await Cache.getEndpoint(
83
+ Config.ENDPOINT_SOURCE_URL,
84
+ fsp,
85
+ endpointType,
86
+ { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' },
87
+ { path: '/additionalPath' }
88
+ )
93
89
  test.equal(result2, `${expected}/additionalPath`, 'The results match')
94
90
 
95
91
  await Cache.stopCache()
@@ -100,54 +96,145 @@ Test('Cache Test', cacheTest => {
100
96
  }
101
97
  })
102
98
 
103
- getEndpointTest.test('return the endpoint if catbox returns decoratedValue object', async (test) => {
104
- const fsp = 'fsp'
105
- const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET, { fsp })
106
- const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
107
- const expected = 'http://localhost:1080/transfers/97b01bd3-b223-415b-b37b-ab5bef9bdbed'
108
-
109
- await Cache.initializeCache({
110
- ...Config.ENDPOINT_CACHE_CONFIG,
111
- getDecoratedValue: true
112
- })
113
- request.sendRequest.withArgs(url, Helper.defaultHeaders()).returns(Promise.resolve(Helper.getEndPointsResponse))
114
-
115
- try {
116
- const result = await Cache.getEndpoint(Config.ENDPOINT_SOURCE_URL, fsp, endpointType, { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' })
117
- test.equal(result, expected, 'The results match')
99
+ getEndpointTest.test(
100
+ 'return the endpoint if catbox returns decoratedValue object',
101
+ async (test) => {
102
+ const fsp = 'fsp'
103
+ const url = Mustache.render(
104
+ Config.ENDPOINT_SOURCE_URL +
105
+ Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET,
106
+ { fsp }
107
+ )
108
+ const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
109
+ const expected =
110
+ 'http://localhost:1080/transfers/97b01bd3-b223-415b-b37b-ab5bef9bdbed'
111
+
112
+ await Cache.initializeCache(
113
+ { ...Config.ENDPOINT_CACHE_CONFIG, getDecoratedValue: true },
114
+ { hubName, hubNameRegex }
115
+ )
116
+ request.sendRequest
117
+ .withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex })
118
+ .returns(Promise.resolve(Helper.getEndPointsResponse))
119
+
120
+ try {
121
+ const result = await Cache.getEndpoint(
122
+ Config.ENDPOINT_SOURCE_URL,
123
+ fsp,
124
+ endpointType,
125
+ { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' }
126
+ )
127
+ test.equal(result, expected, 'The results match')
128
+
129
+ const result2 = await Cache.getEndpoint(
130
+ Config.ENDPOINT_SOURCE_URL,
131
+ fsp,
132
+ endpointType,
133
+ { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' },
134
+ { path: '/additionalPath' }
135
+ )
136
+ test.equal(
137
+ result2,
138
+ `${expected}/additionalPath`,
139
+ 'The results match'
140
+ )
141
+
142
+ await Cache.stopCache()
143
+ test.end()
144
+ } catch (err) {
145
+ test.fail('Error thrown', err)
146
+ test.end()
147
+ }
148
+ }
149
+ )
150
+
151
+ getEndpointTest.test(
152
+ 'return the endpoint if catbox returns decoratedValue object',
153
+ async (test) => {
154
+ const fsp = 'fsp'
155
+ const url = Mustache.render(
156
+ Config.ENDPOINT_SOURCE_URL +
157
+ Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET,
158
+ { fsp }
159
+ )
160
+ const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
161
+ const expected =
162
+ 'http://localhost:1080/transfers/97b01bd3-b223-415b-b37b-ab5bef9bdbed'
163
+
164
+ await Cache.initializeCache(
165
+ { ...Config.ENDPOINT_CACHE_CONFIG, getDecoratedValue: true },
166
+ { hubName, hubNameRegex }
167
+ )
168
+ request.sendRequest
169
+ .withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex })
170
+ .returns(Promise.resolve(Helper.getEndPointsResponse))
171
+
172
+ try {
173
+ const result = await Cache.getEndpoint(
174
+ Config.ENDPOINT_SOURCE_URL,
175
+ fsp,
176
+ endpointType,
177
+ { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' }
178
+ )
179
+ test.equal(result, expected, 'The results match')
180
+ await Cache.stopCache()
181
+ test.end()
182
+ } catch (err) {
183
+ test.fail('Error thrown', err)
184
+ test.end()
185
+ }
186
+ }
187
+ )
188
+
189
+ getEndpointTest.test(
190
+ 'return throw an error if array not returned in response object',
191
+ async (test) => {
192
+ const fsp = 'fsp'
193
+ const url = Mustache.render(
194
+ Config.ENDPOINT_SOURCE_URL +
195
+ Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET,
196
+ { fsp }
197
+ )
198
+ const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
199
+
200
+ await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, {
201
+ hubNameRegex
202
+ })
203
+ request.sendRequest
204
+ .withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex })
205
+ .returns(Promise.resolve({ data: {} }))
206
+
207
+ try {
208
+ await Cache.getEndpoint(
209
+ Config.ENDPOINT_SOURCE_URL,
210
+ fsp,
211
+ endpointType,
212
+ { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' }
213
+ )
214
+ test.fail('should throw error')
215
+ } catch (e) {
216
+ test.ok(e instanceof Error)
217
+ }
118
218
  await Cache.stopCache()
119
219
  test.end()
120
- } catch (err) {
121
- test.fail('Error thrown', err)
122
- test.end()
123
220
  }
124
- })
125
-
126
- getEndpointTest.test('return throw an error if array not returned in response object', async (test) => {
127
- const fsp = 'fsp'
128
- const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET, { fsp })
129
- const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
130
-
131
- await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG)
132
- request.sendRequest.withArgs(url, Helper.defaultHeaders()).returns(Promise.resolve({ data: {} }))
133
-
134
- try {
135
- await Cache.getEndpoint(Config.ENDPOINT_SOURCE_URL, fsp, endpointType, { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' })
136
- test.fail('should throw error')
137
- } catch (e) {
138
- test.ok(e instanceof Error)
139
- }
140
- await Cache.stopCache()
141
- test.end()
142
- })
221
+ )
143
222
 
144
223
  getEndpointTest.test('throw error', async (test) => {
145
224
  const fsp = 'fsp1'
146
- const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET, { fsp })
225
+ const url = Mustache.render(
226
+ Config.ENDPOINT_SOURCE_URL +
227
+ Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET,
228
+ { fsp }
229
+ )
147
230
  const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
148
231
 
149
- await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG)
150
- request.sendRequest.withArgs(url, Helper.defaultHeaders()).throws(new Error())
232
+ await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, {
233
+ hubNameRegex
234
+ })
235
+ request.sendRequest
236
+ .withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex })
237
+ .throws(new Error())
151
238
  try {
152
239
  await Cache.getEndpoint(Config.ENDPOINT_SOURCE_URL, fsp, endpointType)
153
240
  test.fail('should throw error')
@@ -163,124 +250,177 @@ Test('Cache Test', cacheTest => {
163
250
  await getEndpointTest.end()
164
251
  })
165
252
 
166
- cacheTest.test('getEndpointAndRender should', async (getEndpointAndRenderTest) => {
167
- getEndpointAndRenderTest.test('return the rendered endpoint', async (test) => {
168
- const fsp = 'fsp'
169
- const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET, { fsp })
170
- const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
171
- const expected = 'http://localhost:1080/transfers/97b01bd3-b223-415b-b37b-ab5bef9bdbed'
172
-
173
- await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG)
174
- request.sendRequest.withArgs(url, Helper.defaultHeaders()).returns(Promise.resolve(Helper.getEndpointAndRenderResponse))
175
-
176
- try {
177
- const result = await Cache.getEndpointAndRender(
178
- Config.ENDPOINT_SOURCE_URL,
179
- fsp,
180
- endpointType,
181
- 'transfers/{{transferId}}',
182
- { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' })
183
- test.equal(result, expected, 'The results match')
184
- await Cache.stopCache()
185
- test.end()
186
- } catch (err) {
187
- test.fail('Error thrown', err)
188
- test.end()
189
- }
190
- })
191
-
192
- getEndpointAndRenderTest.test('return the rendered endpoint if catbox returns decoratedValue object', async (test) => {
193
- const fsp = 'fsp'
194
- const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET, { fsp })
195
- const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
196
- const expected = 'http://localhost:1080/transfers/97b01bd3-b223-415b-b37b-ab5bef9bdbed'
197
-
198
- await Cache.initializeCache({
199
- ...Config.ENDPOINT_CACHE_CONFIG,
200
- getDecoratedValue: true
253
+ cacheTest.test(
254
+ 'getEndpointAndRender should',
255
+ async (getEndpointAndRenderTest) => {
256
+ getEndpointAndRenderTest.test(
257
+ 'return the rendered endpoint',
258
+ async (test) => {
259
+ const fsp = 'fsp'
260
+ const url = Mustache.render(
261
+ Config.ENDPOINT_SOURCE_URL +
262
+ Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET,
263
+ { fsp }
264
+ )
265
+ const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
266
+ const expected =
267
+ 'http://localhost:1080/transfers/97b01bd3-b223-415b-b37b-ab5bef9bdbed'
268
+
269
+ await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, {
270
+ hubName, hubNameRegex
271
+ })
272
+ request.sendRequest
273
+ .withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex })
274
+ .returns(Promise.resolve(Helper.getEndpointAndRenderResponse))
275
+
276
+ try {
277
+ const result = await Cache.getEndpointAndRender(
278
+ Config.ENDPOINT_SOURCE_URL,
279
+ fsp,
280
+ endpointType,
281
+ 'transfers/{{transferId}}',
282
+ { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' }
283
+ )
284
+ test.equal(result, expected, 'The results match')
285
+ await Cache.stopCache()
286
+ test.end()
287
+ } catch (err) {
288
+ test.fail('Error thrown', err)
289
+ test.end()
290
+ }
291
+ }
292
+ )
293
+
294
+ getEndpointAndRenderTest.test(
295
+ 'return the rendered endpoint if catbox returns decoratedValue object',
296
+ async (test) => {
297
+ const fsp = 'fsp'
298
+ const url = Mustache.render(
299
+ Config.ENDPOINT_SOURCE_URL +
300
+ Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET,
301
+ { fsp }
302
+ )
303
+ const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
304
+ const expected =
305
+ 'http://localhost:1080/transfers/97b01bd3-b223-415b-b37b-ab5bef9bdbed'
306
+
307
+ await Cache.initializeCache(
308
+ { ...Config.ENDPOINT_CACHE_CONFIG, getDecoratedValue: true },
309
+ { hubName, hubNameRegex }
310
+ )
311
+ request.sendRequest
312
+ .withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex })
313
+ .returns(Promise.resolve(Helper.getEndpointAndRenderResponse))
314
+
315
+ try {
316
+ const result = await Cache.getEndpointAndRender(
317
+ Config.ENDPOINT_SOURCE_URL,
318
+ fsp,
319
+ endpointType,
320
+ 'transfers/{{transferId}}',
321
+ { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' }
322
+ )
323
+ test.equal(result, expected, 'The results match')
324
+ await Cache.stopCache()
325
+ test.end()
326
+ } catch (err) {
327
+ test.fail('Error thrown', err)
328
+ test.end()
329
+ }
330
+ }
331
+ )
332
+
333
+ getEndpointAndRenderTest.test(
334
+ 'return throw an error if array not returned in response object',
335
+ async (test) => {
336
+ const fsp = 'fsp'
337
+ const url = Mustache.render(
338
+ Config.ENDPOINT_SOURCE_URL +
339
+ Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET,
340
+ { fsp }
341
+ )
342
+ const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
343
+
344
+ await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, { hubName, hubNameRegex })
345
+ request.sendRequest
346
+ .withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex })
347
+ .returns(Promise.resolve({ data: {} }))
348
+
349
+ try {
350
+ await Cache.getEndpointAndRender(
351
+ Config.ENDPOINT_SOURCE_URL,
352
+ fsp,
353
+ endpointType,
354
+ 'transfers/{{transferId}}',
355
+ { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' }
356
+ )
357
+ test.fail('should throw error')
358
+ await Cache.stopCache()
359
+ test.end()
360
+ } catch (e) {
361
+ test.ok(e instanceof Error)
362
+ await Cache.stopCache()
363
+ test.end()
364
+ }
365
+ }
366
+ )
367
+
368
+ getEndpointAndRenderTest.test('throw error', async (test) => {
369
+ const fsp = 'fsp1'
370
+ const url = Mustache.render(
371
+ Config.ENDPOINT_SOURCE_URL +
372
+ Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET,
373
+ { fsp }
374
+ )
375
+ const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
376
+
377
+ await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, { hubName, hubNameRegex })
378
+ request.sendRequest
379
+ .withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex })
380
+ .throws(new Error())
381
+ try {
382
+ await Cache.getEndpointAndRender(
383
+ Config.ENDPOINT_SOURCE_URL,
384
+ fsp,
385
+ endpointType
386
+ )
387
+ test.fail('should throw error')
388
+ await Cache.stopCache()
389
+ test.end()
390
+ } catch (e) {
391
+ test.ok(e instanceof Error)
392
+ await Cache.stopCache()
393
+ test.end()
394
+ }
201
395
  })
202
- request.sendRequest.withArgs(url, Helper.defaultHeaders()).returns(Promise.resolve(Helper.getEndpointAndRenderResponse))
203
-
204
- try {
205
- const result = await Cache.getEndpointAndRender(
206
- Config.ENDPOINT_SOURCE_URL,
207
- fsp,
208
- endpointType,
209
- 'transfers/{{transferId}}',
210
- { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' })
211
- test.equal(result, expected, 'The results match')
212
- await Cache.stopCache()
213
- test.end()
214
- } catch (err) {
215
- test.fail('Error thrown', err)
216
- test.end()
217
- }
218
- })
219
-
220
- getEndpointAndRenderTest.test('return throw an error if array not returned in response object', async (test) => {
221
- const fsp = 'fsp'
222
- const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET, { fsp })
223
- const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
224
-
225
- await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG)
226
- request.sendRequest.withArgs(url, Helper.defaultHeaders()).returns(Promise.resolve({ data: {} }))
227
-
228
- try {
229
- await Cache.getEndpointAndRender(
230
- Config.ENDPOINT_SOURCE_URL,
231
- fsp,
232
- endpointType,
233
- 'transfers/{{transferId}}',
234
- { transferId: '97b01bd3-b223-415b-b37b-ab5bef9bdbed' })
235
- test.fail('should throw error')
236
- await Cache.stopCache()
237
- test.end()
238
- } catch (e) {
239
- test.ok(e instanceof Error)
240
- await Cache.stopCache()
241
- test.end()
242
- }
243
- })
244
396
 
245
- getEndpointAndRenderTest.test('throw error', async (test) => {
246
- const fsp = 'fsp1'
247
- const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANT_ENDPOINTS_GET, { fsp })
248
- const endpointType = FSPIOP_CALLBACK_URL_TRANSFER_PUT
249
-
250
- await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG)
251
- request.sendRequest.withArgs(url, Helper.defaultHeaders()).throws(new Error())
252
- try {
253
- await Cache.getEndpointAndRender(Config.ENDPOINT_SOURCE_URL, fsp, endpointType)
254
- test.fail('should throw error')
255
- await Cache.stopCache()
256
- test.end()
257
- } catch (e) {
258
- test.ok(e instanceof Error)
259
- await Cache.stopCache()
260
- test.end()
261
- }
262
- })
263
-
264
- await getEndpointAndRenderTest.end()
265
- })
397
+ await getEndpointAndRenderTest.end()
398
+ }
399
+ )
266
400
 
267
401
  cacheTest.test('initializeCache should', async (initializeCacheTest) => {
268
- initializeCacheTest.test('initializeCache cache and return true', async (test) => {
269
- try {
270
- const result = await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG)
271
- test.equal(result, true, 'The results match')
272
- await Cache.stopCache()
273
- test.end()
274
- } catch (err) {
275
- test.fail('Error thrown', err)
276
- test.end()
402
+ initializeCacheTest.test(
403
+ 'initializeCache cache and return true',
404
+ async (test) => {
405
+ try {
406
+ const result = await Cache.initializeCache(
407
+ Config.ENDPOINT_CACHE_CONFIG,
408
+ { hubName, hubNameRegex }
409
+ )
410
+ test.equal(result, true, 'The results match')
411
+ await Cache.stopCache()
412
+ test.end()
413
+ } catch (err) {
414
+ test.fail('Error thrown', err)
415
+ test.end()
416
+ }
277
417
  }
278
- })
418
+ )
279
419
 
280
420
  initializeCacheTest.test('should throw error', async (test) => {
281
421
  try {
282
422
  sandbox.stub(Catbox, 'Client').throws(new Error())
283
- await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG)
423
+ await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, { hubName, hubNameRegex })
284
424
  test.fail('should throw')
285
425
  test.end()
286
426
  } catch (err) {
@@ -34,7 +34,8 @@ const Util = require('../../../../src/util')
34
34
  const headerConfigExample = {
35
35
  httpMethod: 'PUT',
36
36
  sourceFsp: 'switch',
37
- destinationFsp: 'FSPDest'
37
+ destinationFsp: 'FSPDest',
38
+ hubNameRegex: /^Hub$/i
38
39
  }
39
40
 
40
41
  const headerDataInputExample = {
@@ -584,12 +584,12 @@ Test('Utility Test', utilityTest => {
584
584
  })
585
585
 
586
586
  proceedTest.test('produce fromSwitch and do not stop timer', async test => {
587
- const opts = { fromSwitch: true, eventDetail }
587
+ const opts = { fromSwitch: true, eventDetail, hubName: 'Hub' }
588
588
  try {
589
589
  const result = await UtilityProxy.proceed(Config.KAFKA_CONFIG, params, opts)
590
590
  test.ok(produceGeneralMessageStub.withArgs(Config.KAFKA_CONFIG, producer, eventDetail.functionality, eventDetail.action, message.value, successState).calledOnce, 'produceGeneralMessageStub not called')
591
591
  test.equal(message.value.to, from, 'message destination set to sender')
592
- test.equal(message.value.from, Enum.Http.Headers.FSPIOP.SWITCH.value, 'from set to switch')
592
+ test.equal(message.value.from, opts.hubName, 'from set to switch')
593
593
  test.equal(result, true, 'result returned')
594
594
  } catch (err) {
595
595
  test.fail(err.message)
@@ -599,14 +599,14 @@ Test('Utility Test', utilityTest => {
599
599
  })
600
600
 
601
601
  proceedTest.test('produce fromSwitch without headers', async test => {
602
- const opts = { fromSwitch: true, eventDetail }
602
+ const opts = { fromSwitch: true, eventDetail, hubName: 'Hub' }
603
603
  try {
604
604
  const localParams = clone(params)
605
605
  delete localParams.message.value.content.headers
606
606
  const result = await UtilityProxy.proceed(Config.KAFKA_CONFIG, localParams, opts)
607
607
  test.ok(produceGeneralMessageStub.withArgs(Config.KAFKA_CONFIG, producer, eventDetail.functionality, eventDetail.action, localParams.message.value, successState).calledOnce, 'produceGeneralMessageStub not called')
608
608
  test.equal(localParams.message.value.to, from, 'message destination set to sender')
609
- test.equal(localParams.message.value.from, Enum.Http.Headers.FSPIOP.SWITCH.value, 'from set to switch')
609
+ test.equal(localParams.message.value.from, opts.hubName, 'from set to switch')
610
610
  test.equal(result, true, 'result returned')
611
611
  } catch (err) {
612
612
  test.fail(err.message)
@@ -16,6 +16,8 @@ const Metrics = require('@mojaloop/central-services-metrics')
16
16
 
17
17
  Test('Participants Cache Test', participantsCacheTest => {
18
18
  let sandbox
19
+ const hubName = 'Hub'
20
+ const hubNameRegex = /^Hub$/i
19
21
 
20
22
  participantsCacheTest.beforeEach(async test => {
21
23
  Metrics.setup({
@@ -51,8 +53,8 @@ Test('Participants Cache Test', participantsCacheTest => {
51
53
  const fsp = 'fsp2'
52
54
  const expectedName = 'fsp2'
53
55
  const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANTS_GET, { fsp })
54
- await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG)
55
- request.sendRequest.withArgs(url, Helper.defaultHeaders()).returns(Promise.resolve(Helper.getParticipantsResponseFsp2))
56
+ await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, { hubName, hubNameRegex })
57
+ request.sendRequest.withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex }).returns(Promise.resolve(Helper.getParticipantsResponseFsp2))
56
58
 
57
59
  try {
58
60
  const result = await Cache.getParticipant(Config.ENDPOINT_SOURCE_URL, fsp)
@@ -69,17 +71,17 @@ Test('Participants Cache Test', participantsCacheTest => {
69
71
  const fsp = 'fsp2'
70
72
  const expectedName = 'fsp2'
71
73
  const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANTS_GET, { fsp })
72
- await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG)
73
- request.sendRequest.withArgs(url, Helper.defaultHeaders()).returns(Promise.resolve(Helper.getParticipantsResponseFsp2))
74
+ await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, { hubName, hubNameRegex })
75
+ request.sendRequest.withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex }).returns(Promise.resolve(Helper.getParticipantsResponseFsp2))
74
76
 
75
77
  try {
76
78
  const result = await Cache.getParticipant(Config.ENDPOINT_SOURCE_URL, fsp)
77
79
  test.equal(result.name, expectedName, 'The results match')
78
- test.ok(request.sendRequest.calledOnceWith(url, Helper.defaultHeaders()), 'Fetch participants was called once')
80
+ test.ok(request.sendRequest.calledOnceWith({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex }), 'Fetch participants was called once')
79
81
 
80
82
  const resultCached = await Cache.getParticipant(Config.ENDPOINT_SOURCE_URL, fsp)
81
83
  test.equal(request.sendRequest.callCount, 1)
82
- test.ok(request.sendRequest.calledOnceWith(url, Helper.defaultHeaders()), 'Fetch participants was not needlessly called')
84
+ test.ok(request.sendRequest.calledOnceWith({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex }), 'Fetch participants was not needlessly called')
83
85
  test.equal(resultCached.name, expectedName, 'The results match')
84
86
 
85
87
  await Cache.stopCache()
@@ -94,19 +96,19 @@ Test('Participants Cache Test', participantsCacheTest => {
94
96
  const fsp = 'fsp2'
95
97
  const expectedName = 'fsp2'
96
98
  const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANTS_GET, { fsp })
97
- await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG)
98
- request.sendRequest.withArgs(url, Helper.defaultHeaders()).returns(Promise.resolve(Helper.getParticipantsResponseFsp2))
99
+ await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, { hubName, hubNameRegex })
100
+ request.sendRequest.withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex }).returns(Promise.resolve(Helper.getParticipantsResponseFsp2))
99
101
 
100
102
  try {
101
103
  const result = await Cache.getParticipant(Config.ENDPOINT_SOURCE_URL, fsp)
102
104
  test.equal(result.name, expectedName, 'The results match')
103
- test.ok(request.sendRequest.calledOnceWith(url, Helper.defaultHeaders()), 'Fetch participants was called once')
105
+ test.ok(request.sendRequest.calledOnceWith({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex }), 'Fetch participants was called once')
104
106
 
105
107
  await Cache.invalidateParticipantCache(fsp)
106
108
 
107
109
  const resultCached = await Cache.getParticipant(Config.ENDPOINT_SOURCE_URL, fsp)
108
110
  test.equal(request.sendRequest.callCount, 2)
109
- test.ok(request.sendRequest.getCall(1).calledWith(url, Helper.defaultHeaders()), 'Fetch participants was called again')
111
+ test.ok(request.sendRequest.getCall(1).calledWith({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex }), 'Fetch participants was called again')
110
112
  test.equal(resultCached.name, expectedName, 'The results match')
111
113
 
112
114
  await Cache.stopCache()
@@ -124,8 +126,8 @@ Test('Participants Cache Test', participantsCacheTest => {
124
126
  await Cache.initializeCache({
125
127
  ...Config.ENDPOINT_CACHE_CONFIG,
126
128
  getDecoratedValue: true
127
- })
128
- request.sendRequest.withArgs(url, Helper.defaultHeaders()).returns(Promise.resolve(Helper.getParticipantsResponseFsp2))
129
+ }, { hubName, hubNameRegex })
130
+ request.sendRequest.withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex }).returns(Promise.resolve(Helper.getParticipantsResponseFsp2))
129
131
 
130
132
  try {
131
133
  const result = await Cache.getParticipant(Config.ENDPOINT_SOURCE_URL, fsp)
@@ -141,8 +143,8 @@ Test('Participants Cache Test', participantsCacheTest => {
141
143
  getParticipantTest.test('handles error from central-ledger', async (test) => {
142
144
  const fsp = 'fsp2'
143
145
  const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANTS_GET, { fsp })
144
- await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG)
145
- request.sendRequest.withArgs(url, Helper.defaultHeaders()).returns(Promise.resolve(Helper.getParticipantsResponseError))
146
+ await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, { hubName, hubNameRegex })
147
+ request.sendRequest.withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex }).returns(Promise.resolve(Helper.getParticipantsResponseError))
146
148
 
147
149
  try {
148
150
  await Cache.getParticipant(Config.ENDPOINT_SOURCE_URL, fsp)
@@ -159,8 +161,8 @@ Test('Participants Cache Test', participantsCacheTest => {
159
161
  getParticipantTest.test('throw error', async (test) => {
160
162
  const fsp = 'fsp2'
161
163
  const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANTS_GET, { fsp })
162
- await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG)
163
- request.sendRequest.withArgs(url, Helper.defaultHeaders()).throws(new Error())
164
+ await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, { hubName, hubNameRegex })
165
+ request.sendRequest.withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex }).throws(new Error())
164
166
 
165
167
  try {
166
168
  await Cache.getParticipant(Config.ENDPOINT_SOURCE_URL, fsp)
@@ -173,13 +175,50 @@ Test('Participants Cache Test', participantsCacheTest => {
173
175
  test.end()
174
176
  }
175
177
  })
178
+
179
+ getParticipantTest.test('throws error if hubName is not defined', async (test) => {
180
+ const fsp = 'fsp2'
181
+ const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANTS_GET, { fsp })
182
+ await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, { hubNameRegex })
183
+ request.sendRequest.withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex }).returns(Promise.resolve(Helper.getParticipantsResponseFsp2))
184
+
185
+ try {
186
+ await Cache.getParticipant(Config.ENDPOINT_SOURCE_URL, fsp)
187
+ test.fail('should throw error')
188
+ await Cache.stopCache()
189
+ test.end()
190
+ } catch (err) {
191
+ test.ok(err instanceof Error)
192
+ await Cache.stopCache()
193
+ test.end()
194
+ }
195
+ })
196
+
197
+ getParticipantTest.test('throws error if hubNameRegex is not defined', async (test) => {
198
+ const fsp = 'fsp2'
199
+ const url = Mustache.render(Config.ENDPOINT_SOURCE_URL + Enum.EndPoints.FspEndpointTemplates.PARTICIPANTS_GET, { fsp })
200
+ await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, { hubName })
201
+ request.sendRequest.withArgs({ url, headers: Helper.defaultHeaders(), source: hubName, destination: hubName, hubNameRegex }).returns(Promise.resolve(Helper.getParticipantsResponseFsp2))
202
+
203
+ try {
204
+ await Cache.getParticipant(Config.ENDPOINT_SOURCE_URL, fsp)
205
+ test.fail('should throw error')
206
+ await Cache.stopCache()
207
+ test.end()
208
+ } catch (err) {
209
+ test.ok(err instanceof Error)
210
+ await Cache.stopCache()
211
+ test.end()
212
+ }
213
+ })
214
+
176
215
  await getParticipantTest.end()
177
216
  })
178
217
 
179
218
  participantsCacheTest.test('initializeCache should', async (participantsInitializeCacheTest) => {
180
219
  participantsInitializeCacheTest.test('initializeCache cache and return true', async (test) => {
181
220
  try {
182
- const result = await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG)
221
+ const result = await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, { hubName, hubNameRegex })
183
222
  test.equal(result, true, 'The results match')
184
223
  await Cache.stopCache()
185
224
  test.end()
@@ -192,7 +231,7 @@ Test('Participants Cache Test', participantsCacheTest => {
192
231
  participantsInitializeCacheTest.test('should throw error', async (test) => {
193
232
  try {
194
233
  sandbox.stub(Catbox, 'Client').throws(new Error())
195
- await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG)
234
+ await Cache.initializeCache(Config.ENDPOINT_CACHE_CONFIG, { hubName, hubNameRegex })
196
235
  test.fail('should throw')
197
236
  test.end()
198
237
  } catch (err) {
@@ -44,6 +44,9 @@ Test('ParticipantEndpoint Model Test', modelTest => {
44
44
  let sandbox
45
45
  let request
46
46
  let Model
47
+ const hubName = 'Hub'
48
+ const hubNameRegex = /^Hub$/i
49
+
47
50
  Metrics.setup({
48
51
  INSTRUMENTATION: {
49
52
  METRICS: {
@@ -80,7 +83,13 @@ Test('ParticipantEndpoint Model Test', modelTest => {
80
83
  Model = proxyquire('../../../src/util/request', { axios: request })
81
84
 
82
85
  try {
83
- const result = await Model.sendRequest(requestOptions.url, Helper.defaultHeaders(Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.PARTICIPANTS, Enum.Http.HeaderResources.SWITCH), Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.SWITCH)
86
+ const result = await Model.sendRequest({
87
+ url: requestOptions.url,
88
+ headers: Helper.defaultHeaders(hubName, Enum.Http.HeaderResources.PARTICIPANTS, hubName),
89
+ source: hubName,
90
+ destination: hubName,
91
+ hubNameRegex
92
+ })
84
93
  test.deepEqual(result, Helper.getEndPointsResponse, 'The results match')
85
94
  test.end()
86
95
  } catch (err) {
@@ -103,7 +112,16 @@ Test('ParticipantEndpoint Model Test', modelTest => {
103
112
  Model = proxyquire('../../../src/util/request', { axios: requestFunction })
104
113
 
105
114
  try {
106
- const result = await Model.sendRequest(requestOptions.url, Helper.defaultHeaders(Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.PARTICIPANTS, Enum.Http.HeaderResources.SWITCH), Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.SWITCH, Enum.Http.RestMethods.GET, undefined, Enum.Http.ResponseTypes.JSON, span)
115
+ const result = await Model.sendRequest({
116
+ url: requestOptions.url,
117
+ headers: Helper.defaultHeaders(hubName, Enum.Http.HeaderResources.PARTICIPANTS, hubName),
118
+ source: hubName,
119
+ destination: hubName,
120
+ method: Enum.Http.RestMethods.GET,
121
+ responseType: Enum.Http.ResponseTypes.JSON,
122
+ span,
123
+ hubNameRegex
124
+ })
107
125
  test.deepEqual(result, Helper.getEndPointsResponse, 'The results match')
108
126
  test.end()
109
127
  } catch (err) {
@@ -131,7 +149,18 @@ Test('ParticipantEndpoint Model Test', modelTest => {
131
149
  Model = proxyquire('../../../src/util/request', { axios: requestFunction })
132
150
 
133
151
  try {
134
- const result = await Model.sendRequest(requestOptions.url, Helper.defaultHeaders(Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.PARTICIPANTS, Enum.Http.HeaderResources.SWITCH), Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.SWITCH, Enum.Http.RestMethods.GET, undefined, Enum.Http.ResponseTypes.JSON, span, null, protocolVersions)
152
+ const result = await Model.sendRequest({
153
+ url: requestOptions.url,
154
+ headers: Helper.defaultHeaders(hubName, Enum.Http.HeaderResources.PARTICIPANTS, hubName),
155
+ source: hubName,
156
+ destination: hubName,
157
+ method: Enum.Http.RestMethods.GET,
158
+ responseType: Enum.Http.ResponseTypes.JSON,
159
+ span,
160
+ jwsSigner: null,
161
+ protocolVersions,
162
+ hubNameRegex
163
+ })
135
164
  test.deepEqual(result, Helper.getEndPointsResponse, 'The results match')
136
165
  test.end()
137
166
  } catch (err) {
@@ -156,7 +185,18 @@ Test('ParticipantEndpoint Model Test', modelTest => {
156
185
  Model = proxyquire('../../../src/util/request', { axios: requestFunction })
157
186
 
158
187
  try {
159
- const result = await Model.sendRequest(requestOptions.url, Helper.defaultHeaders(Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.PARTICIPANTS, Enum.Http.HeaderResources.SWITCH), Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.SWITCH, Enum.Http.RestMethods.GET, undefined, Enum.Http.ResponseTypes.JSON, span, null, protocolVersions)
188
+ const result = await Model.sendRequest({
189
+ url: requestOptions.url,
190
+ headers: Helper.defaultHeaders(hubName, Enum.Http.HeaderResources.PARTICIPANTS, hubName),
191
+ source: hubName,
192
+ destination: hubName,
193
+ method: Enum.Http.RestMethods.GET,
194
+ responseType: Enum.Http.ResponseTypes.JSON,
195
+ span,
196
+ jwsSigner: null,
197
+ protocolVersions,
198
+ hubNameRegex
199
+ })
160
200
  test.deepEqual(result, Helper.getEndPointsResponse, 'The results match')
161
201
  test.end()
162
202
  } catch (err) {
@@ -177,7 +217,13 @@ Test('ParticipantEndpoint Model Test', modelTest => {
177
217
  Model = proxyquire('../../../src/util/request', { axios: request })
178
218
 
179
219
  try {
180
- await Model.sendRequest(requestOptions.url, Helper.defaultHeaders(Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.PARTICIPANTS, Enum.Http.HeaderResources.SWITCH), Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.SWITCH)
220
+ await Model.sendRequest({
221
+ url: requestOptions.url,
222
+ headers: Helper.defaultHeaders(hubName, Enum.Http.HeaderResources.PARTICIPANTS, hubName),
223
+ source: hubName,
224
+ destination: hubName,
225
+ hubNameRegex
226
+ })
181
227
  test.fail('should throw error')
182
228
  test.end()
183
229
  } catch (e) {
@@ -204,7 +250,13 @@ Test('ParticipantEndpoint Model Test', modelTest => {
204
250
  Model = proxyquire('../../../src/util/request', { axios: request })
205
251
 
206
252
  try {
207
- await Model.sendRequest(requestOptions.url, Helper.defaultHeaders(Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.PARTICIPANTS, Enum.Http.HeaderResources.SWITCH), Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.SWITCH)
253
+ await Model.sendRequest({
254
+ url: requestOptions.url,
255
+ headers: Helper.defaultHeaders(hubName, Enum.Http.HeaderResources.PARTICIPANTS, hubName),
256
+ source: hubName,
257
+ destination: hubName,
258
+ hubNameRegex
259
+ })
208
260
  test.fail('should throw error')
209
261
  test.end()
210
262
  } catch (e) {
@@ -225,7 +277,14 @@ Test('ParticipantEndpoint Model Test', modelTest => {
225
277
  Model = proxyquire('../../../src/util/request', { axios: request })
226
278
 
227
279
  try {
228
- await Model.sendRequest(requestOptions.url, Helper.defaultHeaders(Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.PARTICIPANTS, Enum.Http.HeaderResources.SWITCH), Enum.Http.HeaderResources.SWITCH, Enum.Http.HeaderResources.SWITCH, Enum.Http.RestMethods.POST)
280
+ await Model.sendRequest({
281
+ url: requestOptions.url,
282
+ headers: Helper.defaultHeaders(hubName, Enum.Http.HeaderResources.PARTICIPANTS, hubName),
283
+ source: hubName,
284
+ destination: hubName,
285
+ method: Enum.Http.RestMethods.POST,
286
+ hubNameRegex
287
+ })
229
288
  test.fail('should throw error')
230
289
  test.end()
231
290
  } catch (e) {
@@ -252,7 +311,17 @@ Test('ParticipantEndpoint Model Test', modelTest => {
252
311
  request = sandbox.stub().returns({ status: 200 })
253
312
  Model = proxyquire('../../../src/util/request', { axios: request })
254
313
  const signSpy = Sinon.spy(jwsSigner, 'getSignature')
255
- await Model.sendRequest(requestOptions.url, requestOptions.headers, Enum.Http.HeaderResources.SWITCH, fsp, requestOptions.method, payload, Enum.Http.ResponseTypes.JSON, undefined, jwsSigner)
314
+ await Model.sendRequest({
315
+ url: requestOptions.url,
316
+ headers: requestOptions.headers,
317
+ source: hubName,
318
+ destination: fsp,
319
+ method: requestOptions.method,
320
+ payload,
321
+ responseType: Enum.Http.ResponseTypes.JSON,
322
+ jwsSigner,
323
+ hubNameRegex
324
+ })
256
325
  test.ok(signSpy.calledOnce, 'JwsSigner.sign is called once')
257
326
  test.ok('fspiop-signature' in signSpy.getCall(0).firstArg.headers, 'The header has fspiop-signature')
258
327
  test.end()
@@ -276,7 +345,16 @@ Test('ParticipantEndpoint Model Test', modelTest => {
276
345
  request = sandbox.stub().returns({ status: 200 })
277
346
  Model = proxyquire('../../../src/util/request', { axios: request })
278
347
  const signSpy = Sinon.spy(jwsSigner, 'getSignature')
279
- await Model.sendRequest(requestOptions.url, requestOptions.headers, Enum.Http.HeaderResources.SWITCH, fsp, requestOptions.method, payload, Enum.Http.ResponseTypes.JSON, undefined, undefined)
348
+ await Model.sendRequest({
349
+ url: requestOptions.url,
350
+ headers: requestOptions.headers,
351
+ source: hubName,
352
+ destination: fsp,
353
+ method: requestOptions.method,
354
+ payload,
355
+ responseType: Enum.Http.ResponseTypes.JSON,
356
+ hubNameRegex
357
+ })
280
358
  test.equal(signSpy.callCount, 0, 'JwsSigner.sign is NOT called')
281
359
  test.end()
282
360
  })