refactor and add tests

This commit is contained in:
FrancescoMolinaro
2024-01-25 13:50:23 +01:00
parent 4cca9015a1
commit e77343604e
3 changed files with 147 additions and 37 deletions

View File

@@ -0,0 +1,144 @@
import { of as observableOf } from 'rxjs';
import { TestScheduler } from 'rxjs/testing';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { ObjectCacheService } from '../cache/object-cache.service';
import { HALEndpointService } from '../shared/hal-endpoint.service';
import { RequestService } from './request.service';
import { createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
import { HrefOnlyDataService } from './href-only-data.service';
import { getMockHrefOnlyDataService } from '../../shared/mocks/href-only-data.service.mock';
import { RestResponse } from '../cache/response.models';
import { cold, getTestScheduler, hot } from 'jasmine-marbles';
import { Item } from '../shared/item.model';
import { Version } from '../shared/version.model';
import { VersionHistory } from '../shared/version-history.model';
import { RequestEntry } from './request-entry.model';
import { testPatchDataImplementation } from './base/patch-data.spec';
import { UpdateDataServiceImpl } from './update-data-service';
import { testSearchDataImplementation } from './base/search-data.spec';
import { testDeleteDataImplementation } from './base/delete-data.spec';
import { testCreateDataImplementation } from './base/create-data.spec';
import { testFindAllDataImplementation } from './base/find-all-data.spec';
import { testPutDataImplementation } from './base/put-data.spec';
import { NotificationsService } from '../../shared/notifications/notifications.service';
describe('VersionDataService test', () => {
let scheduler: TestScheduler;
let service: UpdateDataServiceImpl<any>;
let requestService: RequestService;
let rdbService: RemoteDataBuildService;
let objectCache: ObjectCacheService;
let halService: HALEndpointService;
let hrefOnlyDataService: HrefOnlyDataService;
let responseCacheEntry: RequestEntry;
const notificationsService = {} as NotificationsService;
const item = Object.assign(new Item(), {
id: '1234-1234',
uuid: '1234-1234',
bundles: observableOf({}),
metadata: {
'dc.title': [
{
language: 'en_US',
value: 'This is just another title'
}
],
'dc.type': [
{
language: null,
value: 'Article'
}
],
'dc.contributor.author': [
{
language: 'en_US',
value: 'Smith, Donald'
}
],
'dc.date.issued': [
{
language: null,
value: '2015-06-26'
}
]
}
});
const versionHistory = Object.assign(new VersionHistory(), {
id: '1',
draftVersion: true,
});
const mockVersion: Version = Object.assign(new Version(), {
item: createSuccessfulRemoteDataObject$(item),
versionhistory: createSuccessfulRemoteDataObject$(versionHistory),
version: 1,
});
const mockVersionRD = createSuccessfulRemoteDataObject(mockVersion);
const endpointURL = `https://rest.api/rest/api/versioning/versions`;
const requestUUID = '8b3c613a-5a4b-438b-9686-be1d5b4a1c5a';
objectCache = {} as ObjectCacheService;
const comparatorEntry = {} as any;
function initTestService() {
hrefOnlyDataService = getMockHrefOnlyDataService();
return new UpdateDataServiceImpl(
'testLinkPath',
requestService,
rdbService,
objectCache,
halService,
notificationsService,
comparatorEntry,
10 * 1000
);
}
beforeEach(() => {
scheduler = getTestScheduler();
halService = jasmine.createSpyObj('halService', {
getEndpoint: cold('a', { a: endpointURL })
});
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),
});
rdbService = jasmine.createSpyObj('rdbService', {
buildSingle: hot('(a|)', {
a: mockVersionRD
})
});
service = initTestService();
spyOn((service as any), 'findById').and.callThrough();
});
afterEach(() => {
service = null;
});
describe('composition', () => {
const initService = () => new UpdateDataServiceImpl(null, null, null, null, null, null, null, null);
testPatchDataImplementation(initService);
testSearchDataImplementation(initService);
testDeleteDataImplementation(initService);
testCreateDataImplementation(initService);
testFindAllDataImplementation(initService);
testPutDataImplementation(initService);
});
});

View File

@@ -1,5 +1,3 @@
import { HttpClient } from '@angular/common/http';
import { Store } from '@ngrx/store';
import { Operation } from 'fast-json-patch';
import { AsyncSubject, from as observableFrom, Observable } from 'rxjs';
import {
@@ -29,7 +27,6 @@ import { RequestService } from './request.service';
import { RestRequestMethod } from './rest-request-method';
import { NoContent } from '../shared/NoContent.model';
import { CacheableObject } from '../cache/cacheable-object.model';
import { CoreState } from '../core-state.model';
import { FindListOptions } from './find-list-options.model';
import { FindAllData, FindAllDataImpl } from './base/find-all-data';
import { SearchData, SearchDataImpl } from './base/search-data';
@@ -70,10 +67,7 @@ export interface UpdateDataService<T> {
* invalidateByHref - invalidate the href making all requests as stale
*/
export abstract class UpdateDataServiceImpl<T extends CacheableObject> extends IdentifiableDataService<T> implements FindAllData<T>, SearchData<T>, CreateData<T>, PatchData<T>, PutData<T>, DeleteData<T> {
protected abstract store: Store<CoreState>;
protected abstract http: HttpClient;
export class UpdateDataServiceImpl<T extends CacheableObject> extends IdentifiableDataService<T> implements FindAllData<T>, SearchData<T>, CreateData<T>, PatchData<T>, PutData<T>, DeleteData<T> {
private findAllData: FindAllDataImpl<T>;
private searchData: SearchDataImpl<T>;
private createData: CreateDataImpl<T>;
@@ -146,23 +140,6 @@ export abstract class UpdateDataServiceImpl<T extends CacheableObject> extends I
return this.findAllData.findAll(options, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}
/**
* Returns an observable of {@link RemoteData} of an object, based on its ID, with a list of
* {@link FollowLinkConfig}, to automatically resolve {@link HALLink}s of the object
* @param id ID of object we want to retrieve
* @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
*/
findById(id: string, useCachedVersionIfAvailable = true, reRequestOnStale = true, ...linksToFollow: FollowLinkConfig<T>[]): Observable<RemoteData<T>> {
const href$ = this.getIDHrefObs(encodeURIComponent(id), ...linksToFollow);
return this.findByHref(href$, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow);
}
/**
* Make a new FindListRequest with given search method
*

View File

@@ -1,12 +1,10 @@
import { SuggestionsService } from './suggestions.service';
import { AuthService } from '../core/auth/auth.service';
import { ResearcherProfileDataService } from '../core/profile/researcher-profile-data.service';
import {
SuggestionsDataService
} from '../core/suggestion-notifications/suggestions-data.service';
import {
SuggestionSourceDataService
} from '../core/suggestion-notifications/source/suggestion-source-data.service';
import {
SuggestionTargetDataService
} from '../core/suggestion-notifications/target/suggestion-target-data.service';
@@ -29,10 +27,8 @@ import {
describe('SuggestionsService test', () => {
let scheduler: TestScheduler;
let service: SuggestionsService;
let authService: AuthService;
let researcherProfileService: ResearcherProfileDataService;
let suggestionsDataService: SuggestionsDataService;
let suggestionSourceDataService: SuggestionSourceDataService;
let suggestionTargetDataService: SuggestionTargetDataService;
let translateService: any = {
instant: (str) => str,
@@ -53,10 +49,8 @@ describe('SuggestionsService test', () => {
function initTestService() {
return new SuggestionsService(
authService,
researcherProfileService,
suggestionsDataService,
suggestionSourceDataService,
suggestionTargetDataService,
translateService
);
@@ -65,11 +59,6 @@ describe('SuggestionsService test', () => {
beforeEach(() => {
scheduler = getTestScheduler();
suggestionSourceDataService = jasmine.createSpyObj('suggestionSourcesDataService', {
getSources: observableOf(null),
});
researcherProfileService = jasmine.createSpyObj('researcherProfileService', {
findById: createSuccessfulRemoteDataObject$(mockResercherProfile as ResearcherProfile),
findRelatedItemId: observableOf('1234'),