Skip to content

Commit

Permalink
Added getCollection unit tests, added docker-compose changes from Jim…
Browse files Browse the repository at this point in the history
…, removed placeholder content
  • Loading branch information
Matt Mangan committed Mar 14, 2024
1 parent 80973c8 commit c61e99d
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 144 deletions.
24 changes: 0 additions & 24 deletions src/collections/domain/models/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,5 @@ export interface Collection {
id: number
name: string
alias: string
// ownerId: number
affiliation: string
// description?: string
// creationDate: Date
// collectionType: CollectionType
// permissionRoot: boolean
// NOTE: Changed from Dataverse => Collection
// collectionContacts: CollectionContacts
}

export interface CollectionContacts {
displayOrder?: number
contactEmail: string
}

export enum CollectionType {
DEPARTMENT = 'DEPARTMENT',
JOURNALS = 'JOURNALS',
LABORATORY = 'LABORATORY',
ORGANIZATIONS_INSTITUTIONS = 'ORGANIZATIONS_INSTITUTIONS',
RESEARCHERS = 'RESEARCHERS',
RESEARCH_GROUP = 'RESEARCH_GROUP',
RESEARCH_PROJECTS = 'RESEARCH_PROJECTS',
TEACHING_COURSES = 'TEACHING_COURSES',
UNCATEGORIZED = 'UNCATEGORIZED'
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Collection } from '../models/Collection'
export interface ICollectionsRepository {
getCollection(collectionId: string): Promise<Collection>
getCollection(collectionId: number | string): Promise<Collection>
}
4 changes: 2 additions & 2 deletions src/collections/domain/useCases/GetCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export class GetCollection implements UseCase<Collection> {
* Returns a Collection instance, given the search parameters to identify it.
*
* https://guides.dataverse.org/en/6.0/api/native-api.html#view-a-dataverse-collection
* @param {string} [collectionId] - The collection identifier ...
* @param {number | string} [collectionId] - The collection identifier ...
* @returns {Promise<Collection>}
*/
async execute(collectionId: string): Promise<Collection> {
async execute(collectionId: number): Promise<Collection> {
return await this.collectionsRepository.getCollection(collectionId)
}
}
31 changes: 3 additions & 28 deletions src/collections/index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,10 @@
import { GetCollection } from './domain/useCases/GetCollection'
// TODO
// import { GetCollectionRoles } from "./domain/useCases/getCollectionRoles";
// import { GetCollectionIsRoot } from "./domain/useCases/getCollectionIsRoot";
// import { GetCollectionFacets } from "./domain/useCases/getCollectionFacets";
// import { GetCollectionMetadata } from "./domain/useCases/getCollectionMetadata";
// import { GetCollectionStorageSize } from "./domain/useCases/getCollectionStorageSize";
// import { GetCollectionMetadataBlocks } from "./domain/useCases/getCollectionMetadataBlocks";
// import { GetCollectionRolesAssignments } from "./domain/useCases/getCollectionRolesAssignments";

import { CollectionsRepository } from './infra/repositories/CollectionsRepository'

const collectionsRepository = new CollectionsRepository()

const getCollection = new GetCollection(collectionsRepository)

// const getCollectionRoles = new GetCollectionRoles(collectionsRepository)
// const getCollectionIsRoot = new GetCollectionIsRoot(collectionsRepository)
// const getCollectionFacets = new GetCollectionFacets(collectionsRepository)
// const getCollectionMetadata = new GetCollectionMetadata(collectionsRepository)
// const getCollectionStorageSize = new GetCollectionStorageSize(collectionsRepository)
// const getCollectionMetadataBlocks = new GetCollectionMetadataBlocks(collectionsRepository)
// const getCollectionRolesAssignments = new GetCollectionRolesAssignments(collectionsRepository)

export {
getCollection
// TODO
// getCollectionRoles()
// getCollectionIsRoot()
// getCollectionFacets()
// getCollectionMetadata()
// getCollectionStorageSize()
// getCollectionMetadataBlocks()
// getCollectionRolesAssignments()
}
export { Collection, CollectionType, CollectionContacts } from './domain/models/Collection'
export { getCollection }
export { Collection } from './domain/models/Collection'
5 changes: 3 additions & 2 deletions src/collections/infra/repositories/CollectionsRepository.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ApiRepository } from '../../../core/infra/repositories/ApiRepository'
// import { Collection } from '../../domain/models/Collection'
import { ICollectionsRepository } from '../../domain/repositories/ICollectionsRepository'
import { transformCollectionIdResponseToPayload } from './transformers/collectionTransformers'
export class CollectionsRepository extends ApiRepository implements ICollectionsRepository {
private readonly collectionsResourceName: string = 'dataverses'

public async getCollection(collectionId: string): Promise<any> {
// NOTE: Used 'disable` for the type below per gPortas.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public async getCollection(collectionId: any): Promise<any> {
return this.doGet(this.buildApiEndpoint(this.collectionsResourceName, collectionId), true)
.then((response) => transformCollectionIdResponseToPayload(response))
.catch((error) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,5 @@ export interface CollectionPayload {
id: number
alias: string
name: string
// ownerId: number
// creationDate: Date
affiliation: string
// description?: string
// permissionRoot: boolean
// dataverseType: CollectionType
// dataverseContacts?: CollectionContactsPayload
}

export interface CollectionContactsPayload {
displayOrder?: number
contactEmail: string
}

export enum CollectionType {
DEPARTMENT = 'DEPARTMENT',
JOURNALS = 'JOURNALS',
LABORATORY = 'LABORATORY',
ORGANIZATIONS_INSTITUTIONS = 'ORGANIZATIONS_INSTITUTIONS',
RESEARCHERS = 'RESEARCHERS',
RESEARCH_GROUP = 'RESEARCH_GROUP',
RESEARCH_PROJECTS = 'RESEARCH_PROJECTS',
TEACHING_COURSES = 'TEACHING_COURSES',
UNCATEGORIZED = 'UNCATEGORIZED'
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@ const transformPayloadToCollection = (collectionPayload: CollectionPayload): Col
alias: collectionPayload.alias,
name: collectionPayload.name,
affiliation: collectionPayload.affiliation
// collectionContacts: transformPayloadToCollectionContacts(collectionPayload.dataverseContacts),
// permissionRoot: collectionPayload.permissionRoot,
// description: collectionPayload.description,
// collectionType: collectionPayload.dataverseType,
// ownerId: collectionPayload.ownerId,
// creationDate: collectionPayload.creationDate
}
return collectionModel
}

// const transformPayloadToCollectionContacts = (collectionContactsPayload: CollectionContactsPayload): CollectionContacts => {
// const collectionContacts: CollectionContacts = {
// displayOrder: collectionContactsPayload.displayOrder,
// contactEmail: collectionContactsPayload.contactEmail
// }
// return collectionContacts
// }

//
2 changes: 1 addition & 1 deletion src/core/infra/repositories/ApiRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export abstract class ApiRepository {
switch (ApiConfig.dataverseApiAuthMechanism) {
case DataverseApiAuthMechanism.SESSION_COOKIE:
/*
We set { withCredentials: true } to send the JSESSIONID cookie in the requests for API authentication.
We set { withCredentials: true } to send the JSESSIONID cookie in the requests for API authentication.
This is required, along with the session auth feature flag enabled in the backend, to be able to authenticate using the JSESSIONID cookie.
Auth mechanisms like this are configurable to set the one that fits the particular use case of js-dataverse. (For the SPA MVP, it is the session cookie API auth).
*/
Expand Down
36 changes: 9 additions & 27 deletions test/integration/collections/CollectionsRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { TestConstants } from '../../testHelpers/TestConstants'
import { ReadError } from '../../../src'
import { ApiConfig } from '../../../src'
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
// import { Collection } from '../../../src/collections'

describe('CollectionsRepository', () => {
const fooBarBaz: CollectionsRepository = new CollectionsRepository()
const nonExistentCollectionId = 'returnNullResuts'
const testGetCollection: CollectionsRepository = new CollectionsRepository()
const nonExistentCollectionAlias = 'returnNullResuts'

// const collectionId =
beforeEach(async () => {
ApiConfig.init(
TestConstants.TEST_API_URL,
Expand All @@ -27,36 +25,20 @@ describe('CollectionsRepository', () => {
})

describe('getCollection', () => {
describe('by string id', () => {
test('should return collection when it exists filtering by id (alias)', async () => {
const actual = await fooBarBaz.getCollection(TestConstants.TEST_CREATED_COLLECTION_1_ID_STR)
describe('by string alias', () => {
test('should return collection when it exists filtering by id AS (alias)', async () => {
const actual = await testGetCollection.getCollection(
TestConstants.TEST_CREATED_COLLECTION_1_ID_STR
)
expect(actual.alias).toBe(TestConstants.TEST_CREATED_COLLECTION_1_ID_STR)
})

// Case: Unauthenticated
// test('should return dataset when it is deaccessioned, includeDeaccessioned param is set, and user is unauthenticated', async () => {
// ApiConfig.init(TestConstants.TEST_API_URL, DataverseApiAuthMechanism.API_KEY, undefined)
// const actual = await fooBarBaz.getCollection(TestConstants.TEST_CREATED_DATASET_2_ID)
// expect(actual.id).toBe(TestConstants.TEST_CREATED_DATASET_2_ID)
// })

// Case: Unpublished
// test('should return error when collection is XXXX', async () => {
// const expectedError = new ReadError(
// `[404] Dataset version ${latestVersionId} of dataset ${TestConstants.TEST_CREATED_DATASET_2_ID} not found`
// )
// await expect(
// fooBarBaz.getCollection(TestConstants.TEST_CREATED_DATASET_2_ID, latestVersionId, false)
// ).rejects.toThrow(expectedError)
// })

// Case: Does not exist
test('should return error when collection does not exist', async () => {
const expectedError = new ReadError(
`[404] Can't find dataverse with identifier='${nonExistentCollectionId}'`
`[404] Can't find dataverse with identifier='${nonExistentCollectionAlias}'`
)

await expect(fooBarBaz.getCollection(nonExistentCollectionId)).rejects.toThrow(
await expect(testGetCollection.getCollection(nonExistentCollectionAlias)).rejects.toThrow(
expectedError
)
})
Expand Down
6 changes: 2 additions & 4 deletions test/integration/environment/.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
POSTGRES_VERSION=13
DATAVERSE_DB_USER=dataverse
SOLR_VERSION=9.3.0
DATAVERSE_IMAGE_REGISTRY=ghcr.io
DATAVERSE_IMAGE_TAG=10286-add-owner-info
# DATAVERSE_IMAGE_REGISTRY=docker.io
# DATAVERSE_IMAGE_TAG=unstable
DATAVERSE_IMAGE_REGISTRY=docker.io
DATAVERSE_IMAGE_TAG=unstable
DATAVERSE_BOOTSTRAP_TIMEOUT=5m
3 changes: 1 addition & 2 deletions test/integration/environment/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ services:
-Ddataverse.pid.fake.label=FakeDOIProvider
-Ddataverse.pid.fake.authority=10.5072
-Ddataverse.pid.fake.shoulder=FK2/

ports:
- '8080:8080'
networks:
Expand Down Expand Up @@ -106,4 +105,4 @@ services:

networks:
dataverse:
driver: bridge
driver: bridge
4 changes: 3 additions & 1 deletion test/testHelpers/TestConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export class TestConstants {
static readonly TEST_CREATED_DATASET_1_ID = 2
static readonly TEST_CREATED_DATASET_2_ID = 3
static readonly TEST_CREATED_DATASET_3_ID = 4
static readonly TEST_DUMMY_COLLECTION_ID = 10001
static readonly TEST_DUMMY_COLLECTION_ID_STR = 'dummyCollectionId'
static readonly TEST_CREATED_COLLECTION_1_ID_STR = 'firstCollection' // Called 'alias' in the dv object. that's what is *actually* being searched for
static readonly TEST_CREATED_COLLECTION_1_ID = 11111
static readonly TEST_CREATED_COLLECTION_1_ID_STR = 'firstCollection' // Called 'alias' in the dv object.
}
28 changes: 15 additions & 13 deletions test/testHelpers/collections/collectionHelper.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// import { Collection } from "../../../src/collections";
// import axios, { AxiosResponse } from "axios";
// import { TestConstants } from "../TestConstants";
// import { CollectionPayload } from "../../../src/collections/infra/repositories/transformers/CollectionPayload";
// import TurndownService from 'turndown'
import { Collection } from '../../../src/collections'

// const turndownService = new TurndownService()
// const COLLECTION_ID_STR = 'x'
// const COLLECTION_NAME_STR = ''
// const COLLECTION_ALIAS_STR = 'firstCollection'
// const COLLECTION_AFFILIATION_STR = ''
// const COLLECTION_API_REQUEST_HEADERS = {
// headers: { 'Content-Type': 'application/json', 'X-Dataverse-Key': process.env.TEST_API_KEY }
// }
const COLLECTION_ID = 11111
const COLLECTION_NAME_STR = 'Laboratory Research'
const COLLECTION_ALIAS_STR = 'secondCollection'
const COLLECTION_AFFILIATION_STR = 'Laboratory Research Corporation'

export const createCollectionModel = (): Collection => {
const collectionModel: Collection = {
id: COLLECTION_ID,
name: COLLECTION_NAME_STR,
alias: COLLECTION_ALIAS_STR,
affiliation: COLLECTION_AFFILIATION_STR
}
return collectionModel
}
1 change: 1 addition & 0 deletions test/testHelpers/collections/test-collection-1.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"id": 11111,
"name": "Scientific Research",
"alias": "firstCollection",
"dataverseContacts": [
Expand Down
25 changes: 25 additions & 0 deletions test/unit/collections/GetCollection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { GetCollection } from '../../../src/collections/domain/useCases/GetCollection'
import { ICollectionsRepository } from '../../../src/collections/domain/repositories/ICollectionsRepository'
import { ReadError } from '../../../src'
import { createCollectionModel } from '../../testHelpers/collections/collectionHelper'

describe('execute', () => {
test('should return collection on repository success', async () => {
const testCollection = createCollectionModel()
const collectionRepositoryStub: ICollectionsRepository = {} as ICollectionsRepository
collectionRepositoryStub.getCollection = jest.fn().mockResolvedValue(testCollection)
const testGetCollection = new GetCollection(collectionRepositoryStub)

const actual = await testGetCollection.execute(1)

expect(actual).toEqual(testCollection)
})

test('should return error result on repository error', async () => {
const collectionRepositoryStub: ICollectionsRepository = {} as ICollectionsRepository
collectionRepositoryStub.getCollection = jest.fn().mockRejectedValue(new ReadError())
const testGetCollection = new GetCollection(collectionRepositoryStub)

await expect(testGetCollection.execute(1)).rejects.toThrow(ReadError)
})
})

0 comments on commit c61e99d

Please sign in to comment.