Skip to content

Commit

Permalink
add data services tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescoMolinaro committed Jan 9, 2024
1 parent 1f1d594 commit 3149842
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 37 deletions.
26 changes: 21 additions & 5 deletions src/app/core/submission/workspaceitem-data.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpClient } from '@angular/common/http';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { of as observableOf } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';

Expand All @@ -8,7 +8,7 @@ import { ObjectCacheService } from '../cache/object-cache.service';
import { HALEndpointService } from '../shared/hal-endpoint.service';
import { RequestService } from '../data/request.service';
import { PageInfo } from '../shared/page-info.model';
import { createSuccessfulRemoteDataObject } from '../../shared/remote-data.utils';
import { createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
import { HrefOnlyDataService } from '../data/href-only-data.service';
import { getMockHrefOnlyDataService } from '../../shared/mocks/href-only-data.service.mock';
import { WorkspaceitemDataService } from './workspaceitem-data.service';
Expand All @@ -24,6 +24,8 @@ import { testDeleteDataImplementation } from '../data/base/delete-data.spec';
import { SearchData } from '../data/base/search-data';
import { DeleteData } from '../data/base/delete-data';
import { RequestParam } from '../cache/models/request-param.model';
import { PostRequest } from '../data/request.models';
import { HttpOptions } from '../dspace-rest/dspace-rest.service';

describe('WorkspaceitemDataService test', () => {
let scheduler: TestScheduler;
Expand Down Expand Up @@ -109,7 +111,7 @@ describe('WorkspaceitemDataService test', () => {
scheduler = getTestScheduler();

halService = jasmine.createSpyObj('halService', {
getEndpoint: cold('a', { a: endpointURL })
getEndpoint: observableOf(endpointURL)
});
responseCacheEntry = new RequestEntry();
responseCacheEntry.request = { href: 'https://rest.api/' } as any;
Expand All @@ -125,7 +127,8 @@ describe('WorkspaceitemDataService test', () => {
rdbService = jasmine.createSpyObj('rdbService', {
buildSingle: hot('a|', {
a: wsiRD
})
}),
buildFromRequestUUID: createSuccessfulRemoteDataObject$({})
});

service = initTestService();
Expand Down Expand Up @@ -154,6 +157,19 @@ describe('WorkspaceitemDataService test', () => {
});

});
});

describe('importExternalSourceEntry', () => {
it('should send a POST request containing the provided item request', (done) => {
const options: HttpOptions = Object.create({});
let headers = new HttpHeaders();
headers = headers.append('Content-Type', 'text/uri-list');
options.headers = headers;

service.importExternalSourceEntry('externalHref', 'testId').subscribe(() => {
expect(requestService.send).toHaveBeenCalledWith(new PostRequest(requestUUID, `${endpointURL}?owningCollection=testId`, 'externalHref', options));
done();
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class SuggestionSourceDataService extends IdentifiableDataService<Suggest
* The list of Quality Assurance source.
*/
public getSources(options: FindListOptions = {}, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<SuggestionSource>[]): Observable<RemoteData<PaginatedList<SuggestionSource>>> {
return this.findAllData.findAll(options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
return this.findAll(options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}

/**
Expand All @@ -71,4 +71,23 @@ export class SuggestionSourceDataService extends IdentifiableDataService<Suggest
public getSource(id: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<SuggestionSource>[]): Observable<RemoteData<SuggestionSource>> {
return this.findById(id, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}


/**
* Returns {@link RemoteData} of all object with a list of {@link FollowLinkConfig}, to indicate which embedded
* info should be added to the objects
*
* @param options Find list options object
* @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
* no valid cached version. Defaults to true
* @param reRequestOnStale Whether or not the request should automatically be re-
* requested after the response becomes stale
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which
* {@link HALLink}s should be automatically resolved
* @return {Observable<RemoteData<PaginatedList<T>>>}
* Return an observable that emits object list
*/
findAll(options: FindListOptions = {}, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<SuggestionSource>[]): Observable<RemoteData<PaginatedList<SuggestionSource>>> {
return this.findAllData.findAll(options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { TestScheduler } from 'rxjs/testing';
import { RequestService } from '../../../data/request.service';
import { RemoteDataBuildService } from '../../../cache/builders/remote-data-build.service';
import { ObjectCacheService } from '../../../cache/object-cache.service';
import { HALEndpointService } from '../../../shared/hal-endpoint.service';
import { RequestEntry } from '../../../data/request-entry.model';
import { cold, getTestScheduler } from 'jasmine-marbles';
import { RestResponse } from '../../../cache/response.models';
import { of as observableOf } from 'rxjs';
import { Store } from '@ngrx/store';
import { CoreState } from '../../../core-state.model';
import { HttpClient } from '@angular/common/http';
import { NotificationsService } from '../../../../shared/notifications/notifications.service';
import { DefaultChangeAnalyzer } from '../../../data/default-change-analyzer.service';
import { testFindAllDataImplementation } from '../../../data/base/find-all-data.spec';
import { FindAllData } from '../../../data/base/find-all-data';
import { GetRequest } from '../../../data/request.models';
import {
createSuccessfulRemoteDataObject$
} from '../../../../shared/remote-data.utils';
import { RemoteData } from '../../../data/remote-data';
import { RequestEntryState } from '../../../data/request-entry-state.model';
import { SuggestionSourceDataService } from './suggestion-source-data.service';
import { SuggestionSource } from '../models/suggestion-source.model';

describe('SuggestionSourceDataService test', () => {
let scheduler: TestScheduler;
let service: SuggestionSourceDataService;
let requestService: RequestService;
let rdbService: RemoteDataBuildService;
let objectCache: ObjectCacheService;
let halService: HALEndpointService;
let notificationsService: NotificationsService;
let http: HttpClient;
let comparator: DefaultChangeAnalyzer<SuggestionSource>;
let responseCacheEntry: RequestEntry;

const store = {} as Store<CoreState>;
const endpointURL = `https://rest.api/rest/api/suggestionsources`;
const requestUUID = '8b3c613a-5a4b-438b-9686-be1d5b4a1c5a';

const remoteDataMocks = {
Success: new RemoteData(null, null, null, RequestEntryState.Success, null, null, 200),
};

function initTestService() {
return new SuggestionSourceDataService(
requestService,
rdbService,
store,
objectCache,
halService,
notificationsService,
http,
comparator
);
}

beforeEach(() => {
scheduler = getTestScheduler();

objectCache = {} as ObjectCacheService;
http = {} as HttpClient;
notificationsService = {} as NotificationsService;
comparator = {} as DefaultChangeAnalyzer<SuggestionSource>;
responseCacheEntry = new RequestEntry();
responseCacheEntry.request = { href: 'https://rest.api/' } as any;
responseCacheEntry.response = new RestResponse(true, 200, 'Success');

requestService = jasmine.createSpyObj('requestService', {
generateRequestId: requestUUID,
send: true,
removeByHrefSubstring: {},
getByHref: observableOf(responseCacheEntry),
getByUUID: observableOf(responseCacheEntry),
});

halService = jasmine.createSpyObj('halService', {
getEndpoint: observableOf(endpointURL)
});

rdbService = jasmine.createSpyObj('rdbService', {
buildSingle: createSuccessfulRemoteDataObject$({}, 500),
buildList: cold('a', { a: remoteDataMocks.Success })
});


service = initTestService();
});

describe('composition', () => {
const initFindAllService = () => new SuggestionSourceDataService(null, null, null, null, null, null, null, null) as unknown as FindAllData<any>;
testFindAllDataImplementation(initFindAllService);
});

describe('getSources', () => {
it('should send a new GetRequest', () => {
const expected = new GetRequest(requestService.generateRequestId(), `${endpointURL}`);
scheduler.schedule(() => service.getSources().subscribe());
scheduler.flush();

expect(requestService.send).toHaveBeenCalledWith(expected, true);
});
});

describe('getSource', () => {
it('should send a new GetRequest', () => {
const expected = new GetRequest(requestService.generateRequestId(), `${endpointURL}/testId`);
scheduler.schedule(() => service.getSource('testId').subscribe());
scheduler.flush();

expect(requestService.send).toHaveBeenCalledWith(expected, true);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { SuggestionTargetDataService } from './target/suggestion-target-data.ser
/**
* A private DataService implementation to delegate specific methods to.
*/
class SuggestionDataServiceImpl extends DataService<Suggestion> {
export class SuggestionDataServiceImpl extends DataService<Suggestion> {
/**
* The REST endpoint.
*/
Expand Down Expand Up @@ -70,7 +70,6 @@ class SuggestionDataServiceImpl extends DataService<Suggestion> {
@dataService(SUGGESTION)
export class SuggestionsDataService {
protected searchFindBySourceMethod = 'findBySource';
protected searchFindByTargetMethod = 'findByTarget';
protected searchFindByTargetAndSourceMethod = 'findByTargetAndSource';

/**
Expand Down Expand Up @@ -168,7 +167,6 @@ export class SuggestionsDataService {
...linksToFollow: FollowLinkConfig<SuggestionTarget>[]
): Observable<RemoteData<PaginatedList<SuggestionTarget>>> {
options.searchParams = [new RequestParam('target', userId)];
//return this.suggestionTargetsDataService.getTargetsByUser(this.searchFindByTargetMethod, options, ...linksToFollow);
return this.suggestionTargetsDataService.getTargetsByUser(userId, options, ...linksToFollow);
}

Expand All @@ -193,14 +191,6 @@ export class SuggestionsDataService {
return this.suggestionsDataService.delete(suggestionId);
}

/**
* Used to fetch Suggestion notification for user
* @suggestionId
*/
public getSuggestion(suggestionId: string, ...linksToFollow: FollowLinkConfig<Suggestion>[]): Observable<RemoteData<Suggestion>> {
return this.suggestionsDataService.findById(suggestionId, true, true, ...linksToFollow);
}

/**
* Return the list of Suggestion for a given target and source
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ export class SuggestionTargetDataService extends IdentifiableDataService<Suggest

protected linkPath = 'suggestiontargets';
private findAllData: FindAllData<SuggestionTarget>;
private searchBy: SearchData<SuggestionTarget>;
private searchData: SearchData<SuggestionTarget>;
protected searchFindBySourceMethod = 'findBySource';
protected searchFindByTargetMethod = 'findByTarget';
protected searchFindByTargetAndSourceMethod = 'findByTargetAndSource';

constructor(
protected requestService: RequestService,
Expand All @@ -44,7 +43,7 @@ export class SuggestionTargetDataService extends IdentifiableDataService<Suggest
protected comparator: DefaultChangeAnalyzer<SuggestionTarget>) {
super('suggestiontargets', requestService, rdbService, objectCache, halService);
this.findAllData = new FindAllDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, this.responseMsToLive);
this.searchBy = new SearchDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, this.responseMsToLive);
this.searchData = new SearchDataImpl(this.linkPath, requestService, rdbService, objectCache, halService, this.responseMsToLive);
}
/**
* Return the list of Suggestion Target for a given source
Expand All @@ -65,22 +64,7 @@ export class SuggestionTargetDataService extends IdentifiableDataService<Suggest
): Observable<RemoteData<PaginatedList<SuggestionTarget>>> {
options.searchParams = [new RequestParam('source', source)];

return this.searchBy.searchBy(this.searchFindBySourceMethod, options, true, true, ...linksToFollow);
}
/**
* Return a single Suggestion target.
*
* @param id The Suggestion Target id
* @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
* no valid cached version. Defaults to true
* @param reRequestOnStale Whether or not the request should automatically be re-
* requested after the response becomes stale
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which {@link HALLink}s should be automatically resolved.
*
* @return Observable<RemoteData<QualityAssuranceSourceObject>> The Quality Assurance source.
*/
public getTarget(id: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<SuggestionTarget>[]): Observable<RemoteData<SuggestionTarget>> {
return this.findById(id, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
return this.searchBy(this.searchFindBySourceMethod, options, true, true, ...linksToFollow);
}

/**
Expand All @@ -102,7 +86,7 @@ export class SuggestionTargetDataService extends IdentifiableDataService<Suggest
): Observable<RemoteData<PaginatedList<SuggestionTarget>>> {
options.searchParams = [new RequestParam('target', userId)];

return this.searchBy.searchBy(this.searchFindByTargetMethod, options, true, true, ...linksToFollow);
return this.searchBy(this.searchFindByTargetMethod, options, true, true, ...linksToFollow);
}
/**
* Return a Suggestion Target for a given id
Expand All @@ -117,4 +101,41 @@ export class SuggestionTargetDataService extends IdentifiableDataService<Suggest
return this.findById(targetId);
}

/**
* Make a new FindListRequest with given search method
*
* @param searchMethod The search method for the object
* @param options The [[FindListOptions]] object
* @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
* no valid cached version. Defaults to true
* @param reRequestOnStale Whether or not the request should automatically be re-
* requested after the response becomes stale
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which
* {@link HALLink}s should be automatically resolved
* @return {Observable<RemoteData<PaginatedList<T>>}
* Return an observable that emits response from the server
*/
public searchBy(searchMethod: string, options?: FindListOptions, useCachedVersionIfAvailable?: boolean, reRequestOnStale?: boolean, ...linksToFollow: FollowLinkConfig<SuggestionTarget>[]): Observable<RemoteData<PaginatedList<SuggestionTarget>>> {
return this.searchData.searchBy(searchMethod, options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}


/**
* Returns {@link RemoteData} of all object with a list of {@link FollowLinkConfig}, to indicate which embedded
* info should be added to the objects
*
* @param options Find list options object
* @param useCachedVersionIfAvailable If this is true, the request will only be sent if there's
* no valid cached version. Defaults to true
* @param reRequestOnStale Whether or not the request should automatically be re-
* requested after the response becomes stale
* @param linksToFollow List of {@link FollowLinkConfig} that indicate which
* {@link HALLink}s should be automatically resolved
* @return {Observable<RemoteData<PaginatedList<T>>>}
* Return an observable that emits object list
*/
findAll(options: FindListOptions = {}, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<SuggestionTarget>[]): Observable<RemoteData<PaginatedList<SuggestionTarget>>> {
return this.findAllData.findAll(options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}

}
Loading

0 comments on commit 3149842

Please sign in to comment.