Skip to content

Commit 5c6ef67

Browse files
committed
Added unit tests
1 parent 9ddf862 commit 5c6ef67

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

test/service/BlockService.spec.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 2019 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+
17+
import { expect } from 'chai';
18+
import { of as observableOf } from 'rxjs';
19+
import { deepEqual, instance, mock, when } from 'ts-mockito';
20+
import { BlockRepository } from '../../src/infrastructure/BlockRepository';
21+
import { ReceiptRepository } from '../../src/infrastructure/ReceiptRepository';
22+
import { RepositoryFactory } from '../../src/infrastructure/RepositoryFactory';
23+
import { Account } from '../../src/model/account/Account';
24+
import { BlockInfo } from '../../src/model/blockchain/BlockInfo';
25+
import { MerklePathItem } from '../../src/model/blockchain/MerklePathItem';
26+
import { MerkleProofInfo } from '../../src/model/blockchain/MerkleProofInfo';
27+
import { NetworkType } from '../../src/model/blockchain/NetworkType';
28+
import { UInt64 } from '../../src/model/UInt64';
29+
import { BlockService } from '../../src/service/BlockService';
30+
import { TestingAccount } from '../conf/conf.spec';
31+
32+
describe('BlockService', () => {
33+
34+
const mockBlockHash = 'D4EC16FCFE696EFDBF1820F68245C88135ACF4C6F888599C8E18BC09B9F08C7B';
35+
const leaf = '2717C8AAB0A21896D0C56375209E761F84383C3882F37A11D9D0159007263EB2';
36+
let blockService: BlockService;
37+
let account: Account;
38+
before(() => {
39+
account = TestingAccount;
40+
const mockBlockRepository = mock<BlockRepository>();
41+
const mockReceiptRepository = mock<ReceiptRepository>();
42+
const mockRepoFactory = mock<RepositoryFactory>();
43+
44+
when(mockBlockRepository.getBlockByHeight(deepEqual(UInt64.fromUint(1))))
45+
.thenReturn(observableOf(mockBlockInfo()));
46+
when(mockBlockRepository.getBlockByHeight(deepEqual(UInt64.fromUint(2))))
47+
.thenReturn(observableOf(mockBlockInfo(true)));
48+
when(mockBlockRepository.getMerkleTransaction(deepEqual(UInt64.fromUint(1)), leaf))
49+
.thenReturn(observableOf(mockMerklePath()));
50+
when(mockBlockRepository.getMerkleTransaction(deepEqual(UInt64.fromUint(2)), leaf))
51+
.thenReturn(observableOf(mockMerklePath()));
52+
when(mockReceiptRepository.getMerkleReceipts(deepEqual(UInt64.fromUint(1)), leaf))
53+
.thenReturn(observableOf(mockMerklePath()));
54+
when(mockReceiptRepository.getMerkleReceipts(deepEqual(UInt64.fromUint(2)), leaf))
55+
.thenReturn(observableOf(mockMerklePath()));
56+
const blockRepository = instance(mockBlockRepository);
57+
const receiptRepository = instance(mockReceiptRepository);
58+
59+
when(mockRepoFactory.createBlockRepository()).thenReturn(blockRepository);
60+
when(mockRepoFactory.createReceiptRepository()).thenReturn(receiptRepository);
61+
const repoFactory = instance(mockRepoFactory);
62+
blockService = new BlockService(repoFactory);
63+
});
64+
65+
it('should validate transaction', async () => {
66+
const result = await blockService.validateTransactionInBlock(leaf, UInt64.fromUint(1)).toPromise();
67+
expect(result).to.be.true;
68+
});
69+
70+
it('should validate transaction - wrong hash', async () => {
71+
const result = await blockService.validateTransactionInBlock(leaf, UInt64.fromUint(2)).toPromise();
72+
expect(result).to.be.false;
73+
});
74+
75+
it('should validate statement', async () => {
76+
const result = await blockService.validateStatementInBlock(leaf, UInt64.fromUint(1)).toPromise();
77+
expect(result).to.be.true;
78+
});
79+
80+
it('should validate statement - wrong hash', async () => {
81+
const result = await blockService.validateStatementInBlock(leaf, UInt64.fromUint(2)).toPromise();
82+
expect(result).to.be.false;
83+
});
84+
85+
function mockBlockInfo(isFake: boolean = false): BlockInfo {
86+
if (isFake) {
87+
return new BlockInfo(
88+
'hash', 'generationHash', UInt64.fromNumericString('0'), 1, 'signature', account.publicAccount,
89+
NetworkType.MIJIN_TEST, 0, 0, UInt64.fromUint(1), UInt64.fromUint(0), UInt64.fromUint(0), 0, 'previousHash',
90+
'fakeHash', 'fakeHash', 'stateHash', undefined,
91+
);
92+
}
93+
return new BlockInfo(
94+
'hash', 'generationHash', UInt64.fromNumericString('0'), 1, 'signature', account.publicAccount,
95+
NetworkType.MIJIN_TEST, 0, 0, UInt64.fromUint(1), UInt64.fromUint(0), UInt64.fromUint(0), 0, 'previousHash',
96+
mockBlockHash, mockBlockHash, 'stateHash', undefined,
97+
);
98+
}
99+
100+
function mockMerklePath(): MerkleProofInfo {
101+
return new MerkleProofInfo(
102+
[
103+
new MerklePathItem(1, 'CDE45D740536E5361F392025A44B26546A138958E69CD6F18D22908F8F11ECF2'),
104+
new MerklePathItem(2, '4EF55DAB8FEF9711B23DA71D2ACC58EFFF3969C3D572E06ACB898F99BED4827A'),
105+
new MerklePathItem(1, '1BB95470065ED69D184948A0175EDC2EAB9E86A0CEB47B648A58A02A5445AF66'),
106+
new MerklePathItem(2, 'D96B03809B8B198EFA5824191A979F7B85C0E9B7A6623DAFF38D4B2927EFDFB5'),
107+
new MerklePathItem(2, '9981EBDBCA8E36BA4D4D4A450072026AC8C85BA6497666219E0E049BE3362E51'),
108+
],
109+
);
110+
}
111+
});

0 commit comments

Comments
 (0)