Skip to content

Commit 69f4005

Browse files
authored
Merge pull request #172 from IQSS/168-get-all-metadatablocks
Adds new use case GetAllMetadataBlocks
2 parents ceba6f3 + 23fcdfd commit 69f4005

File tree

8 files changed

+147
-26
lines changed

8 files changed

+147
-26
lines changed

docs/useCases.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ The different use cases currently available in the package are classified below,
4343
- [File Uploading Use Cases](#file-uploading-use-cases)
4444
- [Metadata Blocks](#metadata-blocks)
4545
- [Metadata Blocks read use cases](#metadata-blocks-read-use-cases)
46+
- [Get All Metadata Blocks](#get-all-metadata-blocks)
4647
- [Get Metadata Block By Name](#get-metadata-block-by-name)
4748
- [Get Collection Metadata Blocks](#get-collection-metadata-blocks)
4849
- [Users](#Users)
@@ -965,6 +966,26 @@ The following error might arise from the `AddUploadedFileToDataset` use case:
965966

966967
### Metadata Blocks read use cases
967968

969+
#### Get All Metadata Blocks
970+
971+
Returns a [MetadataBlock](../src/metadataBlocks/domain/models/MetadataBlock.ts) array containing the metadata blocks defined in the installation.
972+
973+
##### Example call:
974+
975+
```typescript
976+
import { getAllMetadataBlocks } from '@iqss/dataverse-client-javascript'
977+
978+
/* ... */
979+
980+
getAllMetadataBlocks.execute().then((metadataBlocks: MetadataBlock[]) => {
981+
/* ... */
982+
})
983+
984+
/* ... */
985+
```
986+
987+
_See [use case](../src/metadataBlocks/domain/useCases/GetAllMetadataBlocks.ts) implementation_.
988+
968989
#### Get Metadata Block By Name
969990

970991
Returns a [MetadataBlock](../src/metadataBlocks/domain/models/MetadataBlock.ts) instance, given its name.

src/metadataBlocks/domain/repositories/IMetadataBlocksRepository.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ export interface IMetadataBlocksRepository {
77
collectionIdOrAlias: number | string,
88
onlyDisplayedOnCreate: boolean
99
): Promise<MetadataBlock[]>
10+
11+
getAllMetadataBlocks(): Promise<MetadataBlock[]>
1012
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { UseCase } from '../../../core/domain/useCases/UseCase'
2+
import { MetadataBlock } from '../..'
3+
import { IMetadataBlocksRepository } from '../repositories/IMetadataBlocksRepository'
4+
5+
export class GetAllMetadataBlocks implements UseCase<MetadataBlock[]> {
6+
private metadataBlocksRepository: IMetadataBlocksRepository
7+
8+
constructor(metadataBlocksRepository: IMetadataBlocksRepository) {
9+
this.metadataBlocksRepository = metadataBlocksRepository
10+
}
11+
12+
/**
13+
* Returns a MetadataBlock array containing the metadata blocks defined in the installation.
14+
*
15+
* @returns {Promise<MetadataBlock[]>}
16+
*/
17+
async execute(): Promise<MetadataBlock[]> {
18+
return await this.metadataBlocksRepository.getAllMetadataBlocks()
19+
}
20+
}

src/metadataBlocks/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { GetMetadataBlockByName } from './domain/useCases/GetMetadataBlockByName'
22
import { MetadataBlocksRepository } from './infra/repositories/MetadataBlocksRepository'
33
import { GetCollectionMetadataBlocks } from './domain/useCases/GetCollectionMetadataBlocks'
4+
import { GetAllMetadataBlocks } from './domain/useCases/GetAllMetadataBlocks'
45

56
const metadataBlocksRepository = new MetadataBlocksRepository()
67

78
const getMetadataBlockByName = new GetMetadataBlockByName(metadataBlocksRepository)
89
const getCollectionMetadataBlocks = new GetCollectionMetadataBlocks(metadataBlocksRepository)
10+
const getAllMetadataBlocks = new GetAllMetadataBlocks(metadataBlocksRepository)
911

10-
export { getMetadataBlockByName, getCollectionMetadataBlocks }
12+
export { getMetadataBlockByName, getCollectionMetadataBlocks, getAllMetadataBlocks }
1113
export {
1214
MetadataBlock,
1315
MetadataFieldInfo,

src/metadataBlocks/infra/repositories/MetadataBlocksRepository.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,14 @@ export class MetadataBlocksRepository extends ApiRepository implements IMetadata
2828
throw error
2929
})
3030
}
31+
32+
public async getAllMetadataBlocks(): Promise<MetadataBlock[]> {
33+
return this.doGet('/metadatablocks', false, {
34+
returnDatasetFieldTypes: true
35+
})
36+
.then((response) => transformMetadataBlocksResponseToMetadataBlocks(response))
37+
.catch((error) => {
38+
throw error
39+
})
40+
}
3141
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { ApiConfig, getAllMetadataBlocks, MetadataBlock } from '../../../src'
2+
import { TestConstants } from '../../testHelpers/TestConstants'
3+
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
4+
5+
describe('execute', () => {
6+
beforeEach(async () => {
7+
ApiConfig.init(
8+
TestConstants.TEST_API_URL,
9+
DataverseApiAuthMechanism.API_KEY,
10+
process.env.TEST_API_KEY
11+
)
12+
})
13+
14+
test('should successfully return metadatablocks', async () => {
15+
let metadataBlocks: MetadataBlock[] = null
16+
try {
17+
metadataBlocks = await getAllMetadataBlocks.execute()
18+
} catch (error) {
19+
throw new Error('Should not raise an error')
20+
} finally {
21+
expect(metadataBlocks).not.toBeNull()
22+
expect(metadataBlocks.length).toBe(6)
23+
expect(metadataBlocks[0].metadataFields.title.name).toBe('title')
24+
}
25+
})
26+
})

test/integration/metadataBlocks/MetadataBlocksRepository.test.ts

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,51 @@ import {
66
} from '../../../src/core/infra/repositories/ApiConfig'
77
import { TestConstants } from '../../testHelpers/TestConstants'
88

9-
describe('getMetadataBlockByName', () => {
10-
const sut: MetadataBlocksRepository = new MetadataBlocksRepository()
9+
describe('MetadataBlocksRepository', () => {
1110
const citationMetadataBlockName = 'citation'
11+
const sut: MetadataBlocksRepository = new MetadataBlocksRepository()
1212

13-
ApiConfig.init(
14-
TestConstants.TEST_API_URL,
15-
DataverseApiAuthMechanism.API_KEY,
16-
process.env.TEST_API_KEY
17-
)
18-
19-
test('should return error when metadata block does not exist', async () => {
20-
const nonExistentMetadataBlockName = 'nonExistentMetadataBlock'
21-
const errorExpected: ReadError = new ReadError(
22-
`[404] Can't find metadata block '${nonExistentMetadataBlockName}'`
23-
)
24-
25-
await expect(sut.getMetadataBlockByName(nonExistentMetadataBlockName)).rejects.toThrow(
26-
errorExpected
13+
beforeAll(async () => {
14+
ApiConfig.init(
15+
TestConstants.TEST_API_URL,
16+
DataverseApiAuthMechanism.API_KEY,
17+
process.env.TEST_API_KEY
2718
)
2819
})
2920

30-
test('should return metadata block when it exists', async () => {
31-
const actual = await sut.getMetadataBlockByName(citationMetadataBlockName)
21+
describe('getMetadataBlockByName', () => {
22+
test('should return error when metadata block does not exist', async () => {
23+
const nonExistentMetadataBlockName = 'nonExistentMetadataBlock'
24+
const errorExpected: ReadError = new ReadError(
25+
`[404] Can't find metadata block '${nonExistentMetadataBlockName}'`
26+
)
3227

33-
expect(actual.name).toBe(citationMetadataBlockName)
34-
})
28+
await expect(sut.getMetadataBlockByName(nonExistentMetadataBlockName)).rejects.toThrow(
29+
errorExpected
30+
)
31+
})
32+
33+
test('should return metadata block when it exists', async () => {
34+
const actual = await sut.getMetadataBlockByName(citationMetadataBlockName)
35+
36+
expect(actual.name).toBe(citationMetadataBlockName)
37+
})
3538

36-
test('should return collection metadata blocks', async () => {
37-
const actual = await sut.getCollectionMetadataBlocks('root', true)
39+
test('should return collection metadata blocks', async () => {
40+
const actual = await sut.getCollectionMetadataBlocks('root', true)
41+
42+
expect(actual.length).toBe(1)
43+
expect(actual[0].name).toBe(citationMetadataBlockName)
44+
expect(actual[0].metadataFields.title.name).toBe('title')
45+
})
46+
})
3847

39-
expect(actual.length).toBe(1)
40-
expect(actual[0].name).toBe(citationMetadataBlockName)
41-
expect(actual[0].metadataFields.title.name).toBe('title')
48+
describe('getAllMetadataBlocks', () => {
49+
test('should return all metadata blocks', async () => {
50+
const actual = await sut.getAllMetadataBlocks()
51+
expect(actual.length).toBe(6)
52+
expect(actual[0].name).toBe(citationMetadataBlockName)
53+
expect(actual[0].metadataFields.title.name).toBe('title')
54+
})
4255
})
4356
})
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { ReadError } from '../../../src'
2+
import { createMetadataBlockModel } from '../../testHelpers/metadataBlocks/metadataBlockHelper'
3+
import { IMetadataBlocksRepository } from '../../../src/metadataBlocks/domain/repositories/IMetadataBlocksRepository'
4+
import { GetAllMetadataBlocks } from '../../../src/metadataBlocks/domain/useCases/GetAllMetadataBlocks'
5+
6+
describe('execute', () => {
7+
test('should return metadata blocks on repository success', async () => {
8+
const testMetadataBlocks = [createMetadataBlockModel()]
9+
const metadataBlocksRepositoryStub: IMetadataBlocksRepository = {} as IMetadataBlocksRepository
10+
metadataBlocksRepositoryStub.getAllMetadataBlocks = jest
11+
.fn()
12+
.mockResolvedValue(testMetadataBlocks)
13+
const testGetAllMetadataBlocks = new GetAllMetadataBlocks(metadataBlocksRepositoryStub)
14+
15+
const actual = await testGetAllMetadataBlocks.execute()
16+
17+
expect(actual).toEqual(testMetadataBlocks)
18+
})
19+
20+
test('should return error result on repository error', async () => {
21+
const metadataBlocksRepositoryStub: IMetadataBlocksRepository = {} as IMetadataBlocksRepository
22+
metadataBlocksRepositoryStub.getAllMetadataBlocks = jest.fn().mockRejectedValue(new ReadError())
23+
const testGetCollectionMetadataBlocks = new GetAllMetadataBlocks(metadataBlocksRepositoryStub)
24+
25+
await expect(testGetCollectionMetadataBlocks.execute()).rejects.toThrow(ReadError)
26+
})
27+
})

0 commit comments

Comments
 (0)