diff --git a/src/app/bitstream-page/edit-bitstream-page/edit-bitstream-page.component.spec.ts b/src/app/bitstream-page/edit-bitstream-page/edit-bitstream-page.component.spec.ts index 04ab4e47b0..3be3297880 100644 --- a/src/app/bitstream-page/edit-bitstream-page/edit-bitstream-page.component.spec.ts +++ b/src/app/bitstream-page/edit-bitstream-page/edit-bitstream-page.component.spec.ts @@ -24,8 +24,7 @@ import { createPaginatedList } from '../../shared/testing/utils.test'; import { Item } from '../../core/shared/item.model'; import { MetadataValueFilter } from '../../core/shared/metadata.models'; import { DSONameService } from '../../core/breadcrumbs/dso-name.service'; -import { Bundle } from '../../core/shared/bundle.model'; -import { BundleDataService } from '../../core/data/bundle-data.service'; +import { PrimaryBitstreamService } from '../../core/data/primary-bitstream-data.service'; const infoNotification: INotification = new Notification('id', NotificationType.Info, 'info'); const warningNotification: INotification = new Notification('id', NotificationType.Warning, 'warning'); @@ -34,6 +33,7 @@ const successNotification: INotification = new Notification('id', NotificationTy let notificationsService: NotificationsService; let formService: DynamicFormService; let bitstreamService: BitstreamDataService; +let primaryBitstreamService: PrimaryBitstreamService; let bitstreamFormatService: BitstreamFormatDataService; let dsoNameService: DSONameService; let bitstream: Bitstream; @@ -41,23 +41,19 @@ let bitstreamID: string; let selectedFormat: BitstreamFormat; let allFormats: BitstreamFormat[]; let router: Router; -let bundleDataService; -let bundleWithCurrentPrimary: Bundle; -let bundleWithDifferentPrimary: Bundle; -let bundleWithNoPrimary: Bundle; - +let currentPrimary: string; +let differentPrimary: string; +let bundle; let comp: EditBitstreamPageComponent; let fixture: ComponentFixture; -describe('EditBitstreamPageComponent', () => { +fdescribe('EditBitstreamPageComponent', () => { beforeEach(() => { bitstreamID = 'current-bitstream-id'; - bundleWithCurrentPrimary = Object.assign(new Bundle(), { 'primaryBitstreamUUID': bitstreamID }); - bundleWithDifferentPrimary = Object.assign(new Bundle(), { 'primaryBitstreamUUID': '12345-abcde-54321-edcba' }); - bundleWithNoPrimary = Object.assign(new Bundle(), { 'primaryBitstreamUUID': null }); - bundleDataService = jasmine.createSpyObj('BundleDataService', ['patch']); - bundleDataService.patch.and.callFake((a, b) => createSuccessfulRemoteDataObject$(a)); + currentPrimary = bitstreamID; + differentPrimary = '12345-abcde-54321-edcba'; + allFormats = [ Object.assign({ id: '1', @@ -116,12 +112,47 @@ describe('EditBitstreamPageComponent', () => { success: successNotification } ); + + bundle = { + _links: { + primaryBitstream: { + href: 'bundle-selflink' + } + }, + item: createSuccessfulRemoteDataObject$(Object.assign(new Item(), { + uuid: 'some-uuid', + firstMetadataValue(keyOrKeys: string | string[], valueFilter?: MetadataValueFilter): string { + return undefined; + }, + })) + }; + + const result = createSuccessfulRemoteDataObject$(bundle); + primaryBitstreamService = jasmine.createSpyObj('PrimaryBitstreamService', + { + put: result, + create: result, + delete: result, + }); + }); describe('EditBitstreamPageComponent no IIIF fields', () => { beforeEach(waitForAsync(() => { - + bundle = { + _links: { + primaryBitstream: { + href: 'bundle-selflink' + } + }, + item: createSuccessfulRemoteDataObject$(Object.assign(new Item(), { + uuid: 'some-uuid', + firstMetadataValue(keyOrKeys: string | string[], valueFilter?: MetadataValueFilter): string { + return undefined; + }, + })) + }; const bundleName = 'ORIGINAL'; bitstream = Object.assign(new Bitstream(), { @@ -143,17 +174,11 @@ describe('EditBitstreamPageComponent', () => { _links: { self: 'bitstream-selflink' }, - bundle: createSuccessfulRemoteDataObject$({ - item: createSuccessfulRemoteDataObject$(Object.assign(new Item(), { - uuid: 'some-uuid', - firstMetadataValue(keyOrKeys: string | string[], valueFilter?: MetadataValueFilter): string { - return undefined; - }, - })) - }) + bundle: createSuccessfulRemoteDataObject$(bundle) }); bitstreamService = jasmine.createSpyObj('bitstreamService', { findById: createSuccessfulRemoteDataObject$(bitstream), + findByHref: createSuccessfulRemoteDataObject$(bitstream), update: createSuccessfulRemoteDataObject$(bitstream), updateFormat: createSuccessfulRemoteDataObject$(bitstream), commitUpdates: {}, @@ -182,7 +207,7 @@ describe('EditBitstreamPageComponent', () => { { provide: BitstreamDataService, useValue: bitstreamService }, { provide: DSONameService, useValue: dsoNameService }, { provide: BitstreamFormatDataService, useValue: bitstreamFormatService }, - { provide: BundleDataService, useValue: bundleDataService }, + { provide: PrimaryBitstreamService, useValue: primaryBitstreamService }, ChangeDetectorRef ], schemas: [NO_ERRORS_SCHEMA] @@ -198,7 +223,7 @@ describe('EditBitstreamPageComponent', () => { spyOn(router, 'navigate'); }); - describe('on startup', () => { + fdescribe('on startup', () => { let rawForm; beforeEach(() => { @@ -222,7 +247,7 @@ describe('EditBitstreamPageComponent', () => { }); describe('when the bitstream is the primary bitstream on the bundle', () => { beforeEach(() => { - (comp as any).bundle = bundleWithCurrentPrimary; + (comp as any).primaryBitstreamUUID = currentPrimary; comp.setForm(); rawForm = comp.formGroup.getRawValue(); @@ -233,7 +258,7 @@ describe('EditBitstreamPageComponent', () => { }); describe('when the bitstream is not the primary bitstream on the bundle', () => { beforeEach(() => { - (comp as any).bundle = bundleWithDifferentPrimary; + (comp as any).primaryBitstreamUUID = differentPrimary; comp.setForm(); rawForm = comp.formGroup.getRawValue(); }); @@ -263,38 +288,23 @@ describe('EditBitstreamPageComponent', () => { describe('from a different primary bitstream', () => { beforeEach(() => { - (comp as any).bundle = bundleWithDifferentPrimary; + (comp as any).primaryBitstreamUUID = differentPrimary; comp.onSubmit(); }); - it('should call patch with a replace operation', () => { - expect(bundleDataService.patch).toHaveBeenCalledWith(bundleWithDifferentPrimary, [jasmine.objectContaining({ - op: 'replace' - })]); - }); - - it('should call patch with the correct bitstream uuid', () => { - expect(bundleDataService.patch).toHaveBeenCalledWith(bundleWithDifferentPrimary, [jasmine.objectContaining({ - value: bitstreamID - })]); + it('should call put with the correct bitstream on the PrimaryBitstreamService', () => { + expect(primaryBitstreamService.put).toHaveBeenCalledWith(jasmine.objectContaining({uuid: currentPrimary}), bundle); }); }); + describe('from no primary bitstream', () => { beforeEach(() => { - (comp as any).bundle = bundleWithNoPrimary; + (comp as any).primaryBitstreamUUID = null; comp.onSubmit(); }); - it('should call patch with an add operation', () => { - expect(bundleDataService.patch).toHaveBeenCalledWith(bundleWithNoPrimary, [jasmine.objectContaining({ - op: 'add' - })]); - }); - - it('should call patch with the correct bitstream uuid', () => { - expect(bundleDataService.patch).toHaveBeenCalledWith(bundleWithNoPrimary, [jasmine.objectContaining({ - value: bitstreamID - })]); + it('should call create with the correct bitstream on the PrimaryBitstreamService', () => { + expect(primaryBitstreamService.create).toHaveBeenCalledWith(jasmine.objectContaining({uuid: currentPrimary}), bundle); }); }); }); @@ -306,39 +316,42 @@ describe('EditBitstreamPageComponent', () => { describe('from the current bitstream', () => { beforeEach(() => { - (comp as any).bundle = bundleWithCurrentPrimary; + (comp as any).primaryBitstreamUUID = currentPrimary; comp.onSubmit(); }); - it('should call patch with a remove operation', () => { - expect(bundleDataService.patch).toHaveBeenCalledWith(bundleWithCurrentPrimary, [jasmine.objectContaining({ - op: 'remove' - })]); + it('should call delete on the PrimaryBitstreamService', () => { + expect(primaryBitstreamService.delete).toHaveBeenCalledWith(jasmine.objectContaining(bundle)); }); }); }); }); - describe('when the primaryBitstream did not changed', () => { + describe('when the primaryBitstream did not change', () => { describe('the current bitstream stayed the primary bitstream', () => { beforeEach(() => { const rawValue = Object.assign(comp.formGroup.getRawValue(), { fileNamePrimaryContainer: { primaryBitstream: true } }); spyOn(comp.formGroup, 'getRawValue').and.returnValue(rawValue); - (comp as any).bundle = bundleWithCurrentPrimary; + (comp as any).primaryBitstreamUUID = currentPrimary; comp.onSubmit(); }); - it('should not call patch on the bundle data service', () => { - expect(bundleDataService.patch).not.toHaveBeenCalled(); + it('should not call anything on the PrimaryBitstreamService', () => { + expect(primaryBitstreamService.put).not.toHaveBeenCalled(); + expect(primaryBitstreamService.delete).not.toHaveBeenCalled(); + expect(primaryBitstreamService.create).not.toHaveBeenCalled(); }); }); + describe('the bitstream was not and did not become the primary bitstream', () => { beforeEach(() => { const rawValue = Object.assign(comp.formGroup.getRawValue(), { fileNamePrimaryContainer: { primaryBitstream: false } }); spyOn(comp.formGroup, 'getRawValue').and.returnValue(rawValue); - (comp as any).bundle = bundleWithDifferentPrimary; + (comp as any).primaryBitstreamUUID = differentPrimary; comp.onSubmit(); }); - it('should not call patch on the bundle data service', () => { - expect(bundleDataService.patch).not.toHaveBeenCalled(); + it('should not call anything on the PrimaryBitstreamService', () => { + expect(primaryBitstreamService.put).not.toHaveBeenCalled(); + expect(primaryBitstreamService.delete).not.toHaveBeenCalled(); + expect(primaryBitstreamService.create).not.toHaveBeenCalled(); }); }); }); @@ -451,6 +464,7 @@ describe('EditBitstreamPageComponent', () => { }); bitstreamService = jasmine.createSpyObj('bitstreamService', { findById: createSuccessfulRemoteDataObject$(bitstream), + findByHref: createSuccessfulRemoteDataObject$(bitstream), update: createSuccessfulRemoteDataObject$(bitstream), updateFormat: createSuccessfulRemoteDataObject$(bitstream), commitUpdates: {}, @@ -477,7 +491,7 @@ describe('EditBitstreamPageComponent', () => { {provide: BitstreamDataService, useValue: bitstreamService}, {provide: DSONameService, useValue: dsoNameService}, {provide: BitstreamFormatDataService, useValue: bitstreamFormatService}, - { provide: BundleDataService, useValue: bundleDataService }, + { provide: PrimaryBitstreamService, useValue: primaryBitstreamService }, ChangeDetectorRef ], schemas: [NO_ERRORS_SCHEMA] @@ -595,7 +609,7 @@ describe('EditBitstreamPageComponent', () => { {provide: BitstreamDataService, useValue: bitstreamService}, {provide: DSONameService, useValue: dsoNameService}, {provide: BitstreamFormatDataService, useValue: bitstreamFormatService}, - { provide: BundleDataService, useValue: bundleDataService }, + { provide: PrimaryBitstreamService, useValue: primaryBitstreamService }, ChangeDetectorRef ], schemas: [NO_ERRORS_SCHEMA] @@ -617,7 +631,7 @@ describe('EditBitstreamPageComponent', () => { rawForm = comp.formGroup.getRawValue(); }); - it('should NOT set isIIIF to true', () => { + it('should NOT set is IIIF to true', () => { expect(comp.isIIIF).toBeFalse(); }); it('should put the \"IIIF Label\" input not to be shown', () => { diff --git a/src/app/bitstream-page/edit-bitstream-page/edit-bitstream-page.component.ts b/src/app/bitstream-page/edit-bitstream-page/edit-bitstream-page.component.ts index 8b3dad3c1c..25c7f41868 100644 --- a/src/app/bitstream-page/edit-bitstream-page/edit-bitstream-page.component.ts +++ b/src/app/bitstream-page/edit-bitstream-page/edit-bitstream-page.component.ts @@ -1,7 +1,7 @@ import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, OnInit } from '@angular/core'; import { Bitstream } from '../../core/shared/bitstream.model'; import { ActivatedRoute, Router } from '@angular/router'; -import { map, switchMap, tap, filter } from 'rxjs/operators'; +import { filter, map, switchMap, tap } from 'rxjs/operators'; import { combineLatest, combineLatest as observableCombineLatest, Observable, of as observableOf, Subscription } from 'rxjs'; import { DynamicFormControlModel, DynamicFormGroupModel, DynamicFormLayout, DynamicFormService, DynamicInputModel, DynamicSelectModel } from '@ng-dynamic-forms/core'; import { UntypedFormGroup } from '@angular/forms'; @@ -14,7 +14,7 @@ import { NotificationsService } from '../../shared/notifications/notifications.s import { BitstreamFormatDataService } from '../../core/data/bitstream-format-data.service'; import { BitstreamFormat } from '../../core/shared/bitstream-format.model'; import { BitstreamFormatSupportLevel } from '../../core/shared/bitstream-format-support-level'; -import { hasValue, isEmpty, isNotEmpty, hasValueOperator } from '../../shared/empty.util'; +import { hasValue, hasValueOperator, isEmpty, isNotEmpty } from '../../shared/empty.util'; import { Metadata } from '../../core/shared/metadata.utils'; import { Location } from '@angular/common'; import { RemoteData } from '../../core/data/remote-data'; @@ -25,10 +25,7 @@ import { DSONameService } from '../../core/breadcrumbs/dso-name.service'; import { Item } from '../../core/shared/item.model'; import { DsDynamicInputModel } from '../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-input.model'; import { DsDynamicTextAreaModel } from '../../shared/form/builder/ds-dynamic-form-ui/models/ds-dynamic-textarea.model'; -import { BundleDataService } from '../../core/data/bundle-data.service'; -import { Operation } from 'fast-json-patch'; import { PrimaryBitstreamService } from '../../core/data/primary-bitstream-data.service'; -import { hasSucceeded } from '../../core/data/request-entry-state.model'; @Component({ selector: 'ds-edit-bitstream-page', @@ -374,12 +371,6 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy { */ private bundle: Bundle; - /** - * Path to patch primary bitstream on the bundle - * @private - */ - private readonly primaryBitstreamPath = '/primaryBitstreamUUID'; - constructor(private route: ActivatedRoute, private router: Router, private changeDetectorRef: ChangeDetectorRef, @@ -391,7 +382,7 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy { private notificationsService: NotificationsService, private bitstreamFormatService: BitstreamFormatDataService, private primaryBitstreamService: PrimaryBitstreamService, - private bundleService: BundleDataService) { + ) { } /** @@ -404,33 +395,39 @@ export class EditBitstreamPageComponent implements OnInit, OnDestroy { this.itemId = this.route.snapshot.queryParams.itemId; this.entityType = this.route.snapshot.queryParams.entityType; - this.bitstreamRD$ = this.route.data.pipe(map((data) => data.bitstream)); + this.bitstreamRD$ = this.route.data.pipe(map((data: any) => data.bitstream)); this.bitstreamFormatsRD$ = this.bitstreamFormatService.findAll(this.findAllOptions); const bitstream$ = this.bitstreamRD$.pipe( getFirstSucceededRemoteData(), - getRemoteDataPayload() + getRemoteDataPayload(), + tap(t => console.log(t)), ); const allFormats$ = this.bitstreamFormatsRD$.pipe( getFirstSucceededRemoteData(), - getRemoteDataPayload() + getRemoteDataPayload(), + tap(t => console.log(t)), ); const bundle$ = bitstream$.pipe( switchMap((bitstream: Bitstream) => bitstream.bundle), getFirstSucceededRemoteDataPayload(), + tap(t => console.log(t)), ); const primaryBitstream$ = bundle$.pipe( hasValueOperator(), + tap(t => console.log(t._links.primaryBitstream.href)), switchMap((bundle: Bundle) => this.bitstreamService.findByHref(bundle._links.primaryBitstream.href)), - getFirstSucceededRemoteDataPayload() + getFirstSucceededRemoteDataPayload(), + tap(t => console.log(t)), ); const item$ = bundle$.pipe( switchMap((bundle: Bundle) => bundle.item), - getFirstSucceededRemoteDataPayload() + getFirstSucceededRemoteDataPayload(), + tap(t => console.log(t)), ); this.subs.push( observableCombineLatest(