UNPKG

4.57 kBJavaScriptView Raw
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
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 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.
10
11 Contributors
12 --------------
13 This is the official list of the Mojaloop project contributors for this file.
14 Names of the original copyright holders (individuals or organizations)
15 should be listed with a '*' in the first column. People who have
16 contributed from an organization can be listed under the organization
17 that actually holds the copyright for their contributions (see the
18 Gates Foundation organization for an example). Those individuals should have
19 their names indented and be marked with a '-'. Email address can be added
20 optionally within square brackets <email>.
21
22 * Gates Foundation
23 - Name Surname <name.surname@gatesfoundation.com>
24
25 * ModusBox
26 - Neal Donnan <neal.donnan@modusbox.com>
27 - Juan Correa <juan.correa@modusbox.com>
28 - Miguel de Barros <miguel.debarros@modusbox.com>
29
30 --------------
31 ******/
32
33'use strict'
34
35const Test = require('tape')
36const ErrorEnums = require('../src/enums')
37
38Test('Enum should', enumTest => {
39 enumTest.test('return correct error code string', function (test) {
40 const fspiopErrorCode = ErrorEnums.findFSPIOPErrorCode(ErrorEnums.FSPIOPErrorCodes.PAYEE_FSP_INSUFFICIENT_LIQUIDITY.code)
41 test.ok(fspiopErrorCode)
42 test.equal(fspiopErrorCode.code, ErrorEnums.FSPIOPErrorCodes.PAYEE_FSP_INSUFFICIENT_LIQUIDITY.code)
43 test.equal(fspiopErrorCode.message, ErrorEnums.FSPIOPErrorCodes.PAYEE_FSP_INSUFFICIENT_LIQUIDITY.message)
44 test.end()
45 })
46
47 enumTest.test('return correct error code integer', function (test) {
48 const fspiopErrorCode = ErrorEnums.findFSPIOPErrorCode(parseInt(ErrorEnums.FSPIOPErrorCodes.PAYEE_FSP_INSUFFICIENT_LIQUIDITY.code))
49 test.ok(fspiopErrorCode)
50 test.equal(fspiopErrorCode.code, ErrorEnums.FSPIOPErrorCodes.PAYEE_FSP_INSUFFICIENT_LIQUIDITY.code)
51 test.equal(fspiopErrorCode.message, ErrorEnums.FSPIOPErrorCodes.PAYEE_FSP_INSUFFICIENT_LIQUIDITY.message)
52 test.end()
53 })
54
55 enumTest.test('return undefined result with incorrect error code', function (test) {
56 const fspiopErrorCode = ErrorEnums.findFSPIOPErrorCode(parseInt('9999'))
57 test.equals(fspiopErrorCode, undefined)
58 test.end()
59 })
60
61 enumTest.test('test FSPIOPErrorTypes are correctly added to FSPIOPErrorCodes by validating each entry by its associated regex', function (test) {
62 const fspiopErrorCode = ErrorEnums.findFSPIOPErrorCode(ErrorEnums.FSPIOPErrorCodes.PAYEE_FSP_INSUFFICIENT_LIQUIDITY.code)
63 for (const errorCodeValue of Object.values(ErrorEnums.FSPIOPErrorCodes)) {
64 const regExp = new RegExp(errorCodeValue.type.regex)
65 test.ok(regExp.test(errorCodeValue.code))
66 }
67 test.ok(fspiopErrorCode)
68 test.equal(fspiopErrorCode.code, ErrorEnums.FSPIOPErrorCodes.PAYEE_FSP_INSUFFICIENT_LIQUIDITY.code)
69 test.equal(fspiopErrorCode.message, ErrorEnums.FSPIOPErrorCodes.PAYEE_FSP_INSUFFICIENT_LIQUIDITY.message)
70 test.end()
71 })
72
73 enumTest.test('test FSPIOPErrorCodeMaps contains every error found in the FSPIOPErrorCodes object using the findFSPIOPErrorCode method', function (test) {
74 for (const errorCodeValue of Object.values(ErrorEnums.FSPIOPErrorCodes)) {
75 const errorCodeResult = ErrorEnums.findFSPIOPErrorCode(errorCodeValue.code)
76 test.ok(errorCodeResult)
77 }
78 test.end()
79 })
80
81 enumTest.test('populateOverrides redefines existing errors and allows adding new errors', function (test) {
82 const errorCodes = {
83 INTERNAL_SERVER_ERROR: { code: '2001', message: 'Internal Server Error' }
84 }
85 const override = {
86 INTERNAL_SERVER_ERROR: { code: '9000' },
87 NEW_CUSTOM_ERROR: { code: '9001', message: 'Custom Error' }
88 }
89 const expected = {
90 INTERNAL_SERVER_ERROR: { code: '9000', message: 'Internal Server Error' },
91 NEW_CUSTOM_ERROR: { code: '9001', message: 'Custom Error' }
92 }
93
94 const result = ErrorEnums._populateOverrides(errorCodes, override)
95 test.deepEqual(result, expected)
96 test.end()
97 })
98
99 enumTest.end()
100})