Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #508 from atmire/refactor-submission-parsers
Refactor Submission Parsers
- Loading branch information
Showing
32 changed files
with
476 additions
and
150 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
src/app/core/submission/submission-object-data.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { Observable } from 'rxjs'; | ||
import { SubmissionService } from '../../submission/submission.service'; | ||
import { RemoteData } from '../data/remote-data'; | ||
import { SubmissionObject } from './models/submission-object.model'; | ||
import { WorkspaceItem } from './models/workspaceitem.model'; | ||
import { SubmissionObjectDataService } from './submission-object-data.service'; | ||
import { SubmissionScopeType } from './submission-scope-type'; | ||
import { WorkflowItemDataService } from './workflowitem-data.service'; | ||
import { WorkspaceitemDataService } from './workspaceitem-data.service'; | ||
|
||
describe('SubmissionObjectDataService', () => { | ||
let service: SubmissionObjectDataService; | ||
let submissionService: SubmissionService; | ||
let workspaceitemDataService: WorkspaceitemDataService; | ||
let workflowItemDataService: WorkflowItemDataService; | ||
|
||
const submissionId = '1234'; | ||
const wsiResult = 'wsiResult' as any; | ||
const wfiResult = 'wfiResult' as any; | ||
|
||
beforeEach(() => { | ||
workspaceitemDataService = jasmine.createSpyObj('WorkspaceitemDataService', { | ||
findById: wsiResult | ||
}); | ||
workflowItemDataService = jasmine.createSpyObj('WorkflowItemDataService', { | ||
findById: wfiResult | ||
}); | ||
}); | ||
|
||
describe('findById', () => { | ||
it('should call SubmissionService.getSubmissionScope to determine the type of submission object', () => { | ||
submissionService = jasmine.createSpyObj('SubmissionService', { | ||
getSubmissionScope: {} | ||
}); | ||
service = new SubmissionObjectDataService(workspaceitemDataService, workflowItemDataService, submissionService); | ||
service.findById(submissionId); | ||
expect(submissionService.getSubmissionScope).toHaveBeenCalled(); | ||
}); | ||
|
||
describe('when the submission ID refers to a WorkspaceItem', () => { | ||
beforeEach(() => { | ||
submissionService = jasmine.createSpyObj('SubmissionService', { | ||
getSubmissionScope: SubmissionScopeType.WorkspaceItem | ||
}); | ||
service = new SubmissionObjectDataService(workspaceitemDataService, workflowItemDataService, submissionService); | ||
}); | ||
|
||
it('should forward the result of WorkspaceitemDataService.findById()', () => { | ||
const result = service.findById(submissionId); | ||
expect(workspaceitemDataService.findById).toHaveBeenCalledWith(submissionId); | ||
expect(result).toBe(wsiResult); | ||
}); | ||
}); | ||
|
||
describe('when the submission ID refers to a WorkflowItem', () => { | ||
beforeEach(() => { | ||
submissionService = jasmine.createSpyObj('SubmissionService', { | ||
getSubmissionScope: SubmissionScopeType.WorkflowItem | ||
}); | ||
service = new SubmissionObjectDataService(workspaceitemDataService, workflowItemDataService, submissionService); | ||
}); | ||
|
||
it('should forward the result of WorkflowItemDataService.findById()', () => { | ||
const result = service.findById(submissionId); | ||
expect(workflowItemDataService.findById).toHaveBeenCalledWith(submissionId); | ||
expect(result).toBe(wfiResult); | ||
}); | ||
}); | ||
|
||
describe('when the type of submission object is unknown', () => { | ||
beforeEach(() => { | ||
submissionService = jasmine.createSpyObj('SubmissionService', { | ||
getSubmissionScope: 'Something else' | ||
}); | ||
service = new SubmissionObjectDataService(workspaceitemDataService, workflowItemDataService, submissionService); | ||
}); | ||
|
||
it('shouldn\'t call any data service methods', () => { | ||
service.findById(submissionId); | ||
expect(workspaceitemDataService.findById).not.toHaveBeenCalled(); | ||
expect(workflowItemDataService.findById).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should return a RemoteData containing an error', (done) => { | ||
const result = service.findById(submissionId); | ||
result.subscribe((rd: RemoteData<SubmissionObject>) => { | ||
expect(rd.hasFailed).toBe(true); | ||
expect(rd.error).toBeDefined(); | ||
done(); | ||
}) | ||
}); | ||
}); | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { of as observableOf, Observable } from 'rxjs'; | ||
import { SubmissionService } from '../../submission/submission.service'; | ||
import { RemoteData } from '../data/remote-data'; | ||
import { RemoteDataError } from '../data/remote-data-error'; | ||
import { SubmissionObject } from './models/submission-object.model'; | ||
import { SubmissionScopeType } from './submission-scope-type'; | ||
import { WorkflowItemDataService } from './workflowitem-data.service'; | ||
import { WorkspaceitemDataService } from './workspaceitem-data.service'; | ||
|
||
/** | ||
* A service to retrieve submission objects (WorkspaceItem/WorkflowItem) | ||
* without knowing their type | ||
*/ | ||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class SubmissionObjectDataService { | ||
constructor( | ||
private workspaceitemDataService: WorkspaceitemDataService, | ||
private workflowItemDataService: WorkflowItemDataService, | ||
private submissionService: SubmissionService | ||
) { | ||
} | ||
|
||
/** | ||
* Retrieve a submission object based on its ID. | ||
* | ||
* @param id The identifier of a submission object | ||
*/ | ||
findById(id: string): Observable<RemoteData<SubmissionObject>> { | ||
switch (this.submissionService.getSubmissionScope()) { | ||
case SubmissionScopeType.WorkspaceItem: | ||
return this.workspaceitemDataService.findById(id); | ||
case SubmissionScopeType.WorkflowItem: | ||
return this.workflowItemDataService.findById(id); | ||
default: | ||
const error = new RemoteDataError( | ||
undefined, | ||
undefined, | ||
'The request couldn\'t be sent. Unable to determine the type of submission object' | ||
); | ||
return observableOf(new RemoteData(false, false, false, error, undefined)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.