-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathFinalizationHttp.spec.ts
105 lines (92 loc) · 4.22 KB
/
FinalizationHttp.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* Copyright 2020 NEM
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is 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.
*/
import { deepEqual } from 'assert';
import { expect } from 'chai';
import * as http from 'http';
import { firstValueFrom } from 'rxjs';
import {
BmTreeSignature,
FinalizationProofDTO,
FinalizationRoutesApi,
MessageGroup,
ParentPublicKeySignaturePair,
StageEnum,
} from 'symbol-openapi-typescript-fetch-client';
import { instance, mock, reset, when } from 'ts-mockito';
import { DtoMapping } from '../../src/core/utils/DtoMapping';
import { FinalizationHttp } from '../../src/infrastructure/FinalizationHttp';
import { FinalizationProof } from '../../src/model/finalization/FinalizationProof';
import { UInt64 } from '../../src/model/UInt64';
describe('FinalizationHttp', () => {
const url = 'http://someHost';
const response: http.IncomingMessage = mock();
const finalizationRoutesApi: FinalizationRoutesApi = mock();
const finalizationRepository = DtoMapping.assign(new FinalizationHttp(url), { finalizationRoutesApi: instance(finalizationRoutesApi) });
const dto = {} as FinalizationProofDTO;
dto.finalizationEpoch = 1;
dto.finalizationPoint = 1;
dto.hash = 'proofhash';
dto.height = '1';
dto.version = 1;
const mg = {} as MessageGroup;
mg.hashes = ['mghash'];
mg.height = '1';
mg.stage = StageEnum.NUMBER_0;
const ps = {} as ParentPublicKeySignaturePair;
ps.parentPublicKey = 'pubKey';
ps.signature = 'signature';
const tree = {} as BmTreeSignature;
tree.bottom = ps;
tree.root = ps;
mg.signatures = [tree];
dto.messageGroups = [mg];
before(() => {
reset(response);
reset(finalizationRoutesApi);
});
const assertDto = (model: FinalizationProof): void => {
expect(model).to.be.not.null;
expect(model.version).to.be.equals(dto.version);
expect(model.finalizationEpoch).to.be.equals(dto.finalizationEpoch);
expect(model.finalizationPoint).to.be.equals(dto.finalizationPoint);
expect(model.hash).to.be.equals(dto.hash);
expect(model.height.toString()).to.be.equals(dto.height);
expect(model.messageGroups[0].height.toString()).to.be.equals(dto.messageGroups[0].height);
expect(model.messageGroups[0].stage.valueOf()).to.be.equals(dto.messageGroups[0].stage.valueOf());
expect(model.messageGroups[0].hashes[0]).to.be.equals(dto.messageGroups[0].hashes[0]);
deepEqual(model.messageGroups[0].signatures, dto.messageGroups[0].signatures);
};
it('getFinalizationProofAtEpoch', async () => {
when(finalizationRoutesApi.getFinalizationProofAtEpoch(1)).thenReturn(Promise.resolve(dto));
const model = await firstValueFrom(finalizationRepository.getFinalizationProofAtEpoch(1));
assertDto(model);
});
it('getFinalizationProofAtHeight', async () => {
when(finalizationRoutesApi.getFinalizationProofAtHeight('1')).thenReturn(Promise.resolve(dto));
const model = await firstValueFrom(finalizationRepository.getFinalizationProofAtHeight(UInt64.fromUint(1)));
assertDto(model);
});
it('getFinalizationProofAtEpoch - Error', async () => {
when(finalizationRoutesApi.getFinalizationProofAtEpoch(1)).thenReject(new Error('Mocked Error'));
await firstValueFrom(finalizationRepository.getFinalizationProofAtEpoch(1)).catch((error) => expect(error).not.to.be.undefined);
});
it('getFinalizationProofAtHeight - Error', async () => {
when(finalizationRoutesApi.getFinalizationProofAtHeight('1')).thenReject(new Error('Mocked Error'));
await firstValueFrom(finalizationRepository.getFinalizationProofAtHeight(UInt64.fromUint(1))).catch(
(error) => expect(error).not.to.be.undefined,
);
});
});