Skip to content

Commit

Permalink
chore: renable coverage and improve coverage (#350)
Browse files Browse the repository at this point in the history
* coverage

* improve coverage

* fix

* lint dep
  • Loading branch information
kleyow authored Aug 22, 2024
1 parent 91571b9 commit 0d8fc24
Show file tree
Hide file tree
Showing 14 changed files with 724 additions and 62 deletions.
86 changes: 42 additions & 44 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -245,30 +245,30 @@ jobs:
- store_test_results:
path: ./test/results

# test-coverage:
# executor: default-docker
# environment:
# <<: *defaults_environment
# steps:
# - run:
# name: Install general dependencies
# command: *defaults_docker_Dependencies
# - run:
# name: Install AWS CLI dependencies
# command: *defaults_awsCliDependencies
# - checkout
# - run:
# <<: *defaults_configure_nvm
# - run:
# <<: *defaults_display_versions
# - restore_cache:
# key: dependency-cache-{{ .Environment.CIRCLE_SHA1 }}
# - run:
# name: Execute code coverage check
# command: npm -s run test:coverage-check
# - store_artifacts:
# path: coverage
# destination: test
test-coverage:
executor: default-docker
environment:
<<: *defaults_environment
steps:
- run:
name: Install general dependencies
command: *defaults_docker_Dependencies
- run:
name: Install AWS CLI dependencies
command: *defaults_awsCliDependencies
- checkout
- run:
<<: *defaults_configure_nvm
- run:
<<: *defaults_display_versions
- restore_cache:
key: dependency-cache-{{ .Environment.CIRCLE_SHA1 }}
- run:
name: Execute code coverage check
command: npm -s run test:coverage-check
- store_artifacts:
path: coverage
destination: test

test-integration:
executor: default-machine
Expand Down Expand Up @@ -711,18 +711,18 @@ workflows:
ignore:
- /feature*/
- /bugfix*/
# - test-coverage:
# context: org-global
# requires:
# - setup
# filters:
# tags:
# ignore:
# - /v[0-9]+(\.[0-9]+)*\-snapshot+((\.[0-9]+)?)/
# branches:
# ignore:
# - /feature*/
# - /bugfix*/
- test-coverage:
context: org-global
requires:
- setup
filters:
tags:
ignore:
- /v[0-9]+(\.[0-9]+)*\-snapshot+((\.[0-9]+)?)/
branches:
ignore:
- /feature*/
- /bugfix*/
- test-integration:
context: org-global
requires:
Expand Down Expand Up @@ -811,8 +811,8 @@ workflows:
- pr-tools/pr-title-check
- test-lint
- test-unit
# - test-coverage
# - test-integration
- test-coverage
- test-integration
# - test-functional
- vulnerability-check
- audit-licenses
Expand All @@ -838,12 +838,11 @@ workflows:
- pr-tools/pr-title-check
- test-lint
- test-unit
# - test-coverage
# - test-integration
- test-coverage
- test-integration
# - test-functional
- vulnerability-check
- audit-licenses
# - test-integration
- license-scan
- image-scan
filters:
Expand All @@ -858,12 +857,11 @@ workflows:
- pr-tools/pr-title-check
- test-lint
- test-unit
# - test-coverage
# - test-integration
- test-coverage
- test-integration
# - test-functional
- vulnerability-check
- audit-licenses
# - test-integration
- license-scan
- image-scan
filters:
Expand Down
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module.exports = {
collectCoverageFrom: [
'**/src/**/**/*.js'
],
coveragePathIgnorePatterns: [
'./src/handlers/index.js'
],
coverageThreshold: {
global: {
statements: 90,
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"eslint-plugin-jest": "28.8.0",
"jest": "29.7.0",
"jest-junit": "16.0.0",
"npm-check-updates": "17.0.6",
"npm-check-updates": "17.1.0",
"nyc": "17.0.0",
"pre-commit": "1.2.2",
"proxyquire": "2.1.3",
Expand Down
8 changes: 4 additions & 4 deletions src/model/fxQuotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ class FxQuotesModel {
let txn
const fspiopSource = headers[ENUM.Http.Headers.FSPIOP.SOURCE]
const childSpan = span.getChild('qs_fxQuote_forwardFxQuoteUpdate')

try {
await childSpan.audit({ headers, params: { conversionRequestId }, payload: fxQuoteUpdateRequest }, EventSdk.AuditEventAction.start)

if ('accept' in headers) {
throw ErrorHandler.CreateFSPIOPError(ErrorHandler.Enums.FSPIOPErrorCodes.VALIDATION_ERROR,
`Update for fx quote ${conversionRequestId} failed: "accept" header should not be sent in callbacks.`, null, headers['fspiop-source'])
Expand Down Expand Up @@ -582,7 +582,7 @@ class FxQuotesModel {
const fspiopError = ErrorHandler.ReformatFSPIOPError(err)
await this.handleException(fspiopSource, fxQuoteRequest.conversionRequestId, fspiopError, headers, childSpan)
} finally {
if (!childSpan.isFinished) {
if (childSpan && !childSpan.isFinished) {
await childSpan.finish()
}
}
Expand Down Expand Up @@ -619,7 +619,7 @@ class FxQuotesModel {
this.log.error(`Error forwarding fxQuote response: ${getStackOrInspect(err)}. Attempting to send error callback to ${fspiopSource}`)
await this.handleException(fspiopSource, conversionRequestId, err, headers, childSpan)
} finally {
if (!childSpan.isFinished) {
if (childSpan && !childSpan.isFinished) {
await childSpan.finish()
}
}
Expand Down Expand Up @@ -651,7 +651,7 @@ class FxQuotesModel {
histTimer({ success: false, queryName: 'handleException' })
this.log.error('error in handleException, stop request processing!', err)
} finally {
if (!childSpan.isFinished) {
if (childSpan && !childSpan.isFinished) {
await childSpan.finish()
}
}
Expand Down
28 changes: 28 additions & 0 deletions test/unit/api/quotes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,34 @@ describe('POST /quotes API Tests -->', () => {
expect(producerConfig).toStrictEqual(config)
})

it('should publish an fxQuote request message', async () => {
// Arrange
Producer.produceMessage = jest.fn()
const conversionRequestId = randomUUID()
const mockRequest = mocks.mockHttpRequest({
payload: { conversionRequestId },
headers: {
'content-type': 'application/vnd.interoperability.fxquotes+json;version=1.0'
}
})
const { handler, code } = mocks.createMockHapiHandler()

// Act
await quotesApi.post(mockContext, mockRequest, handler)

// Assert
expect(code).toHaveBeenCalledWith(Http.ReturnCodes.ACCEPTED.CODE)
expect(Producer.produceMessage).toHaveBeenCalledTimes(1)

const [message, topicConfig, producerConfig] = Producer.produceMessage.mock.calls[0]
const { id, type, action } = message.content
expect(id).toBe(conversionRequestId)
expect(type).toBe(Events.Event.Type.FX_QUOTE)
expect(action).toBe(Events.Event.Action.POST)
expect(topicConfig.topicName).toBe(kafkaConfig.PRODUCER.FX_QUOTE.POST.topic)
expect(producerConfig).toStrictEqual(kafkaConfig.PRODUCER.FX_QUOTE.POST.config)
})

it('should rethrow and log error in case of error on publishing quote', async () => {
// Arrange
const error = new Error('Create Quote Test Error')
Expand Down
85 changes: 85 additions & 0 deletions test/unit/api/quotes/{id}.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,34 @@ describe('/quotes/{id} API Tests -->', () => {
expect(producerConfig).toStrictEqual(config)
})

it('should publish an fxQuote request message', async () => {
// Arrange
Producer.produceMessage = jest.fn()
const conversionRequestId = randomUUID()
const mockRequest = mocks.mockHttpRequest({
params: { id: conversionRequestId },
headers: {
'content-type': 'application/vnd.interoperability.fxQuotes+json;version=1.0'
}
})
const { handler, code } = mocks.createMockHapiHandler()

// Act
await quotesApi.get(mockContext, mockRequest, handler)

// Assert
expect(code).toHaveBeenCalledWith(Http.ReturnCodes.ACCEPTED.CODE)
expect(Producer.produceMessage).toHaveBeenCalledTimes(1)

const [message, topicConfig, producerConfig] = Producer.produceMessage.mock.calls[0]
const { id, type, action } = message.content
expect(id).toBe(conversionRequestId)
expect(type).toBe(Events.Event.Type.FX_QUOTE)
expect(action).toBe(Events.Event.Action.GET)
expect(topicConfig.topicName).toBe(kafkaConfig.PRODUCER.FX_QUOTE.GET.topic)
expect(producerConfig).toStrictEqual(kafkaConfig.PRODUCER.FX_QUOTE.GET.config)
})

it('should rethrow error in case of error during publish a message', async () => {
// Arrange
const error = new Error('Get Quote Test Error')
Expand Down Expand Up @@ -122,6 +150,34 @@ describe('/quotes/{id} API Tests -->', () => {
expect(producerConfig).toStrictEqual(config)
})

it('should publish a message with PUT fxQuotes callback payload', async () => {
// Arrange
Producer.produceMessage = jest.fn()
const conversionRequestId = randomUUID()
const mockRequest = mocks.mockHttpRequest({
payload: { conversionRequestId },
headers: {
'content-type': 'application/vnd.interoperability.fxQuotes+json;version=1.0'
}
})
const { handler, code } = mocks.createMockHapiHandler()

// Act
await quotesApi.put(mockContext, mockRequest, handler)

// Assert
expect(code).toHaveBeenCalledWith(Http.ReturnCodes.OK.CODE)
expect(Producer.produceMessage).toHaveBeenCalledTimes(1)

const [message, topicConfig, producerConfig] = Producer.produceMessage.mock.calls[0]
const { id, type, action } = message.content
expect(id).toBe(conversionRequestId)
expect(type).toBe(Events.Event.Type.FX_QUOTE)
expect(action).toBe(Events.Event.Action.PUT)
expect(topicConfig.topicName).toBe(kafkaConfig.PRODUCER.FX_QUOTE.PUT.topic)
expect(producerConfig).toStrictEqual(kafkaConfig.PRODUCER.FX_QUOTE.PUT.config)
})

it('should rethrow error in case of error during publish callback message', async () => {
// Arrange
const error = new Error('Put Quote Test Error')
Expand Down Expand Up @@ -171,6 +227,35 @@ describe('/quotes/{id} API Tests -->', () => {
expect(producerConfig).toStrictEqual(config)
})

it('should publish a message with PUT fxQuotes callback error payload', async () => {
// Arrange
Producer.produceMessage = jest.fn()
const conversionRequestId = randomUUID()
const mockRequest = mocks.mockHttpRequest({
payload: { errorInformation: {} },
params: { id: conversionRequestId },
headers: {
'content-type': 'application/vnd.interoperability.fxQuotes+json;version=1.0'
}
})
const { handler, code } = mocks.createMockHapiHandler()

// Act
await quotesApi.put(mockContext, mockRequest, handler)

// Assert
expect(code).toHaveBeenCalledWith(Http.ReturnCodes.OK.CODE)
expect(Producer.produceMessage).toHaveBeenCalledTimes(1)

const [message, topicConfig, producerConfig] = Producer.produceMessage.mock.calls[0]
const { id, type, action } = message.content
expect(id).toBe(conversionRequestId)
expect(type).toBe(Events.Event.Type.FX_QUOTE)
expect(action).toBe(Events.Event.Action.PUT)
expect(topicConfig.topicName).toBe(kafkaConfig.PRODUCER.FX_QUOTE.PUT.topic)
expect(producerConfig).toStrictEqual(kafkaConfig.PRODUCER.FX_QUOTE.PUT.config)
})

it('should rethrow error case of error during publish a message', async () => {
// Arrange
const error = new Error('PUT Quote Test Error')
Expand Down
2 changes: 2 additions & 0 deletions test/unit/handlers/QuotingHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const createKafkaMessage = (topic) => ({
}
})

Logger.isDebugEnabled = jest.fn(() => true)

describe('QuotingHandler Tests -->', () => {
let handler
let quotesModel
Expand Down
30 changes: 28 additions & 2 deletions test/unit/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,37 @@ const fxQuoteMocks = {
httpRequestOptions: () => ({
}),
db: ({
commit = jest.fn().mockResolvedValue({}),
rollback = jest.fn(),
getParticipant = jest.fn().mockResolvedValue({}),
getParticipantEndpoint = jest.fn().mockResolvedValue(undefined)
getParticipantEndpoint = jest.fn().mockResolvedValue(undefined),
createFxQuoteResponse = jest.fn().mockResolvedValue({}),
createFxQuoteResponseConversionTerms = jest.fn().mockResolvedValue({}),
createFxQuoteResponseFxCharge = jest.fn().mockResolvedValue({}),
createFxQuoteResponseConversionTermsExtension = jest.fn().mockResolvedValue({}),
createFxQuoteResponseDuplicateCheck = jest.fn().mockResolvedValue({}),
newTransaction = jest.fn().mockResolvedValue({ commit, rollback }),
createFxQuoteDuplicateCheck = jest.fn().mockResolvedValue({}),
createFxQuote = jest.fn().mockResolvedValue({}),
createFxQuoteConversionTerms = jest.fn().mockResolvedValue({}),
createFxQuoteConversionTermsExtension = jest.fn().mockResolvedValue({}),
createFxQuoteError = jest.fn().mockResolvedValue({})
} = {}) => ({
getParticipant,
getParticipantEndpoint
getParticipantEndpoint,
createFxQuoteResponse,
createFxQuoteResponseConversionTerms,
createFxQuoteResponseFxCharge,
createFxQuoteResponseConversionTermsExtension,
createFxQuoteResponseDuplicateCheck,
newTransaction,
createFxQuoteDuplicateCheck,
createFxQuote,
createFxQuoteConversionTerms,
createFxQuoteConversionTermsExtension,
createFxQuoteError,
commit,
rollback
}),
proxyClient: ({
isConnected = jest.fn().mockReturnValue(true),
Expand Down
Loading

0 comments on commit 0d8fc24

Please sign in to comment.