Skip to content

Commit cd2369d

Browse files
Micheleboychukvins01-4science
authored andcommitted
Merged in task/dspace-cris-2023_02_x/DSC-2385 (pull request DSpace#3448)
[DSC-2385] refactoring deleting implementation to use batch process Approved-by: Vincenzo Mecca
2 parents 54440c1 + 423321c commit cd2369d

File tree

13 files changed

+337
-139
lines changed

13 files changed

+337
-139
lines changed

src/app/collection-page/delete-collection-page/delete-collection-page.component.spec.ts

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,52 @@ import { RouterTestingModule } from '@angular/router/testing';
66
import { NO_ERRORS_SCHEMA } from '@angular/core';
77
import { SharedModule } from '../../shared/shared.module';
88
import { of as observableOf } from 'rxjs';
9-
import { NotificationsService } from '../../shared/notifications/notifications.service';
109
import { DeleteCollectionPageComponent } from './delete-collection-page.component';
1110
import { CollectionDataService } from '../../core/data/collection-data.service';
12-
import { RequestService } from '../../core/data/request.service';
13-
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
11+
import {
12+
DSPACE_OBJECT_DELETION_SCRIPT_NAME,
13+
ScriptDataService,
14+
} from '../../core/data/processes/script-data.service';
15+
import { Collection } from '../../core/shared/collection.model';
16+
import { ProcessParameter } from '../../process-page/processes/process-parameter.model';
1417
import { DSONameServiceMock } from '../../shared/mocks/dso-name.service.mock';
18+
import { NotificationsService } from '../../shared/notifications/notifications.service';
19+
import {
20+
createFailedRemoteDataObject$,
21+
createSuccessfulRemoteDataObject$,
22+
} from '../../shared/remote-data.utils';
23+
import { NotificationsServiceStub } from '../../shared/testing/notifications-service.stub';
24+
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
1525

1626
describe('DeleteCollectionPageComponent', () => {
27+
28+
let scriptService;
1729
let comp: DeleteCollectionPageComponent;
30+
let notificationService: NotificationsServiceStub;
1831
let fixture: ComponentFixture<DeleteCollectionPageComponent>;
1932

33+
const mockCollection: Collection = Object.assign(new Collection(), {
34+
uuid: 'test-uuid',
35+
id: 'test-uuid',
36+
name: 'Test Collection',
37+
type: 'collection',
38+
});
39+
2040
beforeEach(waitForAsync(() => {
41+
notificationService = new NotificationsServiceStub();
42+
scriptService = jasmine.createSpyObj('scriptService', {
43+
invoke: createSuccessfulRemoteDataObject$({ processId: '123' }),
44+
});
45+
2146
TestBed.configureTestingModule({
2247
imports: [TranslateModule.forRoot(), SharedModule, CommonModule, RouterTestingModule],
2348
declarations: [DeleteCollectionPageComponent],
2449
providers: [
2550
{ provide: DSONameService, useValue: new DSONameServiceMock() },
2651
{ provide: CollectionDataService, useValue: {} },
2752
{ provide: ActivatedRoute, useValue: { data: observableOf({ dso: { payload: {} } }) } },
28-
{ provide: NotificationsService, useValue: {} },
29-
{ provide: RequestService, useValue: {} }
53+
{ provide: NotificationsService, useValue: notificationService },
54+
{ provide: ScriptDataService, useValue: scriptService },
3055
],
3156
schemas: [NO_ERRORS_SCHEMA]
3257
}).compileComponents();
@@ -38,9 +63,37 @@ describe('DeleteCollectionPageComponent', () => {
3863
fixture.detectChanges();
3964
});
4065

41-
describe('frontendURL', () => {
42-
it('should have the right frontendURL set', () => {
43-
expect((comp as any).frontendURL).toEqual('/collections/');
66+
it('should create', () => {
67+
expect(comp).toBeTruthy();
68+
});
69+
70+
it('should have the right frontendURL set', () => {
71+
expect((comp as any).frontendURL).toEqual('/collections/');
72+
});
73+
74+
describe('onConfirm', () => {
75+
it('should invoke the deletion script with correct params, show success notification and redirect on success', (done) => {
76+
const parameterValues: ProcessParameter[] = [
77+
Object.assign(new ProcessParameter(), { name: '-i', value: mockCollection.uuid }),
78+
];
79+
(scriptService.invoke as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$({ processId: '123' }));
80+
comp.onConfirm(mockCollection);
81+
setTimeout(() => {
82+
expect(scriptService.invoke).toHaveBeenCalledWith(DSPACE_OBJECT_DELETION_SCRIPT_NAME, parameterValues, []);
83+
expect(notificationService.success).toHaveBeenCalledWith('collection.delete.notification.success');
84+
expect(notificationService.process).toHaveBeenCalledWith('123', 5000, jasmine.any(Object));
85+
done();
86+
}, 0);
87+
});
88+
89+
it('error notification is shown', (done) => {
90+
(scriptService.invoke as jasmine.Spy).and.returnValue(createFailedRemoteDataObject$('Error', 500));
91+
comp.onConfirm(mockCollection);
92+
setTimeout(() => {
93+
expect(notificationService.error).toHaveBeenCalledWith('collection.delete.notification.fail');
94+
done();
95+
}, 0);
4496
});
4597
});
98+
4699
});

src/app/collection-page/delete-collection-page/delete-collection-page.component.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import { Component } from '@angular/core';
2-
import { ActivatedRoute, Router } from '@angular/router';
3-
import { DeleteComColPageComponent } from '../../shared/comcol/comcol-forms/delete-comcol-page/delete-comcol-page.component';
4-
import { NotificationsService } from '../../shared/notifications/notifications.service';
2+
import {
3+
ActivatedRoute,
4+
Router,
5+
} from '@angular/router';
6+
import {
7+
TranslateService,
8+
} from '@ngx-translate/core';
9+
10+
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
511
import { CollectionDataService } from '../../core/data/collection-data.service';
12+
import { ScriptDataService } from '../../core/data/processes/script-data.service';
613
import { Collection } from '../../core/shared/collection.model';
7-
import { TranslateService } from '@ngx-translate/core';
8-
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
14+
import { DeleteComColPageComponent } from '../../shared/comcol/comcol-forms/delete-comcol-page/delete-comcol-page.component';
15+
import { NotificationsService } from '../../shared/notifications/notifications.service';
916

1017
/**
1118
* Component that represents the page where a user can delete an existing Collection
@@ -25,7 +32,8 @@ export class DeleteCollectionPageComponent extends DeleteComColPageComponent<Col
2532
protected route: ActivatedRoute,
2633
protected notifications: NotificationsService,
2734
protected translate: TranslateService,
35+
protected scriptDataService: ScriptDataService,
2836
) {
29-
super(dsoDataService, dsoNameService, router, route, notifications, translate);
37+
super(dsoDataService, dsoNameService, router, route, notifications, translate, scriptDataService);
3038
}
3139
}
Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,66 @@
11
import { CommonModule } from '@angular/common';
22
import { NO_ERRORS_SCHEMA } from '@angular/core';
3-
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
4-
import { ActivatedRoute } from '@angular/router';
3+
import {
4+
ComponentFixture,
5+
TestBed,
6+
waitForAsync,
7+
} from '@angular/core/testing';
8+
import {
9+
ActivatedRoute,
10+
Router,
11+
} from '@angular/router';
512
import { RouterTestingModule } from '@angular/router/testing';
613
import { TranslateModule } from '@ngx-translate/core';
7-
import { of as observableOf } from 'rxjs';
8-
import { CommunityDataService } from '../../core/data/community-data.service';
14+
import { of } from 'rxjs';
15+
16+
import {
17+
DSPACE_OBJECT_DELETION_SCRIPT_NAME,
18+
ScriptDataService,
19+
} from '../../core/data/processes/script-data.service';
20+
import { Community } from '../../core/shared/community.model';
21+
import { ProcessParameter } from '../../process-page/processes/process-parameter.model';
922
import { NotificationsService } from '../../shared/notifications/notifications.service';
10-
import { SharedModule } from '../../shared/shared.module';
23+
import {
24+
createFailedRemoteDataObject$,
25+
createSuccessfulRemoteDataObject$,
26+
} from '../../shared/remote-data.utils';
27+
import { NotificationsServiceStub } from '../../shared/testing/notifications-service.stub';
1128
import { DeleteCommunityPageComponent } from './delete-community-page.component';
12-
import { RequestService } from '../../core/data/request.service';
1329
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
1430
import { DSONameServiceMock } from '../../shared/mocks/dso-name.service.mock';
31+
import { SharedModule } from '../../shared/shared.module';
32+
import { CommunityDataService } from '../../core/data/community-data.service';
1533

1634
describe('DeleteCommunityPageComponent', () => {
1735
let comp: DeleteCommunityPageComponent;
1836
let fixture: ComponentFixture<DeleteCommunityPageComponent>;
37+
let scriptService;
38+
let notificationService: NotificationsServiceStub;
39+
let router;
40+
41+
const mockCommunity: Community = Object.assign(new Community(), {
42+
uuid: 'test-uuid',
43+
id: 'test-uuid',
44+
name: 'Test Community',
45+
type: 'community',
46+
});
1947

2048
beforeEach(waitForAsync(() => {
49+
notificationService = new NotificationsServiceStub();
50+
scriptService = jasmine.createSpyObj('scriptService', {
51+
invoke: createSuccessfulRemoteDataObject$({ processId: '123' }),
52+
});
53+
router = jasmine.createSpyObj('router', ['navigateByUrl', 'navigate']);
2154
TestBed.configureTestingModule({
2255
imports: [TranslateModule.forRoot(), SharedModule, CommonModule, RouterTestingModule],
2356
declarations: [DeleteCommunityPageComponent],
2457
providers: [
2558
{ provide: DSONameService, useValue: new DSONameServiceMock() },
2659
{ provide: CommunityDataService, useValue: {} },
27-
{ provide: ActivatedRoute, useValue: { data: observableOf({ dso: { payload: {} } }) } },
28-
{ provide: NotificationsService, useValue: {} },
29-
{ provide: RequestService, useValue: {}}
60+
{ provide: ActivatedRoute, useValue: { data: of({ dso: { payload: mockCommunity } }) } },
61+
{ provide: NotificationsService, useValue: notificationService },
62+
{ provide: ScriptDataService, useValue: scriptService },
63+
{ provide: Router, useValue: router },
3064
],
3165
schemas: [NO_ERRORS_SCHEMA]
3266
}).compileComponents();
@@ -38,9 +72,37 @@ describe('DeleteCommunityPageComponent', () => {
3872
fixture.detectChanges();
3973
});
4074

41-
describe('frontendURL', () => {
42-
it('should have the right frontendURL set', () => {
43-
expect((comp as any).frontendURL).toEqual('/communities/');
75+
it('should create', () => {
76+
expect(comp).toBeTruthy();
77+
});
78+
79+
it('should have the right frontendURL set', () => {
80+
expect((comp as any).frontendURL).toEqual('/communities/');
81+
});
82+
83+
describe('onConfirm', () => {
84+
it('should invoke the deletion script with correct params, show success notification and redirect on success', (done) => {
85+
const parameterValues: ProcessParameter[] = [
86+
Object.assign(new ProcessParameter(), { name: '-i', value: mockCommunity.uuid }),
87+
];
88+
(scriptService.invoke as jasmine.Spy).and.returnValue(createSuccessfulRemoteDataObject$({ processId: '123' }));
89+
comp.onConfirm(mockCommunity);
90+
setTimeout(() => {
91+
expect(scriptService.invoke).toHaveBeenCalledWith(DSPACE_OBJECT_DELETION_SCRIPT_NAME, parameterValues, []);
92+
expect(notificationService.success).toHaveBeenCalledWith('community.delete.notification.success');
93+
expect(notificationService.process).toHaveBeenCalledWith('123', 5000, jasmine.any(Object));
94+
done();
95+
}, 0);
96+
});
97+
98+
it('error notification is shown', (done) => {
99+
(scriptService.invoke as jasmine.Spy).and.returnValue(createFailedRemoteDataObject$('Error', 500));
100+
comp.onConfirm(mockCommunity);
101+
setTimeout(() => {
102+
expect(notificationService.error).toHaveBeenCalledWith('community.delete.notification.fail');
103+
done();
104+
}, 0);
44105
});
45106
});
107+
46108
});

src/app/community-page/delete-community-page/delete-community-page.component.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import { Component } from '@angular/core';
2-
import { Community } from '../../core/shared/community.model';
2+
import {
3+
ActivatedRoute,
4+
Router,
5+
} from '@angular/router';
6+
import {
7+
TranslateService,
8+
} from '@ngx-translate/core';
9+
10+
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
311
import { CommunityDataService } from '../../core/data/community-data.service';
4-
import { ActivatedRoute, Router } from '@angular/router';
12+
import { ScriptDataService } from '../../core/data/processes/script-data.service';
13+
import { Community } from '../../core/shared/community.model';
514
import { DeleteComColPageComponent } from '../../shared/comcol/comcol-forms/delete-comcol-page/delete-comcol-page.component';
615
import { NotificationsService } from '../../shared/notifications/notifications.service';
7-
import { TranslateService } from '@ngx-translate/core';
8-
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
916

1017
/**
1118
* Component that represents the page where a user can delete an existing Community
@@ -25,8 +32,9 @@ export class DeleteCommunityPageComponent extends DeleteComColPageComponent<Comm
2532
protected route: ActivatedRoute,
2633
protected notifications: NotificationsService,
2734
protected translate: TranslateService,
35+
protected scriptDataService: ScriptDataService,
2836
) {
29-
super(dsoDataService, dsoNameService, router, route, notifications, translate);
37+
super(dsoDataService, dsoNameService, router, route, notifications, translate, scriptDataService);
3038
}
3139

3240
}

src/app/core/data/processes/script-data.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export const BATCH_IMPORT_SCRIPT_NAME = 'import';
2929
export const BATCH_EXPORT_SCRIPT_NAME = 'export';
3030
export const ITEM_EXPORT_SCRIPT_NAME = 'item-export';
3131
export const BULK_ITEM_EXPORT_SCRIPT_NAME = 'bulk-item-export';
32+
export const DSPACE_OBJECT_DELETION_SCRIPT_NAME = 'dspace-object-deletion';
3233

3334
@Injectable()
3435
@dataService(SCRIPT)

src/app/item-page/edit-item-page/item-delete/item-delete.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ <h5 (click)="setSelected(typeDto.relationshipType, !selected)">
8787

8888
<div class="space-children-mr">
8989
<button [dsBtnDisabled]="isDeleting$ | async" (click)="performAction()"
90-
class="btn btn-outline-secondary perform-action">{{confirmMessage | translate}}
90+
class="btn btn-danger perform-action">{{confirmMessage | translate}}
9191
</button>
9292
<button [dsBtnDisabled]="isDeleting$ | async" [routerLink]="[itemPageRoute, 'edit']"
9393
class="btn btn-outline-secondary cancel">

0 commit comments

Comments
 (0)