Skip to content

Commit a936878

Browse files
committed
101289: tests
1 parent c9324b0 commit a936878

4 files changed

Lines changed: 163 additions & 5 deletions

File tree

src/app/bitstream-page/edit-bitstream-page/edit-bitstream-page.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { createPaginatedList } from '../../shared/testing/utils.test';
2424
import { Item } from '../../core/shared/item.model';
2525
import { MetadataValueFilter } from '../../core/shared/metadata.models';
2626
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
27-
import { PrimaryBitstreamService } from '../../core/data/primary-bitstream-data.service';
27+
import { PrimaryBitstreamService } from '../../core/data/primary-bitstream.service';
2828

2929
const infoNotification: INotification = new Notification('id', NotificationType.Info, 'info');
3030
const warningNotification: INotification = new Notification('id', NotificationType.Warning, 'warning');

src/app/bitstream-page/edit-bitstream-page/edit-bitstream-page.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
2525
import { Item } from '../../core/shared/item.model';
2626
import { DsDynamicInputModel } from '../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model';
2727
import { DsDynamicTextAreaModel } from '../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-textarea.model';
28-
import { PrimaryBitstreamService } from '../../core/data/primary-bitstream-data.service';
28+
import { PrimaryBitstreamService } from '../../core/data/primary-bitstream.service';
2929

3030
@Component({
3131
selector: 'ds-edit-bitstream-page',
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
});

src/app/core/data/primary-bitstream-data.service.ts renamed to src/app/core/data/primary-bitstream.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class PrimaryBitstreamService {
9797
}
9898

9999
/**
100-
* Update an exiting primaryBitstream
100+
* Update an existing primaryBitstream
101101
*
102102
* @param primaryBitstream The object to update
103103
* @param bundle The bundle to update it on
@@ -111,7 +111,7 @@ export class PrimaryBitstreamService {
111111
}
112112

113113
/**
114-
* Delete an exiting primaryBitstream
114+
* Delete an existing primaryBitstream
115115
*
116116
* @param bundle The bundle to delete it from
117117
*/
@@ -125,7 +125,7 @@ export class PrimaryBitstreamService {
125125
if (rd.hasSucceeded) {
126126
return this.bundleDataService.findByHref(bundle.self, false);
127127
} else {
128-
[bundle];
128+
return this.rdbService.buildSingle<Bundle>(bundle.self);
129129
}
130130
})
131131
);

0 commit comments

Comments
 (0)