-
Notifications
You must be signed in to change notification settings - Fork 8
Use Case for Getting Available Licenses Terms #358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export interface License { | ||
id: number | ||
name: string | ||
shortDescription?: string | ||
uri: string | ||
iconUri?: string | ||
active: boolean | ||
isDefault: boolean | ||
sortOrder: number | ||
rightsIdentifier?: string | ||
rightsIdentifierScheme?: string | ||
schemeUri?: string | ||
languageCode?: string | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { License } from '../models/License' | ||
|
||
export interface ILicensesRepository { | ||
getAvailableStandardLicenses(): Promise<License[]> | ||
} |
14 changes: 14 additions & 0 deletions
14
src/licenses/domain/repositories/transformers/LicensePayload.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export interface LicensePayload { | ||
id: number | ||
name: string | ||
shortDescription?: string | ||
uri: string | ||
iconUrl?: string | ||
active: boolean | ||
isDefault: boolean | ||
sortOrder: number | ||
rightsIdentifier?: string | ||
rightsIdentifierScheme?: string | ||
schemeUri?: string | ||
languageCode?: string | ||
} |
22 changes: 22 additions & 0 deletions
22
src/licenses/domain/repositories/transformers/licenseTransformers.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { AxiosResponse } from 'axios' | ||
import { License } from '../../models/License' | ||
import { LicensePayload } from './LicensePayload' | ||
|
||
export const transformPayloadToLicense = (response: AxiosResponse): License[] => { | ||
const payload = response.data.data as LicensePayload[] | ||
|
||
return payload.map((license: LicensePayload) => ({ | ||
id: license.id, | ||
name: license.name, | ||
shortDescription: license.shortDescription, | ||
uri: license.uri, | ||
iconUri: license.iconUrl, // in payload, it is called iconUrl, but iconUri is the name matching everywhere else | ||
active: license.active, | ||
isDefault: license.isDefault, | ||
sortOrder: license.sortOrder, | ||
rightsIdentifier: license.rightsIdentifier, | ||
rightsIdentifierScheme: license.rightsIdentifierScheme, | ||
schemeUri: license.schemeUri, | ||
languageCode: license.languageCode | ||
})) | ||
} |
20 changes: 20 additions & 0 deletions
20
src/licenses/domain/useCases/GetAvailableStandardLicenses.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { UseCase } from '../../../core/domain/useCases/UseCase' | ||
import { License } from '../models/License' | ||
import { ILicensesRepository } from '../repositories/ILicensesRepository' | ||
|
||
export class GetAvailableStandardLicenses implements UseCase<License[]> { | ||
private licensesRepository: ILicensesRepository | ||
|
||
constructor(licensesRepository: ILicensesRepository) { | ||
this.licensesRepository = licensesRepository | ||
} | ||
|
||
/** | ||
* Returns the list of available standard license terms that can be selected for a dataset. | ||
* | ||
* @returns {Promise<License[]>} | ||
*/ | ||
async execute(): Promise<License[]> { | ||
return await this.licensesRepository.getAvailableStandardLicenses() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { LicensesRepository } from './infra/repositories/LicensesRepository' | ||
import { GetAvailableStandardLicenses } from './domain/useCases/GetAvailableStandardLicenses' | ||
|
||
const licensesRepository = new LicensesRepository() | ||
|
||
const getAvailableStandardLicenses = new GetAvailableStandardLicenses(licensesRepository) | ||
|
||
export { getAvailableStandardLicenses } | ||
|
||
export { License } from './domain/models/License' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ApiRepository } from '../../../core/infra/repositories/ApiRepository' | ||
import { ILicensesRepository } from '../../domain/repositories/ILicensesRepository' | ||
import { License } from '../../domain/models/License' | ||
|
||
export class LicensesRepository extends ApiRepository implements ILicensesRepository { | ||
private readonly licensesResourceName: string = 'licenses' | ||
|
||
public async getAvailableStandardLicenses(): Promise<License[]> { | ||
return this.doGet(this.buildApiEndpoint(this.licensesResourceName)) | ||
.then((response) => response.data.data) | ||
.catch((error) => { | ||
throw error | ||
}) | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
test/functional/licenses/GetAvailableStandardLicenses.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { ApiConfig, getAvailableStandardLicenses, License } from '../../../src' | ||
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' | ||
import { TestConstants } from '../../testHelpers/TestConstants' | ||
|
||
describe('getAvailableStandardLicenses', () => { | ||
describe('execute', () => { | ||
beforeAll(async () => { | ||
ApiConfig.init( | ||
TestConstants.TEST_API_URL, | ||
DataverseApiAuthMechanism.API_KEY, | ||
process.env.TEST_API_KEY | ||
) | ||
}) | ||
|
||
test('should return available standard license terms', async () => { | ||
const actualLicenses: License[] = await getAvailableStandardLicenses.execute() | ||
const expectedLicenses = [ | ||
{ | ||
id: 1, | ||
name: 'CC0 1.0', | ||
shortDescription: 'Creative Commons CC0 1.0 Universal Public Domain Dedication.', | ||
uri: 'http://creativecommons.org/publicdomain/zero/1.0', | ||
iconUrl: 'https://licensebuttons.net/p/zero/1.0/88x31.png', | ||
active: true, | ||
isDefault: true, | ||
sortOrder: 0, | ||
rightsIdentifier: 'CC0-1.0', | ||
rightsIdentifierScheme: 'SPDX', | ||
schemeUri: 'https://spdx.org/licenses/', | ||
languageCode: 'en' | ||
}, | ||
{ | ||
id: 2, | ||
name: 'CC BY 4.0', | ||
shortDescription: 'Creative Commons Attribution 4.0 International License.', | ||
uri: 'http://creativecommons.org/licenses/by/4.0', | ||
iconUrl: 'https://licensebuttons.net/l/by/4.0/88x31.png', | ||
active: true, | ||
isDefault: false, | ||
sortOrder: 2, | ||
rightsIdentifier: 'CC-BY-4.0', | ||
rightsIdentifierScheme: 'SPDX', | ||
schemeUri: 'https://spdx.org/licenses/', | ||
languageCode: 'en' | ||
} | ||
] | ||
|
||
expect(actualLicenses).toEqual(expectedLicenses) | ||
}) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { | ||
ApiConfig, | ||
DataverseApiAuthMechanism | ||
} from '../../../src/core/infra/repositories/ApiConfig' | ||
import { TestConstants } from '../../testHelpers/TestConstants' | ||
import { LicensesRepository } from '../../../src/licenses/infra/repositories/LicensesRepository' | ||
|
||
describe('LicensesRepository', () => { | ||
const sut: LicensesRepository = new LicensesRepository() | ||
|
||
describe('getAvailableStandardLicenses', () => { | ||
beforeAll(async () => { | ||
ApiConfig.init( | ||
TestConstants.TEST_API_URL, | ||
DataverseApiAuthMechanism.API_KEY, | ||
process.env.TEST_API_KEY | ||
) | ||
}) | ||
|
||
test('should return list of available standard license terms', async () => { | ||
const actual = await sut.getAvailableStandardLicenses() | ||
|
||
const licenses = [ | ||
{ | ||
id: 1, | ||
name: 'CC0 1.0', | ||
shortDescription: 'Creative Commons CC0 1.0 Universal Public Domain Dedication.', | ||
uri: 'http://creativecommons.org/publicdomain/zero/1.0', | ||
iconUrl: 'https://licensebuttons.net/p/zero/1.0/88x31.png', | ||
active: true, | ||
isDefault: true, | ||
sortOrder: 0, | ||
rightsIdentifier: 'CC0-1.0', | ||
rightsIdentifierScheme: 'SPDX', | ||
schemeUri: 'https://spdx.org/licenses/', | ||
languageCode: 'en' | ||
}, | ||
{ | ||
id: 2, | ||
name: 'CC BY 4.0', | ||
shortDescription: 'Creative Commons Attribution 4.0 International License.', | ||
uri: 'http://creativecommons.org/licenses/by/4.0', | ||
iconUrl: 'https://licensebuttons.net/l/by/4.0/88x31.png', | ||
active: true, | ||
isDefault: false, | ||
sortOrder: 2, | ||
rightsIdentifier: 'CC-BY-4.0', | ||
rightsIdentifierScheme: 'SPDX', | ||
schemeUri: 'https://spdx.org/licenses/', | ||
languageCode: 'en' | ||
} | ||
] | ||
|
||
expect(actual).toEqual(licenses) | ||
}) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { License, ReadError } from '../../../src' | ||
import { ILicensesRepository } from '../../../src/licenses/domain/repositories/ILicensesRepository' | ||
import { GetAvailableStandardLicenses } from '../../../src/licenses/domain/useCases/GetAvailableStandardLicenses' | ||
|
||
describe('GetAvailableStandardLicenses', () => { | ||
describe('execute', () => { | ||
test('should return licenses array on repository success', async () => { | ||
const licensesRepositoryStub: ILicensesRepository = {} as ILicensesRepository | ||
|
||
const testLicenses: License[] = [ | ||
{ | ||
id: 1, | ||
name: 'CC0 1.0', | ||
uri: 'http://creativecommons.org/publicdomain/zero/1.0', | ||
iconUri: 'https://licensebuttons.net/p/zero/1.0/88x31.png', | ||
active: true, | ||
isDefault: true, | ||
sortOrder: 0, | ||
rightsIdentifier: 'CC0-1.0', | ||
rightsIdentifierScheme: 'SPDX', | ||
schemeUri: 'https://spdx.org/licenses/', | ||
languageCode: 'en' | ||
}, | ||
{ | ||
id: 2, | ||
name: 'CC BY 4.0', | ||
uri: 'http://creativecommons.org/licenses/by/4.0', | ||
iconUri: 'https://licensebuttons.net/l/by/4.0/88x31.png', | ||
active: true, | ||
isDefault: false, | ||
sortOrder: 2, | ||
rightsIdentifier: 'CC-BY-4.0', | ||
rightsIdentifierScheme: 'SPDX', | ||
schemeUri: 'https://spdx.org/licenses/', | ||
languageCode: 'en' | ||
} | ||
] | ||
|
||
licensesRepositoryStub.getAvailableStandardLicenses = jest | ||
.fn() | ||
.mockResolvedValue(testLicenses) | ||
const sut = new GetAvailableStandardLicenses(licensesRepositoryStub) | ||
|
||
const actual = await sut.execute() | ||
|
||
expect(actual).toEqual(testLicenses) | ||
expect(licensesRepositoryStub.getAvailableStandardLicenses).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('should return error result on repository error', async () => { | ||
const licensesRepositoryStub: ILicensesRepository = {} as ILicensesRepository | ||
const expectedError = new ReadError('Failed to fetch licenses') | ||
licensesRepositoryStub.getAvailableStandardLicenses = jest | ||
.fn() | ||
.mockRejectedValue(expectedError) | ||
const sut = new GetAvailableStandardLicenses(licensesRepositoryStub) | ||
|
||
await expect(sut.execute()).rejects.toThrow(ReadError) | ||
expect(licensesRepositoryStub.getAvailableStandardLicenses).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.