Skip to content

Commit f812348

Browse files
authored
Merge pull request #5 from vgenev/fix/new-event-sdk-version
Integrated new event sdk version
2 parents 41a05c5 + ebb831b commit f812348

File tree

9 files changed

+899
-72
lines changed

9 files changed

+899
-72
lines changed

package-lock.json

Lines changed: 656 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"version": "7.2.0",
55
"author": "Modusbox",
66
"contributors": [
7-
"Rajiv Mothilal <rajiv.mothilal@modusbox.com>"
7+
"Rajiv Mothilal <rajiv.mothilal@modusbox.com>",
8+
"Valentin Genev <valentin.genev@modusbox.com>",
9+
"Miguel de Barros <miguel.debarros@modusbox.com"
810
],
911
"repository": {
1012
"type": "git",
@@ -29,9 +31,9 @@
2931
"@hapi/inert": "5.2.1",
3032
"@hapi/vision": "5.5.2",
3133
"@mojaloop/central-services-metrics": "5.2.0",
32-
"@mojaloop/central-services-shared": "6.4.1",
34+
"@mojaloop/central-services-shared": "7.2.0",
3335
"@mojaloop/central-services-stream": "6.2.2",
34-
"@mojaloop/event-sdk": "7.2.0-snapshot",
36+
"@mojaloop/event-sdk": "7.2.1",
3537
"blipp": "4.0.0",
3638
"glob": "7.1.4",
3739
"hapi-openapi": "1.2.2",

src/domain/event/handler.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ const logEvent = async (message) => {
4949
* @returns {boolean} Returns if the logging of the event is successful or not
5050
*/
5151
const handleRestRequest = async (payload) => {
52-
try {
53-
return logEvent(payload)
54-
} catch (e) {
55-
Logger.error(e)
56-
throw e
57-
}
52+
return logEvent(payload)
5853
}
5954

6055
module.exports = {

src/lib/tracer.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// const initJaegerTracer = require('jaeger-client').initTracer
2+
// const Logger = require('@mojaloop/central-services-shared').Logger
3+
4+
// function initTracer(serviceName) {
5+
// let config = {
6+
// serviceName: serviceName,
7+
// sampler: {
8+
// type: 'const',
9+
// param: 1,
10+
// },
11+
// reporter: {
12+
// logSpans: true,
13+
// },
14+
// }
15+
// let options = {
16+
// logger: {
17+
// info: function logInfo(msg) {
18+
// Logger.info(msg)
19+
// },
20+
// error: function logError(msg) {
21+
// Logger.error(msg)
22+
// },
23+
// }
24+
// }
25+
// return initJaegerTracer(config, options)
26+
// }
27+
28+
// module.exports = {
29+
// initTracer
30+
// }

src/server.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
optionally within square brackets <email>.
1818
* Gates Foundation
1919
20+
* ModusBox
2021
- Rajiv Mothilal <rajiv.mothilal@modusbox.com>
21-
2222
--------------
2323
******/
2424

@@ -107,16 +107,22 @@ const createRPCServer = async () => {
107107
Logger.info('Received eventMessage:', JSON.stringify(eventMessage, null, 2))
108108
await eventHandler.logEvent(eventMessage)
109109
})
110+
grpcServer.on('error', async (error) => {
111+
Logger.error('Error', JSON.stringify(error, null, 2))
112+
})
110113
grpcServer.start()
111-
return null
114+
return grpcServer
112115
}
113116

114117
const initialize = async (port = Config.PORT) => {
115-
await createRPCServer()
118+
const grpcServer = await createRPCServer()
116119
const server = await createServer(port)
117120
server.plugins.openapi.setHost(server.info.host + ':' + server.info.port)
118121
Logger.info(`Server running on ${server.info.host}:${server.info.port}`)
119-
return server
122+
return {
123+
server,
124+
grpcServer
125+
}
120126
}
121127

122128
module.exports = {
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
* Georgi Georgiev <georgi.georgiev@modusbox.com>
22+
* Valentin Genev <valentin.genev@modusbox.com>
23+
--------------
24+
******/
25+
26+
'use strict'
27+
28+
const setupTest = require('ava')
29+
const Sinon = require('sinon')
30+
const Logger = require('@mojaloop/central-services-shared').Logger
31+
const Proxyquire = require('proxyquire')
32+
33+
let sandbox
34+
let eventHandlerProxy
35+
let produceGeneralMessageStub
36+
37+
setupTest.serial.beforeEach(async () => {
38+
try {
39+
sandbox = Sinon.createSandbox()
40+
} catch (err) {
41+
Logger.error(`setupTest failed with error - ${err}`)
42+
}
43+
})
44+
45+
setupTest.serial.afterEach(async () => {
46+
sandbox.restore()
47+
})
48+
49+
setupTest.serial('handleRestRequest success', async test => {
50+
produceGeneralMessageStub = sandbox.stub().returns(Promise.resolve(true))
51+
eventHandlerProxy = Proxyquire('../../../../src/domain/event/handler', {
52+
'../../lib/kafka/util': {
53+
produceGeneralMessage: produceGeneralMessageStub
54+
}
55+
})
56+
57+
const payload = {
58+
metadata: {
59+
trace: {
60+
traceId: '123'
61+
}
62+
}
63+
}
64+
try {
65+
await eventHandlerProxy.handleRestRequest(payload)
66+
test.assert(produceGeneralMessageStub.calledOnce, 'return server object')
67+
} catch (err) {
68+
Logger.error(`init failed with error - ${err}`)
69+
test.fail()
70+
}
71+
})
72+
73+
setupTest.serial('handleRestRequest with exception', async test => {
74+
const errorName = 'ERROROMG!'
75+
produceGeneralMessageStub = sandbox.stub().throws(errorName)
76+
eventHandlerProxy = Proxyquire('../../../../src/domain/event/handler', {
77+
'../../lib/kafka/util': {
78+
produceGeneralMessage: produceGeneralMessageStub
79+
}
80+
})
81+
82+
const payload = {
83+
metadata: {
84+
trace: {}
85+
}
86+
}
87+
88+
const errorMsg = 'This is an error!'
89+
try {
90+
await eventHandlerProxy.handleRestRequest(payload)
91+
test.fail()
92+
} catch (err) {
93+
test.assert(err.name === errorName, `Error message matches - ${errorMsg.message}`)
94+
}
95+
})

test/unit/handlers/event.test.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
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+
* ModusBox
22+
- Valentin Genev <valentin.genev@modusbox.com>
23+
--------------
24+
******/
25+
126
'use strict'
227

328
const Test = require('ava')
@@ -178,7 +203,7 @@ Test.serial('test Event processes correctly', async function (t) {
178203

179204
Test.serial('test Event throws error and is handled correctly', async function (t) {
180205
const sandbox = Sinon.createSandbox()
181-
const server = await Initialise(await getPort())
206+
const { server } = await Initialise(await getPort())
182207
const requests = new Promise((resolve, reject) => {
183208
Mockgen().requests({
184209
path: '/event',
@@ -221,7 +246,7 @@ Test.serial('test Event throws error and is handled correctly', async function (
221246

222247
Test.serial('test Event processes and response is logged correctly', async function (t) {
223248
const sandbox = Sinon.createSandbox()
224-
const server = await Initialise(await getPort())
249+
const { server } = await Initialise(await getPort())
225250
const requests = new Promise((resolve, reject) => {
226251
Mockgen().requests({
227252
path: '/event',

test/unit/handlers/health.test.js

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,56 @@
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+
* ModusBox
22+
- Miguel de Barros <miguel.debarros@modusbox.com>
23+
- Rajiv Mothilal <rajiv.mothilal@modusbox.com>
24+
--------------
25+
******/
26+
127
'use strict'
228

329
const Test = require('ava')
4-
const Hapi = require('@hapi/hapi')
5-
const HapiOpenAPI = require('hapi-openapi')
6-
const Path = require('path')
730
const Mockgen = require('../../util/mockgen.js')
831
const Handler = require('../../../src/domain/metadata/health')
932
const Sinon = require('sinon')
10-
const Initialise = require('../../../src/server').initialize
11-
const getPort = require('get-port')
33+
const Logger = require('@mojaloop/central-services-shared').Logger
34+
const Server = require('../../../src/server')
35+
36+
let sandbox
37+
let server
38+
39+
Test.beforeEach(async () => {
40+
try {
41+
sandbox = Sinon.createSandbox()
42+
43+
const initResult = await Server.initialize()
44+
server = initResult.server
45+
} catch (err) {
46+
Logger.error(`setupTest failed with error - ${err}`)
47+
}
48+
})
49+
50+
Test.afterEach(async () => {
51+
sandbox.restore()
52+
await server.stop()
53+
})
1254

1355
/**
1456
* summary: Get Health
@@ -18,15 +60,7 @@ const getPort = require('get-port')
1860
* responses: 200, 400, 401, 403, 404, 405, 406, 501, 503
1961
*/
2062
Test.serial('test Health get operation', async function (t) {
21-
const server = new Hapi.Server()
22-
await server.register({
23-
plugin: HapiOpenAPI,
24-
options: {
25-
api: Path.resolve(__dirname, '../../../src/interface/swagger.json'),
26-
handlers: Path.join(__dirname, '../../../src/handlers'),
27-
outputvalidation: true
28-
}
29-
})
63+
// const server = new Hapi.Server()
3064
const requests = new Promise((resolve, reject) => {
3165
Mockgen().requests({
3266
path: '/health',
@@ -35,6 +69,7 @@ Test.serial('test Health get operation', async function (t) {
3569
return error ? reject(error) : resolve(mock)
3670
})
3771
})
72+
3873
const mock = await requests
3974
t.pass(mock)
4075
t.pass(mock.request)
@@ -58,13 +93,12 @@ Test.serial('test Health get operation', async function (t) {
5893
options.headers = mock.request.headers
5994
}
6095
const response = await server.inject(options)
61-
await server.stop()
96+
6297
t.is(response.statusCode, 200, 'Ok response status')
6398
})
6499

65100
Test.serial('test Health throws and error', async function (t) {
66-
const sandbox = Sinon.createSandbox()
67-
const server = await Initialise(await getPort())
101+
// const server = await Initialise(await getPort())
68102
const requests = new Promise((resolve, reject) => {
69103
Mockgen().requests({
70104
path: '/health',
@@ -97,7 +131,6 @@ Test.serial('test Health throws and error', async function (t) {
97131
}
98132
sandbox.stub(Handler, 'getHealth').throws(new Error('Error'))
99133
const response = await server.inject(options)
100-
await server.stop()
134+
101135
t.is(response.statusCode, 400, 'Bad request error thrown')
102-
sandbox.restore()
103136
})

0 commit comments

Comments
 (0)