|
| 1 | +import { ObjectCacheService } from '../cache/object-cache.service'; |
| 2 | +import { RequestService } from './request.service'; |
| 3 | +import { Bitstream } from '../shared/bitstream.model'; |
| 4 | +import { HALEndpointService } from '../shared/hal-endpoint.service'; |
| 5 | +import { getMockRequestService } from '../../shared/mocks/request.service.mock'; |
| 6 | +import { HALEndpointServiceStub } from '../../shared/testing/hal-endpoint-service.stub'; |
| 7 | +import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service'; |
| 8 | +import { getMockRemoteDataBuildService } from '../../shared/mocks/remote-data-build.service.mock'; |
| 9 | +import { PrimaryBitstreamService } from './primary-bitstream.service'; |
| 10 | +import { BundleDataService } from './bundle-data.service'; |
| 11 | +import { NotificationsService } from '../../shared/notifications/notifications.service'; |
| 12 | +import { NotificationsServiceStub } from '../../shared/testing/notifications-service.stub'; |
| 13 | +import { CreateRequest, DeleteRequest, PostRequest, PutRequest } from './request.models'; |
| 14 | +import { createFailedRemoteDataObject, createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils'; |
| 15 | +import { Bundle } from '../shared/bundle.model'; |
| 16 | +import { getTestScheduler } from 'jasmine-marbles'; |
| 17 | +import { of as observableOf } from 'rxjs'; |
| 18 | + |
| 19 | +fdescribe('PrimaryBitstreamService', () => { |
| 20 | + let service: PrimaryBitstreamService; |
| 21 | + let objectCache: ObjectCacheService; |
| 22 | + let requestService: RequestService; |
| 23 | + let halService: HALEndpointService; |
| 24 | + let rdbService: RemoteDataBuildService; |
| 25 | + let notificationService: NotificationsService; |
| 26 | + let bundleDataService: BundleDataService; |
| 27 | + |
| 28 | + const bitstream = Object.assign(new Bitstream(), { |
| 29 | + uuid: 'fake-bitstream', |
| 30 | + _links: { |
| 31 | + self: { href: 'fake-bitstream-self' } |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + const bundle = Object.assign(new Bundle(), { |
| 36 | + uuid: 'fake-bundle', |
| 37 | + _links: { |
| 38 | + self: { href: 'fake-bundle-self' }, |
| 39 | + primaryBitstream: { href: 'fake-primary-bitstream-self' }, |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + const url = 'fake-bitstream-url'; |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + objectCache = jasmine.createSpyObj('objectCache', { |
| 47 | + remove: jasmine.createSpy('remove') |
| 48 | + }); |
| 49 | + requestService = getMockRequestService(); |
| 50 | + halService = Object.assign(new HALEndpointServiceStub(url)); |
| 51 | + |
| 52 | + rdbService = getMockRemoteDataBuildService(); |
| 53 | + notificationService = new NotificationsServiceStub() as any; |
| 54 | + bundleDataService = jasmine.createSpyObj('bundleDataService', {'findByHref': createSuccessfulRemoteDataObject$(bundle)}); |
| 55 | + service = new PrimaryBitstreamService(requestService, rdbService, objectCache, halService, notificationService, bundleDataService); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('getHttpOptions', () => { |
| 59 | + it('should return a HttpOptions object with text/url-list Context-Type header', () => { |
| 60 | + const result = (service as any).getHttpOptions() |
| 61 | + expect(result.headers.get('Content-Type')).toEqual('text/uri-list'); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('createAndSendRequest', () => { |
| 66 | + const testId = '12345-12345'; |
| 67 | + const options = {}; |
| 68 | + const testResult = createSuccessfulRemoteDataObject(new Bundle()); |
| 69 | + |
| 70 | + beforeEach(() => { |
| 71 | + spyOn(service as any, 'getHttpOptions').and.returnValue(options); |
| 72 | + (requestService.generateRequestId as jasmine.Spy<any>).and.returnValue(testId); |
| 73 | + spyOn(rdbService, 'buildFromRequestUUID').and.returnValue(observableOf(testResult)); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should return a Request object with the given constructor and the given parameters', () => { |
| 77 | + const result = (service as any).createAndSendRequest(CreateRequest, url, bitstream.self); |
| 78 | + const request = new CreateRequest(testId, url, bitstream.self, options); |
| 79 | + getTestScheduler().expectObservable(result).toBe('(a|)', { a: testResult }); |
| 80 | + |
| 81 | + expect(requestService.send).toHaveBeenCalledWith(request); |
| 82 | + expect(rdbService.buildFromRequestUUID).toHaveBeenCalledWith(testId); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('create', () => { |
| 87 | + const testResult = createSuccessfulRemoteDataObject(new Bundle()); |
| 88 | + beforeEach(() => { |
| 89 | + spyOn((service as any), 'createAndSendRequest').and.returnValue(observableOf(testResult)); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should delegate the call to createAndSendRequest', () => { |
| 93 | + const result = service.create(bitstream, bundle); |
| 94 | + getTestScheduler().expectObservable(result).toBe('(a|)', { a: testResult }); |
| 95 | + |
| 96 | + expect((service as any).createAndSendRequest).toHaveBeenCalledWith( |
| 97 | + PostRequest, |
| 98 | + bundle._links.primaryBitstream.href, |
| 99 | + bitstream.self |
| 100 | + ); |
| 101 | + }); |
| 102 | + }); |
| 103 | + describe('put', () => { |
| 104 | + const testResult = createSuccessfulRemoteDataObject(new Bundle()); |
| 105 | + beforeEach(() => { |
| 106 | + spyOn((service as any), 'createAndSendRequest').and.returnValue(observableOf(testResult)); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should delegate the call to createAndSendRequest and return the requested bundle', () => { |
| 110 | + const result = service.put(bitstream, bundle); |
| 111 | + getTestScheduler().expectObservable(result).toBe('(a|)', { a: testResult }); |
| 112 | + |
| 113 | + expect((service as any).createAndSendRequest).toHaveBeenCalledWith( |
| 114 | + PutRequest, |
| 115 | + bundle._links.primaryBitstream.href, |
| 116 | + bitstream.self |
| 117 | + ); |
| 118 | + }); |
| 119 | + }); |
| 120 | + describe('delete', () => { |
| 121 | + describe('when the delete request succeeds', () => { |
| 122 | + const testResult = createSuccessfulRemoteDataObject(new Bundle()); |
| 123 | + const bundleServiceResult = createSuccessfulRemoteDataObject(bundle); |
| 124 | + |
| 125 | + beforeEach(() => { |
| 126 | + spyOn((service as any), 'createAndSendRequest').and.returnValue(observableOf(testResult)); |
| 127 | + (bundleDataService.findByHref as jasmine.Spy<any>).and.returnValue(observableOf(bundleServiceResult)); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should delegate the call to createAndSendRequest', () => { |
| 131 | + const result = service.delete(bundle); |
| 132 | + getTestScheduler().expectObservable(result).toBe('(a|)', { a: testResult }); |
| 133 | + |
| 134 | + expect((service as any).createAndSendRequest).toHaveBeenCalledWith( |
| 135 | + DeleteRequest, |
| 136 | + bundle._links.primaryBitstream.href, |
| 137 | + ); |
| 138 | + }); |
| 139 | + }); |
| 140 | + describe('when the delete request fails', () => { |
| 141 | + const testResult = createFailedRemoteDataObject(); |
| 142 | + |
| 143 | + beforeEach(() => { |
| 144 | + spyOn((service as any), 'createAndSendRequest').and.returnValue(observableOf(testResult)); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should delegate the call to createAndSendRequest and retrieve the bundle from the rdbService', () => { |
| 148 | + const result = service.delete(bundle); |
| 149 | + getTestScheduler().expectObservable(result).toBe('(a|)', { a: bundle }); |
| 150 | + |
| 151 | + expect((service as any).createAndSendRequest).toHaveBeenCalledWith( |
| 152 | + DeleteRequest, |
| 153 | + bundle._links.primaryBitstream.href, |
| 154 | + ); |
| 155 | + }); |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments