From 550eb6c7abc071fe5fe6f173bbd40f777f02930b Mon Sep 17 00:00:00 2001 From: nibou230 Date: Wed, 20 Apr 2022 09:12:59 -0400 Subject: [PATCH] Adapt the service to a LinkedRepository result --- src/app/core/data/item-data.service.spec.ts | 36 ++++++++++++++++--- src/app/core/data/item-data.service.ts | 30 +++++++++------- .../data/version-history-data.service.spec.ts | 2 +- .../access-status.model.ts | 14 ++++++-- .../testing/hal-endpoint-service.stub.ts | 6 +++- 5 files changed, 67 insertions(+), 21 deletions(-) diff --git a/src/app/core/data/item-data.service.spec.ts b/src/app/core/data/item-data.service.spec.ts index cc1e3b6e20..e85ddb2f38 100644 --- a/src/app/core/data/item-data.service.spec.ts +++ b/src/app/core/data/item-data.service.spec.ts @@ -10,12 +10,13 @@ import { ObjectCacheService } from '../cache/object-cache.service'; import { RestResponse } from '../cache/response.models'; import { ExternalSourceEntry } from '../shared/external-source-entry.model'; import { ItemDataService } from './item-data.service'; -import { DeleteRequest, PostRequest } from './request.models'; +import { DeleteRequest, GetRequest, PostRequest } from './request.models'; import { RequestService } from './request.service'; import { getMockRemoteDataBuildService } from '../../shared/mocks/remote-data-build.service.mock'; import { CoreState } from '../core-state.model'; import { RequestEntry } from './request-entry.model'; import { FindListOptions } from './find-list-options.model'; +import { HALEndpointServiceStub } from 'src/app/shared/testing/hal-endpoint-service.stub'; describe('ItemDataService', () => { let scheduler: TestScheduler; @@ -36,13 +37,11 @@ describe('ItemDataService', () => { }) as RequestService; const rdbService = getMockRemoteDataBuildService(); - const itemEndpoint = 'https://rest.api/core/items'; + const itemEndpoint = 'https://rest.api/core'; const store = {} as Store; const objectCache = {} as ObjectCacheService; - const halEndpointService = jasmine.createSpyObj('halService', { - getEndpoint: observableOf(itemEndpoint) - }); + const halEndpointService: any = new HALEndpointServiceStub(itemEndpoint); const bundleService = jasmine.createSpyObj('bundleService', { findByHref: {} }); @@ -185,6 +184,33 @@ describe('ItemDataService', () => { }); }); + describe('getAccessStatusEndpoint', () => { + beforeEach(() => { + service = initTestService(); + }); + it('should retrieve the access status endpoint', () => { + const itemId = '3de6ea60-ec39-419b-ae6f-065930ac1429'; + const result = service.getAccessStatusEndpoint(itemId); + result.subscribe(((value) => { + expect(value).toEqual(`${itemEndpoint}/items/${itemId}/accessStatus`); + })); + }); + }); + + describe('getAccessStatus', () => { + beforeEach(() => { + service = initTestService(); + }); + it('should send a GET request', (done) => { + const itemId = '3de6ea60-ec39-419b-ae6f-065930ac1429'; + const result = service.getAccessStatus(itemId); + result.subscribe(() => { + expect(requestService.send).toHaveBeenCalledWith(jasmine.any(GetRequest)); + done(); + }); + }); + }); + describe('when cache is invalidated', () => { beforeEach(() => { service = initTestService(); diff --git a/src/app/core/data/item-data.service.ts b/src/app/core/data/item-data.service.ts index 9a2dc5a8af..c7f0f541f8 100644 --- a/src/app/core/data/item-data.service.ts +++ b/src/app/core/data/item-data.service.ts @@ -292,25 +292,31 @@ export class ItemDataService extends DataService { ); } + /** + * Get the endpoint for an item's access status + * @param itemId + */ + public getAccessStatusEndpoint(itemId: string): Observable { + return this.halService.getEndpoint(this.linkPath).pipe( + switchMap((url: string) => this.halService.getEndpoint('accessStatus', `${url}/${itemId}`)) + ); + } + /** * Get the the access status * @param itemId */ public getAccessStatus(itemId: string): Observable> { - const requestId = this.requestService.generateRequestId(); - const href$ = this.halService.getEndpoint('accessStatus').pipe( - map((href) => href.replace('{?uuid}', `?uuid=${itemId}`)) - ); + const hrefObs = this.getAccessStatusEndpoint(itemId); - href$.pipe( - find((href: string) => hasValue(href)), - map((href: string) => { - const request = new GetRequest(requestId, href); - this.requestService.send(request); - }) - ).subscribe(); + hrefObs.pipe( + take(1) + ).subscribe((href) => { + const request = new GetRequest(this.requestService.generateRequestId(), href); + this.requestService.send(request); + }); - return this.rdbService.buildFromRequestUUID(requestId); + return this.rdbService.buildSingle(hrefObs); } /** diff --git a/src/app/core/data/version-history-data.service.spec.ts b/src/app/core/data/version-history-data.service.spec.ts index 207093b4d5..26ed370026 100644 --- a/src/app/core/data/version-history-data.service.spec.ts +++ b/src/app/core/data/version-history-data.service.spec.ts @@ -151,7 +151,7 @@ describe('VersionHistoryDataService', () => { describe('when getVersionsEndpoint is called', () => { it('should return the correct value', () => { service.getVersionsEndpoint(versionHistoryId).subscribe((res) => { - expect(res).toBe(url + '/versions'); + expect(res).toBe(url + '/versionhistories/version-history-id/versions'); }); }); }); diff --git a/src/app/shared/object-list/access-status-badge/access-status.model.ts b/src/app/shared/object-list/access-status-badge/access-status.model.ts index 6cdcafdbd8..31de1a3a38 100644 --- a/src/app/shared/object-list/access-status-badge/access-status.model.ts +++ b/src/app/shared/object-list/access-status-badge/access-status.model.ts @@ -1,11 +1,13 @@ -import { autoserialize } from 'cerialize'; +import { autoserialize, deserialize } from 'cerialize'; import { typedObject } from 'src/app/core/cache/builders/build-decorators'; +import { CacheableObject } from 'src/app/core/cache/object-cache.reducer'; +import { HALLink } from 'src/app/core/shared/hal-link.model'; import { ResourceType } from 'src/app/core/shared/resource-type'; import { excludeFromEquals } from 'src/app/core/utilities/equals.decorators'; import { ACCESS_STATUS } from './access-status.resource-type'; @typedObject -export class AccessStatusObject { +export class AccessStatusObject implements CacheableObject { static type = ACCESS_STATUS; /** @@ -20,4 +22,12 @@ export class AccessStatusObject { */ @autoserialize status: string; + + /** + * The {@link HALLink}s for this AccessStatusObject + */ + @deserialize + _links: { + self: HALLink; + }; } diff --git a/src/app/shared/testing/hal-endpoint-service.stub.ts b/src/app/shared/testing/hal-endpoint-service.stub.ts index 19f95d577c..753efcdb5d 100644 --- a/src/app/shared/testing/hal-endpoint-service.stub.ts +++ b/src/app/shared/testing/hal-endpoint-service.stub.ts @@ -1,9 +1,13 @@ import { of as observableOf } from 'rxjs'; +import { hasValue } from '../empty.util'; export class HALEndpointServiceStub { constructor(private url: string) {} - getEndpoint(path: string) { + getEndpoint(path: string, startHref?: string) { + if (hasValue(startHref)) { + return observableOf(startHref + '/' + path); + } return observableOf(this.url + '/' + path); } }