From b12dfd6174a3d72f8c3972dfa4ecba3b89dcc641 Mon Sep 17 00:00:00 2001 From: arafath Date: Tue, 31 Dec 2019 12:36:56 +0530 Subject: [PATCH 1/2] Issue: #SB-16374 fix: REPLACED: dependency sunbird-client-data-models -> @project-sunbird/client-services --- package-lock.json | 22 ++++++++++---------- package.json | 2 +- src/content/def/content.ts | 4 ++-- src/course/def/batch.ts | 2 +- src/course/def/course.ts | 2 +- src/faq/def/faq.ts | 2 +- src/framework/def/channel.ts | 2 +- src/framework/def/framework-category-code.ts | 2 +- src/framework/def/framework.ts | 2 +- src/page/def/page-assemble.ts | 2 +- src/profile/def/server-profile.ts | 2 +- 11 files changed, 22 insertions(+), 22 deletions(-) diff --git a/package-lock.json b/package-lock.json index 96ed4ba31..f02ee9a74 100755 --- a/package-lock.json +++ b/package-lock.json @@ -1285,6 +1285,10 @@ "@types/yargs": "^12.0.9" } }, + "@project-sunbird/client-services": { + "version": "git+https://github.com/Sunbird-Ed/sunbird-client-services.git#1d0a9d592d397f9a2ef032c29ec04db19b5b5597", + "from": "git+https://github.com/Sunbird-Ed/sunbird-client-services.git" + }, "@types/babel__core": { "version": "7.1.2", "resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.1.2.tgz", @@ -2061,7 +2065,7 @@ }, "util": { "version": "0.10.3", - "resolved": "http://registry.npmjs.org/util/-/util-0.10.3.tgz", + "resolved": "https://registry.npmjs.org/util/-/util-0.10.3.tgz", "integrity": "sha1-evsa/lCAUkZInj23/g7TeTNqwPk=", "dev": true, "requires": { @@ -3293,7 +3297,7 @@ }, "diffie-hellman": { "version": "5.0.3", - "resolved": "http://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz", + "resolved": "https://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz", "integrity": "sha512-kqag/Nl+f3GwyK25fhUMYj81BUOrZ9IuJsjIcDE5icNM9FJHAVm3VcUDxdLPoQtTuUylWm6ZIknYJwwaPxsUzg==", "dev": true, "requires": { @@ -9099,9 +9103,9 @@ } }, "rxjs": { - "version": "6.5.3", - "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.5.3.tgz", - "integrity": "sha512-wuYsAYYFdWTAnAaPoKGNhfpWwKZbJW+HgAJ+mImp+Epl7BG8oNWBCTyRM8gba9k4lk8BgWdoYm21Mo/RYhhbgA==", + "version": "6.5.4", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.5.4.tgz", + "integrity": "sha512-naMQXcgEo3csAEGvw/NydRA0fuS2nDZJiw1YUWFKU7aPPAPGZEsD4Iimit96qwCieH6y614MCLYwdkrWx7z/7Q==", "dev": true, "requires": { "tslib": "^1.9.0" @@ -9532,7 +9536,7 @@ }, "sha.js": { "version": "2.4.11", - "resolved": "http://registry.npmjs.org/sha.js/-/sha.js-2.4.11.tgz", + "resolved": "https://registry.npmjs.org/sha.js/-/sha.js-2.4.11.tgz", "integrity": "sha512-QMEp5B7cftE7APOjk5Y6xgrbWu+WkLVQwk8JNjZ8nKRciZaByEW6MubieAiToS7+dwvrjGhH8jRXz3MVd0AYqQ==", "dev": true, "requires": { @@ -9955,10 +9959,6 @@ "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=", "dev": true }, - "sunbird-client-data-models": { - "version": "git+https://github.com/arfath-gwl/sunbird-client-data-models.git#e1a86e2d2fe04e1534281eb29198c7d78c458a9e", - "from": "git+https://github.com/arfath-gwl/sunbird-client-data-models.git#v1.0.4" - }, "supports-color": { "version": "5.5.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", @@ -10621,7 +10621,7 @@ }, "tty-browserify": { "version": "0.0.0", - "resolved": "http://registry.npmjs.org/tty-browserify/-/tty-browserify-0.0.0.tgz", + "resolved": "https://registry.npmjs.org/tty-browserify/-/tty-browserify-0.0.0.tgz", "integrity": "sha1-oVe6QC2iTpv5V/mqadUk7tQpAaY=", "dev": true }, diff --git a/package.json b/package.json index 1b12c891b..eec585c61 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "author": "", "license": "ISC", "dependencies": { + "@project-sunbird/client-services": "git+https://github.com/Sunbird-Ed/sunbird-client-services.git", "crypto-js": "^3.1.9-1", "dayjs": "^1.8.17", "inversify": "^5.0.1", @@ -26,7 +27,6 @@ "pako": "^1.0.8", "qs": "^6.6.0", "reflect-metadata": "^0.1.13", - "sunbird-client-data-models": "git+https://github.com/arfath-gwl/sunbird-client-data-models#v1.0.4", "typescript-collections": "^1.3.2", "uuid": "^3.3.2", "whatwg-fetch": "^3.0.0" diff --git a/src/content/def/content.ts b/src/content/def/content.ts index 097320853..30b20a02b 100644 --- a/src/content/def/content.ts +++ b/src/content/def/content.ts @@ -1,5 +1,5 @@ -import {Content as ContentData} from 'sunbird-client-data-models'; -export {Content as ContentData, LicenseDetails, AltMsg as ComingSoonMsg, OriginData} from 'sunbird-client-data-models'; +import {Content as ContentData} from '@project-sunbird/client-services'; +export {Content as ContentData, LicenseDetails, AltMsg as ComingSoonMsg, OriginData} from '@project-sunbird/client-services'; import {Rollup} from '../../telemetry'; import {ContentAccess} from '../../profile'; diff --git a/src/course/def/batch.ts b/src/course/def/batch.ts index 4a212f03b..df4e3455e 100644 --- a/src/course/def/batch.ts +++ b/src/course/def/batch.ts @@ -1 +1 @@ -export {Batch} from 'sunbird-client-data-models'; +export {Batch} from '@project-sunbird/client-services'; diff --git a/src/course/def/course.ts b/src/course/def/course.ts index ce7e81733..a8cac9ab2 100644 --- a/src/course/def/course.ts +++ b/src/course/def/course.ts @@ -1 +1 @@ -export {Course, CourseCertificate} from 'sunbird-client-data-models'; +export {Course, CourseCertificate} from '@project-sunbird/client-services'; diff --git a/src/faq/def/faq.ts b/src/faq/def/faq.ts index 51ae298dd..429e9c128 100644 --- a/src/faq/def/faq.ts +++ b/src/faq/def/faq.ts @@ -1 +1 @@ -export {Faq, Faqs} from 'sunbird-client-data-models'; +export {Faq, Faqs} from '@project-sunbird/client-services'; diff --git a/src/framework/def/channel.ts b/src/framework/def/channel.ts index 1e71d64c2..8116d65bc 100644 --- a/src/framework/def/channel.ts +++ b/src/framework/def/channel.ts @@ -1 +1 @@ -export {Channel} from 'sunbird-client-data-models'; +export {Channel} from '@project-sunbird/client-services'; diff --git a/src/framework/def/framework-category-code.ts b/src/framework/def/framework-category-code.ts index 1fc9ad0e0..c3ba4a725 100644 --- a/src/framework/def/framework-category-code.ts +++ b/src/framework/def/framework-category-code.ts @@ -1 +1 @@ -export {FrameworkCategoryCode, FrameworkCategoryCodesGroup} from 'sunbird-client-data-models'; +export {FrameworkCategoryCode, FrameworkCategoryCodesGroup} from '@project-sunbird/client-services'; diff --git a/src/framework/def/framework.ts b/src/framework/def/framework.ts index 3abff17ec..16f927927 100644 --- a/src/framework/def/framework.ts +++ b/src/framework/def/framework.ts @@ -1 +1 @@ -export {Framework, FrameworkCategory, CategoryTerm, CategoryAssociation} from 'sunbird-client-data-models'; +export {Framework, FrameworkCategory, CategoryTerm, CategoryAssociation} from '@project-sunbird/client-services'; diff --git a/src/page/def/page-assemble.ts b/src/page/def/page-assemble.ts index 4a1648e53..b206e898c 100644 --- a/src/page/def/page-assemble.ts +++ b/src/page/def/page-assemble.ts @@ -1 +1 @@ -export {PageAssemble, PageSections} from 'sunbird-client-data-models'; +export {PageAssemble, PageSections} from '@project-sunbird/client-services'; diff --git a/src/profile/def/server-profile.ts b/src/profile/def/server-profile.ts index 60c6ce709..367c24631 100644 --- a/src/profile/def/server-profile.ts +++ b/src/profile/def/server-profile.ts @@ -1 +1 @@ -export {User as ServerProfile, RootOrg} from 'sunbird-client-data-models'; +export {User as ServerProfile, RootOrg} from '@project-sunbird/client-services'; From 1f521ae2a842b1941e411e374fb069ba65c11e8a Mon Sep 17 00:00:00 2001 From: Ajoymaity Date: Fri, 3 Jan 2020 13:34:19 +0530 Subject: [PATCH 2/2] Issue #SB-16090 test: Unit test for storage and file service --- .../get-child-contents-handler.spec.ts | 101 ++++++------- .../get-content-heirarchy-handler.spec.ts | 47 ++++++ src/content/impl/content-service-impl.spec.ts | 31 ++-- src/storage/impl/storage-service-impl.spec.ts | 136 ++++++++++++++++-- .../impl/download-service-impl.spec.ts | 54 ++++++- src/util/file/util/file-util.spec.ts | 31 ++++ 6 files changed, 326 insertions(+), 74 deletions(-) create mode 100644 src/content/handlers/get-content-heirarchy-handler.spec.ts create mode 100644 src/util/file/util/file-util.spec.ts diff --git a/src/content/handlers/get-child-contents-handler.spec.ts b/src/content/handlers/get-child-contents-handler.spec.ts index c312ad9d5..3ced0b90a 100644 --- a/src/content/handlers/get-child-contents-handler.spec.ts +++ b/src/content/handlers/get-child-contents-handler.spec.ts @@ -1,20 +1,23 @@ -import {ChildContentsHandler} from './get-child-contents-handler'; -import {DbService} from '../../db'; -import {GetContentDetailsHandler} from './get-content-details-handler'; -import {ContentEntry} from '../db/schema'; -import {ContentMapper} from '../util/content-mapper'; -import {of} from 'rxjs'; -import {ChildContent, HierarchyInfo} from '..'; +import { ChildContentsHandler } from './get-child-contents-handler'; +import { DbService } from '../../db'; +import { GetContentDetailsHandler } from './get-content-details-handler'; +import { ContentEntry } from '../db/schema'; +import { ContentMapper } from '../util/content-mapper'; +import { of } from 'rxjs'; +import { ChildContent, HierarchyInfo } from '..'; +import { FileService } from '../../util/file/def/file-service'; describe('ChildContentsHandler', () => { let childContentHandler: ChildContentsHandler; const mockDbService: Partial = {}; const mockGetContentDetailsHandler: Partial = {}; + const mockFileService: Partial = {}; beforeAll(() => { childContentHandler = new ChildContentsHandler( mockDbService as DbService, - mockGetContentDetailsHandler as GetContentDetailsHandler + mockGetContentDetailsHandler as GetContentDetailsHandler, + mockFileService as FileService ); }); @@ -47,7 +50,7 @@ describe('ChildContentsHandler', () => { }; const childContentsMap = new Map(); childContentsMap.set('IDENTIFIER', 'd0_id'); - ContentMapper.mapContentDBEntryToContent = jest.fn(() => of([])); + ContentMapper.mapContentDBEntryToContent = jest.fn(() => ({hierarchyInfo: {data: ''}})); const data = JSON.parse(request[ContentEntry.COLUMN_NAME_LOCAL_DATA]); mockDbService.execute = jest.fn(() => of([])); // act @@ -60,34 +63,6 @@ describe('ChildContentsHandler', () => { }); }); - // it('should not be fetch content', async(done) => { - // // arrange - // const request: ContentEntry.SchemaMap = { - // identifier: 'IDENTIFIER', - // server_data: 'SERVER_DATA', - // local_data: '{"children": [{"ALL": 0}, "do_234", "do_345"]}', - // mime_type: 'MIME_TYPE', - // manifest_version: 'MAINFEST_VERSION', - // content_type: 'CONTENT_TYPE' - // }; - // const currentLevel = -1; - // const level = 2; - // ContentMapper.mapContentDBEntryToContent = jest.fn(() => of([])); - // mockDbService.execute = jest.fn(() => of([])); - // const data = JSON.parse(request[ContentEntry.COLUMN_NAME_LOCAL_DATA]); - // console.log(data.children[0].ALL); - // // JSON.parse = jest.fn().mockImplementationOnce(() => { - // // return request[ContentEntry.COLUMN_NAME_LOCAL_DATA]; - // // }); - // // act - // await childContentHandler.fetchChildrenOfContent(request, currentLevel, level).then(() => { - // // expect(JSON.parse(request[ContentEntry.COLUMN_NAME_LOCAL_DATA])).toHaveBeenCalled(); - // expect(data.children[0].ALL).toEqual(0); - // done(); - // }); - // // assert - // }); - it('should parent child relation', (done) => { // arrange const request: ContentEntry.SchemaMap = { @@ -114,10 +89,10 @@ describe('ChildContentsHandler', () => { identifier: 'SAMPLE_IDENTIFIER_1', contentType: 'SAMPLE_CONTENT_TYPE_1' }, - { - identifier: 'SAMPLE_IDENTIFIER_2', - contentType: 'SAMPLE_CONTENT_TYPE_2' - }]; + { + identifier: 'SAMPLE_IDENTIFIER_2', + contentType: 'SAMPLE_CONTENT_TYPE_2' + }]; const identifier = 'IDENTIFIER'; mockGetContentDetailsHandler.fetchFromDB = jest.fn(() => of([])); // act @@ -133,10 +108,10 @@ describe('ChildContentsHandler', () => { identifier: 'SAMPLE_IDENTIFIER_1', contentType: 'SAMPLE_CONTENT_TYPE_1' }, - { - identifier: 'SAMPLE_IDENTIFIER_2', - contentType: 'SAMPLE_CONTENT_TYPE_2' - }]; + { + identifier: 'SAMPLE_IDENTIFIER_2', + contentType: 'SAMPLE_CONTENT_TYPE_2' + }]; const currentIdentifier = 'SAMPLE_CURRENT_IDENTIFIER'; const contentKeyList = ['SAMPLE_IDENTIFIER_1', 'SAMPLE_IDENTIFIER_2', 'SAMPLE_IDENTIFIER_1/SAMPLE_IDENTIFIER_2/SAMPLE_CURRENT_IDENTIFIER']; @@ -152,10 +127,10 @@ describe('ChildContentsHandler', () => { identifier: 'SAMPLE_IDENTIFIER_1', contentType: 'SAMPLE_CONTENT_TYPE_1' }, - { - identifier: 'SAMPLE_IDENTIFIER_2', - contentType: 'SAMPLE_CONTENT_TYPE_2' - }]; + { + identifier: 'SAMPLE_IDENTIFIER_2', + contentType: 'SAMPLE_CONTENT_TYPE_2' + }]; const currentIdentifier = 'SAMPLE_CURRENT_IDENTIFIER'; const contentKeyList = ['SAMPLE_IDENTIFIER_1', 'SAMPLE_IDENTIFIER_2', 'SAMPLE_IDENTIFIER_1/SAMPLE_IDENTIFIER_2/SAMPLE_CURRENT_IDENTIFIER']; @@ -164,4 +139,32 @@ describe('ChildContentsHandler', () => { // assert done(); }); + + it('should get ChildIdentifiers From Manifest', (done) => { + // arrange + mockFileService.readAsText = jest.fn(() => { + }); + const readAsText = (mockFileService.readAsText as jest.Mock) + .mockResolvedValue(JSON.stringify({ archive: { items: [{ identifier: 'pass' }, { identifier: 'pass-2' }] } })); + readAsText().then((value) => { + return value; + }); + // act + childContentHandler.getChildIdentifiersFromManifest('textbook_unit_1').then(() => { + // assert + expect(mockFileService.readAsText).toHaveBeenCalledWith('file:///textbook_unit_1', 'manifest.json'); + done(); + }); + }); + + it('should get ChildIdentifiers From Manifest for catch part', async(done) => { + // arrange + mockFileService.readAsText = jest.fn(() => Promise.reject('textbook')); + // act + childContentHandler.getChildIdentifiersFromManifest('textbook_unit_1').then(() => { + // assert + expect(mockFileService.readAsText).toHaveBeenCalledWith('file:///textbook_unit_1', 'manifest.json'); + done(); + }); + }); }); diff --git a/src/content/handlers/get-content-heirarchy-handler.spec.ts b/src/content/handlers/get-content-heirarchy-handler.spec.ts new file mode 100644 index 000000000..3e65b144c --- /dev/null +++ b/src/content/handlers/get-content-heirarchy-handler.spec.ts @@ -0,0 +1,47 @@ +import { GetContentHeirarchyHandler } from './get-content-heirarchy-handler'; +import { ApiService } from '../..'; +import { ContentServiceConfig, ContentDetailRequest } from '..'; +import { of } from 'rxjs'; + +describe('GetContentHeirarchyHandler', () => { + let getContentHeirarchyHandler: GetContentHeirarchyHandler; + const mockApiService: Partial = {}; + const mockContentServiceConfig: Partial = {}; + + beforeAll(() => { + getContentHeirarchyHandler = new GetContentHeirarchyHandler( + mockApiService as ApiService, + mockContentServiceConfig as ContentServiceConfig + ); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should be create a instance of getContentHeirarchyHandler', () => { + expect(getContentHeirarchyHandler).toBeTruthy(); + }); + + it('shpuld handle mapContentFromContentHeirarchyData by invoked handle()', (done) => { + // arrange + const request: ContentDetailRequest = { + contentId: 'do_123' + }; + mockApiService.fetch = jest.fn(() => of({ + body: { + result: { + content: { + children: ['child-1', 'child-2'] + } + } + } + })); + // act + getContentHeirarchyHandler.handle(request).subscribe(() => { + expect(mockApiService.fetch).toHaveBeenCalled(); + done(); + }); + // assert + }); +}); diff --git a/src/content/impl/content-service-impl.spec.ts b/src/content/impl/content-service-impl.spec.ts index 31f596a0d..ce3f4797d 100644 --- a/src/content/impl/content-service-impl.spec.ts +++ b/src/content/impl/content-service-impl.spec.ts @@ -92,7 +92,7 @@ describe('ContentServiceImpl', () => { }; beforeAll(() => { - container.bind(InjectionTokens.CONTENT_SERVICE).to(ContentServiceImpl); + container.bind(InjectionTokens.CONTENT_SERVICE).to(ContentServiceImpl).inTransientScope(); container.bind(InjectionTokens.SDK_CONFIG).toConstantValue(mockSdkConfig as SdkConfig); container.bind(InjectionTokens.API_SERVICE).toConstantValue(mockApiService as ApiService); container.bind(InjectionTokens.DB_SERVICE).toConstantValue(mockDbService as DbService); @@ -553,6 +553,12 @@ describe('ContentServiceImpl', () => { }); it('should be find child content', (done) => { // arrange + (ChildContentsHandler as any as jest.Mock).mockImplementation(() => { + return { + fetchChildrenOfContent: jest.fn(() => Promise.resolve({})) + }; + }); + contentService = container.get(InjectionTokens.CONTENT_SERVICE); const hierarInfoData: HierarchyInfo[] = [{ identifier: 'd0_123', contentType: 'content_type' @@ -567,19 +573,15 @@ describe('ContentServiceImpl', () => { JSON.parse = jest.fn().mockImplementationOnce(() => { return mockDbService.read; }); - (ChildContentsHandler as any as jest.Mock).mockImplementation(() => { - return { - fetchChildrenOfContent: jest.fn(() => Promise.resolve({})) - }; - }); + ArrayUtil.joinPreservingQuotes = jest.fn(() => of([])); mockDbService.execute = jest.fn(() => of([])); // act contentService.getChildContents(request).subscribe(() => { // assert expect(mockDbService.read).toHaveBeenCalled(); - expect(ArrayUtil.joinPreservingQuotes).toHaveBeenCalled(); - expect(mockDbService.execute).toBeCalled(); + // expect(ArrayUtil.joinPreservingQuotes).toHaveBeenCalled(); + // expect(mockDbService.execute).toBeCalled(); done(); }); // assert @@ -611,16 +613,17 @@ describe('ContentServiceImpl', () => { }); it('should used for search content', (done) => { // arrange - const request: ContentSearchCriteria = { - limit: 1, - offset: 2 - }; (SearchContentHandler as any as jest.Mock).mockImplementation(() => { return { - getSearchContentRequest: jest.fn(() => of('')), - mapSearchResponse: jest.fn(() => of('')) + getSearchContentRequest: jest.fn(() => ({filter: {}})), + mapSearchResponse: jest.fn(() => ({id: 'sid'})) }; }); + const request: ContentSearchCriteria = { + limit: 1, + offset: 2 + }; + contentService = container.get(InjectionTokens.CONTENT_SERVICE); mockSharedPreferences.getString = jest.fn(() => of([])); spyOn(mockApiService, 'fetch').and.returnValue(of({ diff --git a/src/storage/impl/storage-service-impl.spec.ts b/src/storage/impl/storage-service-impl.spec.ts index eab1bad63..9c3ff1856 100644 --- a/src/storage/impl/storage-service-impl.spec.ts +++ b/src/storage/impl/storage-service-impl.spec.ts @@ -1,12 +1,23 @@ -import {Container} from 'inversify'; +import { Container } from 'inversify'; import { InjectionTokens } from '../../injection-tokens'; -import {StorageServiceImpl} from './storage-service-impl'; -import { EventsBusService, SharedPreferences, DeviceInfo } from '../..'; +import { StorageServiceImpl } from './storage-service-impl'; +import { EventsBusService, SharedPreferences, DeviceInfo, StorageDestination, TransferContentsRequest, ExistingContentAction } from '../..'; import { instance, mock } from 'ts-mockito'; import { DbService } from '../../db'; import { FileService } from '../../util/file/def/file-service'; import { SdkConfig } from '../../sdk-config'; import { of } from 'rxjs'; +import { StorageKeys } from '../../preference-keys'; +import { StorageHandler } from '../handler/storage-handler'; +import { GetModifiedContentHandler } from '../handler/scan/get-modified-content-handler'; +import { PerformActoinOnContentHandler } from '../handler/scan/perform-actoin-on-content-handler'; +import { TransferContentHandler } from '../handler/transfer-content-handler'; +import { StorageService } from '../def/storage-service'; + +jest.mock('../handler/storage-handler'); +jest.mock('../handler/scan/get-modified-content-handler'); +jest.mock('../handler/scan/perform-actoin-on-content-handler'); +jest.mock('../handler/transfer-content-handler'); describe('StorageServiceImpl', () => { let storageServiceImpl: StorageServiceImpl; @@ -19,7 +30,7 @@ describe('StorageServiceImpl', () => { const mockSharedPreferences: SharedPreferences = instance(mock()); beforeAll(() => { - container.bind(InjectionTokens.STORAGE_SERVICE).to(StorageServiceImpl); + container.bind(InjectionTokens.STORAGE_SERVICE).to(StorageServiceImpl).inTransientScope(); container.bind(InjectionTokens.CONTAINER).toConstantValue(container); container.bind(InjectionTokens.EVENTS_BUS_SERVICE).toConstantValue(mockEventsBusService as EventsBusService); container.bind(InjectionTokens.SHARED_PREFERENCES).toConstantValue(mockSharedPreferences as SharedPreferences); @@ -33,18 +44,127 @@ describe('StorageServiceImpl', () => { beforeEach(() => { jest.clearAllMocks(); + (StorageHandler as any as jest.Mock).mockClear(); + (GetModifiedContentHandler as any as jest.Mock).mockClear(); + (PerformActoinOnContentHandler as any as jest.Mock).mockClear(); + (TransferContentHandler as any as jest.Mock).mockClear(); }); it('should be create a instance of storageServiceImpl', () => { expect(storageServiceImpl).toBeTruthy(); }); - it('should get available storage volume', () => { + it('should get available storage volume', (done) => { // arrange - mockDeviceInfo.getStorageVolumes = jest.fn(() => {}); + mockDeviceInfo.getStorageVolumes = jest.fn(() => ({ name: 's-name' })); (mockDeviceInfo.getStorageVolumes as jest.Mock).mockReturnValue(of([])); - spyOn(storageServiceImpl, 'getStorageDestination').and.returnValue([]); + jest.spyOn(storageServiceImpl, 'getStorageDestination').mockResolvedValue(StorageDestination.EXTERNAL_STORAGE); + mockDbService.execute = jest.fn(() => of({ name: 's-name' })); + mockSharedPreferences.putString = jest.fn(() => of('undefined')); + (GetModifiedContentHandler as any as jest.Mock).mockImplementation(() => { + return { + execute: jest.fn(() => of({ name: 's-name' })), + }; + }); + (PerformActoinOnContentHandler as any as jest.Mock).mockImplementation(() => { + return { + exexute: jest.fn(() => of({ currentStoragePath: 's-name' })), + }; + }); + // act + storageServiceImpl.onInit().subscribe(() => { + setTimeout(() => { + expect(mockDeviceInfo.getStorageVolumes).toHaveBeenCalled(); + expect(mockSharedPreferences.putString).toHaveBeenCalledWith( + StorageKeys.KEY_STORAGE_DESTINATION, + StorageDestination.INTERNAL_STORAGE); + done(); + }, 0); + }); + }); + + it('should handle content trasfer', (done) => { + // arrange + (TransferContentHandler as any as jest.Mock).mockImplementation(() => { + return { + cancel: jest.fn(() => of(undefined)), + }; + }); + // act + storageServiceImpl.cancelTransfer(); + // assert + expect(TransferContentHandler.prototype.cancel).toHaveBeenCalled(); + done(); + }); + + it('should return storage location', (done) => { + // arrange + mockSharedPreferences.getString = jest.fn(() => of('storage-size')); + // act + storageServiceImpl.getStorageDestination().subscribe(() => { + // assert + expect(mockSharedPreferences.getString).toHaveBeenCalledWith(StorageKeys.KEY_STORAGE_DESTINATION); + done(); + }); + }); + + it('should return storage volume information', (done) => { + // arrange + // act + storageServiceImpl.getStorageDestinationVolumeInfo().subscribe(() => { + // assert + done(); + }); + }); + + it('should getToTransferContents', (done) => { + storageServiceImpl.getToTransferContents().subscribe(() => { + done(); + }); + }); + + it('should getToTransferContents', (done) => { + storageServiceImpl.getTransferringContent().subscribe(() => { + done(); + }); + }); + + it('should retryCurrentTransfer', (done) => { + storageServiceImpl.retryCurrentTransfer().subscribe(() => { + done(); + }); + }); + + it('should retryCurrentTransfer', (done) => { + storageServiceImpl.retryCurrentTransfer().subscribe(() => { + done(); + }); + }); + + it('should transfer Contents ', (done) => { + // arrange + (TransferContentHandler as any as jest.Mock).mockImplementation(() => { + return { + transfer: jest.fn(() => of(undefined)), + }; + }); + + const storageService = container.get(InjectionTokens.STORAGE_SERVICE); + + const request: TransferContentsRequest = { + contentIds: ['sid'], + existingContentAction: ExistingContentAction.KEEP_DESTINATION, + destinationFolder: 'd-folder', + deleteDestination: true + }; + jest.spyOn(storageService, 'getStorageDestinationDirectoryPath').mockResolvedValue('getStorageDestinationDirectoryPath'); + jest.spyOn(storageService, 'getStorageDestination').mockResolvedValue(StorageDestination.EXTERNAL_STORAGE); + // act - storageServiceImpl.onInit(); + storageService.transferContents(request).subscribe((e) => { + expect(storageService.getStorageDestination).toHaveBeenCalled(); + expect(storageService.getStorageDestinationDirectoryPath).toHaveBeenCalled(); + done(); + }); }); }); diff --git a/src/util/download/impl/download-service-impl.spec.ts b/src/util/download/impl/download-service-impl.spec.ts index d13018396..95f0b2d96 100644 --- a/src/util/download/impl/download-service-impl.spec.ts +++ b/src/util/download/impl/download-service-impl.spec.ts @@ -1,11 +1,27 @@ import { DownloadServiceImpl } from './download-service-impl'; import { SharedPreferences, EventsBusService, DownloadRequest, DownloadCancelRequest } from '../../..'; import { of, BehaviorSubject, Subject } from 'rxjs'; +import { take, sample } from 'rxjs/operators'; +import { SharedPreferencesSetCollectionImpl } from '../../shared-preferences/impl/shared-preferences-set-collection-impl'; +import { SharedPreferencesSetCollection } from '../../shared-preferences/def/shared-preferences-set-collection'; + +const mockSharedPreferencesSetCollection = + { + identifier: 'SAMPLE_ID', + downloadUrl: 'http://sample-url/', + mimeType: 'SAMPLE_MIME_TYPE', + destinationFolder: 'DESTINATION_FOLDER', + filename: 'ASSESSMENT' + } as Partial> as SharedPreferencesSetCollection; +jest.mock('', () => { + return mockSharedPreferencesSetCollection; +}); describe('DownloadServiceImpl', () => { let downloadServiceImpl: DownloadServiceImpl; const mockEventsBusService: Partial = {}; - const mockSharedPreferences: Partial = {}; + const mockSharedPreferences: Partial = { + }; beforeAll(() => { downloadServiceImpl = new DownloadServiceImpl( @@ -14,8 +30,20 @@ describe('DownloadServiceImpl', () => { ); }); + const downloadRequestSet = new Set(); + + + mockSharedPreferencesSetCollection.asSet = jest.fn(() => of(downloadRequestSet.add({ + identifier: 'SAMPLE_ID', + downloadUrl: 'http://sample-url/', + mimeType: 'SAMPLE_MIME_TYPE', + destinationFolder: 'DESTINATION_FOLDER', + filename: 'ASSESSMENT' + }))); + beforeEach(() => { jest.clearAllMocks(); + // (SharedPreferencesSetCollectionImpl as any as jest.Mock>).mockClear(); }); @@ -70,17 +98,37 @@ describe('DownloadServiceImpl', () => { destinationFolder: 'DESTINATION_FOLDER', filename: 'ASSESSMENT' }; - const subject = new BehaviorSubject(downloadRequest); + const subject$ = new BehaviorSubject(downloadRequest); // expect(subject).toBeInstanceOf(downloadRequest); const request: DownloadCancelRequest = { identifier: 'SAMPLE_ID' }; + const currentDownloadRequest: DownloadRequest = { + identifier: 'SAMPLE_ID', + downloadUrl: 'http://sample-url/', + mimeType: 'SAMPLE_MIME_TYPE', + destinationFolder: 'DESTINATION_FOLDER', + filename: 'ASSESSMENT' + }; const generateTelemetry = true; - // spyOn(downloadServiceImpl, 'cancel').and.returnValue(of({})); + mockSharedPreferences.getString = jest.fn(() => of(undefined)); // act downloadServiceImpl.cancel(request, generateTelemetry).subscribe((res) => { // assert + console.log('//////', res) + subject$.pipe(take(1)) + .subscribe( + result => expect(result).toBe(subject$) + ); done(); }); }); + + it('', () => { + // arrange + // act + // downloadServiceImpl.cancelAll(); + // assert + }); }); + diff --git a/src/util/file/util/file-util.spec.ts b/src/util/file/util/file-util.spec.ts new file mode 100644 index 000000000..a9bb2687b --- /dev/null +++ b/src/util/file/util/file-util.spec.ts @@ -0,0 +1,31 @@ +import {FileUtil} from './file-util'; + +describe('fileUtil', () => { + it('should return file extension', () => { + expect(FileUtil.getFileExtension('var/lib/telemetry.json')).toEqual('json'); + }); + + it('should return fileName from filePath', () => { + expect(FileUtil.getFileName('var/lib/telemetry.json')).toEqual('telemetry.json'); + }); + + it('should return parent directory', () => { + expect(FileUtil.getParentDir('var/lib/telemetry.json')).toEqual('var/lib/'); + }); + + it('should return directory name', () => { + expect(FileUtil.getDirectoryName('var/lib/telemetry.json')).toEqual('lib'); + }); + + it('should return temporary dir path', () => { + expect(FileUtil.getTempDirPath('var/lib/telemetry.json')).toEqual('var/lib/telemetry.json/tmp'); + }); + + it('should return free space is available or not', () => { + expect(FileUtil.isFreeSpaceAvailable(1024, 256, 48)).toEqual(true); + }); + + it('should return directory', () => { + expect(FileUtil.getDirecory('var/lib/telemetry.json')).toEqual('var/lib'); + }); +});