Skip to content

Commit

Permalink
Merge branch '90-file-counts-criteria' into 94-total-file-download-si…
Browse files Browse the repository at this point in the history
…ze-with-criteria
  • Loading branch information
GPortas authored Oct 17, 2023
2 parents 3eb2a0c + 14ac106 commit 418bf5e
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 43 deletions.
2 changes: 2 additions & 0 deletions src/info/domain/repositories/IDataverseInfoRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ import { DataverseVersion } from '../models/DataverseVersion';

export interface IDataverseInfoRepository {
getDataverseVersion(): Promise<DataverseVersion>;
getZipDownloadLimit(): Promise<number>;
getMaxEmbargoDurationInMonths(): Promise<number>;
}
14 changes: 14 additions & 0 deletions src/info/domain/useCases/GetMaxEmbargoDurationInMonths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { UseCase } from '../../../core/domain/useCases/UseCase';
import { IDataverseInfoRepository } from '../repositories/IDataverseInfoRepository';

export class GetMaxEmbargoDurationInMonths implements UseCase<number> {
private dataverseInfoRepository: IDataverseInfoRepository;

constructor(dataverseInfoRepository: IDataverseInfoRepository) {
this.dataverseInfoRepository = dataverseInfoRepository;
}

async execute(): Promise<number> {
return await this.dataverseInfoRepository.getMaxEmbargoDurationInMonths();
}
}
14 changes: 14 additions & 0 deletions src/info/domain/useCases/GetZipDownloadLimit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { UseCase } from '../../../core/domain/useCases/UseCase';
import { IDataverseInfoRepository } from '../repositories/IDataverseInfoRepository';

export class GetZipDownloadLimit implements UseCase<number> {
private dataverseInfoRepository: IDataverseInfoRepository;

constructor(dataverseInfoRepository: IDataverseInfoRepository) {
this.dataverseInfoRepository = dataverseInfoRepository;
}

async execute(): Promise<number> {
return await this.dataverseInfoRepository.getZipDownloadLimit();
}
}
10 changes: 8 additions & 2 deletions src/info/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { DataverseInfoRepository } from './infra/repositories/DataverseInfoRepository';
import { GetDataverseVersion } from './domain/useCases/GetDataverseVersion';
import { GetZipDownloadLimit } from './domain/useCases/GetZipDownloadLimit';
import { GetMaxEmbargoDurationInMonths } from './domain/useCases/GetMaxEmbargoDurationInMonths';

const getDataverseVersion = new GetDataverseVersion(new DataverseInfoRepository());
const dataverseInfoRepository = new DataverseInfoRepository();

export { getDataverseVersion };
const getDataverseVersion = new GetDataverseVersion(dataverseInfoRepository);
const getZipDownloadLimit = new GetZipDownloadLimit(dataverseInfoRepository);
const getMaxEmbargoDurationInMonths = new GetMaxEmbargoDurationInMonths(dataverseInfoRepository);

export { getDataverseVersion, getZipDownloadLimit, getMaxEmbargoDurationInMonths };
20 changes: 19 additions & 1 deletion src/info/infra/repositories/DataverseInfoRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,32 @@ import { DataverseVersion } from '../../domain/models/DataverseVersion';
import { AxiosResponse } from 'axios';

export class DataverseInfoRepository extends ApiRepository implements IDataverseInfoRepository {
private readonly infoResourceName: string = 'info';

public async getDataverseVersion(): Promise<DataverseVersion> {
return this.doGet('/info/version')
return this.doGet(this.buildApiEndpoint(this.infoResourceName, `version`))
.then((response) => this.getVersionFromResponse(response))
.catch((error) => {
throw error;
});
}

public async getZipDownloadLimit(): Promise<number> {
return this.doGet(this.buildApiEndpoint(this.infoResourceName, `zipDownloadLimit`))
.then((response) => response.data.data as number)
.catch((error) => {
throw error;
});
}

public async getMaxEmbargoDurationInMonths(): Promise<number> {
return this.doGet(this.buildApiEndpoint(this.infoResourceName, `settings/:MaxEmbargoDurationInMonths`))
.then((response) => response.data.data.message as number)
.catch((error) => {
throw error;
});
}

private getVersionFromResponse(response: AxiosResponse): DataverseVersion {
const responseData = response.data.data;
return {
Expand Down
4 changes: 2 additions & 2 deletions test/integration/environment/.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
POSTGRES_VERSION=13
DATAVERSE_DB_USER=dataverse
SOLR_VERSION=9.3.0
DATAVERSE_IMAGE_REGISTRY=ghcr.io
DATAVERSE_IMAGE_TAG=9995-file-total-download-size-criteria
DATAVERSE_IMAGE_REGISTRY=docker.io
DATAVERSE_IMAGE_TAG=unstable
DATAVERSE_BOOTSTRAP_TIMEOUT=5m
47 changes: 42 additions & 5 deletions test/integration/info/DataverseInfoRepository.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,51 @@
import { DataverseInfoRepository } from '../../../src/info/infra/repositories/DataverseInfoRepository';
import { ApiConfig, DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig';
import { TestConstants } from '../../testHelpers/TestConstants';
import { setMaxEmbargoDurationInMonthsViaApi } from '../../testHelpers/info/infoHelper';
import { ReadError } from '../../../src/core/domain/repositories/ReadError';
import { assert } from 'sinon';
import { fail } from 'assert';

describe('getDataverseVersion', () => {
describe('DataverseInfoRepository', () => {
const sut: DataverseInfoRepository = new DataverseInfoRepository();

ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.API_KEY);
beforeAll(async () => {
ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.API_KEY, process.env.TEST_API_KEY);
});

describe('getDataverseVersion', () => {
test('should return Dataverse version', async () => {
const actual = await sut.getDataverseVersion();
expect(typeof actual.number).toBe('string');
});
});

describe('getZipDownloadLimit', () => {
test('should return zip download limit', async () => {
const actual = await sut.getZipDownloadLimit();
expect(typeof actual).toBe('number');
});
});

describe('getMaxEmbargoDurationInMonths', () => {
test('should return error when the setting does not exist', async () => {
let error: ReadError = undefined;
await sut.getMaxEmbargoDurationInMonths().catch((e) => (error = e));
assert.match(
error.message,
'There was an error when reading the resource. Reason was: [404] Setting :MaxEmbargoDurationInMonths not found',
);
});

test('should return Dataverse version', async () => {
const actual = await sut.getDataverseVersion();
expect(typeof actual.number).toBe('string');
test('should return duration when the setting exists', async () => {
const testMaxEmbargoDurationInMonths = 12;
await setMaxEmbargoDurationInMonthsViaApi(testMaxEmbargoDurationInMonths)
.then()
.catch(() => {
fail('Test getMaxEmbargoDurationInMonths: Error while setting :MaxEmbargoDurationInMonths');
});
const actual = await sut.getMaxEmbargoDurationInMonths();
assert.match(actual, testMaxEmbargoDurationInMonths);
});
});
});
14 changes: 14 additions & 0 deletions test/testHelpers/info/infoHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import axios, { AxiosResponse } from 'axios';
import { TestConstants } from '../TestConstants';

export const setMaxEmbargoDurationInMonthsViaApi = async (
maxEmbargoDurationInMonths: number,
): Promise<AxiosResponse> => {
return await axios.put(
`${TestConstants.TEST_API_URL}/admin/settings/:MaxEmbargoDurationInMonths`,
maxEmbargoDurationInMonths.toString(),
{
headers: { 'Content-Type': 'text/plain' },
},
);
};
144 changes: 111 additions & 33 deletions test/unit/info/DataverseInfoRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,130 @@ import { ReadError } from '../../../src/core/domain/repositories/ReadError';
import { ApiConfig, DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig';
import { TestConstants } from '../../testHelpers/TestConstants';

describe('getDataverseVersion', () => {
describe('DataverseInfoRepository', () => {
const sandbox: SinonSandbox = createSandbox();
const sut: DataverseInfoRepository = new DataverseInfoRepository();

ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.API_KEY, TestConstants.TEST_DUMMY_API_KEY);
beforeEach(() => {
ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.API_KEY, TestConstants.TEST_DUMMY_API_KEY);
});

afterEach(() => {
sandbox.restore();
});

test('should return Dataverse version on successful response', async () => {
const testVersionNumber = '5.13';
const testVersionBuild = 'testBuild';
const testSuccessfulResponse = {
data: {
status: 'OK',
describe('getDataverseVersion', () => {
test('should return Dataverse version on successful response', async () => {
const testVersionNumber = '5.13';
const testVersionBuild = 'testBuild';
const testSuccessfulResponse = {
data: {
status: 'OK',
data: {
version: testVersionNumber,
build: testVersionBuild,
},
},
};
const axiosGetStub = sandbox.stub(axios, 'get').resolves(testSuccessfulResponse);

const actual = await sut.getDataverseVersion();

assert.calledWithExactly(
axiosGetStub,
`${TestConstants.TEST_API_URL}/info/version`,
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG,
);
assert.match(actual.number, testVersionNumber);
assert.match(actual.build, testVersionBuild);
});

test('should return error result on error response', async () => {
const axiosGetStub = sandbox.stub(axios, 'get').rejects(TestConstants.TEST_ERROR_RESPONSE);

let error: ReadError = undefined;
await sut.getDataverseVersion().catch((e) => (error = e));

assert.calledWithExactly(
axiosGetStub,
`${TestConstants.TEST_API_URL}/info/version`,
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG,
);
expect(error).to.be.instanceOf(Error);
});
});

describe('getZipDownloadLimit', () => {
test('should return zip download limit on successful response', async () => {
const testZipDownloadLimit = 100;
const testSuccessfulResponse = {
data: {
version: testVersionNumber,
build: testVersionBuild,
status: 'OK',
data: testZipDownloadLimit.toString(),
},
},
};
const axiosGetStub = sandbox.stub(axios, 'get').resolves(testSuccessfulResponse);

const actual = await sut.getDataverseVersion();

assert.calledWithExactly(
axiosGetStub,
`${TestConstants.TEST_API_URL}/info/version`,
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG,
);
assert.match(actual.number, testVersionNumber);
assert.match(actual.build, testVersionBuild);
};
const axiosGetStub = sandbox.stub(axios, 'get').resolves(testSuccessfulResponse);

const actual = await sut.getZipDownloadLimit();

assert.calledWithExactly(
axiosGetStub,
`${TestConstants.TEST_API_URL}/info/zipDownloadLimit`,
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG,
);
assert.match(actual, testZipDownloadLimit);
});

test('should return error result on error response', async () => {
const axiosGetStub = sandbox.stub(axios, 'get').rejects(TestConstants.TEST_ERROR_RESPONSE);

let error: ReadError = undefined;
await sut.getZipDownloadLimit().catch((e) => (error = e));

assert.calledWithExactly(
axiosGetStub,
`${TestConstants.TEST_API_URL}/info/zipDownloadLimit`,
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG,
);
expect(error).to.be.instanceOf(Error);
});
});

test('should return error result on error response', async () => {
const axiosGetStub = sandbox.stub(axios, 'get').rejects(TestConstants.TEST_ERROR_RESPONSE);
describe('getMaxEmbargoDurationInMonths', () => {
test('should return duration on successful response', async () => {
const testDuration = 12;
const testSuccessfulResponse = {
data: {
status: 'OK',
data: {
message: testDuration.toString(),
},
},
};
const axiosGetStub = sandbox.stub(axios, 'get').resolves(testSuccessfulResponse);

const actual = await sut.getMaxEmbargoDurationInMonths();

assert.calledWithExactly(
axiosGetStub,
`${TestConstants.TEST_API_URL}/info/settings/:MaxEmbargoDurationInMonths`,
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG,
);
assert.match(actual, testDuration);
});

test('should return error result on error response', async () => {
const axiosGetStub = sandbox.stub(axios, 'get').rejects(TestConstants.TEST_ERROR_RESPONSE);

let error: ReadError = undefined;
await sut.getDataverseVersion().catch((e) => (error = e));
let error: ReadError = undefined;
await sut.getMaxEmbargoDurationInMonths().catch((e) => (error = e));

assert.calledWithExactly(
axiosGetStub,
`${TestConstants.TEST_API_URL}/info/version`,
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG,
);
expect(error).to.be.instanceOf(Error);
assert.calledWithExactly(
axiosGetStub,
`${TestConstants.TEST_API_URL}/info/settings/:MaxEmbargoDurationInMonths`,
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG,
);
expect(error).to.be.instanceOf(Error);
});
});
});
35 changes: 35 additions & 0 deletions test/unit/info/GetMaxEmbargoDurationInMonths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { GetMaxEmbargoDurationInMonths } from '../../../src/info/domain/useCases/GetMaxEmbargoDurationInMonths';
import { IDataverseInfoRepository } from '../../../src/info/domain/repositories/IDataverseInfoRepository';
import { ReadError } from '../../../src/core/domain/repositories/ReadError';
import { assert, createSandbox, SinonSandbox } from 'sinon';

describe('execute', () => {
const sandbox: SinonSandbox = createSandbox();

afterEach(() => {
sandbox.restore();
});

test('should return duration on repository success', async () => {
const testDuration = 12;
const dataverseInfoRepositoryStub = <IDataverseInfoRepository>{};
dataverseInfoRepositoryStub.getMaxEmbargoDurationInMonths = sandbox.stub().returns(testDuration);
const sut = new GetMaxEmbargoDurationInMonths(dataverseInfoRepositoryStub);

const actual = await sut.execute();

assert.match(actual, testDuration);
});

test('should return error result on repository error', async () => {
const dataverseInfoRepositoryStub = <IDataverseInfoRepository>{};
const testReadError = new ReadError();
dataverseInfoRepositoryStub.getMaxEmbargoDurationInMonths = sandbox.stub().throwsException(testReadError);
const sut = new GetMaxEmbargoDurationInMonths(dataverseInfoRepositoryStub);

let actualError: ReadError = undefined;
await sut.execute().catch((e) => (actualError = e));

assert.match(actualError, testReadError);
});
});
Loading

0 comments on commit 418bf5e

Please sign in to comment.