From e77343604e2288c0310c7c1565fce47e781cf11a Mon Sep 17 00:00:00 2001 From: FrancescoMolinaro Date: Thu, 25 Jan 2024 13:50:23 +0100 Subject: [PATCH] refactor and add tests --- src/app/core/data/update-data-service.spec.ts | 144 ++++++++++++++++++ src/app/core/data/update-data-service.ts | 25 +-- .../suggestion.service.spec.ts | 15 +- 3 files changed, 147 insertions(+), 37 deletions(-) create mode 100644 src/app/core/data/update-data-service.spec.ts diff --git a/src/app/core/data/update-data-service.spec.ts b/src/app/core/data/update-data-service.spec.ts new file mode 100644 index 0000000000..6912527b91 --- /dev/null +++ b/src/app/core/data/update-data-service.spec.ts @@ -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; + 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); + }); + +}); diff --git a/src/app/core/data/update-data-service.ts b/src/app/core/data/update-data-service.ts index 9a31067f88..715b2ee413 100644 --- a/src/app/core/data/update-data-service.ts +++ b/src/app/core/data/update-data-service.ts @@ -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 { * invalidateByHref - invalidate the href making all requests as stale */ -export abstract class UpdateDataServiceImpl extends IdentifiableDataService implements FindAllData, SearchData, CreateData, PatchData, PutData, DeleteData { - protected abstract store: Store; - protected abstract http: HttpClient; - +export class UpdateDataServiceImpl extends IdentifiableDataService implements FindAllData, SearchData, CreateData, PatchData, PutData, DeleteData { private findAllData: FindAllDataImpl; private searchData: SearchDataImpl; private createData: CreateDataImpl; @@ -146,23 +140,6 @@ export abstract class UpdateDataServiceImpl 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[]): Observable> { - const href$ = this.getIDHrefObs(encodeURIComponent(id), ...linksToFollow); - return this.findByHref(href$, useCachedVersionIfAvailable, reRequestOnStale, ...linksToFollow); - } - - /** * Make a new FindListRequest with given search method * diff --git a/src/app/suggestion-notifications/suggestion.service.spec.ts b/src/app/suggestion-notifications/suggestion.service.spec.ts index ad2cb8fcc4..acb0bb3be2 100644 --- a/src/app/suggestion-notifications/suggestion.service.spec.ts +++ b/src/app/suggestion-notifications/suggestion.service.spec.ts @@ -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'),