|
| 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 { UnresolvedMapping } from '../../../src/core/utils/UnresolvedMapping'; |
| 19 | +import { CreateStatementFromDTO } from '../../../src/infrastructure/receipt/CreateReceiptFromDTO'; |
| 20 | +import { Account } from '../../../src/model/account/Account'; |
| 21 | +import { NetworkType } from '../../../src/model/blockchain/NetworkType'; |
| 22 | +import { Address, MosaicId, NamespaceId, ResolutionType } from '../../../src/model/model'; |
| 23 | + |
| 24 | +describe('ResolutionStatement', () => { |
| 25 | + let account: Account; |
| 26 | + let transactionStatementsDTO; |
| 27 | + let addressResolutionStatementsDTO; |
| 28 | + let mosaicResolutionStatementsDTO; |
| 29 | + let statementDTO; |
| 30 | + |
| 31 | + before(() => { |
| 32 | + account = Account.createFromPrivateKey('81C18245507F9C15B61BDEDAFA2C10D9DC2C4E401E573A10935D45AA2A461FD5', NetworkType.MIJIN_TEST); |
| 33 | + transactionStatementsDTO = [ |
| 34 | + { |
| 35 | + statement: { |
| 36 | + height: '1473', |
| 37 | + source: { |
| 38 | + primaryId: 0, |
| 39 | + secondaryId: 0, |
| 40 | + }, |
| 41 | + receipts: [ |
| 42 | + { |
| 43 | + version: 1, |
| 44 | + type: 8515, |
| 45 | + targetPublicKey: 'B2708D49C46F8AB5CDBD7A09C959EEA12E4A782592F3D1D3D17D54622E655D7F', |
| 46 | + mosaicId: '504677C3281108DB', |
| 47 | + amount: '0', |
| 48 | + }, |
| 49 | + ], |
| 50 | + }, |
| 51 | + }, |
| 52 | + ]; |
| 53 | + addressResolutionStatementsDTO = [ |
| 54 | + { |
| 55 | + statement: { |
| 56 | + height: '1473', |
| 57 | + unresolved: '9156258DE356F030A500000000000000000000000000000000', |
| 58 | + resolutionEntries: [ |
| 59 | + { |
| 60 | + source: { |
| 61 | + primaryId: 1, |
| 62 | + secondaryId: 0, |
| 63 | + }, |
| 64 | + resolved: '901D8D4741F80299E66BF7FEEB4F30943DA7B68E068B182319', |
| 65 | + }, |
| 66 | + ], |
| 67 | + }, |
| 68 | + }, |
| 69 | + ]; |
| 70 | + mosaicResolutionStatementsDTO = [ |
| 71 | + { |
| 72 | + statement: { |
| 73 | + height: '1473', |
| 74 | + unresolved: '85BBEA6CC462B244', |
| 75 | + resolutionEntries: [ |
| 76 | + { |
| 77 | + source: { |
| 78 | + primaryId: 1, |
| 79 | + secondaryId: 0, |
| 80 | + }, |
| 81 | + resolved: '504677C3281108DB', |
| 82 | + }, |
| 83 | + { |
| 84 | + source: { |
| 85 | + primaryId: 3, |
| 86 | + secondaryId: 5, |
| 87 | + }, |
| 88 | + resolved: '401F622A3111A3E4', |
| 89 | + }, |
| 90 | + ], |
| 91 | + }, |
| 92 | + }, |
| 93 | + { |
| 94 | + statement: { |
| 95 | + height: '1473', |
| 96 | + unresolved: 'E81F622A5B11A340', |
| 97 | + resolutionEntries: [ |
| 98 | + { |
| 99 | + source: { |
| 100 | + primaryId: 3, |
| 101 | + secondaryId: 1, |
| 102 | + }, |
| 103 | + resolved: '756482FB80FD406C', |
| 104 | + }, |
| 105 | + ], |
| 106 | + }, |
| 107 | + }, |
| 108 | + ]; |
| 109 | + |
| 110 | + statementDTO = { |
| 111 | + transactionStatements: transactionStatementsDTO, |
| 112 | + addressResolutionStatements: addressResolutionStatementsDTO, |
| 113 | + mosaicResolutionStatements: mosaicResolutionStatementsDTO, |
| 114 | + }; |
| 115 | + }); |
| 116 | + |
| 117 | + it('should get resolve entry when both primaryId and secondaryId matched', () => { |
| 118 | + const statement = CreateStatementFromDTO(statementDTO, NetworkType.MIJIN_TEST); |
| 119 | + const entry = statement.addressResolutionStatements[0].getResolutionEntryById(1, 0); |
| 120 | + |
| 121 | + expect(entry!.resolved instanceof Address).to.be.true; |
| 122 | + expect((entry!.resolved as Address).equals(account.address)).to.be.true; |
| 123 | + }); |
| 124 | + |
| 125 | + it('should get resolved entry when primaryId is greater than max', () => { |
| 126 | + const statement = CreateStatementFromDTO(statementDTO, NetworkType.MIJIN_TEST); |
| 127 | + const entry = statement.mosaicResolutionStatements[0].getResolutionEntryById(4, 0); |
| 128 | + expect(entry!.source.primaryId).to.be.equal(3); |
| 129 | + expect(entry!.source.secondaryId).to.be.equal(5); |
| 130 | + expect(entry!.resolved instanceof MosaicId).to.be.true; |
| 131 | + expect((entry!.resolved as MosaicId).equals(new MosaicId('401F622A3111A3E4'))).to.be.true; |
| 132 | + }); |
| 133 | + |
| 134 | + it('should get resolved entry when primaryId is in middle of 2 pirmaryIds', () => { |
| 135 | + const statement = CreateStatementFromDTO(statementDTO, NetworkType.MIJIN_TEST); |
| 136 | + const entry = statement.mosaicResolutionStatements[0].getResolutionEntryById(2, 1); |
| 137 | + expect(entry!.source.primaryId).to.be.equal(1); |
| 138 | + expect(entry!.source.secondaryId).to.be.equal(0); |
| 139 | + expect(entry!.resolved instanceof MosaicId).to.be.true; |
| 140 | + expect((entry!.resolved as MosaicId).equals(new MosaicId('504677C3281108DB'))).to.be.true; |
| 141 | + }); |
| 142 | + |
| 143 | + it('should get resolved entry when primaryId matches but not secondaryId', () => { |
| 144 | + const statement = CreateStatementFromDTO(statementDTO, NetworkType.MIJIN_TEST); |
| 145 | + const entry = statement.mosaicResolutionStatements[0].getResolutionEntryById(3, 6); |
| 146 | + expect(entry!.source.primaryId).to.be.equal(3); |
| 147 | + expect(entry!.source.secondaryId).to.be.equal(5); |
| 148 | + expect(entry!.resolved instanceof MosaicId).to.be.true; |
| 149 | + expect((entry!.resolved as MosaicId).equals(new MosaicId('401F622A3111A3E4'))).to.be.true; |
| 150 | + }); |
| 151 | + |
| 152 | + it('should get resolved entry when primaryId matches but secondaryId less than minimum', () => { |
| 153 | + const statement = CreateStatementFromDTO(statementDTO, NetworkType.MIJIN_TEST); |
| 154 | + const entry = statement.mosaicResolutionStatements[0].getResolutionEntryById(3, 1); |
| 155 | + expect(entry!.source.primaryId).to.be.equal(1); |
| 156 | + expect(entry!.source.secondaryId).to.be.equal(0); |
| 157 | + expect(entry!.resolved instanceof MosaicId).to.be.true; |
| 158 | + expect((entry!.resolved as MosaicId).equals(new MosaicId('504677C3281108DB'))).to.be.true; |
| 159 | + }); |
| 160 | + |
| 161 | + it('should return undefined', () => { |
| 162 | + const statement = CreateStatementFromDTO(statementDTO, NetworkType.MIJIN_TEST); |
| 163 | + const entry = statement.addressResolutionStatements[0].getResolutionEntryById(0, 0); |
| 164 | + expect(entry).to.be.undefined; |
| 165 | + }); |
| 166 | + |
| 167 | +}); |
0 commit comments