From a31b7616f3d33698d3b108cc0ff0e2db83f8c8c7 Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Tue, 28 Nov 2023 15:24:27 -0500 Subject: [PATCH 1/9] Add includeDeaccessioned parameter to API call for getting dataset citation --- .../infra/repositories/DatasetsRepository.ts | 11 ++++++++++- .../datasets/DatasetsRepository.test.ts | 18 ++++++++++++++++++ test/testHelpers/TestConstants.ts | 14 ++++++++++++++ test/testHelpers/datasets/datasetHelper.ts | 7 +++++++ test/unit/datasets/DatasetsRepository.test.ts | 6 +++--- 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/datasets/infra/repositories/DatasetsRepository.ts b/src/datasets/infra/repositories/DatasetsRepository.ts index 55791700..b370738c 100644 --- a/src/datasets/infra/repositories/DatasetsRepository.ts +++ b/src/datasets/infra/repositories/DatasetsRepository.ts @@ -7,6 +7,9 @@ import { transformDatasetUserPermissionsResponseToDatasetUserPermissions } from import { DatasetLock } from '../../domain/models/DatasetLock'; import { transformDatasetLocksResponseToDatasetLocks } from './transformers/datasetLocksTransformers'; +export interface GetCitationQueryParams { + includeDeaccessioned: boolean; +} export class DatasetsRepository extends ApiRepository implements IDatasetsRepository { private readonly datasetsResourceName: string = 'datasets'; @@ -45,10 +48,16 @@ export class DatasetsRepository extends ApiRepository implements IDatasetsReposi }); } + + public async getDatasetCitation(datasetId: number, datasetVersionId: string): Promise { + const queryParams: GetCitationQueryParams = { + includeDeaccessioned: true, + + }; return this.doGet( this.buildApiEndpoint(this.datasetsResourceName, `versions/${datasetVersionId}/citation`, datasetId), - true, + true,queryParams ) .then((response) => response.data.data.message) .catch((error) => { diff --git a/test/integration/datasets/DatasetsRepository.test.ts b/test/integration/datasets/DatasetsRepository.test.ts index d6215363..d9d0cb74 100644 --- a/test/integration/datasets/DatasetsRepository.test.ts +++ b/test/integration/datasets/DatasetsRepository.test.ts @@ -6,6 +6,7 @@ import { createDatasetViaApi, createPrivateUrlViaApi, publishDatasetViaApi, + deaccessionDatasetViaApi } from '../../testHelpers/datasets/datasetHelper'; import { ReadError } from '../../../src/core/domain/repositories/ReadError'; import { DatasetNotNumberedVersion, DatasetLockType } from '../../../src/datasets'; @@ -95,6 +96,23 @@ describe('DatasetsRepository', () => { `There was an error when reading the resource. Reason was: [404] Dataset with ID ${nonExistentTestDatasetId} not found.`, ); }); + test('should return citation when dataset is deaccessioned', async () => { + await publishDatasetViaApi(TestConstants.TEST_CREATED_DATASET_ID).then((response) => + { console.log(JSON.stringify(response.data.data))}) + await deaccessionDatasetViaApi(TestConstants.TEST_CREATED_DATASET_ID,'1.0') + .then() + .catch((error) => { + console.log(JSON.stringify(error)); + assert.fail('Error while deaccessioning test Dataset'); + }); + + const actualDatasetCitation = await sut.getDatasetCitation( + TestConstants.TEST_CREATED_DATASET_ID, + latestVersionId, + ); + expect(typeof actualDatasetCitation).toBe('string'); + + }); }); describe('Private URLs', () => { diff --git a/test/testHelpers/TestConstants.ts b/test/testHelpers/TestConstants.ts index 37dca855..d3356a7e 100644 --- a/test/testHelpers/TestConstants.ts +++ b/test/testHelpers/TestConstants.ts @@ -15,6 +15,13 @@ export class TestConstants { 'X-Dataverse-Key': TestConstants.TEST_DUMMY_API_KEY, }, }; + static readonly TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY_INCLUDE_DEACCESSIONED = { + params: { includeDeaccessioned: true }, + headers: { + 'Content-Type': 'application/json', + 'X-Dataverse-Key': TestConstants.TEST_DUMMY_API_KEY, + }, + }; static readonly TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_SESSION_COOKIE = { withCredentials: true, params: {}, @@ -22,6 +29,13 @@ export class TestConstants { 'Content-Type': 'application/json', }, }; + static readonly TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_SESSION_COOKIE_INCLUDE_DEACCESSIONED = { + withCredentials: true, + params: { includeDeaccessioned: true }, + headers: { + 'Content-Type': 'application/json', + }, + }; static readonly TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG = { params: {}, headers: { diff --git a/test/testHelpers/datasets/datasetHelper.ts b/test/testHelpers/datasets/datasetHelper.ts index dc1119a9..5195262d 100644 --- a/test/testHelpers/datasets/datasetHelper.ts +++ b/test/testHelpers/datasets/datasetHelper.ts @@ -212,6 +212,13 @@ export const publishDatasetViaApi = async (datasetId: number): Promise => { + return await axios.post( + `${TestConstants.TEST_API_URL}/datasets/${datasetId}/versions/${versionId}/deaccession`, + {}, + DATAVERSE_API_REQUEST_HEADERS, + ); +} export const createPrivateUrlViaApi = async (datasetId: number): Promise => { return await axios.post( `${TestConstants.TEST_API_URL}/datasets/${datasetId}/privateUrl`, diff --git a/test/unit/datasets/DatasetsRepository.test.ts b/test/unit/datasets/DatasetsRepository.test.ts index 34844403..3aceb645 100644 --- a/test/unit/datasets/DatasetsRepository.test.ts +++ b/test/unit/datasets/DatasetsRepository.test.ts @@ -242,7 +242,7 @@ describe('DatasetsRepository', () => { assert.calledWithExactly( axiosGetStub, expectedApiEndpoint, - TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY, + TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY_INCLUDE_DEACCESSIONED, ); assert.match(actual, testCitation); @@ -254,7 +254,7 @@ describe('DatasetsRepository', () => { assert.calledWithExactly( axiosGetStub, expectedApiEndpoint, - TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_SESSION_COOKIE, + TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_SESSION_COOKIE_INCLUDE_DEACCESSIONED, ); assert.match(actual, testCitation); }); @@ -268,7 +268,7 @@ describe('DatasetsRepository', () => { assert.calledWithExactly( axiosGetStub, `${TestConstants.TEST_API_URL}/datasets/${testDatasetModel.id}/versions/${testVersionId}/citation`, - TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY, + TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY_INCLUDE_DEACCESSIONED, ); expect(error).to.be.instanceOf(Error); }); From f1113fa03e2349ebb70014fbc7e78720690af5da Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Wed, 29 Nov 2023 21:22:58 -0500 Subject: [PATCH 2/9] fix: add deaccessionReason to API call --- test/testHelpers/datasets/datasetHelper.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/testHelpers/datasets/datasetHelper.ts b/test/testHelpers/datasets/datasetHelper.ts index 5195262d..f620fccf 100644 --- a/test/testHelpers/datasets/datasetHelper.ts +++ b/test/testHelpers/datasets/datasetHelper.ts @@ -213,9 +213,10 @@ export const publishDatasetViaApi = async (datasetId: number): Promise => { + const data = { deaccessionReason: 'Test reason.'}; return await axios.post( `${TestConstants.TEST_API_URL}/datasets/${datasetId}/versions/${versionId}/deaccession`, - {}, + JSON.stringify(data), DATAVERSE_API_REQUEST_HEADERS, ); } From 66d1623c6e03c66551ace46028842c35f41fa37b Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Thu, 30 Nov 2023 15:12:40 -0500 Subject: [PATCH 3/9] fix: integration test --- .../datasets/DatasetsRepository.test.ts | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/test/integration/datasets/DatasetsRepository.test.ts b/test/integration/datasets/DatasetsRepository.test.ts index d9d0cb74..0ea82ddd 100644 --- a/test/integration/datasets/DatasetsRepository.test.ts +++ b/test/integration/datasets/DatasetsRepository.test.ts @@ -97,9 +97,25 @@ describe('DatasetsRepository', () => { ); }); test('should return citation when dataset is deaccessioned', async () => { - await publishDatasetViaApi(TestConstants.TEST_CREATED_DATASET_ID).then((response) => - { console.log(JSON.stringify(response.data.data))}) - await deaccessionDatasetViaApi(TestConstants.TEST_CREATED_DATASET_ID,'1.0') + let createdDatasetId = undefined; + await createDatasetViaApi() + .then((response) => (createdDatasetId = response.data.data.id)) + .catch(() => { + assert.fail('Error while creating test Dataset'); + }); + // We publish the new test dataset we can deaccession it + await publishDatasetViaApi(createdDatasetId) + .then() + .catch(() => { + assert.fail('Error while publishing test Dataset'); + }); + + let locks = await sut.getDatasetLocks(createdDatasetId); + while (locks.length > 0) { + await new Promise(resolve => setTimeout(resolve, 1000)); + locks = await sut.getDatasetLocks(createdDatasetId); + } + await deaccessionDatasetViaApi(createdDatasetId,'1.0') .then() .catch((error) => { console.log(JSON.stringify(error)); @@ -107,7 +123,7 @@ describe('DatasetsRepository', () => { }); const actualDatasetCitation = await sut.getDatasetCitation( - TestConstants.TEST_CREATED_DATASET_ID, + createdDatasetId, latestVersionId, ); expect(typeof actualDatasetCitation).toBe('string'); From 779a3f4ccd2b9f1795f48d6805854a73376db4ec Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Thu, 30 Nov 2023 15:58:10 -0500 Subject: [PATCH 4/9] fix: add maxTries to wait locks to be released --- test/integration/datasets/DatasetsRepository.test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/integration/datasets/DatasetsRepository.test.ts b/test/integration/datasets/DatasetsRepository.test.ts index 0ea82ddd..23d09590 100644 --- a/test/integration/datasets/DatasetsRepository.test.ts +++ b/test/integration/datasets/DatasetsRepository.test.ts @@ -111,9 +111,15 @@ describe('DatasetsRepository', () => { }); let locks = await sut.getDatasetLocks(createdDatasetId); - while (locks.length > 0) { + const maxTries = 10; + let tries = 0; + while (locks.length > 0 && tries < maxTries) { await new Promise(resolve => setTimeout(resolve, 1000)); locks = await sut.getDatasetLocks(createdDatasetId); + tries++ + } + if (tries >= maxTries && locks.length > 0) { + assert.fail('Error while waiting for locks to be released'); } await deaccessionDatasetViaApi(createdDatasetId,'1.0') .then() From 51cf1cbe61bd2571a76ca4d8fa9fffe041c9f6ac Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Sat, 16 Dec 2023 13:00:27 -0500 Subject: [PATCH 5/9] fix: add includeDeaccessioned parameter to getDatasetCitation() --- src/datasets/domain/repositories/IDatasetsRepository.ts | 2 +- src/datasets/domain/useCases/GetDatasetCitation.ts | 3 ++- src/datasets/infra/repositories/DatasetsRepository.ts | 8 +++----- test/integration/datasets/DatasetsRepository.test.ts | 5 +++-- test/unit/datasets/DatasetsRepository.test.ts | 7 ++++--- test/unit/datasets/GetDatasetCitation.test.ts | 2 +- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/datasets/domain/repositories/IDatasetsRepository.ts b/src/datasets/domain/repositories/IDatasetsRepository.ts index 6a7954d4..9c51935d 100644 --- a/src/datasets/domain/repositories/IDatasetsRepository.ts +++ b/src/datasets/domain/repositories/IDatasetsRepository.ts @@ -6,7 +6,7 @@ export interface IDatasetsRepository { getDatasetSummaryFieldNames(): Promise; getDataset(datasetId: number | string, datasetVersionId: string, includeDeaccessioned: boolean): Promise; getPrivateUrlDataset(token: string): Promise; - getDatasetCitation(datasetId: number, datasetVersionId: string): Promise; + getDatasetCitation(datasetId: number, datasetVersionId: string, includeDeaccessioned: boolean): Promise; getPrivateUrlDatasetCitation(token: string): Promise; getDatasetUserPermissions(datasetId: number | string): Promise; getDatasetLocks(datasetId: number | string): Promise; diff --git a/src/datasets/domain/useCases/GetDatasetCitation.ts b/src/datasets/domain/useCases/GetDatasetCitation.ts index e919b4b5..1783614d 100644 --- a/src/datasets/domain/useCases/GetDatasetCitation.ts +++ b/src/datasets/domain/useCases/GetDatasetCitation.ts @@ -12,7 +12,8 @@ export class GetDatasetCitation implements UseCase { async execute( datasetId: number, datasetVersionId: string | DatasetNotNumberedVersion = DatasetNotNumberedVersion.LATEST, + includeDeaccessioned: boolean = false, ): Promise { - return await this.datasetsRepository.getDatasetCitation(datasetId, datasetVersionId); + return await this.datasetsRepository.getDatasetCitation(datasetId, datasetVersionId, includeDeaccessioned) } } diff --git a/src/datasets/infra/repositories/DatasetsRepository.ts b/src/datasets/infra/repositories/DatasetsRepository.ts index b370738c..def499d4 100644 --- a/src/datasets/infra/repositories/DatasetsRepository.ts +++ b/src/datasets/infra/repositories/DatasetsRepository.ts @@ -50,14 +50,12 @@ export class DatasetsRepository extends ApiRepository implements IDatasetsReposi - public async getDatasetCitation(datasetId: number, datasetVersionId: string): Promise { - const queryParams: GetCitationQueryParams = { - includeDeaccessioned: true, + public async getDatasetCitation(datasetId: number, datasetVersionId: string, + includeDeaccessioned: boolean): Promise { - }; return this.doGet( this.buildApiEndpoint(this.datasetsResourceName, `versions/${datasetVersionId}/citation`, datasetId), - true,queryParams + true, { includeDeaccessioned: includeDeaccessioned} ) .then((response) => response.data.data.message) .catch((error) => { diff --git a/test/integration/datasets/DatasetsRepository.test.ts b/test/integration/datasets/DatasetsRepository.test.ts index 23d09590..5c31e86f 100644 --- a/test/integration/datasets/DatasetsRepository.test.ts +++ b/test/integration/datasets/DatasetsRepository.test.ts @@ -82,14 +82,14 @@ describe('DatasetsRepository', () => { const actualDatasetCitation = await sut.getDatasetCitation( TestConstants.TEST_CREATED_DATASET_ID, latestVersionId, + false, ); expect(typeof actualDatasetCitation).toBe('string'); }); test('should return error when dataset does not exist', async () => { let error: ReadError = undefined; - - await sut.getDatasetCitation(nonExistentTestDatasetId, latestVersionId).catch((e) => (error = e)); + await sut.getDatasetCitation(nonExistentTestDatasetId, latestVersionId,false).catch((e) => (error = e)); assert.match( error.message, @@ -131,6 +131,7 @@ describe('DatasetsRepository', () => { const actualDatasetCitation = await sut.getDatasetCitation( createdDatasetId, latestVersionId, + true, ); expect(typeof actualDatasetCitation).toBe('string'); diff --git a/test/unit/datasets/DatasetsRepository.test.ts b/test/unit/datasets/DatasetsRepository.test.ts index 3aceb645..684e21ad 100644 --- a/test/unit/datasets/DatasetsRepository.test.ts +++ b/test/unit/datasets/DatasetsRepository.test.ts @@ -232,12 +232,13 @@ describe('DatasetsRepository', () => { }); describe('getDatasetCitation', () => { + const testIncludeDeaccessioned = true; test('should return citation when response is successful', async () => { const axiosGetStub = sandbox.stub(axios, 'get').resolves(testCitationSuccessfulResponse); const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/datasets/${testDatasetModel.id}/versions/${testVersionId}/citation`; // API Key auth - let actual = await sut.getDatasetCitation(testDatasetModel.id, testVersionId); + let actual = await sut.getDatasetCitation(testDatasetModel.id, testVersionId, testIncludeDeaccessioned); assert.calledWithExactly( axiosGetStub, @@ -249,7 +250,7 @@ describe('DatasetsRepository', () => { // Session cookie auth ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.SESSION_COOKIE); - actual = await sut.getDatasetCitation(testDatasetModel.id, testVersionId); + actual = await sut.getDatasetCitation(testDatasetModel.id, testVersionId, testIncludeDeaccessioned); assert.calledWithExactly( axiosGetStub, @@ -263,7 +264,7 @@ describe('DatasetsRepository', () => { const axiosGetStub = sandbox.stub(axios, 'get').rejects(TestConstants.TEST_ERROR_RESPONSE); let error: ReadError = undefined; - await sut.getDatasetCitation(1, testVersionId).catch((e) => (error = e)); + await sut.getDatasetCitation(1, testVersionId,testIncludeDeaccessioned).catch((e) => (error = e)); assert.calledWithExactly( axiosGetStub, diff --git a/test/unit/datasets/GetDatasetCitation.test.ts b/test/unit/datasets/GetDatasetCitation.test.ts index 3c48f009..580d96b1 100644 --- a/test/unit/datasets/GetDatasetCitation.test.ts +++ b/test/unit/datasets/GetDatasetCitation.test.ts @@ -23,7 +23,7 @@ describe('execute', () => { const actual = await sut.execute(testId); assert.match(actual, testCitation); - assert.calledWithExactly(getDatasetCitationStub, testId, DatasetNotNumberedVersion.LATEST); + assert.calledWithExactly(getDatasetCitationStub, testId, DatasetNotNumberedVersion.LATEST, false); }); test('should return error result on repository error', async () => { From 22ff4c09ac18d69db7f3695d16b6782327574609 Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Sat, 16 Dec 2023 13:04:46 -0500 Subject: [PATCH 6/9] fix: remove unused interface --- src/datasets/infra/repositories/DatasetsRepository.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/datasets/infra/repositories/DatasetsRepository.ts b/src/datasets/infra/repositories/DatasetsRepository.ts index def499d4..01a01780 100644 --- a/src/datasets/infra/repositories/DatasetsRepository.ts +++ b/src/datasets/infra/repositories/DatasetsRepository.ts @@ -7,9 +7,7 @@ import { transformDatasetUserPermissionsResponseToDatasetUserPermissions } from import { DatasetLock } from '../../domain/models/DatasetLock'; import { transformDatasetLocksResponseToDatasetLocks } from './transformers/datasetLocksTransformers'; -export interface GetCitationQueryParams { - includeDeaccessioned: boolean; -} + export class DatasetsRepository extends ApiRepository implements IDatasetsRepository { private readonly datasetsResourceName: string = 'datasets'; From a50347bfbd2f87bb772fc23a4d54346ae9f1b641 Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Sun, 17 Dec 2023 20:45:01 -0500 Subject: [PATCH 7/9] refactor: added a helper method to wait for locks --- .../datasets/DatasetsRepository.test.ts | 10 ++++++---- test/testHelpers/datasets/datasetHelper.ts | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/test/integration/datasets/DatasetsRepository.test.ts b/test/integration/datasets/DatasetsRepository.test.ts index 5c31e86f..6ecca9ea 100644 --- a/test/integration/datasets/DatasetsRepository.test.ts +++ b/test/integration/datasets/DatasetsRepository.test.ts @@ -6,7 +6,8 @@ import { createDatasetViaApi, createPrivateUrlViaApi, publishDatasetViaApi, - deaccessionDatasetViaApi + deaccessionDatasetViaApi + // , waitForNoLocks } from '../../testHelpers/datasets/datasetHelper'; import { ReadError } from '../../../src/core/domain/repositories/ReadError'; import { DatasetNotNumberedVersion, DatasetLockType } from '../../../src/datasets'; @@ -110,13 +111,14 @@ describe('DatasetsRepository', () => { assert.fail('Error while publishing test Dataset'); }); + // await waitForNoLocks(createdDatasetId,2) let locks = await sut.getDatasetLocks(createdDatasetId); const maxTries = 10; let tries = 0; while (locks.length > 0 && tries < maxTries) { - await new Promise(resolve => setTimeout(resolve, 1000)); - locks = await sut.getDatasetLocks(createdDatasetId); - tries++ + await new Promise(resolve => setTimeout(resolve, 1000)); + locks = await sut.getDatasetLocks(createdDatasetId); + tries++ } if (tries >= maxTries && locks.length > 0) { assert.fail('Error while waiting for locks to be released'); diff --git a/test/testHelpers/datasets/datasetHelper.ts b/test/testHelpers/datasets/datasetHelper.ts index f620fccf..622ba345 100644 --- a/test/testHelpers/datasets/datasetHelper.ts +++ b/test/testHelpers/datasets/datasetHelper.ts @@ -227,3 +227,22 @@ export const createPrivateUrlViaApi = async (datasetId: number): Promise => { + return axios.get(`${TestConstants.TEST_API_URL}/datasets/:persistentId/locks?persistentId=${persistentId}`).then((response) => { + return response.data.data; + }) +} + +export const waitForNoLocks = async (persistentId: string, maxRetries = 20, delay = 1000): Promise => { + for (let retry = 0; retry < maxRetries; retry++) { + await getLocksViaApi(persistentId) + .then( + response => { + if (Object.keys(response).length === 1) { + return; + }}) + await new Promise(resolve => setTimeout(resolve, delay)); + } + throw new Error('Max retries reached.'); +}; + From b08f37fa780251c362ee55ad75494ed0ff26fbbe Mon Sep 17 00:00:00 2001 From: GPortas Date: Tue, 19 Dec 2023 12:37:12 +0000 Subject: [PATCH 8/9] Fixed: waitForNoLocks test helper --- .../datasets/DatasetsRepository.test.ts | 8 +-- test/testHelpers/datasets/datasetHelper.ts | 57 +++++++++++-------- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/test/integration/datasets/DatasetsRepository.test.ts b/test/integration/datasets/DatasetsRepository.test.ts index 6ecca9ea..aeb1571c 100644 --- a/test/integration/datasets/DatasetsRepository.test.ts +++ b/test/integration/datasets/DatasetsRepository.test.ts @@ -6,8 +6,8 @@ import { createDatasetViaApi, createPrivateUrlViaApi, publishDatasetViaApi, - deaccessionDatasetViaApi - // , waitForNoLocks + deaccessionDatasetViaApi, + waitForNoLocks } from '../../testHelpers/datasets/datasetHelper'; import { ReadError } from '../../../src/core/domain/repositories/ReadError'; import { DatasetNotNumberedVersion, DatasetLockType } from '../../../src/datasets'; @@ -110,8 +110,8 @@ describe('DatasetsRepository', () => { .catch(() => { assert.fail('Error while publishing test Dataset'); }); - - // await waitForNoLocks(createdDatasetId,2) + + await waitForNoLocks(createdDatasetId, 10) let locks = await sut.getDatasetLocks(createdDatasetId); const maxTries = 10; let tries = 0; diff --git a/test/testHelpers/datasets/datasetHelper.ts b/test/testHelpers/datasets/datasetHelper.ts index 622ba345..f155641b 100644 --- a/test/testHelpers/datasets/datasetHelper.ts +++ b/test/testHelpers/datasets/datasetHelper.ts @@ -212,14 +212,15 @@ export const publishDatasetViaApi = async (datasetId: number): Promise => { - const data = { deaccessionReason: 'Test reason.'}; - return await axios.post( - `${TestConstants.TEST_API_URL}/datasets/${datasetId}/versions/${versionId}/deaccession`, - JSON.stringify(data), - DATAVERSE_API_REQUEST_HEADERS, - ); -} +export const deaccessionDatasetViaApi = async (datasetId: number, versionId: string): Promise => { + const data = { deaccessionReason: 'Test reason.' }; + return await axios.post( + `${TestConstants.TEST_API_URL}/datasets/${datasetId}/versions/${versionId}/deaccession`, + JSON.stringify(data), + DATAVERSE_API_REQUEST_HEADERS, + ); +}; + export const createPrivateUrlViaApi = async (datasetId: number): Promise => { return await axios.post( `${TestConstants.TEST_API_URL}/datasets/${datasetId}/privateUrl`, @@ -227,22 +228,30 @@ export const createPrivateUrlViaApi = async (datasetId: number): Promise => { - return axios.get(`${TestConstants.TEST_API_URL}/datasets/:persistentId/locks?persistentId=${persistentId}`).then((response) => { - return response.data.data; - }) -} -export const waitForNoLocks = async (persistentId: string, maxRetries = 20, delay = 1000): Promise => { - for (let retry = 0; retry < maxRetries; retry++) { - await getLocksViaApi(persistentId) - .then( - response => { - if (Object.keys(response).length === 1) { - return; - }}) - await new Promise(resolve => setTimeout(resolve, delay)); +export const waitForNoLocks = async (datasetId: number, maxRetries = 20, delay = 1000): Promise => { + let hasLocks = true; + let retry = 0; + while (hasLocks && retry < maxRetries) { + await axios + .get(`${TestConstants.TEST_API_URL}/datasets/${datasetId}/locks`) + .then((response) => { + const nLocks = response.data.data.length; + if (nLocks == 0) { + hasLocks = false; + } + }) + .catch((error) => { + console.log( + `Error while waiting for no dataset locks: [${error.response.status}]${ + error.response.data ? ` ${error.response.data.message}` : '' + }`, + ); + }); + await new Promise((resolve) => setTimeout(resolve, delay)); + retry++; + } + if (hasLocks) { + throw new Error('Max retries reached.'); } - throw new Error('Max retries reached.'); }; - From 302daff589ab3962f93ced2dda6f39291a15ca84 Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Tue, 19 Dec 2023 10:13:30 -0500 Subject: [PATCH 9/9] fix: removed code that is now in datasetHelper.ts --- .../datasets/DatasetsRepository.test.ts | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/test/integration/datasets/DatasetsRepository.test.ts b/test/integration/datasets/DatasetsRepository.test.ts index aeb1571c..bc813161 100644 --- a/test/integration/datasets/DatasetsRepository.test.ts +++ b/test/integration/datasets/DatasetsRepository.test.ts @@ -112,17 +112,10 @@ describe('DatasetsRepository', () => { }); await waitForNoLocks(createdDatasetId, 10) - let locks = await sut.getDatasetLocks(createdDatasetId); - const maxTries = 10; - let tries = 0; - while (locks.length > 0 && tries < maxTries) { - await new Promise(resolve => setTimeout(resolve, 1000)); - locks = await sut.getDatasetLocks(createdDatasetId); - tries++ - } - if (tries >= maxTries && locks.length > 0) { - assert.fail('Error while waiting for locks to be released'); - } + .then() + .catch(() => { + assert.fail('Error while waiting for no locks'); + }); await deaccessionDatasetViaApi(createdDatasetId,'1.0') .then() .catch((error) => {