diff --git a/src/profile/handler/export/clean-up-export-file.spec.ts b/src/profile/handler/export/clean-up-export-file.spec.ts new file mode 100644 index 000000000..06084387a --- /dev/null +++ b/src/profile/handler/export/clean-up-export-file.spec.ts @@ -0,0 +1,82 @@ +import { CleanupExportedFile } from './clean-up-exported-file'; +import { DbService, ErrorCode } from '../../..'; +import { FileService } from '../../../util/file/def/file-service'; +import { ExportProfileContext } from '../../def/export-profile-context'; +import { of } from 'rxjs'; + +describe('CleanupExportedFile', () => { + let cleanupExportedFile: CleanupExportedFile; + const mockFileService: Partial = {}; + const mockDbService: Partial = {}; + + beforeAll(() => { + cleanupExportedFile = new CleanupExportedFile( + mockDbService as DbService, + mockFileService as FileService + ); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should be create a instance of cleanupExportedFile', () => { + // arrange + // act + // assert + expect(cleanupExportedFile).toBeTruthy(); + }); + + it('should return all table details and remove table', (done) => { + // arrange + const request: ExportProfileContext = { + userIds: ['sample-user-id', 'sample-user-id'], + groupIds: ['group-1', 'group-2'], + destinationFolder: 'dest/folder/file', + destinationDBFilePath: 'dest/db/file/path', + size: '32MB' + }; + mockDbService.execute = jest.fn(() => of([{ tableName: 'sample-table', name: 'db' }])); + mockDbService.read = jest.fn(() => of([{}])); + mockDbService.insert = jest.fn(() => of(2)); + mockFileService.getMetaData = jest.fn(() => Promise.resolve({ size: 4 })); + mockFileService.removeFile = jest.fn(() => Promise.resolve({ success: true })); + // act + cleanupExportedFile.execute(request).then(() => { + expect(mockDbService.execute).toHaveBeenCalled(); + expect(mockDbService.read).toHaveBeenCalled(); + expect(mockDbService.insert).toHaveBeenCalled(); + expect(mockFileService.getMetaData).toHaveBeenCalled(); + expect(mockFileService.removeFile).toHaveBeenCalled(); + done(); + }).catch((e) => { + console.error(e); + fail(e); + }); + }); + + it('should return errorMesg for for failed removeFile', (done) => { + // arrange + const request: ExportProfileContext = { + userIds: [], + groupIds: [], + destinationFolder: '', + destinationDBFilePath: '', + size: '32MB' + }; + mockDbService.execute = jest.fn(() => of([{ tableName: 'sample-table', name: 'db' }])); + mockDbService.read = jest.fn(() => of([{}])); + mockDbService.insert = jest.fn(() => of(2)); + mockFileService.getMetaData = jest.fn(() => Promise.resolve({ size: 4 })); + mockFileService.removeFile = jest.fn(() => Promise.reject({ errorMesg: '' })); + // act + cleanupExportedFile.execute(request).catch((e) => { + expect(mockDbService.execute).toHaveBeenCalled(); + expect(mockDbService.insert).toHaveBeenCalled(); + expect(mockFileService.getMetaData).toHaveBeenCalled(); + expect(mockFileService.removeFile).toHaveBeenCalled(); + expect(e._errorMesg).toBe(ErrorCode.EXPORT_FAILED); + done(); + }); + }); +}); diff --git a/src/profile/handler/export/copy-database.spec.ts b/src/profile/handler/export/copy-database.spec.ts new file mode 100644 index 000000000..cf5c60869 --- /dev/null +++ b/src/profile/handler/export/copy-database.spec.ts @@ -0,0 +1,39 @@ +import { CopyDatabase } from './copy-database'; +import { DbService } from '../../..'; +import { ExportProfileContext } from '../../def/export-profile-context'; +import { of } from 'rxjs'; + +describe('CopyDatabase', () => { + let copyDatabase: CopyDatabase; + const mockDbService: Partial = {}; + + beforeAll(() => { + copyDatabase = new CopyDatabase( + mockDbService as DbService + ); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should be create a instance of CopyDatabase', () => { + expect(copyDatabase).toBeTruthy(); + }); + + it('should copy file from destination file path', () => { + // arrange + const request: ExportProfileContext = { + userIds: ['sample-user-id'], + groupIds: ['group-1', 'group-2'], + destinationFolder: 'dest/folder/file', + destinationDBFilePath: 'dest/db/file/path', + size: '32MB' + }; + mockDbService.copyDatabase = jest.fn(() => of({})); + // act + copyDatabase.execute(request); + // assert + expect(mockDbService.copyDatabase).toHaveBeenCalled(); + }); +}); diff --git a/src/profile/handler/export/create-metadata.spec.ts b/src/profile/handler/export/create-metadata.spec.ts new file mode 100644 index 000000000..ce833076c --- /dev/null +++ b/src/profile/handler/export/create-metadata.spec.ts @@ -0,0 +1,77 @@ +import { CreateMetaData } from './create-metadata'; +import { DbService, DeviceInfo } from '../../..'; +import { FileService } from '../../../util/file/def/file-service'; +import { ExportProfileContext } from '../../def/export-profile-context'; +import { of } from 'rxjs'; + +describe('CreateMetaData', () => { + let createMetaData: CreateMetaData; + const mockDbService: Partial = {}; + const mockFileService: Partial = {}; + const mockDeviceInfo: Partial = {}; + + beforeAll(() => { + createMetaData = new CreateMetaData( + mockDbService as DbService, + mockFileService as FileService, + mockDeviceInfo as DeviceInfo + ); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should create a instance of CreateMetaData', () => { + expect(createMetaData).toBeTruthy(); + }); + + it('should generate metadata and populate metadata', (done) => { + // arrange + const request: ExportProfileContext = { + userIds: ['sample-user-id'], + groupIds: ['group-1', 'group-2'], + destinationFolder: 'dest/folder/file', + destinationDBFilePath: 'dest/db/file/path', + size: '32MB' + }; + mockDeviceInfo.getDeviceID = jest.fn(() => 'device-id'); + mockDbService.open = jest.fn(() => Promise.resolve(undefined)); + mockDbService.execute = jest.fn(() => of({})); + mockDbService.insert = jest.fn(() => of(1)); + // act + createMetaData.execute(request).then(() => { + // assert + expect(mockDeviceInfo.getDeviceID).toHaveBeenCalled(); + expect(mockDbService.open).toHaveBeenCalledWith(request.destinationDBFilePath); + expect(mockDbService.execute).toHaveBeenCalled(); + expect(mockDbService.insert).toHaveBeenCalled(); + done(); + }).catch((e) => { + console.error(e); + fail(e); + }); + }); + + it('should generate metadata and populate metadata for error part', (done) => { + // arrange + const request: ExportProfileContext = { + userIds: ['sample-user-id'], + groupIds: ['group-1', 'group-2'], + destinationFolder: 'dest/folder/file', + destinationDBFilePath: 'dest/db/file/path', + size: '32MB' + }; + mockDeviceInfo.getDeviceID = jest.fn(() => 'device-id'); + mockDbService.open = jest.fn(() => Promise.reject(undefined)); + mockDbService.execute = jest.fn(() => of({})); + mockDbService.insert = jest.fn(() => of(1)); + // act + createMetaData.execute(request).catch((e) => { + // assert + expect(mockDeviceInfo.getDeviceID).toHaveBeenCalled(); + expect(mockDbService.open).toHaveBeenCalledWith(request.destinationDBFilePath); + done(); + }); + }); +}); diff --git a/src/profile/handler/export/generate-profile-export-telemetry.spec.ts b/src/profile/handler/export/generate-profile-export-telemetry.spec.ts new file mode 100644 index 000000000..9cc2e4069 --- /dev/null +++ b/src/profile/handler/export/generate-profile-export-telemetry.spec.ts @@ -0,0 +1,55 @@ +import {GenerateProfileExportTelemetry} from './generate-profile-export-telemetry'; +import { DbService, ImportTelemetryContext } from '../../..'; +import { of } from 'rxjs'; +import { TelemetryLogger } from '../../../telemetry/util/telemetry-logger'; +import { share } from 'rxjs/operators'; +import { ProfileServiceImpl } from '../../impl/profile-service-impl'; +import { TelemetryService } from '../../../telemetry'; + +jest.mock('../../../telemetry/util/telemetry-logger'); + +describe('GenerateProfileExportTelemetry', () => { + let generateProfileExportTelemetry: GenerateProfileExportTelemetry; + const mockDbService: Partial = {}; + const mockTelemetryService: Partial = { + share: jest.fn(() => of(undefined)) + }; + (TelemetryLogger as any)['log'] = mockTelemetryService; + + beforeAll(() => { + generateProfileExportTelemetry = new GenerateProfileExportTelemetry( + mockDbService as DbService + ); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should create a instance of GenerateProfileExportTelemetry', () => { + expect(generateProfileExportTelemetry).toBeTruthy(); + }); + + it('should generate export profile telemetry', (done) => { + // arrange + const request: ImportTelemetryContext = { + sourceDBFilePath: 'src/db/path', + metadata: { 'index': 1 } + }; + mockDbService.read = jest.fn(() => of([{ + imported_id: 'sample-imported_id', + device_id: 'sample-device_id', + count: 'no-of-count' + }])); + // act + generateProfileExportTelemetry.execute(request).then(() => { + // assert + expect(mockDbService.read).toHaveBeenCalled(); + expect(mockTelemetryService.share).toHaveBeenCalled(); + done(); + }).catch((e) => { + console.error(e); + fail(e); + }); + }); +}); diff --git a/src/profile/handler/export/get-epar-file-path.spec.ts b/src/profile/handler/export/get-epar-file-path.spec.ts new file mode 100644 index 000000000..26dd4f228 --- /dev/null +++ b/src/profile/handler/export/get-epar-file-path.spec.ts @@ -0,0 +1,42 @@ +import {GetEparFilePath} from './get-epar-file-path'; +import { FileService } from '../../../util/file/def/file-service'; +import { ExportProfileContext } from '../../def/export-profile-context'; + +describe('GetEparFilePath', () => { + let getEparFilePath: GetEparFilePath; + const mockFileService: Partial = {}; + Date.now = jest.fn(() => 1487076708000); + + beforeAll(() => { + getEparFilePath = new GetEparFilePath( + mockFileService as FileService + ); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should be create a instance of GetEparFilePath', () => { + expect(getEparFilePath).toBeTruthy(); + }); + + it('should create a directory and a file', (done) => { + // arrange + const request: ExportProfileContext = { + userIds: ['sample-user-id', 'sample-user-id'], + groupIds: ['group-1', 'group-2'], + destinationFolder: 'dest/folder/file', + destinationDBFilePath: 'dest/db/file/path', + size: '32MB' + }; + mockFileService.createDir = jest.fn(() => Promise.resolve({})); + mockFileService.createFile = jest.fn(() => Promise.resolve({})); + // act + getEparFilePath.execute(request).then(() => { + // assert + expect(mockFileService.createDir).toHaveBeenCalled(); + done(); + }); + }); +}); diff --git a/src/profile/handler/export/get-epar-file-path.ts b/src/profile/handler/export/get-epar-file-path.ts index 855fb5910..011f9cb31 100644 --- a/src/profile/handler/export/get-epar-file-path.ts +++ b/src/profile/handler/export/get-epar-file-path.ts @@ -1,7 +1,7 @@ import {Response} from '../../../api'; import {FileService} from '../../../util/file/def/file-service'; import {ExportProfileContext} from '../../def/export-profile-context'; -import dayjs from 'dayjs'; +import * as dayjs from 'dayjs'; export class GetEparFilePath { constructor(private fileService: FileService) {