|
| 1 | +/* |
| 2 | + * Copyright 2020 NEM |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { expect } from 'chai'; |
| 17 | +import * as http from 'http'; |
| 18 | +import { ChainRoutesApi, ChainScoreDTO, HeightInfoDTO, } from 'symbol-openapi-typescript-node-client'; |
| 19 | +import { instance, mock, reset, when } from 'ts-mockito'; |
| 20 | +import { DtoMapping } from '../../src/core/utils/DtoMapping'; |
| 21 | +import { ChainHttp } from '../../src/infrastructure/ChainHttp'; |
| 22 | +import { ChainRepository } from '../../src/infrastructure/ChainRepository'; |
| 23 | + |
| 24 | +describe('ChainHttp', () => { |
| 25 | + |
| 26 | + const url = 'http://someHost'; |
| 27 | + const response: http.IncomingMessage = mock(); |
| 28 | + const chainRoutesApi: ChainRoutesApi = mock(); |
| 29 | + const chainRepository: ChainRepository = DtoMapping.assign(new ChainHttp(url), {chainRoutesApi: instance(chainRoutesApi)}); |
| 30 | + |
| 31 | + before(() => { |
| 32 | + reset(response); |
| 33 | + reset(chainRoutesApi); |
| 34 | + }); |
| 35 | + |
| 36 | + it('getBlockchainHeight', async () => { |
| 37 | + const heightInfoDTO = new HeightInfoDTO(); |
| 38 | + heightInfoDTO.height = '3'; |
| 39 | + when(chainRoutesApi.getChainHeight()).thenReturn(Promise.resolve({response, body: heightInfoDTO})); |
| 40 | + const heightInfo = await chainRepository.getBlockchainHeight().toPromise(); |
| 41 | + expect(heightInfo).to.be.not.null; |
| 42 | + expect(heightInfo.toString()).to.be.equals('3'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('getChainScore', async () => { |
| 46 | + const chainScoreDTO = new ChainScoreDTO(); |
| 47 | + chainScoreDTO.scoreLow = '2'; |
| 48 | + chainScoreDTO.scoreHigh = '3'; |
| 49 | + when(chainRoutesApi.getChainScore()).thenReturn(Promise.resolve({response, body: chainScoreDTO})); |
| 50 | + const chainScore = await chainRepository.getChainScore().toPromise(); |
| 51 | + expect(chainScore).to.be.not.null; |
| 52 | + expect(chainScore.scoreLow.toString()).to.be.equals('2'); |
| 53 | + expect(chainScore.scoreHigh.toString()).to.be.equals('3'); |
| 54 | + }); |
| 55 | + |
| 56 | +}); |
0 commit comments