-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EW-998 created a new sync Module #5222
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1e8cb29
EW-998 provided feature flag & new sync module for tsp
Fshmit 20c7297
Merge branch 'main' into EW-998
Fshmit 1e743b2
Merge branch 'main' into EW-998
Fshmit c3cd8e1
EW-998 deleted a comment
Fshmit 5f03b69
Merge branch 'main' into EW-998
Fshmit 3279993
Merge branch 'main' into EW-998
Fshmit 87effd9
Ew-998 added some tests
Fshmit 6f059be
Merge branch 'main' into EW-998
Fshmit 1eea3b1
Merge branch 'main' into EW-998
Fshmit 8833be3
EW-998 modified a test
Fshmit 185fe7e
Merge branch 'EW-998' of https://github.com/hpi-schul-cloud/schulclou…
Fshmit c160611
EW-998 review comments
Fshmit adb653f
EW-998 initialize tsp strategy depending on feature flag
Fshmit 08860a5
EW-998 added a start script
Fshmit eba0682
EW-998 modified a test + impl of service
Fshmit ed40ed6
EW-998 added a invalid target loggable
Fshmit 4b9a852
Merge branch 'main' into EW-998
Fshmit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { createMock } from '@golevelup/ts-jest'; | ||
import { Test } from '@nestjs/testing'; | ||
import { Logger } from '@src/core/logger'; | ||
import { SyncUc } from '../uc/sync.uc'; | ||
import { SyncConsole } from './sync.console'; | ||
|
||
describe(SyncConsole.name, () => { | ||
let syncConsole: SyncConsole; | ||
let syncUc: SyncUc; | ||
|
||
beforeAll(async () => { | ||
const module = await Test.createTestingModule({ | ||
providers: [ | ||
SyncConsole, | ||
{ | ||
provide: SyncUc, | ||
useValue: createMock<SyncUc>(), | ||
}, | ||
{ | ||
provide: Logger, | ||
useValue: createMock<Logger>(), | ||
}, | ||
], | ||
}).compile(); | ||
|
||
syncConsole = module.get(SyncConsole); | ||
syncUc = module.get(SyncUc); | ||
}); | ||
|
||
describe('when sync console is initialized', () => { | ||
it('should be defined', () => { | ||
expect(syncConsole).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('startSync', () => { | ||
const setup = () => { | ||
const target = 'tsp'; | ||
return { target }; | ||
}; | ||
|
||
it('should call startSync method of syncUc', async () => { | ||
const { target } = setup(); | ||
await syncConsole.startSync(target); | ||
|
||
expect(syncUc.startSync).toHaveBeenCalledWith(target); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Logger } from '@src/core/logger'; | ||
import { Command, Console } from 'nestjs-console'; | ||
import { SyncUc } from '../uc/sync.uc'; | ||
|
||
@Console({ command: 'sync', description: 'Prefixes all synchronization related console commands.' }) | ||
export class SyncConsole { | ||
constructor(private readonly syncUc: SyncUc, private readonly logger: Logger) { | ||
this.logger.setContext(SyncConsole.name); | ||
} | ||
|
||
@Command({ | ||
command: 'run <target>', | ||
description: 'Starts the synchronization process.', | ||
}) | ||
public async startSync(target: string): Promise<void> { | ||
await this.syncUc.startSync(target); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
apps/server/src/infra/sync/errors/invalid-target.loggable.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { SyncStrategyTarget } from '../sync-strategy.types'; | ||
import { InvalidTargetLoggable } from './invalid-target.loggable'; | ||
|
||
describe(InvalidTargetLoggable.name, () => { | ||
let loggable: InvalidTargetLoggable; | ||
|
||
beforeAll(() => { | ||
loggable = new InvalidTargetLoggable('invalid-target'); | ||
}); | ||
|
||
describe('when loggable is initialized', () => { | ||
it('should be defined', () => { | ||
expect(loggable).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('getLogMessage', () => { | ||
it('should return a log message with the entered target and available targets', () => { | ||
expect(loggable.getLogMessage()).toEqual({ | ||
message: 'Either synchronization is not activated or the target entered is invalid', | ||
data: { | ||
enteredTarget: 'invalid-target', | ||
avaliableTargets: SyncStrategyTarget, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
apps/server/src/infra/sync/errors/invalid-target.loggable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ErrorLogMessage, LogMessage, Loggable, ValidationErrorLogMessage } from '@src/core/logger'; | ||
import { SyncStrategyTarget } from '../sync-strategy.types'; | ||
|
||
export class InvalidTargetLoggable implements Loggable { | ||
constructor(private readonly target: string) {} | ||
|
||
getLogMessage(): LogMessage | ErrorLogMessage | ValidationErrorLogMessage { | ||
return { | ||
message: 'Either synchronization is not activated or the target entered is invalid', | ||
data: { | ||
enteredTarget: this.target, | ||
avaliableTargets: SyncStrategyTarget, | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { createMock } from '@golevelup/ts-jest'; | ||
import { faker } from '@faker-js/faker'; | ||
import { Logger } from '@src/core/logger'; | ||
import { SyncService } from './sync.service'; | ||
import { TspSyncStrategy } from '../tsp/tsp-sync.strategy'; | ||
import { SyncStrategyTarget } from '../sync-strategy.types'; | ||
import { InvalidTargetLoggable } from '../errors/invalid-target.loggable'; | ||
|
||
describe(SyncService.name, () => { | ||
let module: TestingModule; | ||
let service: SyncService; | ||
let tspSyncStrategy: TspSyncStrategy; | ||
let logger: Logger; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
providers: [ | ||
SyncService, | ||
{ | ||
provide: TspSyncStrategy, | ||
useValue: createMock<TspSyncStrategy>({ | ||
getType(): SyncStrategyTarget { | ||
return SyncStrategyTarget.TSP; | ||
}, | ||
sync(): Promise<void> { | ||
return Promise.resolve(); | ||
}, | ||
}), | ||
}, | ||
{ | ||
provide: Logger, | ||
useValue: createMock<Logger>(), | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get(SyncService); | ||
tspSyncStrategy = module.get(TspSyncStrategy); | ||
logger = module.get(Logger); | ||
}); | ||
|
||
afterAll(async () => { | ||
await module.close(); | ||
}); | ||
|
||
describe('when sync service is initialized', () => { | ||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('startSync', () => { | ||
describe('when provided target is invalid or the sync is deactivated', () => { | ||
const setup = () => { | ||
const invalidTarget = faker.lorem.word(); | ||
const output = new InvalidTargetLoggable(invalidTarget); | ||
|
||
return { output, invalidTarget }; | ||
}; | ||
|
||
it('should throw an invalid provided target error', async () => { | ||
const { output, invalidTarget } = setup(); | ||
await service.startSync(invalidTarget); | ||
|
||
expect(logger.info).toHaveBeenCalledWith(output); | ||
}); | ||
}); | ||
|
||
describe('when provided target is valid and synchronization is activated', () => { | ||
const setup = () => { | ||
const validSystem = 'tsp'; | ||
Reflect.set(service, 'strategies', new Map([[SyncStrategyTarget.TSP, tspSyncStrategy]])); | ||
|
||
return { validSystem }; | ||
}; | ||
|
||
it('should call sync method of the provided target', async () => { | ||
const { validSystem } = setup(); | ||
await service.startSync(validSystem); | ||
|
||
expect(tspSyncStrategy.sync).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Injectable, Optional } from '@nestjs/common'; | ||
import { Logger } from '@src/core/logger'; | ||
import { TspSyncStrategy } from '../tsp/tsp-sync.strategy'; | ||
import { SyncStrategy } from '../strategy/sync-strategy'; | ||
import { SyncStrategyTarget } from '../sync-strategy.types'; | ||
import { InvalidTargetLoggable } from '../errors/invalid-target.loggable'; | ||
|
||
@Injectable() | ||
export class SyncService { | ||
private strategies: Map<SyncStrategyTarget, SyncStrategy> = new Map<SyncStrategyTarget, SyncStrategy>(); | ||
|
||
constructor(private readonly logger: Logger, @Optional() private readonly tspSyncStrategy?: TspSyncStrategy) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this works alright wit hone strategy, but we need to find a better way if there is ever a second one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. noticed ✅ |
||
this.logger.setContext(SyncService.name); | ||
this.registerStrategy(tspSyncStrategy); | ||
} | ||
|
||
protected registerStrategy(strategy?: SyncStrategy) { | ||
if (strategy) { | ||
this.strategies.set(strategy.getType(), strategy); | ||
} | ||
} | ||
|
||
public async startSync(target: string): Promise<void> { | ||
const targetStrategy = target as SyncStrategyTarget; | ||
mkreuzkam-cap marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!this.strategies.has(targetStrategy)) { | ||
this.logger.info(new InvalidTargetLoggable(target)); | ||
} | ||
await this.strategies.get(targetStrategy)?.sync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { SyncStrategyTarget } from '../sync-strategy.types'; | ||
|
||
export abstract class SyncStrategy { | ||
abstract getType(): SyncStrategyTarget; | ||
|
||
abstract sync(): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum SyncStrategyTarget { | ||
TSP = 'tsp', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { LoggerModule } from '@src/core/logger'; | ||
import { ConsoleWriterModule } from '@infra/console'; | ||
import { Configuration } from '@hpi-schul-cloud/commons/lib'; | ||
import { SyncConsole } from './console/sync.console'; | ||
import { SyncUc } from './uc/sync.uc'; | ||
import { SyncService } from './service/sync.service'; | ||
import { TspSyncStrategy } from './tsp/tsp-sync.strategy'; | ||
|
||
@Module({ | ||
imports: [LoggerModule, ConsoleWriterModule], | ||
providers: [ | ||
SyncConsole, | ||
SyncUc, | ||
SyncService, | ||
...((Configuration.get('FEATURE_TSP_SYNC_ENABLED') as boolean) ? [TspSyncStrategy] : []), | ||
], | ||
exports: [SyncConsole], | ||
}) | ||
export class SyncModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { TestingModule, Test } from '@nestjs/testing'; | ||
import { SyncStrategyTarget } from '../sync-strategy.types'; | ||
import { TspSyncStrategy } from './tsp-sync.strategy'; | ||
|
||
describe(TspSyncStrategy.name, () => { | ||
let module: TestingModule; | ||
let strategy: TspSyncStrategy; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
providers: [TspSyncStrategy], | ||
}).compile(); | ||
|
||
strategy = module.get(TspSyncStrategy); | ||
}); | ||
|
||
afterAll(async () => { | ||
await module.close(); | ||
}); | ||
|
||
describe('when tsp sync strategy is initialized', () => { | ||
it('should be defined', () => { | ||
expect(strategy).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('getType', () => { | ||
describe('when tsp sync strategy is initialized', () => { | ||
it('should return tsp', () => { | ||
expect(strategy.getType()).toBe(SyncStrategyTarget.TSP); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('sync', () => { | ||
it('should return a promise', () => { | ||
const result = strategy.sync(); | ||
|
||
expect(result).toBeInstanceOf(Promise); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { SyncStrategy } from '../strategy/sync-strategy'; | ||
import { SyncStrategyTarget } from '../sync-strategy.types'; | ||
|
||
@Injectable() | ||
export class TspSyncStrategy extends SyncStrategy { | ||
getType(): SyncStrategyTarget { | ||
return SyncStrategyTarget.TSP; | ||
} | ||
|
||
sync(): Promise<void> { | ||
// implementation | ||
return Promise.resolve(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { createMock } from '@golevelup/ts-jest'; | ||
import { SyncUc } from './sync.uc'; | ||
import { SyncService } from '../service/sync.service'; | ||
|
||
describe(SyncUc.name, () => { | ||
let module: TestingModule; | ||
let uc: SyncUc; | ||
let service: SyncService; | ||
|
||
beforeAll(async () => { | ||
module = await Test.createTestingModule({ | ||
providers: [ | ||
SyncUc, | ||
{ | ||
provide: SyncService, | ||
useValue: createMock<SyncService>({}), | ||
}, | ||
], | ||
}).compile(); | ||
uc = module.get(SyncUc); | ||
service = module.get(SyncService); | ||
}); | ||
|
||
describe('when sync uc is initialized', () => { | ||
it('should be defined', () => { | ||
expect(uc).toBeDefined(); | ||
}); | ||
}); | ||
|
||
describe('startSync', () => { | ||
describe('when calling startSync', () => { | ||
const setup = () => { | ||
const validTarget = 'tsp'; | ||
|
||
return { validTarget }; | ||
}; | ||
|
||
it('should call sync method', async () => { | ||
const { validTarget } = setup(); | ||
await uc.startSync(validTarget); | ||
|
||
expect(service.startSync).toHaveBeenCalledWith(validTarget); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if target is empty?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is aborted with the error message "missing required argument "