diff --git a/src/app/core/resource-policy/resource-policy.service.spec.ts b/src/app/core/resource-policy/resource-policy.service.spec.ts index 3408663db3..6c26973e02 100644 --- a/src/app/core/resource-policy/resource-policy.service.spec.ts +++ b/src/app/core/resource-policy/resource-policy.service.spec.ts @@ -1,4 +1,5 @@ import { HttpClient } from '@angular/common/http'; +import { async } from '@angular/core/testing'; import { cold, getTestScheduler, hot } from 'jasmine-marbles'; import { of as observableOf } from 'rxjs'; @@ -27,15 +28,16 @@ describe('ResourcePolicyService', () => { let rdbService: RemoteDataBuildService; let objectCache: ObjectCacheService; let halService: HALEndpointService; + let responseCacheEntry: RequestEntry; - const resourcePolicy = { + const resourcePolicy: any = { id: '1', name: null, description: null, policyType: PolicyType.TYPE_SUBMISSION, action: ActionType.READ, - startDate : null, - endDate : null, + startDate: null, + endDate: null, type: 'resourcepolicy', uuid: 'resource-policy-1', _links: { @@ -51,14 +53,14 @@ describe('ResourcePolicyService', () => { } }; - const anotherResourcePolicy = { + const anotherResourcePolicy: any = { id: '2', name: null, description: null, policyType: PolicyType.TYPE_SUBMISSION, action: ActionType.WRITE, - startDate : null, - endDate : null, + startDate: null, + endDate: null, type: 'resourcepolicy', uuid: 'resource-policy-2', _links: { @@ -82,12 +84,10 @@ describe('ResourcePolicyService', () => { const resourceUUID = '8b39g7ya-5a4b-438b-851f-be1d5b4a1c5a'; const pageInfo = new PageInfo(); - const array = [resourcePolicy, anotherResourcePolicy ]; + const array = [resourcePolicy, anotherResourcePolicy]; const paginatedList = new PaginatedList(pageInfo, array); const resourcePolicyRD = createSuccessfulRemoteDataObject(resourcePolicy); const paginatedListRD = createSuccessfulRemoteDataObject(paginatedList); - const responseCacheEntry = new RequestEntry(); - responseCacheEntry.response = new RestResponse(true, 200, 'Success'); beforeEach(() => { scheduler = getTestScheduler(); @@ -96,11 +96,15 @@ describe('ResourcePolicyService', () => { getEndpoint: cold('a', { a: endpointURL }) }); + responseCacheEntry = new RequestEntry(); + responseCacheEntry.response = new RestResponse(true, 200, 'Success'); + requestService = jasmine.createSpyObj('requestService', { generateRequestId: requestUUID, configure: true, removeByHrefSubstring: {}, getByHref: observableOf(responseCacheEntry), + getByUUID: observableOf(responseCacheEntry), }); rdbService = jasmine.createSpyObj('rdbService', { buildSingle: hot('a|', { @@ -125,12 +129,67 @@ describe('ResourcePolicyService', () => { comparator ); + spyOn((service as any).dataService, 'create').and.callThrough(); + spyOn((service as any).dataService, 'delete').and.callThrough(); spyOn((service as any).dataService, 'findById').and.callThrough(); spyOn((service as any).dataService, 'findByHref').and.callThrough(); spyOn((service as any).dataService, 'searchBy').and.callThrough(); spyOn((service as any).dataService, 'getSearchByHref').and.returnValue(observableOf(requestURL)); }); + describe('create', () => { + it('should proxy the call to dataservice.create with eperson UUID', () => { + scheduler.schedule(() => service.create(resourcePolicy, resourceUUID, epersonUUID)); + const params = [ + new RequestParam('resource', resourceUUID), + new RequestParam('eperson', epersonUUID) + ]; + scheduler.flush(); + + expect((service as any).dataService.create).toHaveBeenCalledWith(resourcePolicy, ...params); + }); + + it('should proxy the call to dataservice.create with group UUID', () => { + scheduler.schedule(() => service.create(resourcePolicy, resourceUUID, null, groupUUID)); + const params = [ + new RequestParam('resource', resourceUUID), + new RequestParam('group', groupUUID) + ]; + scheduler.flush(); + + expect((service as any).dataService.create).toHaveBeenCalledWith(resourcePolicy, ...params); + }); + + it('should return a RemoteData for the object with the given id', () => { + const result = service.create(resourcePolicy, resourceUUID, epersonUUID); + const expected = cold('a|', { + a: resourcePolicyRD + }); + expect(result).toBeObservable(expected); + }); + }); + + describe('delete', () => { + beforeEach(async(() => { + scheduler = getTestScheduler(); + responseCacheEntry.completed = true; + requestService = jasmine.createSpyObj('requestService', { + configure: {}, + getByHref: observableOf(responseCacheEntry), + getByUUID: hot('a', { a: responseCacheEntry }), + generateRequestId: 'request-id', + removeByHrefSubstring: {} + }); + })); + + it('should proxy the call to dataservice.create', () => { + scheduler.schedule(() => service.delete(resourcePolicyId)); + scheduler.flush(); + + expect((service as any).dataService.delete).toHaveBeenCalledWith(resourcePolicyId); + }); + }); + describe('findById', () => { it('should proxy the call to dataservice.findById', () => { scheduler.schedule(() => service.findById(resourcePolicyId)); diff --git a/src/app/core/resource-policy/resource-policy.service.ts b/src/app/core/resource-policy/resource-policy.service.ts index aef407e9e1..44938ec6a3 100644 --- a/src/app/core/resource-policy/resource-policy.service.ts +++ b/src/app/core/resource-policy/resource-policy.service.ts @@ -69,6 +69,40 @@ export class ResourcePolicyService { this.dataService = new DataServiceImpl(requestService, rdbService, null, objectCache, halService, notificationsService, http, comparator); } + /** + * Create a new ResourcePolicy on the server, and store the response + * in the object cache + * + * @param {ResourcePolicy} resourcePolicy + * The resource policy to create + * @param {string} resourceUUID + * The uuid of the resource target of the policy + * @param {string} epersonUUID + * The uuid of the eperson that will be grant of the permission. Exactly one of eperson or group is required + * @param {string} groupUUID + * The uuid of the group that will be grant of the permission. Exactly one of eperson or group is required + */ + create(resourcePolicy: ResourcePolicy, resourceUUID: string, epersonUUID?: string, groupUUID?: string): Observable> { + const params = []; + params.push(new RequestParam('resource', resourceUUID)); + if (isNotEmpty(epersonUUID)) { + params.push(new RequestParam('eperson', epersonUUID)); + } else if (isNotEmpty(groupUUID)) { + params.push(new RequestParam('group', groupUUID)); + } + return this.dataService.create(resourcePolicy, ...params); + } + + /** + * Delete an existing ResourcePolicy on the server + * + * @param resourcePolicyID The resource policy's id to be removed + * @return an observable that emits true when the deletion was successful, false when it failed + */ + delete(resourcePolicyID: string): Observable { + return this.dataService.delete(resourcePolicyID); + } + /** * Returns an observable of {@link RemoteData} of a {@link ResourcePolicy}, based on an href, with a list of {@link FollowLinkConfig}, * to automatically resolve {@link HALLink}s of the {@link ResourcePolicy}