-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65f1342
commit d8408ad
Showing
18 changed files
with
315 additions
and
17 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/context/virtual-drive/files/application/override/FileOverrider.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,36 @@ | ||
import { ContentsId } from '../../../contents/domain/ContentsId'; | ||
import { EventBus } from '../../../shared/domain/EventBus'; | ||
import { File } from '../../domain/File'; | ||
import { FileRepository } from '../../domain/FileRepository'; | ||
import { FileSize } from '../../domain/FileSize'; | ||
import { FileNotFoundError } from '../../domain/errors/FileNotFoundError'; | ||
import { RemoteFileSystem } from '../../domain/file-systems/RemoteFileSystem'; | ||
|
||
export class FileOverrider { | ||
constructor( | ||
private readonly repository: FileRepository, | ||
private readonly rfs: RemoteFileSystem, | ||
private readonly eventBus: EventBus | ||
) {} | ||
|
||
async run( | ||
fileId: File['id'], | ||
contentsId: File['contentsId'], | ||
size: File['size'] | ||
): Promise<void> { | ||
const file = await this.repository.searchById(fileId); | ||
|
||
if (!file) { | ||
throw new FileNotFoundError(fileId); | ||
} | ||
|
||
const newContentsId = new ContentsId(contentsId); | ||
const newSize = new FileSize(size); | ||
|
||
file.changeContents(newContentsId, newSize); | ||
|
||
this.rfs.override(file); | ||
|
||
this.eventBus.publish(file.pullDomainEvents()); | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
src/context/virtual-drive/files/domain/errors/FileNotFoundError.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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export class FileNotFoundError extends Error { | ||
constructor(id: string) { | ||
constructor(id: string | number) { | ||
super(`File ${id} not found`); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/context/virtual-drive/files/domain/events/FileOverriddenDomainEvent.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,46 @@ | ||
import { DomainEvent } from '../../../../shared/domain/DomainEvent'; | ||
|
||
export class FileOverriddenDomainEvent extends DomainEvent { | ||
static readonly EVENT_NAME = 'file.overridden'; | ||
|
||
readonly previousContentsId: string; | ||
readonly previousSize: number; | ||
|
||
readonly currentContentsId: string; | ||
readonly currentSize: number; | ||
|
||
constructor({ | ||
aggregateId, | ||
previousContentsId, | ||
previousSize, | ||
currentContentsId, | ||
currentSize, | ||
}: { | ||
aggregateId: string; | ||
previousContentsId: string; | ||
previousSize: number; | ||
currentContentsId: string; | ||
currentSize: number; | ||
}) { | ||
super({ | ||
eventName: FileOverriddenDomainEvent.EVENT_NAME, | ||
aggregateId, | ||
}); | ||
|
||
this.previousContentsId = previousContentsId; | ||
this.previousSize = previousSize; | ||
|
||
this.currentContentsId = currentContentsId; | ||
this.currentSize = currentSize; | ||
} | ||
|
||
toPrimitives() { | ||
return { | ||
aggregateId: this.aggregateId, | ||
previousContentsId: this.previousContentsId, | ||
previousSize: this.previousSize, | ||
currentContentsId: this.currentContentsId, | ||
currentSize: this.currentSize, | ||
}; | ||
} | ||
} |
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
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
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
27 changes: 27 additions & 0 deletions
27
tests/context/virtual-drive/files/__test-class__/FileCreatorTestClass.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,27 @@ | ||
import { FileCreator } from '../../../../../src/context/virtual-drive/files/application/FileCreator'; | ||
import { FileDeleter } from '../../../../../src/context/virtual-drive/files/application/FileDeleter'; | ||
import { File } from '../../../../../src/context/virtual-drive/files/domain/File'; | ||
import { FileRepository } from '../../../../../src/context/virtual-drive/files/domain/FileRepository'; | ||
import { SyncFileMessenger } from '../../../../../src/context/virtual-drive/files/domain/SyncFileMessenger'; | ||
import { RemoteFileSystem } from '../../../../../src/context/virtual-drive/files/domain/file-systems/RemoteFileSystem'; | ||
import { ParentFolderFinder } from '../../../../../src/context/virtual-drive/folders/application/ParentFolderFinder'; | ||
import { EventBus } from '../../../../../src/context/virtual-drive/shared/domain/EventBus'; | ||
|
||
export class FileCreatorTestClass extends FileCreator { | ||
public readonly mock = jest.fn(); | ||
|
||
constructor() { | ||
super( | ||
{} as RemoteFileSystem, | ||
{} as FileRepository, | ||
{} as ParentFolderFinder, | ||
{} as FileDeleter, | ||
{} as EventBus, | ||
{} as SyncFileMessenger | ||
); | ||
} | ||
|
||
run(path: string, contentsId: string, size: number): Promise<File> { | ||
return this.mock(path, contentsId, size); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/context/virtual-drive/files/__test-class__/FileToOverrideProviderTestClass.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,17 @@ | ||
import { FileToOverrideProvider } from '../../../../../src/context/virtual-drive/files/application/FileToOverrideProvider'; | ||
import { File } from '../../../../../src/context/virtual-drive/files/domain/File'; | ||
import { FileRepository } from '../../../../../src/context/virtual-drive/files/domain/FileRepository'; | ||
import { EventRepository } from '../../../../../src/context/virtual-drive/shared/domain/EventRepository'; | ||
import { Optional } from '../../../../../src/shared/types/Optional'; | ||
|
||
export class FileToOverrideProviderTestClass extends FileToOverrideProvider { | ||
readonly mock = jest.fn(); | ||
|
||
constructor() { | ||
super({} as EventRepository, {} as FileRepository); | ||
} | ||
|
||
run(): Promise<Optional<File>> { | ||
return this.mock(); | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
...virtual-drive/files/application/event-subscribers/CreateFileOnOfflineFileUploaded.test.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,40 @@ | ||
import { CreateFileOnOfflineFileUploaded } from '../../../../../../src/context/virtual-drive/files/application/event-subsribers/CreateFileOnOfflineFileUplodaded'; | ||
import { Optional } from '../../../../../../src/shared/types/Optional'; | ||
import { FileCreatorTestClass } from '../../__test-class__/FileCreatorTestClass'; | ||
import { FileToOverrideProviderTestClass } from '../../__test-class__/FileToOverrideProviderTestClass'; | ||
import { FileMother } from '../../domain/FileMother'; | ||
import { OfflineContentsUploadedDomainEventMother } from '../../domain/events/OfflineContentsUploadedDomainEventMother'; | ||
|
||
describe('Create File On Offline File Uploaded', () => { | ||
it('creates a new file when no file to be overridden is found', async () => { | ||
const creator = new FileCreatorTestClass(); | ||
const toOverride = new FileToOverrideProviderTestClass(); | ||
const uploadedEvent = OfflineContentsUploadedDomainEventMother.any(); | ||
|
||
const sut = new CreateFileOnOfflineFileUploaded(creator, toOverride); | ||
|
||
toOverride.mock.mockReturnValueOnce(Optional.empty()); | ||
|
||
await sut.on(uploadedEvent); | ||
|
||
expect(creator.mock).toBeCalledWith( | ||
uploadedEvent.path, | ||
uploadedEvent.aggregateId, | ||
uploadedEvent.size | ||
); | ||
}); | ||
|
||
it('does not create a new file an overridden file is provided', async () => { | ||
const creator = new FileCreatorTestClass(); | ||
const toOverride = new FileToOverrideProviderTestClass(); | ||
const uploadedEvent = OfflineContentsUploadedDomainEventMother.any(); | ||
|
||
const sut = new CreateFileOnOfflineFileUploaded(creator, toOverride); | ||
|
||
toOverride.mock.mockReturnValueOnce(Optional.of(FileMother.any())); | ||
|
||
await sut.on(uploadedEvent); | ||
|
||
expect(creator.mock).not.toBeCalled(); | ||
}); | ||
}); |
Oops, something went wrong.