|
| 1 | +import { EventEmitter } from '@angular/core'; |
| 2 | +import { TranslateService } from '@ngx-translate/core'; |
| 3 | +import { of } from 'rxjs'; |
| 4 | + |
| 5 | +import { RequestParam } from '../cache/models/request-param.model'; |
| 6 | +import { |
| 7 | + SortDirection, |
| 8 | + SortOptions, |
| 9 | +} from '../cache/models/sort-options.model'; |
| 10 | +import { PaginatedList } from '../data/paginated-list.model'; |
| 11 | +import { |
| 12 | + BULK_ITEM_EXPORT_SCRIPT_NAME, |
| 13 | + ITEM_EXPORT_SCRIPT_NAME, |
| 14 | + ScriptDataService, |
| 15 | +} from '../data/processes/script-data.service'; |
| 16 | +import { NotificationsService } from '../notification-system/notifications.service'; |
| 17 | +import { Process } from '../processes/process.model'; |
| 18 | +import { ProcessParameter } from '../processes/process-parameter.model'; |
| 19 | +import { PaginatedSearchOptions } from '../shared/search/models/paginated-search-options.model'; |
| 20 | +import { SearchFilter } from '../shared/search/models/search-filter.model'; |
| 21 | +import { NotificationsServiceStub } from '../testing/notifications-service.stub'; |
| 22 | +import { createPaginatedList } from '../testing/utils.test'; |
| 23 | +import { createSuccessfulRemoteDataObject$ } from '../utilities/remote-data.utils'; |
| 24 | +import { |
| 25 | + ItemExportFormatMolteplicity, |
| 26 | + ItemExportFormatService, |
| 27 | +} from './item-export-format.service'; |
| 28 | +import { ItemExportFormat } from './model/item-export-format.model'; |
| 29 | +import createSpyObj = jasmine.createSpyObj; |
| 30 | + |
| 31 | +const ItemExportFormatsMap = { |
| 32 | + Publication: [ |
| 33 | + Object.assign(new ItemExportFormat(), { id: 'publication-xml', entityType: 'Publication', molteplicity: 'MULTIPLE' }), |
| 34 | + Object.assign(new ItemExportFormat(), { id: 'publication-csv', entityType: 'Publication', molteplicity: 'MULTIPLE' }), |
| 35 | + ], |
| 36 | + Project: [ |
| 37 | + Object.assign(new ItemExportFormat(), { id: 'project-xml', entityType: 'Project', molteplicity: 'MULTIPLE' }), |
| 38 | + ], |
| 39 | +}; |
| 40 | + |
| 41 | +describe('ItemExportFormatService', () => { |
| 42 | + |
| 43 | + let service: ItemExportFormatService; |
| 44 | + const notificationsService: NotificationsService = new NotificationsServiceStub() as any; |
| 45 | + const translateService: TranslateService = { |
| 46 | + get: () => of('test-message'), |
| 47 | + onLangChange: new EventEmitter(), |
| 48 | + onTranslationChange: new EventEmitter(), |
| 49 | + onDefaultLangChange: new EventEmitter(), |
| 50 | + } as any; |
| 51 | + let scriptDataService: ScriptDataService; |
| 52 | + const TheProcess = Object.assign(new Process(), { |
| 53 | + processId: 1234, |
| 54 | + resourceSelfLinks: ['process/1234'], |
| 55 | + }); |
| 56 | + |
| 57 | + beforeEach(() => { |
| 58 | + scriptDataService = createSpyObj('scriptDataService', ['invoke']); |
| 59 | + service = new ItemExportFormatService( |
| 60 | + null, |
| 61 | + null, |
| 62 | + null, |
| 63 | + null, |
| 64 | + notificationsService, |
| 65 | + null, |
| 66 | + null, |
| 67 | + translateService, |
| 68 | + scriptDataService); |
| 69 | + }); |
| 70 | + |
| 71 | + describe('byEntityTypeAndMolteplicity', () => { |
| 72 | + |
| 73 | + beforeEach(() => { |
| 74 | + const searchResult: any = [...ItemExportFormatsMap.Publication, ...ItemExportFormatsMap.Project]; |
| 75 | + const paginatedList: PaginatedList<ItemExportFormat> = createPaginatedList(searchResult); |
| 76 | + spyOn((service as any).searchData, 'searchBy').and.returnValue(createSuccessfulRemoteDataObject$(paginatedList)); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should configure and call dataService.searchBy and map results by entityType', (done) => { |
| 80 | + const entityTypeId = 'Publication'; |
| 81 | + const molteplicity = ItemExportFormatMolteplicity.MULTIPLE; |
| 82 | + |
| 83 | + const searchParams = [ |
| 84 | + new RequestParam('molteplicity', molteplicity), |
| 85 | + new RequestParam('entityTypeId', entityTypeId), |
| 86 | + ]; |
| 87 | + |
| 88 | + service.byEntityTypeAndMolteplicity(entityTypeId, molteplicity).subscribe((result) => { |
| 89 | + expect((service as any).searchData.searchBy).toHaveBeenCalledWith('byEntityTypeAndMolteplicity', { searchParams, elementsPerPage: 100 }); |
| 90 | + expect(result).toEqual(ItemExportFormatsMap); |
| 91 | + done(); |
| 92 | + }); |
| 93 | + |
| 94 | + expect(service).toBeTruthy(); |
| 95 | + }); |
| 96 | + |
| 97 | + }); |
| 98 | + |
| 99 | + describe('doExport', () => { |
| 100 | + |
| 101 | + beforeEach(() => { |
| 102 | + (scriptDataService as any).invoke.and.returnValue(createSuccessfulRemoteDataObject$(TheProcess)); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should invoke a configured single item export', (done) => { |
| 106 | + const format = Object.assign(new ItemExportFormat(), { id: 'publication-xml' }); |
| 107 | + const uuid = 'itemUUID'; |
| 108 | + const expectedParameters = [ |
| 109 | + Object.assign(new ProcessParameter(), { name: '-i', value: 'itemUUID' }), |
| 110 | + Object.assign(new ProcessParameter(), { name: '-f', value: 'publication-xml' }), |
| 111 | + ]; |
| 112 | + service.doExport(uuid, format).subscribe((result) => { |
| 113 | + expect(result).toEqual(1234); |
| 114 | + expect((service as any).scriptDataService.invoke).toHaveBeenCalledWith(ITEM_EXPORT_SCRIPT_NAME, expectedParameters, []); |
| 115 | + done(); |
| 116 | + }); |
| 117 | + |
| 118 | + }); |
| 119 | + |
| 120 | + }); |
| 121 | + |
| 122 | + describe('doExportMulti', () => { |
| 123 | + |
| 124 | + beforeEach(() => { |
| 125 | + (scriptDataService as any).invoke.and.returnValue(createSuccessfulRemoteDataObject$(TheProcess)); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should invoke a configured bulk item export', (done) => { |
| 129 | + const entityType = 'Publication'; |
| 130 | + const format = Object.assign(new ItemExportFormat(), { id: 'publication-xml' }); |
| 131 | + const searchOptions = new PaginatedSearchOptions({ |
| 132 | + query: 'queryX', |
| 133 | + filters: [ |
| 134 | + new SearchFilter('f.name', ['nameX', 'nameY']), |
| 135 | + new SearchFilter('f.type', ['typeX']), |
| 136 | + new SearchFilter('other.name', ['nameY']), |
| 137 | + ], |
| 138 | + fixedFilter: 'scope=scopeX', |
| 139 | + configuration: 'configurationX', |
| 140 | + sort: new SortOptions('fieldX', SortDirection.ASC), |
| 141 | + }); |
| 142 | + |
| 143 | + const expectedParameters = [ |
| 144 | + Object.assign(new ProcessParameter(), { name: '-f', value: 'publication-xml' }), |
| 145 | + Object.assign(new ProcessParameter(), { name: '-t', value: 'Publication' }), |
| 146 | + Object.assign(new ProcessParameter(), { name: '-q', value: 'queryX' }), |
| 147 | + Object.assign(new ProcessParameter(), { name: '-sf', value: 'name=nameX&name=nameY&type=typeX' }), |
| 148 | + Object.assign(new ProcessParameter(), { name: '-s', value: 'scopeX' }), |
| 149 | + Object.assign(new ProcessParameter(), { name: '-c', value: 'configurationX' }), |
| 150 | + Object.assign(new ProcessParameter(), { name: '-so', value: 'fieldX,ASC' }), |
| 151 | + ]; |
| 152 | + |
| 153 | + service.doExportMulti(entityType, format, searchOptions).subscribe((result) => { |
| 154 | + expect(result).toEqual(1234); |
| 155 | + expect((service as any).scriptDataService.invoke).toHaveBeenCalledWith(BULK_ITEM_EXPORT_SCRIPT_NAME, expectedParameters, []); |
| 156 | + done(); |
| 157 | + }); |
| 158 | + |
| 159 | + }); |
| 160 | + |
| 161 | + it('should invoke a configured bulk item export with a list', (done) => { |
| 162 | + const entityType = 'Publication'; |
| 163 | + const format = Object.assign(new ItemExportFormat(), { id: 'publication-xml' }); |
| 164 | + const searchOptions = new PaginatedSearchOptions({ |
| 165 | + query: 'queryX', |
| 166 | + filters: [ |
| 167 | + new SearchFilter('f.name', ['nameX']), |
| 168 | + new SearchFilter('f.type', ['typeX']), |
| 169 | + new SearchFilter('other.name', ['nameY']), |
| 170 | + ], |
| 171 | + fixedFilter: 'scope=scopeX', |
| 172 | + configuration: 'configurationX', |
| 173 | + sort: new SortOptions('fieldX', SortDirection.ASC), |
| 174 | + }); |
| 175 | + |
| 176 | + const expectedParameters = [ |
| 177 | + Object.assign(new ProcessParameter(), { name: '-f', value: 'publication-xml' }), |
| 178 | + Object.assign(new ProcessParameter(), { name: '-si', value: 'item1;item2' }), |
| 179 | + ]; |
| 180 | + |
| 181 | + service.doExportMulti(entityType, format, searchOptions, ['item1', 'item2']).subscribe((result) => { |
| 182 | + expect(result).toEqual(1234); |
| 183 | + expect((service as any).scriptDataService.invoke).toHaveBeenCalledWith(BULK_ITEM_EXPORT_SCRIPT_NAME, expectedParameters, []); |
| 184 | + done(); |
| 185 | + }); |
| 186 | + |
| 187 | + }); |
| 188 | + |
| 189 | + }); |
| 190 | + |
| 191 | +}); |
0 commit comments