diff --git a/src/profile/handler/import/transport-framework-n-channel.spec.ts b/src/profile/handler/import/transport-framework-n-channel.spec.ts index fd3d5a9ca..0b9eaf8a6 100644 --- a/src/profile/handler/import/transport-framework-n-channel.spec.ts +++ b/src/profile/handler/import/transport-framework-n-channel.spec.ts @@ -26,17 +26,22 @@ describe('TransportFrameworkNChannel', () => { sourceDBFilePath: 'src/db/path', metadata: { 'index': 1 } }; - mockDbService.read = jest.fn(() => of([{ - key: 'sample-key', - value: 'sample-value', - _id: 'sample-id', - uid: 'uid', - gid: 'gid' - }])); + mockDbService.read = jest.fn((req) => { + if (req.useExternalDb) { + return of([{ + uid: 'sample-uid', + gid: 'sample-gid' + }]); + } + + return of({}); + }); + mockDbService.insert = jest.fn(() => of(1)); // act transportFrameworkNChannel.execute(request).then(() => { // assert expect(mockDbService.read).toHaveBeenCalled(); + expect(mockDbService.insert).toHaveBeenCalled(); done(); }); }); diff --git a/src/profile/handler/import/transport-group-profile.spec.ts b/src/profile/handler/import/transport-group-profile.spec.ts new file mode 100644 index 000000000..d1196d663 --- /dev/null +++ b/src/profile/handler/import/transport-group-profile.spec.ts @@ -0,0 +1,48 @@ +import {TransportGroupProfile} from './transport-group-profile'; +import { DbService, ImportTelemetryContext } from '../../..'; +import { of } from 'rxjs'; + +describe('TransportGroupProfile', () => { + let transportGroupProfile: TransportGroupProfile; + const mockDbService: Partial = {}; + + beforeAll(() => { + transportGroupProfile = new TransportGroupProfile( + mockDbService as DbService + ); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should be create a instance of TransportGroupProfile', () => { + expect(transportGroupProfile).toBeTruthy(); + }); + + it('should saved group profile in local from Db', (done) => { + // arrange + const request: ImportTelemetryContext = { + sourceDBFilePath: 'src/db/path', + metadata: { 'index': 1 } + }; + mockDbService.read = jest.fn((req) => { + if (req.useExternalDb) { + return of([{ + uid: 'sample-uid', + gid: 'sample-gid' + }]); + } + + return of({}); + }); + mockDbService.insert = jest.fn(() => of(1)); + // act + transportGroupProfile.execute(request).then(() => { + // assert + expect(mockDbService.read).toHaveBeenCalled(); + expect(mockDbService.insert).toHaveBeenCalled(); + done(); + }); + }); +}); diff --git a/src/profile/handler/import/transport-group.spec.ts b/src/profile/handler/import/transport-group.spec.ts new file mode 100644 index 000000000..d1442ee68 --- /dev/null +++ b/src/profile/handler/import/transport-group.spec.ts @@ -0,0 +1,75 @@ +import { TransportGroup } from './transport-group'; +import { DbService, ImportTelemetryContext } from '../../..'; +import { of } from 'rxjs'; + +describe('TransportGroup', () => { + let transportGroup: TransportGroup; + const mockDbService: Partial = {}; + + beforeAll(() => { + transportGroup = new TransportGroup( + mockDbService as DbService + ); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should be create a instance of TransportGroupProfile', () => { + expect(transportGroup).toBeTruthy(); + }); + + it('should saved group in local from Db', (done) => { + // arrange + const request: ImportTelemetryContext = { + sourceDBFilePath: 'src/db/path', + metadata: { 'index': 1 } + }; + mockDbService.read = jest.fn((req) => { + if (req.useExternalDb) { + return of([{ + uid: 'sample-uid', + gid: 'sample-gid' + }]); + } + + return of({}); + }); + mockDbService.insert = jest.fn(() => of(1)); + + // act + transportGroup.execute(request).then(() => { + // assert + expect(mockDbService.read).toHaveBeenCalled(); + expect(mockDbService.insert).toHaveBeenCalled(); + done(); + }); + }); + + + it('should saved group in local from Db for Failed', (done) => { + // arrange + const request: ImportTelemetryContext = { + sourceDBFilePath: 'src/db/path', + metadata: { 'index': 1 } + }; + mockDbService.read = jest.fn((req) => { + if (req.useExternalDb) { + return of([{ + uid: 'sample-uid', + gid: 'sample-gid' + }]); + } + + return of([{}]); + }); + + // act + transportGroup.execute(request).then(() => { + // assert + expect(mockDbService.read).toHaveBeenCalled(); + done(); + }); + }); +}); diff --git a/src/profile/handler/import/transport-profiles.spec.ts b/src/profile/handler/import/transport-profiles.spec.ts new file mode 100644 index 000000000..5c58305da --- /dev/null +++ b/src/profile/handler/import/transport-profiles.spec.ts @@ -0,0 +1,72 @@ +import {TransportProfiles} from './transport-profiles'; +import { DbService, ImportTelemetryContext } from '../../..'; +import { of } from 'rxjs'; + +describe('TransportProfiles', () => { + let transportProfiles: TransportProfiles; + const mockDbService: Partial = {}; + + beforeAll(() => { + transportProfiles = new TransportProfiles( + mockDbService as DbService + ); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should be create a instance of TransportGroupProfile', () => { + expect(transportProfiles).toBeTruthy(); + }); + + it('should saved profile and transport to db', (done) => { + // arrange + const request: ImportTelemetryContext = { + sourceDBFilePath: 'src/db/path', + metadata: { 'index': 1 } + }; + mockDbService.read = jest.fn((req) => { + if (req.useExternalDb) { + return of([{ + uid: 'sample-uid', + gid: 'sample-gid' + }]); + } + + return of({}); + }); + mockDbService.insert = jest.fn(() => of(1)); + // act + transportProfiles.execute(request).then(() => { + // assert + expect(mockDbService.read).toHaveBeenCalled(); + expect(mockDbService.insert).toHaveBeenCalled(); + done(); + }); + }); + + it('should saved profile to db for import context failed', (done) => { + // arrange + const request: ImportTelemetryContext = { + sourceDBFilePath: 'src/db/path', + metadata: { 'index': 1 } + }; + mockDbService.read = jest.fn((req) => { + if (req.useExternalDb) { + return of([{ + uid: 'sample-uid', + gid: 'sample-gid' + }]); + } + + return of([{}]); + }); + // act + transportProfiles.execute(request).then(() => { + // assert + expect(mockDbService.read).toHaveBeenCalled(); + done(); + }); + }); +}); diff --git a/src/profile/handler/import/transport-user.spec.ts b/src/profile/handler/import/transport-user.spec.ts new file mode 100644 index 000000000..7cfbed374 --- /dev/null +++ b/src/profile/handler/import/transport-user.spec.ts @@ -0,0 +1,48 @@ +import { TransportUser } from './transport-user'; +import { DbService, ImportTelemetryContext } from '../../..'; +import { of } from 'rxjs'; + +describe('TransportUser', () => { + let transportUser: TransportUser; + const mockDbService: Partial = {}; + + beforeAll(() => { + transportUser = new TransportUser( + mockDbService as DbService + ); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should be create a instance of TransportGroupProfile', () => { + expect(transportUser).toBeTruthy(); + }); + + it('should saved user profile in Db', (done) => { + // arrange + const request: ImportTelemetryContext = { + sourceDBFilePath: 'src/db/path', + metadata: { 'index': 1 } + }; + mockDbService.read = jest.fn((req) => { + if (req.useExternalDb) { + return of([{ + uid: 'sample-uid', + gid: 'sample-gid' + }]); + } + + return of({}); + }); + mockDbService.insert = jest.fn(() => of(1)); + // act + transportUser.execute(request).then(() => { + // assert + expect(mockDbService.read).toHaveBeenCalled(); + expect(mockDbService.insert).toHaveBeenCalled(); + done(); + }); + }); +}); diff --git a/src/profile/handler/import/update-imported-profile-metadata.spec.ts b/src/profile/handler/import/update-imported-profile-metadata.spec.ts new file mode 100644 index 000000000..0bd8d1c05 --- /dev/null +++ b/src/profile/handler/import/update-imported-profile-metadata.spec.ts @@ -0,0 +1,62 @@ +import { UpdateImportedProfileMetadata } from './update-imported-profile-metadata'; +import { DbService, ImportTelemetryContext } from '../../..'; +import { of } from 'rxjs'; + +describe('TransportUser', () => { + let updateImportedProfileMetadata: UpdateImportedProfileMetadata; + const mockDbService: Partial = {}; + + beforeAll(() => { + updateImportedProfileMetadata = new UpdateImportedProfileMetadata( + mockDbService as DbService + ); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should be create a instance of TransportGroupProfile', () => { + expect(updateImportedProfileMetadata).toBeTruthy(); + }); + + it('should update import profile metadate', (done) => { + // arrange + const request: ImportTelemetryContext = { + sourceDBFilePath: 'src/db/path', + metadata: { 'index': 1 } + }; + mockDbService.read = jest.fn(() => { + return of([{ + imported_id: 'sample-imported_id', + device_id: 'sample-device_id', + count: 'count-1' + }]); + }); + mockDbService.update = jest.fn(() => of(1)); + // act + updateImportedProfileMetadata.execute(request).then(() => { + // assert + expect(mockDbService.read).toHaveBeenCalled(); + expect(mockDbService.update).toHaveBeenCalled(); + done(); + }); + }); + + it('should should not updated profile metadata for else part', (done) => { + // arrange + const request: ImportTelemetryContext = { + sourceDBFilePath: 'src/db/path', + metadata: { 'index': 1 } + }; + mockDbService.read = jest.fn(() => of({})); + mockDbService.insert = jest.fn(() => of(1)); + // act + updateImportedProfileMetadata.execute(request).then(() => { + // assert + expect(mockDbService.read).toHaveBeenCalled(); + expect(mockDbService.insert).toHaveBeenCalled(); + done(); + }); + }); +}); diff --git a/src/profile/handler/import/validate-profile-metadata.spec.ts b/src/profile/handler/import/validate-profile-metadata.spec.ts new file mode 100644 index 000000000..faa1a24e1 --- /dev/null +++ b/src/profile/handler/import/validate-profile-metadata.spec.ts @@ -0,0 +1,93 @@ +import { ValidateProfileMetadata } from './validate-profile-metadata'; +import { DbService, ImportTelemetryContext } from '../../..'; +import { of } from 'rxjs'; + +describe('TransportUser', () => { + let validateProfileMetadata: ValidateProfileMetadata; + const mockDbService: Partial = {}; + + beforeAll(() => { + validateProfileMetadata = new ValidateProfileMetadata( + mockDbService as DbService + ); + }); + + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('should be create a instance of TransportGroupProfile', () => { + expect(validateProfileMetadata).toBeTruthy(); + }); + + it('should import valid profile metadate', (done) => { + // arrange + const request: ImportTelemetryContext = { + sourceDBFilePath: 'src/db/path', + metadata: { 'index': 1 } + }; + mockDbService.open = jest.fn(() => Promise.resolve(undefined)); + mockDbService.read = jest.fn(() => of([{ + _id: 'sample_id', + meta_data: 'sample-meta_data', + key: 'sample-key' + }])); + // act + validateProfileMetadata.execute(request).catch((e) => { + // assert + expect(mockDbService.open).toHaveBeenCalled(); + expect(mockDbService.read).toHaveBeenCalled(); + expect(e._errorMesg).toBe('IMPORT_FAILED'); + done(); + }); + }); + + + it('should be valid profile metadate for error case', (done) => { + // arrange + const request: ImportTelemetryContext = { + sourceDBFilePath: 'src/db/path', + }; + mockDbService.open = jest.fn(() => Promise.resolve( + [{ + _id: 'sample_id', + meta_data: 'sample-meta_data', + }] + )); + mockDbService.read = jest.fn(() => of([])); + // act + validateProfileMetadata.execute(request).catch((e) => { + // assert + expect(mockDbService.open).toHaveBeenCalled(); + expect(mockDbService.read).toHaveBeenCalled(); + expect(e._errorMesg).toBe('IMPORT_FAILED'); + done(); + }); + }); + + it('should import valid profile metadate for metadata key', (done) => { + // arrange + const request: ImportTelemetryContext = { + sourceDBFilePath: 'src/db/path', + metadata: { 'key': 's1-key', 'types': 'sample-type' } + }; + mockDbService.open = jest.fn(() => Promise.resolve( + [{ + _id: 'sample_id', + meta_data: 'sample-meta_data', + }] + )); + mockDbService.read = jest.fn(() => of([{ + _id: 'sample-id', + meta_data: 'sample-meta-data', + key: 'sample-key' + }])); + // act + validateProfileMetadata.execute(request).catch((e) => { + // assert + expect(mockDbService.open).toHaveBeenCalled(); + expect(mockDbService.read).toHaveBeenCalled(); + done(); + }); + }); +});