Skip to content

Commit 8d25ba7

Browse files
committed
Implement getVocabularyByMetadataAndCollection in VocabularyService
1 parent 45650c1 commit 8d25ba7

5 files changed

Lines changed: 87 additions & 0 deletions

File tree

src/app/core/submission/vocabularies/vocabulary.data.service.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77
*/
88
import { VocabularyDataService } from './vocabulary.data.service';
99
import { testFindAllDataImplementation } from '../../data/base/find-all-data.spec';
10+
import { FindListOptions } from '../../data/find-list-options.model';
11+
import { RequestParam } from '../../cache/models/request-param.model';
12+
import { createSuccessfulRemoteDataObject$ } from 'src/app/shared/remote-data.utils';
1013

1114
describe('VocabularyDataService', () => {
15+
let service: VocabularyDataService;
16+
service = initTestService();
17+
let restEndpointURL = 'https://rest.api/server/api/submission/vocabularies';
18+
let vocabularyByMetadataAndCollectionEndpoint = `${restEndpointURL}/search/byMetadataAndCollection?metadata=dc.contributor.author&collection=1234-1234`;
19+
1220
function initTestService() {
1321
return new VocabularyDataService(null, null, null, null);
1422
}
@@ -17,4 +25,18 @@ describe('VocabularyDataService', () => {
1725
const initService = () => new VocabularyDataService(null, null, null, null);
1826
testFindAllDataImplementation(initService);
1927
});
28+
29+
describe('getVocabularyByMetadataAndCollection', () => {
30+
it('search vocabulary by metadata and collection calls expected methods', () => {
31+
spyOn((service as any).searchData, 'getSearchByHref').and.returnValue(vocabularyByMetadataAndCollectionEndpoint);
32+
spyOn(service, 'findByHref').and.returnValue(createSuccessfulRemoteDataObject$(null));
33+
service.getVocabularyByMetadataAndCollection('dc.contributor.author', '1234-1234');
34+
const options = Object.assign(new FindListOptions(), {
35+
searchParams: [Object.assign(new RequestParam('metadata', encodeURIComponent('dc.contributor.author'))),
36+
Object.assign(new RequestParam('collection', encodeURIComponent('1234-1234')))]
37+
});
38+
expect((service as any).searchData.getSearchByHref).toHaveBeenCalledWith('byMetadataAndCollection', options);
39+
expect(service.findByHref).toHaveBeenCalledWith(vocabularyByMetadataAndCollectionEndpoint, true, true);
40+
});
41+
});
2042
});

src/app/core/submission/vocabularies/vocabulary.data.service.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ import { PaginatedList } from '../../data/paginated-list.model';
2020
import { Injectable } from '@angular/core';
2121
import { VOCABULARY } from './models/vocabularies.resource-type';
2222
import { dataService } from '../../data/base/data-service.decorator';
23+
import { SearchDataImpl } from '../../data/base/search-data';
24+
import { RequestParam } from '../../cache/models/request-param.model';
2325

2426
/**
2527
* Data service to retrieve vocabularies from the REST server.
2628
*/
2729
@Injectable()
2830
@dataService(VOCABULARY)
2931
export class VocabularyDataService extends IdentifiableDataService<Vocabulary> implements FindAllData<Vocabulary> {
32+
protected searchByMetadataAndCollectionPath = 'byMetadataAndCollection';
33+
3034
private findAllData: FindAllData<Vocabulary>;
35+
private searchData: SearchDataImpl<Vocabulary>;
3136

3237
constructor(
3338
protected requestService: RequestService,
@@ -38,6 +43,7 @@ export class VocabularyDataService extends IdentifiableDataService<Vocabulary> i
3843
super('vocabularies', requestService, rdbService, objectCache, halService);
3944

4045
this.findAllData = new FindAllDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, this.responseMsToLive);
46+
this.searchData = new SearchDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, this.responseMsToLive);
4147
}
4248

4349
/**
@@ -57,4 +63,23 @@ export class VocabularyDataService extends IdentifiableDataService<Vocabulary> i
5763
public findAll(options?: FindListOptions, useCachedVersionIfAvailable?: boolean, reRequestOnStale?: boolean, ...linksToFollow: FollowLinkConfig<Vocabulary>[]): Observable<RemoteData<PaginatedList<Vocabulary>>> {
5864
return this.findAllData.findAll(options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
5965
}
66+
67+
/**
68+
* Return the controlled vocabulary configured for the specified metadata and collection if any (/submission/vocabularies/search/{@link searchByMetadataAndCollectionPath}?metadata=<>&collection=<>)
69+
* @param metadataField metadata field to search
70+
* @param collectionUUID collection UUID where is configured the vocabulary
71+
* @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
72+
* no valid cached version. Defaults to true
73+
* @param reRequestOnStale Whether or not the request should automatically be re-
74+
* requested after the response becomes stale
75+
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which
76+
* {@link HALLink}s should be automatically resolved
77+
*/
78+
public getVocabularyByMetadataAndCollection(metadataField: string, collectionUUID: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<Vocabulary>[]): Observable<RemoteData<Vocabulary>> {
79+
const findListOptions = new FindListOptions();
80+
findListOptions.searchParams = [new RequestParam('metadata', encodeURIComponent(metadataField)),
81+
new RequestParam('collection', encodeURIComponent(collectionUUID))];
82+
const href$ = this.searchData.getSearchByHref(this.searchByMetadataAndCollectionPath, findListOptions, ...linksToFollow);
83+
return this.findByHref(href$, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
84+
}
6085
}

src/app/core/submission/vocabularies/vocabulary.service.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ describe('VocabularyService', () => {
255255
spyOn((service as any).vocabularyDataService, 'findById').and.callThrough();
256256
spyOn((service as any).vocabularyDataService, 'findAll').and.callThrough();
257257
spyOn((service as any).vocabularyDataService, 'findByHref').and.callThrough();
258+
spyOn((service as any).vocabularyDataService, 'getVocabularyByMetadataAndCollection').and.callThrough();
258259
spyOn((service as any).vocabularyDataService.findAllData, 'getFindAllHref').and.returnValue(observableOf(entriesRequestURL));
260+
spyOn((service as any).vocabularyDataService.searchData, 'getSearchByHref').and.returnValue(observableOf(searchRequestURL));
259261
});
260262

261263
afterEach(() => {
@@ -312,6 +314,23 @@ describe('VocabularyService', () => {
312314
expect(result).toBeObservable(expected);
313315
});
314316
});
317+
318+
describe('getVocabularyByMetadataAndCollection', () => {
319+
it('should proxy the call to vocabularyDataService.getVocabularyByMetadataAndCollection', () => {
320+
scheduler.schedule(() => service.getVocabularyByMetadataAndCollection(metadata, collectionUUID));
321+
scheduler.flush();
322+
323+
expect((service as any).vocabularyDataService.getVocabularyByMetadataAndCollection).toHaveBeenCalledWith(metadata, collectionUUID, true, true);
324+
});
325+
326+
it('should return a RemoteData<Vocabulary> for the object with the given metadata and collection', () => {
327+
const result = service.getVocabularyByMetadataAndCollection(metadata, collectionUUID);
328+
const expected = cold('a|', {
329+
a: vocabularyRD
330+
});
331+
expect(result).toBeObservable(expected);
332+
});
333+
});
315334
});
316335

317336
describe('vocabulary entries', () => {

src/app/core/submission/vocabularies/vocabulary.service.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ export class VocabularyService {
8787
return this.vocabularyDataService.findAll(options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
8888
}
8989

90+
/**
91+
* Return the controlled vocabulary configured for the specified metadata and collection if any
92+
* @param metadataField metadata field to search
93+
* @param collectionUUID collection UUID where is configured the vocabulary
94+
* @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
95+
* no valid cached version. Defaults to true
96+
* @param reRequestOnStale Whether or not the request should automatically be re-
97+
* requested after the response becomes stale
98+
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which
99+
* {@link HALLink}s should be automatically resolved
100+
* @return {Observable<RemoteData<Vocabulary>>}
101+
* Return an observable that emits vocabulary object
102+
*/
103+
getVocabularyByMetadataAndCollection(metadataField: string, collectionUUID: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<Vocabulary>[]): Observable<RemoteData<Vocabulary>> {
104+
return this.vocabularyDataService.getVocabularyByMetadataAndCollection(metadataField, collectionUUID, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
105+
}
106+
90107
/**
91108
* Return the {@link VocabularyEntry} list for a given {@link Vocabulary}
92109
*

src/app/shared/testing/vocabulary-service.stub.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ export class VocabularyServiceStub {
4242
findVocabularyById(id: string): Observable<RemoteData<Vocabulary>> {
4343
return;
4444
}
45+
46+
getVocabularyByMetadataAndCollection(metadataField: string, collectionUUID: string): Observable<RemoteData<Vocabulary>> {
47+
return createSuccessfulRemoteDataObject$(null);
48+
}
4549
}

0 commit comments

Comments
 (0)