-
Notifications
You must be signed in to change notification settings - Fork 10
Add linkCollection, unlinkCollection and getCollectionLinks use cases #346
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f199086
add linkCollection and unlinkCollection use cases
ekraffmiller 548ca2c
add functional tests
ekraffmiller 9db4c47
use consistent names for UnlinkCollection
ekraffmiller 380dedc
fix import
ekraffmiller ed2ebf3
fix roles test data
ekraffmiller 2f24d32
add GetCollectionLinks use case
ekraffmiller c2564a1
remove console.log()
ekraffmiller 1d78fbb
remove .DS_Store
ekraffmiller 3a969cb
Remove remaining .DS_Store file
ekraffmiller c439f93
add GetCollectionLinks to index.ts
ekraffmiller 8d47d04
fix string for API url, update unit UnlinkCollection test
ekraffmiller b9d0422
Update LinkCollection.test.ts
ekraffmiller 14e7968
fix test description
ekraffmiller eb2925b
resolve merge conflicts
ekraffmiller 396c348
fix integrations
ekraffmiller 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 hidden or 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 |
|---|---|---|
|
|
@@ -10,6 +10,9 @@ node_modules | |
| # unit tests | ||
| coverage | ||
|
|
||
| # macOS | ||
| .DS_Store | ||
|
|
||
| # ignore npm lock | ||
| package-json.lock | ||
| .npmrc | ||
This file contains hidden or 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,8 @@ | ||
| import { CollectionSummary } from './CollectionSummary' | ||
| import { DatasetSummary } from '../../../datasets/domain/models/DatasetSummary' | ||
|
|
||
| export interface CollectionLinks { | ||
| linkedCollections: CollectionSummary[] | ||
| collectionsLinkingToThis: CollectionSummary[] | ||
| linkedDatasets: DatasetSummary[] | ||
| } |
This file contains hidden or 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,5 @@ | ||
| export interface CollectionSummary { | ||
| id: number | ||
| alias: string | ||
| displayName: string | ||
| } | ||
This file contains hidden or 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 hidden or 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,22 @@ | ||
| import { UseCase } from '../../../core/domain/useCases/UseCase' | ||
| import { ICollectionsRepository } from '../repositories/ICollectionsRepository' | ||
| import { CollectionLinks } from '../models/CollectionLinks' | ||
|
|
||
| export class GetCollectionLinks implements UseCase<CollectionLinks> { | ||
| private collectionsRepository: ICollectionsRepository | ||
|
|
||
| constructor(collectionsRepository: ICollectionsRepository) { | ||
| this.collectionsRepository = collectionsRepository | ||
| } | ||
|
|
||
| /** | ||
| * Returns a CollectionLinks object containing other collections this collection is linked to, the other collections linking to this collection, and datasets linked to this collection, given the collection identifier or alias. | ||
| * | ||
| * @param {number | string} [collectionIdOrAlias] - A generic collection identifier, which can be either a string (for queries by CollectionAlias), or a number (for queries by CollectionId) | ||
| * If this parameter is not set, the default value is: ':root' | ||
| * @returns {Promise<CollectionLinks>} | ||
| */ | ||
| async execute(collectionId: number | string): Promise<CollectionLinks> { | ||
| return await this.collectionsRepository.getCollectionLinks(collectionId) | ||
| } | ||
| } |
This file contains hidden or 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 { UseCase } from '../../../core/domain/useCases/UseCase' | ||
| import { ICollectionsRepository } from '../repositories/ICollectionsRepository' | ||
|
|
||
| export class LinkCollection implements UseCase<void> { | ||
| private collectionsRepository: ICollectionsRepository | ||
|
|
||
| constructor(collectionsRepository: ICollectionsRepository) { | ||
| this.collectionsRepository = collectionsRepository | ||
| } | ||
|
|
||
| /** | ||
| * Creates a link between two collections. The linked collection will be linked to the linking collection.: | ||
| * | ||
| * @param {number| string} [linkedCollectionIdOrAlias] - The collection to be linked. Can be either a string (collection alias), or a number (collection id) | ||
| * @param { number | string} [linkingCollectionIdOrAlias] - The collection that will be linking to the linked collection. Can be either a string (collection alias), or a number (collection id) | ||
| * @returns {Promise<void>} -This method does not return anything upon successful completion. | ||
| */ | ||
| async execute( | ||
| linkedCollectionIdOrAlias: number | string, | ||
| linkingCollectionIdOrAlias: number | string | ||
| ): Promise<void> { | ||
| return await this.collectionsRepository.linkCollection( | ||
| linkedCollectionIdOrAlias, | ||
| linkingCollectionIdOrAlias | ||
| ) | ||
| } | ||
| } |
This file contains hidden or 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 { UseCase } from '../../../core/domain/useCases/UseCase' | ||
| import { ICollectionsRepository } from '../repositories/ICollectionsRepository' | ||
|
|
||
| export class UnlinkCollection implements UseCase<void> { | ||
| private collectionsRepository: ICollectionsRepository | ||
|
|
||
| constructor(collectionsRepository: ICollectionsRepository) { | ||
| this.collectionsRepository = collectionsRepository | ||
| } | ||
|
|
||
| /** | ||
| * Unlinks a collection from the collection that links to it | ||
| * | ||
| * @param {number| string} [linkedCollectionIdOrAlias] - The collection that is linked. Can be either a string (collection alias), or a number (collection id) | ||
| * @param { number | string} [linkingCollectionIdOrAlias] - The collection that links to the linked collection. Can be either a string (collection alias), or a number (collection id) | ||
| * @returns {Promise<void>} -This method does not return anything upon successful completion. | ||
| */ | ||
| async execute( | ||
| linkedCollectionIdOrAlias: number | string, | ||
| linkingCollectionIdOrAlias: number | string | ||
| ): Promise<void> { | ||
| return await this.collectionsRepository.unlinkCollection( | ||
| linkedCollectionIdOrAlias, | ||
| linkingCollectionIdOrAlias | ||
| ) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,4 @@ | ||
| export interface DatasetSummary { | ||
| persistentId: string | ||
| title: string | ||
| } |
This file contains hidden or 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,71 @@ | ||
| import { | ||
| ApiConfig, | ||
| WriteError, | ||
| createCollection, | ||
| getCollection, | ||
| linkCollection, | ||
| deleteCollection, | ||
| getCollectionItems | ||
| } from '../../../src' | ||
| import { TestConstants } from '../../testHelpers/TestConstants' | ||
| import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' | ||
| import { createCollectionDTO } from '../../testHelpers/collections/collectionHelper' | ||
|
|
||
| describe('execute', () => { | ||
| const firstCollectionAlias = 'linkCollection-functional-test-first' | ||
| const secondCollectionAlias = 'linkCollection-functional-test-second' | ||
| let firstCollectionId: number | ||
| let secondCollectionId: number | ||
| beforeEach(async () => { | ||
| ApiConfig.init( | ||
| TestConstants.TEST_API_URL, | ||
| DataverseApiAuthMechanism.API_KEY, | ||
| process.env.TEST_API_KEY | ||
| ) | ||
| const firstCollection = createCollectionDTO(firstCollectionAlias) | ||
| const secondCollection = createCollectionDTO(secondCollectionAlias) | ||
| firstCollectionId = await createCollection.execute(firstCollection) | ||
| secondCollectionId = await createCollection.execute(secondCollection) | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| await Promise.all([ | ||
g-saracca marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| deleteCollection.execute(firstCollectionId), | ||
| deleteCollection.execute(secondCollectionId) | ||
| ]) | ||
| }) | ||
|
|
||
| test('should successfully link two collections', async () => { | ||
| expect.assertions(1) | ||
| try { | ||
| await linkCollection.execute(secondCollectionAlias, firstCollectionAlias) | ||
| } catch (error) { | ||
| throw new Error('Collections should be linked successfully') | ||
| } finally { | ||
| // Wait for the linking to be processed by Solr | ||
| await new Promise((resolve) => setTimeout(resolve, 5000)) | ||
g-saracca marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const collectionItemSubset = await getCollectionItems.execute(firstCollectionAlias) | ||
|
|
||
| expect(collectionItemSubset.items.length).toBe(1) | ||
| } | ||
| }) | ||
|
|
||
| test('should throw an error when linking a non-existent collection', async () => { | ||
| const invalidCollectionId = 99999 | ||
| const firstCollection = await getCollection.execute(firstCollectionAlias) | ||
|
|
||
| expect.assertions(2) | ||
| let writeError: WriteError | undefined = undefined | ||
| try { | ||
| await linkCollection.execute(invalidCollectionId, firstCollection.id) | ||
| throw new Error('Use case should throw an error') | ||
| } catch (error) { | ||
| writeError = error as WriteError | ||
| } finally { | ||
| expect(writeError).toBeInstanceOf(WriteError) | ||
| expect(writeError?.message).toEqual( | ||
| `There was an error when writing the resource. Reason was: [404] Can't find dataverse with identifier='${invalidCollectionId}'` | ||
| ) | ||
| } | ||
| }) | ||
| }) | ||
This file contains hidden or 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,71 @@ | ||
| import { | ||
| ApiConfig, | ||
| WriteError, | ||
| createCollection, | ||
| linkCollection, | ||
| deleteCollection, | ||
| getCollectionItems, | ||
| unlinkCollection | ||
| } from '../../../src' | ||
| import { TestConstants } from '../../testHelpers/TestConstants' | ||
| import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' | ||
| import { createCollectionDTO } from '../../testHelpers/collections/collectionHelper' | ||
|
|
||
| describe('execute', () => { | ||
| const firstCollectionAlias = 'unlinkCollection-functional-test-first' | ||
| const secondCollectionAlias = 'unlinkCollection-functional-test-second' | ||
|
|
||
| let firstCollectionId: number | ||
| let secondCollectionId: number | ||
| beforeEach(async () => { | ||
| ApiConfig.init( | ||
| TestConstants.TEST_API_URL, | ||
| DataverseApiAuthMechanism.API_KEY, | ||
| process.env.TEST_API_KEY | ||
| ) | ||
| const firstCollectionDTO = createCollectionDTO(firstCollectionAlias) | ||
| const secondCollectionDTO = createCollectionDTO(secondCollectionAlias) | ||
| firstCollectionId = await createCollection.execute(firstCollectionDTO) | ||
| secondCollectionId = await createCollection.execute(secondCollectionDTO) | ||
| await linkCollection.execute(secondCollectionAlias, firstCollectionAlias) | ||
| // Give enough time to Solr for indexing | ||
| await new Promise((resolve) => setTimeout(resolve, 5000)) | ||
| }) | ||
|
|
||
| afterEach(async () => { | ||
| await Promise.all([ | ||
g-saracca marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| deleteCollection.execute(firstCollectionId), | ||
| deleteCollection.execute(secondCollectionId) | ||
| ]) | ||
| }) | ||
|
|
||
| test('should successfully unlink two collections', async () => { | ||
| // Verify that the collections are linked | ||
| const collectionItemSubset = await getCollectionItems.execute(firstCollectionAlias) | ||
| expect(collectionItemSubset.items.length).toBe(1) | ||
|
|
||
| await unlinkCollection.execute(secondCollectionAlias, firstCollectionAlias) | ||
| // Wait for the unlinking to be processed by Solr | ||
| await new Promise((resolve) => setTimeout(resolve, 5000)) | ||
| const collectionItemSubset2 = await getCollectionItems.execute(firstCollectionAlias) | ||
| expect(collectionItemSubset2.items.length).toBe(0) | ||
| }) | ||
|
|
||
| test('should throw an error when unlinking a non-existent collection', async () => { | ||
| const invalidCollectionId = 99999 | ||
|
|
||
| expect.assertions(2) | ||
| let writeError: WriteError | undefined = undefined | ||
| try { | ||
| await unlinkCollection.execute(invalidCollectionId, firstCollectionId) | ||
| throw new Error('Use case should throw an error') | ||
| } catch (error) { | ||
| writeError = error as WriteError | ||
| } finally { | ||
| expect(writeError).toBeInstanceOf(WriteError) | ||
| expect(writeError?.message).toEqual( | ||
| `There was an error when writing the resource. Reason was: [404] Can't find dataverse with identifier='${invalidCollectionId}'` | ||
| ) | ||
| } | ||
| }) | ||
| }) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.