From 8fe2ffc3fafdbb9c6e629601fe4df5d5ff2d8200 Mon Sep 17 00:00:00 2001 From: Steven Liu Date: Mon, 30 Mar 2020 17:13:58 +0100 Subject: [PATCH 1/5] Fixed #509 --- test/service/MosaicService.spec.ts | 78 +++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 18 deletions(-) diff --git a/test/service/MosaicService.spec.ts b/test/service/MosaicService.spec.ts index d1fa62292d..eeeab2b4e3 100644 --- a/test/service/MosaicService.spec.ts +++ b/test/service/MosaicService.spec.ts @@ -15,10 +15,7 @@ */ import {expect} from 'chai'; -import {AccountHttp} from '../../src/infrastructure/AccountHttp'; -import {MosaicHttp} from '../../src/infrastructure/MosaicHttp'; -import {Address} from '../../src/model/account/Address'; -import { NetworkType } from '../../src/model/model'; +import { MosaicFlags, AccountInfo, AccountType } from '../../src/model/model'; import {Mosaic} from '../../src/model/mosaic/Mosaic'; import {MosaicId} from '../../src/model/mosaic/MosaicId'; import {MosaicInfo} from '../../src/model/mosaic/MosaicInfo'; @@ -26,13 +23,23 @@ import {UInt64} from '../../src/model/UInt64'; import {MosaicAmountView} from '../../src/service/MosaicAmountView'; import {MosaicService} from '../../src/service/MosaicService'; import {MosaicView} from '../../src/service/MosaicView'; -import * as conf from '../conf/conf.spec'; +import { AccountRepository } from '../../src/infrastructure/AccountRepository'; +import { mock, when, instance, deepEqual } from 'ts-mockito'; +import { MosaicRepository } from '../../src/infrastructure/MosaicRepository'; +import { of as observableOf } from 'rxjs'; +import { PublicAccount } from '../../src/model/account/PublicAccount'; +import { TestingAccount } from '../conf/conf.spec'; describe('MosaicService', () => { + const accountRepositoryMock = mock(); + const mosaicRepositoryMock = mock(); + it('mosaicsView', () => { const mosaicId = new MosaicId([3294802500, 2243684972]); - const mosaicService = new MosaicService( - new AccountHttp(conf.NIS2_URL), new MosaicHttp(conf.NIS2_URL, NetworkType.MIJIN_TEST)); + when(mosaicRepositoryMock.getMosaics(deepEqual([mosaicId]))).thenReturn(observableOf( + [buildMosaicInfo(mosaicId, TestingAccount.publicAccount)] + )); + const mosaicService = new MosaicService(instance(accountRepositoryMock), instance(mosaicRepositoryMock)); return mosaicService.mosaicsView([mosaicId]).subscribe((mosaicsView: MosaicView[]) => { const mosaicView = mosaicsView[0]; expect(mosaicView.mosaicInfo).to.be.an.instanceof(MosaicInfo); @@ -41,17 +48,26 @@ describe('MosaicService', () => { it('mosaicsView of no existing mosaicId', () => { const mosaicId = new MosaicId([1234, 1234]); - const mosaicService = new MosaicService( - new AccountHttp(conf.NIS2_URL), new MosaicHttp(conf.NIS2_URL, NetworkType.MIJIN_TEST)); + when(mosaicRepositoryMock.getMosaics(deepEqual([mosaicId]))).thenReturn(observableOf( + [] + )); + const mosaicService = new MosaicService(instance(accountRepositoryMock), instance(mosaicRepositoryMock)); return mosaicService.mosaicsView([mosaicId]).subscribe((mosaicsView: MosaicView[]) => { expect(mosaicsView.length).to.be.equal(0); }); }); it('mosaicsAmountView', () => { - const mosaicService = new MosaicService( - new AccountHttp(conf.NIS2_URL), new MosaicHttp(conf.NIS2_URL, NetworkType.MIJIN_TEST)); - return mosaicService.mosaicsAmountViewFromAddress(Address.createFromRawAddress('SARNASAS2BIAB6LMFA3FPMGBPGIJGK6IJETM3ZSP')) + const mosaicId = new MosaicId([3294802500, 2243684972]); + when(mosaicRepositoryMock.getMosaics(deepEqual([mosaicId]))).thenReturn(observableOf( + [buildMosaicInfo(mosaicId, TestingAccount.publicAccount)] + )); + when(accountRepositoryMock.getAccountInfo(deepEqual(TestingAccount.address))).thenReturn(observableOf( + buildAccountInfo(mosaicId), + )); + + const mosaicService = new MosaicService(instance(accountRepositoryMock), instance(mosaicRepositoryMock)); + return mosaicService.mosaicsAmountViewFromAddress(TestingAccount.address) .subscribe((mosaicsAmountView: MosaicAmountView[]) => { const mosaicAmountView = mosaicsAmountView[0]; expect(mosaicAmountView.mosaicInfo).to.be.an.instanceof(MosaicInfo); @@ -59,18 +75,29 @@ describe('MosaicService', () => { }); it('mosaicsAmountView of no existing account', () => { - const mosaicService = new MosaicService( - new AccountHttp(conf.NIS2_URL), new MosaicHttp(conf.NIS2_URL, NetworkType.MIJIN_TEST)); - return mosaicService.mosaicsAmountViewFromAddress(Address.createFromRawAddress('SCKBZAMIQ6F46QMZUANE6E33KA63KA7KEQ5X6WJW')) + const mosaicId = new MosaicId([3294802500, 2243684972]); + when(mosaicRepositoryMock.getMosaics(deepEqual([mosaicId]))).thenReturn(observableOf( + [] + )); + when(accountRepositoryMock.getAccountInfo(deepEqual(TestingAccount.address))).thenReturn(observableOf( + buildAccountInfo(mosaicId), + )); + + const mosaicService = new MosaicService(instance(accountRepositoryMock), instance(mosaicRepositoryMock)); + return mosaicService.mosaicsAmountViewFromAddress(TestingAccount.address) .subscribe((mosaicsAmountView: MosaicAmountView[]) => { expect(mosaicsAmountView.length).to.be.equal(0); }); }); it('mosaicsAmountView', () => { - const mosaic = new Mosaic(new MosaicId([3646934825, 3576016193]), UInt64.fromUint(1000)); - const mosaicService = new MosaicService( - new AccountHttp(conf.NIS2_URL), new MosaicHttp(conf.NIS2_URL, NetworkType.MIJIN_TEST)); + const mosaicId = new MosaicId([3294802500, 2243684972]); + when(mosaicRepositoryMock.getMosaics(deepEqual([mosaicId]))).thenReturn(observableOf( + [buildMosaicInfo(mosaicId, TestingAccount.publicAccount)] + )); + + const mosaicService = new MosaicService(instance(accountRepositoryMock), instance(mosaicRepositoryMock)); + const mosaic = new Mosaic(mosaicId, UInt64.fromUint(1000)); return mosaicService.mosaicsAmountView([mosaic]).subscribe((mosaicsAmountView: MosaicAmountView[]) => { const mosaicAmountView = mosaicsAmountView[0]; expect(mosaicAmountView.mosaicInfo).to.be.an.instanceof(MosaicInfo); @@ -78,4 +105,19 @@ describe('MosaicService', () => { }); }); + function buildMosaicInfo(mosaicId: MosaicId, publicAccount: PublicAccount): MosaicInfo { + return new MosaicInfo( + mosaicId, UInt64.fromUint(10), UInt64.fromUint(1), publicAccount, 0, + new MosaicFlags(1), 6, UInt64.fromUint(1) + ); + } + + function buildAccountInfo(mosaicId: MosaicId, isEmptyMosaic: boolean = false): AccountInfo { + return new AccountInfo( + TestingAccount.address, UInt64.fromUint(1), TestingAccount.publicKey, UInt64.fromUint(1), + AccountType.Main, '', [], isEmptyMosaic ? [] : [new Mosaic(mosaicId, UInt64.fromUint(100))], UInt64.fromUint(1), + UInt64.fromUint(1), + ); + } + }); From e83f6f3ccc9eee49d0c12dcd4e5627f1364d83f6 Mon Sep 17 00:00:00 2001 From: Steven Liu Date: Mon, 30 Mar 2020 18:41:45 +0100 Subject: [PATCH 2/5] Added mosaicHttp unit tests --- src/infrastructure/MosaicHttp.ts | 16 ++-- test/infrastructure/MosaicHttp.spec.ts | 114 +++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 test/infrastructure/MosaicHttp.spec.ts diff --git a/src/infrastructure/MosaicHttp.ts b/src/infrastructure/MosaicHttp.ts index ad2afc8c4e..44c277b353 100644 --- a/src/infrastructure/MosaicHttp.ts +++ b/src/infrastructure/MosaicHttp.ts @@ -16,7 +16,7 @@ import { from as observableFrom, Observable, throwError } from 'rxjs'; import { catchError, map, mergeMap } from 'rxjs/operators'; -import { MosaicRoutesApi } from 'symbol-openapi-typescript-node-client'; +import { MosaicRoutesApi, MosaicIds, AccountIds } from 'symbol-openapi-typescript-node-client'; import { Address } from '../model/account/Address'; import { PublicAccount } from '../model/account/PublicAccount'; import { MosaicFlags } from '../model/mosaic/MosaicFlags'; @@ -86,12 +86,11 @@ export class MosaicHttp extends Http implements MosaicRepository { * @returns Observable */ public getMosaics(mosaicIds: MosaicId[]): Observable { - const mosaicIdsBody = { - mosaicIds: mosaicIds.map((id) => id.toHex()), - }; + const ids = new MosaicIds(); + ids.mosaicIds = mosaicIds.map((id) => id.toHex()); return this.networkTypeObservable.pipe( mergeMap((networkType) => observableFrom( - this.mosaicRoutesApi.getMosaics(mosaicIdsBody)).pipe( + this.mosaicRoutesApi.getMosaics(ids)).pipe( map(({body}) => body.map((mosaicInfoDTO) => { return new MosaicInfo( new MosaicId(mosaicInfoDTO.mosaic.id), @@ -140,12 +139,11 @@ export class MosaicHttp extends Http implements MosaicRepository { * @param addresses Array of addresses */ public getMosaicsFromAccounts(addresses: Address[]): Observable { - const accountIdsBody = { - addresses: addresses.map((address) => address.plain()), - }; + const accountIds = new AccountIds(); + accountIds.addresses = addresses.map((address) => address.plain()); return this.networkTypeObservable.pipe( mergeMap((networkType) => observableFrom( - this.mosaicRoutesApi.getMosaicsFromAccounts(accountIdsBody)).pipe( + this.mosaicRoutesApi.getMosaicsFromAccounts(accountIds)).pipe( map(({body}) => body.mosaics.map((mosaicInfoDTO) => { return new MosaicInfo( new MosaicId(mosaicInfoDTO.id), diff --git a/test/infrastructure/MosaicHttp.spec.ts b/test/infrastructure/MosaicHttp.spec.ts new file mode 100644 index 0000000000..bb6251598e --- /dev/null +++ b/test/infrastructure/MosaicHttp.spec.ts @@ -0,0 +1,114 @@ +/* + * 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 { expect } from 'chai'; +import * as http from 'http'; +import { + Mosaic, + MosaicRoutesApi, + MosaicInfoDTO, + MosaicDTO, + MosaicsInfoDTO, + MosaicIds, +} from 'symbol-openapi-typescript-node-client'; +import { instance, mock, reset, when, deepEqual } from 'ts-mockito'; +import { DtoMapping } from '../../src/core/utils/DtoMapping'; +import { MosaicRepository } from '../../src/infrastructure/MosaicRepository'; +import { MosaicHttp } from '../../src/infrastructure/MosaicHttp'; +import { NetworkType } from '../../src/model/network/NetworkType'; +import { MosaicId } from '../../src/model/mosaic/MosaicId'; +import { MosaicInfo } from '../../src/model/mosaic/MosaicInfo'; +import { PublicAccount } from '../../src/model/account/PublicAccount'; +import { AccountIds } from 'symbol-openapi-typescript-node-client'; + +describe('MosaicHttp', () => { + + const publicAccount = + PublicAccount.createFromPublicKey('9801508C58666C746F471538E43002B85B1CD542F9874B2861183919BA8787B6', NetworkType.MIJIN_TEST); + const address = publicAccount.address; + const mosaicId = new MosaicId('941299B2B7E1291C'); + const mosaic = new Mosaic(); + mosaic.amount = '777'; + mosaic.id = mosaicId.toHex(); + + const mosaicDto = new MosaicDTO(); + mosaicDto.divisibility = 6; + mosaicDto.duration = '10'; + mosaicDto.flags = 1; + mosaicDto.id = mosaicId.toHex(); + mosaicDto.ownerAddress = address.encoded(); + mosaicDto.ownerPublicKey = publicAccount.publicKey; + mosaicDto.revision = 0; + mosaicDto.startHeight = '1'; + mosaicDto.supply = '100'; + + const mosaicInfoDto = new MosaicInfoDTO(); + const mosaicsInfoDto = new MosaicsInfoDTO(); + mosaicInfoDto.mosaic = mosaicDto; + mosaicsInfoDto.mosaics = [mosaicDto]; + + const url = 'http://someHost'; + const response: http.IncomingMessage = mock(); + const mosaicRoutesApi: MosaicRoutesApi = mock(); + const mosaicRepository: MosaicRepository = DtoMapping.assign(new MosaicHttp(url, NetworkType.MIJIN_TEST), + {mosaicRoutesApi: instance(mosaicRoutesApi)}); + + before(() => { + reset(response); + reset(mosaicRoutesApi); + }); + + function assertMosaicInfo(mosaicInfo: MosaicInfo) { + expect(mosaicInfo).to.be.not.null; + expect(mosaicInfo.divisibility).to.be.equals(6); + expect(mosaicInfo.duration.toString()).to.be.equals(mosaicDto.duration); + expect(mosaicInfo.flags.getValue()).to.be.equals(mosaicDto.flags); + expect(mosaicInfo.height.toString()).to.be.equals(mosaicDto.startHeight); + expect(mosaicInfo.supply.toString()).to.be.equals(mosaicDto.supply); + expect(mosaicInfo.owner.publicKey).to.be.equals(mosaicDto.ownerPublicKey); + expect(mosaicInfo.owner.address.encoded()).to.be.equals(mosaicDto.ownerAddress); + expect(mosaicInfo.revision).to.be.equals(mosaicDto.revision); + expect(mosaicInfo.id.toHex()).to.be.equals(mosaicId.toHex()); + } + + it('getMosaic', async () => { + when(mosaicRoutesApi.getMosaic(mosaicId.toHex())).thenReturn(Promise.resolve({response, body: mosaicInfoDto})); + const mosaicInfo = await mosaicRepository.getMosaic(mosaicId).toPromise(); + assertMosaicInfo(mosaicInfo); + }); + + it('getMosaics', async () => { + const mosaicIds = new MosaicIds(); + mosaicIds.mosaicIds = [mosaicId.toHex()]; + when(mosaicRoutesApi.getMosaics(deepEqual(mosaicIds))).thenReturn(Promise.resolve({response, body: [mosaicInfoDto]})); + const mosaicInfos = await mosaicRepository.getMosaics([mosaicId]).toPromise(); + assertMosaicInfo(mosaicInfos[0]); + }); + + it('getMosaicsFromAccount', async () => { + when(mosaicRoutesApi.getMosaicsFromAccount(address.plain())).thenReturn(Promise.resolve({response, body: mosaicsInfoDto})); + const mosaicsInfo = await mosaicRepository.getMosaicsFromAccount(address).toPromise(); + assertMosaicInfo(mosaicsInfo[0]); + }); + + it('getMosaicsFromAccounts', async () => { + const accountIds = new AccountIds(); + accountIds.addresses = [address.plain()]; + when(mosaicRoutesApi.getMosaicsFromAccounts(deepEqual(accountIds))) + .thenReturn(Promise.resolve({response, body: mosaicsInfoDto})); + const mosaicsInfo = await mosaicRepository.getMosaicsFromAccounts([address]).toPromise(); + assertMosaicInfo(mosaicsInfo[0]); + }); +}); From 6685bccac07d549713578c7c306ffba83ecef971 Mon Sep 17 00:00:00 2001 From: Steven Liu Date: Mon, 30 Mar 2020 19:15:18 +0100 Subject: [PATCH 3/5] Refactored mosaicHttp to use abstract caller --- src/infrastructure/MosaicHttp.ts | 118 ++++++++++++++----------------- 1 file changed, 55 insertions(+), 63 deletions(-) diff --git a/src/infrastructure/MosaicHttp.ts b/src/infrastructure/MosaicHttp.ts index 44c277b353..a401a5c60a 100644 --- a/src/infrastructure/MosaicHttp.ts +++ b/src/infrastructure/MosaicHttp.ts @@ -16,7 +16,7 @@ import { from as observableFrom, Observable, throwError } from 'rxjs'; import { catchError, map, mergeMap } from 'rxjs/operators'; -import { MosaicRoutesApi, MosaicIds, AccountIds } from 'symbol-openapi-typescript-node-client'; +import { MosaicRoutesApi, MosaicIds, AccountIds, MosaicInfoDTO, MosaicDTO } from 'symbol-openapi-typescript-node-client'; import { Address } from '../model/account/Address'; import { PublicAccount } from '../model/account/PublicAccount'; import { MosaicFlags } from '../model/mosaic/MosaicFlags'; @@ -63,21 +63,10 @@ export class MosaicHttp extends Http implements MosaicRepository { */ public getMosaic(mosaicId: MosaicId): Observable { return this.networkTypeObservable.pipe( - mergeMap((networkType) => observableFrom( - this.mosaicRoutesApi.getMosaic(mosaicId.toHex())).pipe( - map(({body}) => new MosaicInfo( - new MosaicId(body.mosaic.id), - UInt64.fromNumericString(body.mosaic.supply), - UInt64.fromNumericString(body.mosaic.startHeight), - PublicAccount.createFromPublicKey(body.mosaic.ownerPublicKey, networkType), - body.mosaic.revision, - new MosaicFlags(body.mosaic.flags), - body.mosaic.divisibility, - UInt64.fromNumericString(body.mosaic.duration), - )), + mergeMap((networkType) => + this.call(this.mosaicRoutesApi.getMosaic(mosaicId.toHex()), (body) => this.toMosaicInfo(body, networkType))), catchError((error) => throwError(this.errorHandling(error))), - )), - ); + ); } /** @@ -89,23 +78,9 @@ export class MosaicHttp extends Http implements MosaicRepository { const ids = new MosaicIds(); ids.mosaicIds = mosaicIds.map((id) => id.toHex()); return this.networkTypeObservable.pipe( - mergeMap((networkType) => observableFrom( - this.mosaicRoutesApi.getMosaics(ids)).pipe( - map(({body}) => body.map((mosaicInfoDTO) => { - return new MosaicInfo( - new MosaicId(mosaicInfoDTO.mosaic.id), - UInt64.fromNumericString(mosaicInfoDTO.mosaic.supply), - UInt64.fromNumericString(mosaicInfoDTO.mosaic.startHeight), - PublicAccount.createFromPublicKey(mosaicInfoDTO.mosaic.ownerPublicKey, networkType), - mosaicInfoDTO.mosaic.revision, - new MosaicFlags(mosaicInfoDTO.mosaic.flags), - mosaicInfoDTO.mosaic.divisibility, - UInt64.fromNumericString(mosaicInfoDTO.mosaic.duration), - ); - })), - catchError((error) => throwError(this.errorHandling(error))), - ), - ), + mergeMap((networkType) => + this.call(this.mosaicRoutesApi.getMosaics(ids), (body) => body.map((b) => this.toMosaicInfo(b, networkType)))), + catchError((error) => throwError(this.errorHandling(error))), ); } @@ -116,20 +91,11 @@ export class MosaicHttp extends Http implements MosaicRepository { */ public getMosaicsFromAccount(address: Address): Observable { return this.networkTypeObservable.pipe( - mergeMap((networkType) => observableFrom( - this.mosaicRoutesApi.getMosaicsFromAccount(address.plain())).pipe( - map(({body}) => body.mosaics.map((mosaicInfo) => - new MosaicInfo( - new MosaicId(mosaicInfo.id), - UInt64.fromNumericString(mosaicInfo.supply), - UInt64.fromNumericString(mosaicInfo.startHeight), - PublicAccount.createFromPublicKey(mosaicInfo.ownerPublicKey, networkType), - mosaicInfo.revision, - new MosaicFlags(mosaicInfo.flags), - mosaicInfo.divisibility, - UInt64.fromNumericString(mosaicInfo.duration)))), - catchError((error) => throwError(this.errorHandling(error))), - )), + mergeMap((networkType) => + this.call(this.mosaicRoutesApi.getMosaicsFromAccount(address.plain()), + (body) => body.mosaics.map((b) => this.toMosaicInfoFromMosaicDto(b, networkType)))), + catchError((error) => throwError(this.errorHandling(error))), + ); } @@ -142,23 +108,49 @@ export class MosaicHttp extends Http implements MosaicRepository { const accountIds = new AccountIds(); accountIds.addresses = addresses.map((address) => address.plain()); return this.networkTypeObservable.pipe( - mergeMap((networkType) => observableFrom( - this.mosaicRoutesApi.getMosaicsFromAccounts(accountIds)).pipe( - map(({body}) => body.mosaics.map((mosaicInfoDTO) => { - return new MosaicInfo( - new MosaicId(mosaicInfoDTO.id), - UInt64.fromNumericString(mosaicInfoDTO.supply), - UInt64.fromNumericString(mosaicInfoDTO.startHeight), - PublicAccount.createFromPublicKey(mosaicInfoDTO.ownerPublicKey, networkType), - mosaicInfoDTO.revision, - new MosaicFlags(mosaicInfoDTO.flags), - mosaicInfoDTO.divisibility, - UInt64.fromNumericString(mosaicInfoDTO.duration), - ); - })), - catchError((error) => throwError(this.errorHandling(error))), - ), - ), + mergeMap((networkType) => + this.call(this.mosaicRoutesApi.getMosaicsFromAccounts(accountIds), + (body) => body.mosaics.map((b) => this.toMosaicInfoFromMosaicDto(b, networkType)))), + catchError((error) => throwError(this.errorHandling(error))), + + ); + } + + /** + * Maps MosaicInfoDTO to MosaicInfo + * + * @param mosaicInfo the dto object. + * @returns the model object + */ + private toMosaicInfo(mosaicInfo: MosaicInfoDTO, networkType: NetworkType): MosaicInfo { + return new MosaicInfo( + new MosaicId(mosaicInfo.mosaic.id), + UInt64.fromNumericString(mosaicInfo.mosaic.supply), + UInt64.fromNumericString(mosaicInfo.mosaic.startHeight), + PublicAccount.createFromPublicKey(mosaicInfo.mosaic.ownerPublicKey, networkType), + mosaicInfo.mosaic.revision, + new MosaicFlags(mosaicInfo.mosaic.flags), + mosaicInfo.mosaic.divisibility, + UInt64.fromNumericString(mosaicInfo.mosaic.duration), + ); + } + + /** + * Maps MosaicDTO to MosaicInfo + * + * @param mosaicInfo the dto object. + * @returns the model object + */ + private toMosaicInfoFromMosaicDto(mosaicInfo: MosaicDTO, networkType: NetworkType): MosaicInfo { + return new MosaicInfo( + new MosaicId(mosaicInfo.id), + UInt64.fromNumericString(mosaicInfo.supply), + UInt64.fromNumericString(mosaicInfo.startHeight), + PublicAccount.createFromPublicKey(mosaicInfo.ownerPublicKey, networkType), + mosaicInfo.revision, + new MosaicFlags(mosaicInfo.flags), + mosaicInfo.divisibility, + UInt64.fromNumericString(mosaicInfo.duration), ); } } From 3c05413f67e11b5bc5ece9de30bde0ab6fb6a840 Mon Sep 17 00:00:00 2001 From: Steven Liu Date: Mon, 30 Mar 2020 19:36:06 +0100 Subject: [PATCH 4/5] Add error trapping unit test --- test/infrastructure/MosaicHttp.spec.ts | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/infrastructure/MosaicHttp.spec.ts b/test/infrastructure/MosaicHttp.spec.ts index bb6251598e..715095e28d 100644 --- a/test/infrastructure/MosaicHttp.spec.ts +++ b/test/infrastructure/MosaicHttp.spec.ts @@ -32,6 +32,7 @@ import { MosaicId } from '../../src/model/mosaic/MosaicId'; import { MosaicInfo } from '../../src/model/mosaic/MosaicInfo'; import { PublicAccount } from '../../src/model/account/PublicAccount'; import { AccountIds } from 'symbol-openapi-typescript-node-client'; +import { assert } from 'chai'; describe('MosaicHttp', () => { @@ -111,4 +112,32 @@ describe('MosaicHttp', () => { const mosaicsInfo = await mosaicRepository.getMosaicsFromAccounts([address]).toPromise(); assertMosaicInfo(mosaicsInfo[0]); }); + + it('getMosaic - Error', async () => { + when(mosaicRoutesApi.getMosaic(mosaicId.toHex())).thenThrow(new Error('Mocked Error')); + await mosaicRepository.getMosaic(mosaicId).toPromise().catch((error) => + expect(error).not.to.be.undefined); + }); + + it('getMosaics - Error', async () => { + const mosaicIds = new MosaicIds(); + mosaicIds.mosaicIds = [mosaicId.toHex()]; + when(mosaicRoutesApi.getMosaics(deepEqual(mosaicIds))).thenThrow(new Error('Mocked Error')); + await mosaicRepository.getMosaics([mosaicId]).toPromise().catch((error) => + expect(error).not.to.be.undefined); + }); + + it('getMosaicsFromAccount - Error', async () => { + when(mosaicRoutesApi.getMosaicsFromAccount(address.plain())).thenThrow(new Error('Mocked Error')); + await mosaicRepository.getMosaicsFromAccount(address).toPromise().catch((error) => + expect(error).not.to.be.undefined); + }); + + it('getMosaicsFromAccounts - Error', async () => { + const accountIds = new AccountIds(); + accountIds.addresses = [address.plain()]; + when(mosaicRoutesApi.getMosaicsFromAccounts(deepEqual(accountIds))).thenThrow(new Error('Mocked Error')); + await mosaicRepository.getMosaicsFromAccounts([address]).toPromise().catch((error) => + expect(error).not.to.be.undefined); + }); }); From aa1ec2dd697a84d5f30cc59535e49338bc4170c9 Mon Sep 17 00:00:00 2001 From: Steven Liu Date: Mon, 30 Mar 2020 20:01:07 +0100 Subject: [PATCH 5/5] removed unused error catcher --- src/infrastructure/MosaicHttp.ts | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/infrastructure/MosaicHttp.ts b/src/infrastructure/MosaicHttp.ts index a401a5c60a..42491a23f1 100644 --- a/src/infrastructure/MosaicHttp.ts +++ b/src/infrastructure/MosaicHttp.ts @@ -14,8 +14,8 @@ * limitations under the License. */ -import { from as observableFrom, Observable, throwError } from 'rxjs'; -import { catchError, map, mergeMap } from 'rxjs/operators'; +import { Observable, throwError } from 'rxjs'; +import { catchError, mergeMap } from 'rxjs/operators'; import { MosaicRoutesApi, MosaicIds, AccountIds, MosaicInfoDTO, MosaicDTO } from 'symbol-openapi-typescript-node-client'; import { Address } from '../model/account/Address'; import { PublicAccount } from '../model/account/PublicAccount'; @@ -65,7 +65,6 @@ export class MosaicHttp extends Http implements MosaicRepository { return this.networkTypeObservable.pipe( mergeMap((networkType) => this.call(this.mosaicRoutesApi.getMosaic(mosaicId.toHex()), (body) => this.toMosaicInfo(body, networkType))), - catchError((error) => throwError(this.errorHandling(error))), ); } @@ -80,7 +79,6 @@ export class MosaicHttp extends Http implements MosaicRepository { return this.networkTypeObservable.pipe( mergeMap((networkType) => this.call(this.mosaicRoutesApi.getMosaics(ids), (body) => body.map((b) => this.toMosaicInfo(b, networkType)))), - catchError((error) => throwError(this.errorHandling(error))), ); } @@ -94,8 +92,6 @@ export class MosaicHttp extends Http implements MosaicRepository { mergeMap((networkType) => this.call(this.mosaicRoutesApi.getMosaicsFromAccount(address.plain()), (body) => body.mosaics.map((b) => this.toMosaicInfoFromMosaicDto(b, networkType)))), - catchError((error) => throwError(this.errorHandling(error))), - ); } @@ -111,8 +107,6 @@ export class MosaicHttp extends Http implements MosaicRepository { mergeMap((networkType) => this.call(this.mosaicRoutesApi.getMosaicsFromAccounts(accountIds), (body) => body.mosaics.map((b) => this.toMosaicInfoFromMosaicDto(b, networkType)))), - catchError((error) => throwError(this.errorHandling(error))), - ); }