|
| 1 | +import { |
| 2 | + ApiConfig, |
| 3 | + CreatedDatasetIdentifiers, |
| 4 | + DatasetPreview, |
| 5 | + FilePreview, |
| 6 | + ReadError, |
| 7 | + createDataset, |
| 8 | + getMyDataCollectionItems |
| 9 | +} from '../../../src' |
| 10 | +import { TestConstants } from '../../testHelpers/TestConstants' |
| 11 | +import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' |
| 12 | +import { |
| 13 | + createCollectionViaApi, |
| 14 | + deleteCollectionViaApi |
| 15 | +} from '../../testHelpers/collections/collectionHelper' |
| 16 | +import { uploadFileViaApi } from '../../testHelpers/files/filesHelper' |
| 17 | +import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper' |
| 18 | +import { CollectionItemType } from '../../../dist' |
| 19 | +import { PublicationStatus } from '../../../dist/core/domain/models/PublicationStatus' |
| 20 | + |
| 21 | +const testRoleIds = [1, 2, 3, 4, 5, 6, 7, 8] |
| 22 | +const testCollectionItemTypes = [CollectionItemType.DATASET, CollectionItemType.FILE] |
| 23 | +const testPublishingStatuses = [ |
| 24 | + PublicationStatus.Published, |
| 25 | + PublicationStatus.Draft, |
| 26 | + PublicationStatus.Unpublished |
| 27 | +] |
| 28 | + |
| 29 | +describe('execute', () => { |
| 30 | + beforeAll(() => { |
| 31 | + ApiConfig.init( |
| 32 | + TestConstants.TEST_API_URL, |
| 33 | + DataverseApiAuthMechanism.API_KEY, |
| 34 | + process.env.TEST_API_KEY |
| 35 | + ) |
| 36 | + }) |
| 37 | + test('should return ? when repository returns empty item subset', async () => { |
| 38 | + expect.assertions(2) |
| 39 | + let readError: ReadError | undefined = undefined |
| 40 | + try { |
| 41 | + await getMyDataCollectionItems.execute( |
| 42 | + testRoleIds, |
| 43 | + testCollectionItemTypes, |
| 44 | + testPublishingStatuses, |
| 45 | + undefined, |
| 46 | + undefined, |
| 47 | + undefined |
| 48 | + ) |
| 49 | + throw new Error('Use case should throw an error') |
| 50 | + } catch (error) { |
| 51 | + readError = error as ReadError |
| 52 | + } finally { |
| 53 | + expect(readError).toBeInstanceOf(ReadError) |
| 54 | + expect(readError?.message).toEqual( |
| 55 | + 'There was an error when reading the resource. Reason was: Sorry, no results were found.' |
| 56 | + ) |
| 57 | + } |
| 58 | + }), |
| 59 | + describe('test with created collection items', () => { |
| 60 | + const testCollectionAlias = 'collectionsRepositoryFunctionalTestCollection' |
| 61 | + let testDatasetIds: CreatedDatasetIdentifiers |
| 62 | + const testTextFile1Name = 'test-file-1.txt' |
| 63 | + |
| 64 | + beforeAll(async () => { |
| 65 | + await createCollectionViaApi(testCollectionAlias) |
| 66 | + try { |
| 67 | + testDatasetIds = await createDataset.execute( |
| 68 | + TestConstants.TEST_NEW_DATASET_DTO, |
| 69 | + testCollectionAlias |
| 70 | + ) |
| 71 | + } catch (error) { |
| 72 | + throw new Error('Tests beforeAll(): Error while creating test dataset') |
| 73 | + } |
| 74 | + await uploadFileViaApi(testDatasetIds.numericId, testTextFile1Name).catch(() => { |
| 75 | + throw new Error(`Tests beforeAll(): Error while uploading file ${testTextFile1Name}`) |
| 76 | + }) |
| 77 | + }) |
| 78 | + |
| 79 | + afterAll(async () => { |
| 80 | + try { |
| 81 | + await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId) |
| 82 | + } catch (error) { |
| 83 | + throw new Error('Tests afterAll(): Error while deleting test dataset') |
| 84 | + } |
| 85 | + try { |
| 86 | + await deleteCollectionViaApi(testCollectionAlias) |
| 87 | + } catch (error) { |
| 88 | + throw new Error('Tests afterAll(): Error while deleting test collection') |
| 89 | + } |
| 90 | + }) |
| 91 | + |
| 92 | + test('should return items when valid roles,collection types, and publishingStatuses are provided', async () => { |
| 93 | + // Give enough time to Solr for indexing |
| 94 | + await new Promise((resolve) => setTimeout(resolve, 5000)) |
| 95 | + |
| 96 | + try { |
| 97 | + const actual = await getMyDataCollectionItems.execute( |
| 98 | + testRoleIds, |
| 99 | + testCollectionItemTypes, |
| 100 | + testPublishingStatuses |
| 101 | + ) |
| 102 | + |
| 103 | + const actualFilePreview = actual.items[1] as FilePreview |
| 104 | + const actualDatasetPreview = actual.items[0] as DatasetPreview |
| 105 | + |
| 106 | + expect(actualFilePreview.name).toBe('test-file-1.txt') |
| 107 | + expect(actualDatasetPreview.title).toBe( |
| 108 | + 'Dataset created using the createDataset use case' |
| 109 | + ) |
| 110 | + |
| 111 | + expect(actual.totalItemCount).toBe(2) |
| 112 | + } catch (error) { |
| 113 | + throw new Error('Item subset should be retrieved') |
| 114 | + } |
| 115 | + }) |
| 116 | + |
| 117 | + test('should return an error message when no role is specified', async () => { |
| 118 | + expect.assertions(2) |
| 119 | + let readError: ReadError | undefined = undefined |
| 120 | + try { |
| 121 | + await getMyDataCollectionItems.execute([], [], [], undefined, undefined, undefined) |
| 122 | + throw new Error('Use case should throw an error') |
| 123 | + } catch (error) { |
| 124 | + readError = error as ReadError |
| 125 | + } finally { |
| 126 | + expect(readError).toBeInstanceOf(ReadError) |
| 127 | + expect(readError?.message).toEqual( |
| 128 | + `There was an error when reading the resource. Reason was: No results. Please select at least one Role.` |
| 129 | + ) |
| 130 | + } |
| 131 | + }) |
| 132 | + test('should return an error message when no publication status is specified', async () => { |
| 133 | + expect.assertions(2) |
| 134 | + let readError: ReadError | undefined = undefined |
| 135 | + try { |
| 136 | + await getMyDataCollectionItems.execute( |
| 137 | + testRoleIds, |
| 138 | + testCollectionItemTypes, |
| 139 | + [], |
| 140 | + undefined, |
| 141 | + undefined, |
| 142 | + undefined |
| 143 | + ) |
| 144 | + throw new Error('Use case should throw an error') |
| 145 | + } catch (error) { |
| 146 | + readError = error as ReadError |
| 147 | + } finally { |
| 148 | + expect(readError).toBeInstanceOf(ReadError) |
| 149 | + expect(readError?.message).toEqual( |
| 150 | + `There was an error when reading the resource. Reason was: No user found for: "Published, Unpublished, Draft, In Review, Deaccessioned"` |
| 151 | + ) |
| 152 | + } |
| 153 | + }) |
| 154 | + test('should an error message when no collection type is specified', async () => { |
| 155 | + expect.assertions(2) |
| 156 | + let readError: ReadError | undefined = undefined |
| 157 | + try { |
| 158 | + await getMyDataCollectionItems.execute( |
| 159 | + testRoleIds, |
| 160 | + testCollectionItemTypes, |
| 161 | + [], |
| 162 | + undefined, |
| 163 | + undefined, |
| 164 | + undefined |
| 165 | + ) |
| 166 | + throw new Error('Use case should throw an error') |
| 167 | + } catch (error) { |
| 168 | + readError = error as ReadError |
| 169 | + } finally { |
| 170 | + expect(readError).toBeInstanceOf(ReadError) |
| 171 | + expect(readError?.message).toEqual( |
| 172 | + `There was an error when reading the resource. Reason was: No user found for: "Published, Unpublished, Draft, In Review, Deaccessioned"` |
| 173 | + ) |
| 174 | + } |
| 175 | + }) |
| 176 | + }) |
| 177 | +}) |
0 commit comments