From 12f83f174d2a29fae18a26895e4629d7b08546e8 Mon Sep 17 00:00:00 2001 From: GPortas Date: Mon, 26 Feb 2024 11:24:38 +0000 Subject: [PATCH] Added: datasetVersionId support in getFile use case --- .../models/DatasetNotNumberedVersion.ts | 1 + .../infra/repositories/FilesRepository.ts | 16 +-- .../integration/files/FilesRepository.test.ts | 109 +++++++----------- test/unit/files/FilesRepository.test.ts | 4 +- test/unit/files/GetFile.test.ts | 2 +- 5 files changed, 47 insertions(+), 85 deletions(-) diff --git a/src/datasets/domain/models/DatasetNotNumberedVersion.ts b/src/datasets/domain/models/DatasetNotNumberedVersion.ts index a646b5be..00f46a25 100644 --- a/src/datasets/domain/models/DatasetNotNumberedVersion.ts +++ b/src/datasets/domain/models/DatasetNotNumberedVersion.ts @@ -1,4 +1,5 @@ export enum DatasetNotNumberedVersion { DRAFT = ':draft', LATEST = ':latest', + LATEST_PUBLISHED = ':latest-published', } diff --git a/src/files/infra/repositories/FilesRepository.ts b/src/files/infra/repositories/FilesRepository.ts index 25a54296..9126a49f 100644 --- a/src/files/infra/repositories/FilesRepository.ts +++ b/src/files/infra/repositories/FilesRepository.ts @@ -11,7 +11,6 @@ import { FileSearchCriteria, FileOrderCriteria } from '../../domain/models/FileC import { FileCounts } from '../../domain/models/FileCounts'; import { transformFileCountsResponseToFileCounts } from './transformers/fileCountsTransformers'; import { FileDownloadSizeMode } from '../../domain/models/FileDownloadSizeMode'; -import { DatasetNotNumberedVersion } from '../../../datasets'; export interface GetFilesQueryParams { includeDeaccessioned: boolean; @@ -146,7 +145,7 @@ export class FilesRepository extends ApiRepository implements IFilesRepository { } public async getFile(fileId: number | string, datasetVersionId: string): Promise { - return this.doGet(this.getFileEndpoint(fileId, datasetVersionId), true) + return this.doGet(this.buildApiEndpoint(this.filesResourceName, `versions/${datasetVersionId}`, fileId), true) .then((response) => transformFileResponseToFile(response)) .catch((error) => { throw error; @@ -169,19 +168,6 @@ export class FilesRepository extends ApiRepository implements IFilesRepository { }); } - private getFileEndpoint(fileId: number | string, datasetVersionId: string): string { - if (datasetVersionId === DatasetNotNumberedVersion.DRAFT) { - return this.buildApiEndpoint(this.filesResourceName, 'draft', fileId); - } - if (datasetVersionId === DatasetNotNumberedVersion.LATEST) { - return this.buildApiEndpoint(this.filesResourceName, '', fileId); - } - // TODO: Implement once it is supported by the API https://github.com/IQSS/dataverse/issues/10280 - throw new Error( - `Requesting a file by its dataset version is not yet supported. Requested version: ${datasetVersionId}. Please try using the :latest or :draft version instead.`, - ); - } - private applyFileSearchCriteriaToQueryParams( queryParams: GetFilesQueryParams | GetFilesTotalDownloadSizeQueryParams, fileSearchCriteria: FileSearchCriteria, diff --git a/test/integration/files/FilesRepository.test.ts b/test/integration/files/FilesRepository.test.ts index 7241f163..baafaf2a 100644 --- a/test/integration/files/FilesRepository.test.ts +++ b/test/integration/files/FilesRepository.test.ts @@ -3,7 +3,7 @@ import { ApiConfig, DataverseApiAuthMechanism } from '../../../src/core/infra/re import { assert } from 'sinon'; import { expect } from 'chai'; import { TestConstants } from '../../testHelpers/TestConstants'; -import {registerFileViaApi, uploadFileViaApi} from '../../testHelpers/files/filesHelper'; +import { registerFileViaApi, uploadFileViaApi } from '../../testHelpers/files/filesHelper'; import { DatasetsRepository } from '../../../src/datasets/infra/repositories/DatasetsRepository'; import { ReadError } from '../../../src/core/domain/repositories/ReadError'; import { FileSearchCriteria, FileAccessStatus, FileOrderCriteria } from '../../../src/files/domain/models/FileCriteria'; @@ -11,7 +11,11 @@ import { DatasetNotNumberedVersion } from '../../../src/datasets'; import { FileCounts } from '../../../src/files/domain/models/FileCounts'; import { FileDownloadSizeMode } from '../../../src'; import { fail } from 'assert'; -import {deaccessionDatasetViaApi, publishDatasetViaApi, waitForNoLocks} from "../../testHelpers/datasets/datasetHelper"; +import { + deaccessionDatasetViaApi, + publishDatasetViaApi, + waitForNoLocks, +} from '../../testHelpers/datasets/datasetHelper'; describe('FilesRepository', () => { const sut: FilesRepository = new FilesRepository(); @@ -33,7 +37,9 @@ describe('FilesRepository', () => { beforeAll(async () => { ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.API_KEY, process.env.TEST_API_KEY); // Uploading test file 1 with some categories - const uploadFileResponse = await uploadFileViaApi(TestConstants.TEST_CREATED_DATASET_1_ID, testTextFile1Name, { categories: [testCategoryName] }) + const uploadFileResponse = await uploadFileViaApi(TestConstants.TEST_CREATED_DATASET_1_ID, testTextFile1Name, { + categories: [testCategoryName], + }) .then() .catch((e) => { console.log(e); @@ -63,11 +69,11 @@ describe('FilesRepository', () => { // Registering test file 1 await registerFileViaApi(uploadFileResponse.data.data.files[0].dataFile.id); const filesSubset = await sut.getDatasetFiles( - TestConstants.TEST_CREATED_DATASET_1_ID, - latestDatasetVersionId, - false, - FileOrderCriteria.NAME_AZ, - ) + TestConstants.TEST_CREATED_DATASET_1_ID, + latestDatasetVersionId, + false, + FileOrderCriteria.NAME_AZ, + ); testFileId = filesSubset.files[0].id; testFilePersistentId = filesSubset.files[0].persistentId; }); @@ -447,11 +453,11 @@ describe('FilesRepository', () => { describe('getFile', () => { describe('by numeric id', () => { - test('should return file when providing a valid id', async () => { - const actual = await sut.getFile(testFileId, DatasetNotNumberedVersion.LATEST); + test('should return file when providing a valid id', async () => { + const actual = await sut.getFile(testFileId, DatasetNotNumberedVersion.LATEST); - assert.match(actual.name, testTextFile1Name); - }); + assert.match(actual.name, testTextFile1Name); + }); test('should return file draft when providing a valid id and version is draft', async () => { const actual = await sut.getFile(testFileId, DatasetNotNumberedVersion.DRAFT); @@ -459,28 +465,16 @@ describe('FilesRepository', () => { assert.match(actual.name, testTextFile1Name); }); - test('should return Not Implemented Yet error when when providing a valid id and version is different than latest and draft', async () => { - // This tests can be removed once the API supports getting a file by version + test('should return error when file does not exist', async () => { let error: ReadError = undefined; - await sut.getFile(testFileId, '1.0').catch((e) => (error = e)); + await sut.getFile(nonExistentFiledId, DatasetNotNumberedVersion.LATEST).catch((e) => (error = e)); assert.match( - error.message, - `Requesting a file by its dataset version is not yet supported. Requested version: 1.0. Please try using the :latest or :draft version instead.`, + error.message, + `There was an error when reading the resource. Reason was: [404] File with ID ${nonExistentFiledId} not found.`, ); }); - - test('should return error when file does not exist', async () => { - let error: ReadError = undefined; - - await sut.getFile(nonExistentFiledId, DatasetNotNumberedVersion.LATEST).catch((e) => (error = e)); - - assert.match( - error.message, - `There was an error when reading the resource. Reason was: [400] Error attempting get the requested data file.`, - ); - }); }); describe('by persistent id', () => { test('should return file when providing a valid persistent id', async () => { @@ -495,18 +489,6 @@ describe('FilesRepository', () => { assert.match(actual.name, testTextFile1Name); }); - test('should return Not Implemented Yet error when when providing a valid persistent id and version is different than latest and draft', async () => { - // This tests can be removed once the API supports getting a file by version - let error: ReadError = undefined; - - await sut.getFile(testFilePersistentId, '1.0').catch((e) => (error = e)); - - assert.match( - error.message, - `Requesting a file by its dataset version is not yet supported. Requested version: 1.0. Please try using the :latest or :draft version instead.`, - ); - }); - test('should return error when file does not exist', async () => { let error: ReadError = undefined; @@ -514,46 +496,39 @@ describe('FilesRepository', () => { await sut.getFile(nonExistentFiledPersistentId, DatasetNotNumberedVersion.LATEST).catch((e) => (error = e)); assert.match( - error.message, - `There was an error when reading the resource. Reason was: [400] Error attempting get the requested data file.`, + error.message, + `There was an error when reading the resource. Reason was: [404] Datafile with Persistent ID ${nonExistentFiledPersistentId} not found.`, ); }); }); }); + describe('getFileCitation', () => { test('should return citation when file exists', async () => { - const actualFileCitation = await sut.getFileCitation( - testFileId, - DatasetNotNumberedVersion.LATEST, - false, - ); + const actualFileCitation = await sut.getFileCitation(testFileId, DatasetNotNumberedVersion.LATEST, false); expect(typeof actualFileCitation).to.be.a('string'); }); test('should return citation when dataset is deaccessioned', async () => { await publishDatasetViaApi(TestConstants.TEST_CREATED_DATASET_1_ID) - .then() - .catch(() => { - assert.fail('Error while publishing test Dataset'); - }); + .then() + .catch(() => { + assert.fail('Error while publishing test Dataset'); + }); await waitForNoLocks(TestConstants.TEST_CREATED_DATASET_1_ID, 10) - .then() - .catch(() => { - assert.fail('Error while waiting for no locks'); - }); + .then() + .catch(() => { + assert.fail('Error while waiting for no locks'); + }); await deaccessionDatasetViaApi(TestConstants.TEST_CREATED_DATASET_1_ID, '1.0') - .then() - .catch(() => { - assert.fail('Error while deaccessioning test Dataset'); - }); - - const actualFileCitation = await sut.getFileCitation( - testFileId, - DatasetNotNumberedVersion.LATEST, - true, - ); + .then() + .catch(() => { + assert.fail('Error while deaccessioning test Dataset'); + }); + + const actualFileCitation = await sut.getFileCitation(testFileId, DatasetNotNumberedVersion.LATEST, true); expect(typeof actualFileCitation).to.be.a('string'); }); @@ -562,8 +537,8 @@ describe('FilesRepository', () => { await sut.getFileCitation(nonExistentFiledId, DatasetNotNumberedVersion.LATEST, false).catch((e) => (error = e)); assert.match( - error.message, - `There was an error when reading the resource. Reason was: [404] File with ID ${nonExistentFiledId} not found.`, + error.message, + `There was an error when reading the resource. Reason was: [404] File with ID ${nonExistentFiledId} not found.`, ); }); }); diff --git a/test/unit/files/FilesRepository.test.ts b/test/unit/files/FilesRepository.test.ts index 38cd4d92..74ff4bfb 100644 --- a/test/unit/files/FilesRepository.test.ts +++ b/test/unit/files/FilesRepository.test.ts @@ -778,7 +778,7 @@ describe('FilesRepository', () => { }); describe('getFile' , () => { describe('by numeric id', () => { - const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/files/${testFile.id}/`; + const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/files/${testFile.id}/versions/${DatasetNotNumberedVersion.LATEST}`; const testGetFileResponse = { data: { status: 'OK', @@ -826,7 +826,7 @@ describe('FilesRepository', () => { }); }); describe('by persistent id', () => { - const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/files/:persistentId/?persistentId=${TestConstants.TEST_DUMMY_PERSISTENT_ID}`; + const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/files/:persistentId/versions/${DatasetNotNumberedVersion.LATEST}?persistentId=${TestConstants.TEST_DUMMY_PERSISTENT_ID}`; const testGetFileResponse = { data: { status: 'OK', diff --git a/test/unit/files/GetFile.test.ts b/test/unit/files/GetFile.test.ts index 239d7362..385666f8 100644 --- a/test/unit/files/GetFile.test.ts +++ b/test/unit/files/GetFile.test.ts @@ -61,4 +61,4 @@ describe('execute', () => { assert.match(actualError, testReadError); }) -}); \ No newline at end of file +});