Skip to content

Commit 28687fc

Browse files
committed
Improved ChainHttp, added unit tests
1 parent 3440d7c commit 28687fc

File tree

2 files changed

+62
-12
lines changed

2 files changed

+62
-12
lines changed

src/infrastructure/ChainHttp.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { from as observableFrom, Observable, throwError } from 'rxjs';
18-
import { catchError, map } from 'rxjs/operators';
17+
import { Observable } from 'rxjs';
1918
import { ChainRoutesApi } from 'symbol-openapi-typescript-node-client';
2019
import { BlockchainScore } from '../model/blockchain/BlockchainScore';
2120
import { UInt64 } from '../model/UInt64';
@@ -48,23 +47,18 @@ export class ChainHttp extends Http implements ChainRepository {
4847
* @returns Observable<UInt64>
4948
*/
5049
public getBlockchainHeight(): Observable<UInt64> {
51-
return observableFrom(this.chainRoutesApi.getChainHeight()).pipe(
52-
map(({body}) => UInt64.fromNumericString(body.height)),
53-
catchError((error) => throwError(this.errorHandling(error))),
54-
);
50+
return this.call(this.chainRoutesApi.getChainHeight(), (body) => UInt64.fromNumericString(body.height));
5551
}
5652

5753
/**
5854
* Gets current blockchain score
5955
* @returns Observable<BlockchainScore>
6056
*/
6157
public getChainScore(): Observable<BlockchainScore> {
62-
return observableFrom(this.chainRoutesApi.getChainScore()).pipe(
63-
map(({body}) => new BlockchainScore(
64-
UInt64.fromNumericString(body.scoreLow),
65-
UInt64.fromNumericString(body.scoreHigh),
66-
)),
67-
catchError((error) => throwError(this.errorHandling(error))),
58+
return this.call(this.chainRoutesApi.getChainScore(), (body) => new BlockchainScore(
59+
UInt64.fromNumericString(body.scoreLow),
60+
UInt64.fromNumericString(body.scoreHigh),
61+
),
6862
);
6963
}
7064
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)