New packages org (#2639)

New packages org
This commit is contained in:
Eugenio Romano
2017-11-16 14:12:52 +00:00
committed by GitHub
parent 6a24c6ef75
commit a52bb5600a
1984 changed files with 17179 additions and 40423 deletions
@@ -0,0 +1,169 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { TestBed } from '@angular/core/testing';
import { async } from '@angular/core/testing';
import { AlfrescoApi } from 'alfresco-js-api';
import { AlfrescoApiService } from '@alfresco/core';
import { fakeError, fakeProcessFilters } from '../../mock';
import { FilterProcessRepresentationModel } from '../models/filter-process.model';
import { ProcessFilterService } from './process-filter.service';
describe('Process filter', () => {
let service: ProcessFilterService;
let apiService: AlfrescoApiService;
let alfrescoApi: AlfrescoApi;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
ProcessFilterService
]
});
service = TestBed.get(ProcessFilterService);
apiService = TestBed.get(AlfrescoApiService);
alfrescoApi = apiService.getInstance();
});
describe('filters', () => {
let getFilters: jasmine.Spy;
let createFilter: jasmine.Spy;
beforeEach(() => {
getFilters = spyOn(alfrescoApi.activiti.userFiltersApi, 'getUserProcessInstanceFilters')
.and
.returnValue(Promise.resolve(fakeProcessFilters));
createFilter = spyOn(alfrescoApi.activiti.userFiltersApi, 'createUserProcessInstanceFilter')
.and
.callFake((filter: FilterProcessRepresentationModel) => Promise.resolve(filter));
});
describe('get filters', () => {
it('should call the API without an appId defined by default', () => {
service.getProcessFilters(null);
expect(getFilters).toHaveBeenCalled();
});
it('should call the API with the correct appId when specified', () => {
service.getProcessFilters(226);
expect(getFilters).toHaveBeenCalledWith({appId: 226});
});
it('should return the task filter by id', (done) => {
service.getProcessFilterById(333).subscribe(
(processFilter: FilterProcessRepresentationModel) => {
expect(processFilter).toBeDefined();
expect(processFilter.id).toEqual(333);
expect(processFilter.name).toEqual('Running');
expect(processFilter.filter.sort).toEqual('created-desc');
expect(processFilter.filter.state).toEqual('running');
done();
}
);
});
it('should return the task filter by name', (done) => {
service.getProcessFilterByName('Running').subscribe(
(res: FilterProcessRepresentationModel) => {
expect(res).toBeDefined();
expect(res.id).toEqual(333);
expect(res.name).toEqual('Running');
expect(res.filter.sort).toEqual('created-desc');
expect(res.filter.state).toEqual('running');
done();
}
);
});
it('should return the non-empty filter list that is returned by the API', async(() => {
service.getProcessFilters(null).subscribe(
(res) => {
expect(res.length).toBe(1);
}
);
}));
it('should return the default filters', (done) => {
service.createDefaultFilters(1234).subscribe(
(res: FilterProcessRepresentationModel []) => {
expect(res).toBeDefined();
expect(res.length).toEqual(3);
expect(res[0].name).toEqual('Running');
expect(res[1].name).toEqual('Completed');
expect(res[2].name).toEqual('All');
done();
}
);
});
it('should pass on any error that is returned by the API', async(() => {
getFilters = getFilters.and.returnValue(Promise.reject(fakeError));
service.getProcessFilters(null).subscribe(
() => {},
(res) => {
expect(res).toBe(fakeError);
}
);
}));
});
describe('add filter', () => {
let filter = fakeProcessFilters.data[0];
it('should call the API to create the filter', () => {
service.addProcessFilter(filter);
expect(createFilter).toHaveBeenCalledWith(filter);
});
it('should return the created filter', async(() => {
service.addProcessFilter(filter).subscribe((createdFilter: FilterProcessRepresentationModel) => {
expect(createdFilter).toBe(filter);
});
}));
it('should pass on any error that is returned by the API', async(() => {
createFilter = createFilter.and.returnValue(Promise.reject(fakeError));
service.addProcessFilter(filter).subscribe(
() => {},
(res) => {
expect(res).toBe(fakeError);
}
);
}));
it('should return a default error if no data is returned by the API', async(() => {
createFilter = createFilter.and.returnValue(Promise.reject(null));
service.addProcessFilter(filter).subscribe(
() => {},
(res) => {
expect(res).toBe('Server error');
}
);
}));
});
});
});
@@ -0,0 +1,174 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AlfrescoApiService, LogService } from '@alfresco/core';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { FilterProcessRepresentationModel } from '../models/filter-process.model';
@Injectable()
export class ProcessFilterService {
constructor(private alfrescoApiService: AlfrescoApiService,
private logService: LogService) {
}
getProcessFilters(appId: number): Observable<FilterProcessRepresentationModel[]> {
return Observable.fromPromise(this.callApiProcessFilters(appId))
.map((response: any) => {
let filters: FilterProcessRepresentationModel[] = [];
response.data.forEach((filter: FilterProcessRepresentationModel) => {
let filterModel = new FilterProcessRepresentationModel(filter);
filters.push(filterModel);
});
return filters;
})
.catch(err => this.handleProcessError(err));
}
/**
* Retrieve the process filter by id
* @param filterId - number - The id of the filter
* @param appId - string - optional - The id of app
* @returns {Observable<FilterProcessRepresentationModel>}
*/
getProcessFilterById(filterId: number, appId?: number): Observable<FilterProcessRepresentationModel> {
return Observable.fromPromise(this.callApiProcessFilters(appId))
.map((response: any) => {
return response.data.find(filter => filter.id === filterId);
}).catch(err => this.handleProcessError(err));
}
/**
* Retrieve the process filter by name
* @param filterName - string - The name of the filter
* @param appId - string - optional - The id of app
* @returns {Observable<FilterProcessRepresentationModel>}
*/
getProcessFilterByName(filterName: string, appId?: number): Observable<FilterProcessRepresentationModel> {
return Observable.fromPromise(this.callApiProcessFilters(appId))
.map((response: any) => {
return response.data.find(filter => filter.name === filterName);
}).catch(err => this.handleProcessError(err));
}
/**
* Create and return the default filters
* @param appId
* @returns {FilterProcessRepresentationModel[]}
*/
public createDefaultFilters(appId: number): Observable<any[]> {
let runningFilter = this.getRunningFilterInstance(appId);
let runningObservable = this.addProcessFilter(runningFilter);
let completedFilter = this.getCompletedFilterInstance(appId);
let completedObservable = this.addProcessFilter(completedFilter);
let allFilter = this.getAllFilterInstance(appId);
let allObservable = this.addProcessFilter(allFilter);
return Observable.create(observer => {
Observable.forkJoin(
runningObservable,
completedObservable,
allObservable
).subscribe(
(res) => {
let filters: FilterProcessRepresentationModel[] = [];
res.forEach((filter) => {
if (filter.name === runningFilter.name) {
filters.push(runningFilter);
} else if (filter.name === completedFilter.name) {
filters.push(completedFilter);
} else if (filter.name === allFilter.name) {
filters.push(allFilter);
}
});
observer.next(filters);
observer.complete();
},
(err: any) => {
this.logService.error(err);
});
});
}
public getRunningFilterInstance(appId: number): FilterProcessRepresentationModel {
return new FilterProcessRepresentationModel({
'name': 'Running',
'appId': appId,
'recent': true,
'icon': 'glyphicon-random',
'filter': { 'sort': 'created-desc', 'name': '', 'state': 'running' }
});
}
/**
* Return a static Completed filter instance
* @param appId
* @returns {FilterProcessRepresentationModel}
*/
private getCompletedFilterInstance(appId: number): FilterProcessRepresentationModel {
return new FilterProcessRepresentationModel({
'name': 'Completed',
'appId': appId,
'recent': false,
'icon': 'glyphicon-ok-sign',
'filter': { 'sort': 'created-desc', 'name': '', 'state': 'completed' }
});
}
/**
* Return a static All filter instance
* @param appId
* @returns {FilterProcessRepresentationModel}
*/
private getAllFilterInstance(appId: number): FilterProcessRepresentationModel {
return new FilterProcessRepresentationModel({
'name': 'All',
'appId': appId,
'recent': true,
'icon': 'glyphicon-th',
'filter': { 'sort': 'created-desc', 'name': '', 'state': 'all' }
});
}
/**
* Add a filter
* @param filter - FilterProcessRepresentationModel
* @returns {FilterProcessRepresentationModel}
*/
addProcessFilter(filter: FilterProcessRepresentationModel): Observable<FilterProcessRepresentationModel> {
return Observable.fromPromise(this.alfrescoApiService.getInstance().activiti.userFiltersApi.createUserProcessInstanceFilter(filter))
.map(res => res)
.map((response: FilterProcessRepresentationModel) => {
return response;
}).catch(err => this.handleProcessError(err));
}
callApiProcessFilters(appId?: number) {
if (appId) {
return this.alfrescoApiService.getInstance().activiti.userFiltersApi.getUserProcessInstanceFilters({ appId: appId });
} else {
return this.alfrescoApiService.getInstance().activiti.userFiltersApi.getUserProcessInstanceFilters();
}
}
private handleProcessError(error: any) {
return Observable.throw(error || 'Server error');
}
}
@@ -0,0 +1,538 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { TestBed } from '@angular/core/testing';
import { async } from '@angular/core/testing';
import { AlfrescoApi } from 'alfresco-js-api';
import { AlfrescoApiService } from '@alfresco/core';
import { exampleProcess, fakeProcessInstances } from '../../mock';
import { fakeError, fakeProcessDef, fakeTasksList } from '../../mock';
import { ProcessFilterParamRepresentationModel } from '../models/filter-process.model';
import { ProcessInstanceVariable } from '../models/process-instance-variable.model';
import { ProcessService } from './process.service';
declare let moment: any;
describe('ProcessService', () => {
let service: ProcessService;
let apiService: AlfrescoApiService;
let alfrescoApi: AlfrescoApi;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
ProcessService
]
});
service = TestBed.get(ProcessService);
apiService = TestBed.get(AlfrescoApiService);
alfrescoApi = apiService.getInstance();
});
describe('process instances', () => {
let getProcessInstances: jasmine.Spy;
let filter: ProcessFilterParamRepresentationModel = new ProcessFilterParamRepresentationModel({
processDefinitionId: '1',
appDefinitionId: '1',
page: 1,
sort: 'created-asc',
state: 'completed'
});
beforeEach(() => {
getProcessInstances = spyOn(alfrescoApi.activiti.processApi, 'getProcessInstances')
.and
.returnValue(Promise.resolve({ data: [ exampleProcess ] }));
});
it('should return the correct number of instances', async(() => {
service.getProcessInstances(filter).subscribe((instances) => {
expect(instances.length).toBe(1);
});
}));
it('should return the correct instance data', async(() => {
service.getProcessInstances(filter).subscribe((instances) => {
let instance = instances[0];
expect(instance.id).toBe(exampleProcess.id);
expect(instance.name).toBe(exampleProcess.name);
expect(instance.started).toBe(exampleProcess.started);
});
}));
it('should filter by processDefinitionKey', async(() => {
getProcessInstances = getProcessInstances.and.returnValue(Promise.resolve(fakeProcessInstances));
service.getProcessInstances(filter, 'fakeProcessDefinitionKey1').subscribe((instances) => {
expect(instances.length).toBe(1);
let instance = instances[0];
expect(instance.id).toBe('340124');
expect(instance.name).toBe('James Franklin EMEA Onboarding');
expect(instance.started).toEqual(new Date('2017-10-09T12:19:44.560+0000'));
});
}));
it('should call service to fetch process instances', () => {
service.getProcessInstances(filter);
expect(getProcessInstances).toHaveBeenCalled();
});
it('should call service with supplied parameters', () => {
service.getProcessInstances(filter);
expect(getProcessInstances).toHaveBeenCalledWith(filter);
});
it('should pass on any error that is returned by the API', async(() => {
getProcessInstances = getProcessInstances.and.returnValue(Promise.reject(fakeError));
service.getProcessInstances(null).subscribe(
() => {},
(res) => {
expect(res).toBe(fakeError);
}
);
}));
it('should return a default error if no data is returned by the API', async(() => {
getProcessInstances = getProcessInstances.and.returnValue(Promise.reject(null));
service.getProcessInstances(null).subscribe(
() => {},
(res) => {
expect(res).toBe('Server error');
}
);
}));
});
describe('process instance', () => {
const processId = 'test';
let getProcessInstance: jasmine.Spy;
beforeEach(() => {
getProcessInstance = spyOn(alfrescoApi.activiti.processApi, 'getProcessInstance')
.and
.returnValue(Promise.resolve(exampleProcess));
});
it('should return the correct instance data', async(() => {
service.getProcess(processId).subscribe((instance) => {
expect(instance.id).toBe(exampleProcess.id);
expect(instance.name).toBe(exampleProcess.name);
expect(instance.started).toBe(exampleProcess.started);
});
}));
it('should call service to fetch process instances', () => {
service.getProcess(processId);
expect(getProcessInstance).toHaveBeenCalled();
});
it('should call service with supplied process ID', () => {
service.getProcess(processId);
expect(getProcessInstance).toHaveBeenCalledWith(processId);
});
it('should pass on any error that is returned by the API', async(() => {
getProcessInstance = getProcessInstance.and.returnValue(Promise.reject(fakeError));
service.getProcess(null).subscribe(
() => {},
(res) => {
expect(res).toBe(fakeError);
}
);
}));
it('should return a default error if no data is returned by the API', async(() => {
getProcessInstance = getProcessInstance.and.returnValue(Promise.reject(null));
service.getProcess(null).subscribe(
() => {},
(res) => {
expect(res).toBe('Server error');
}
);
}));
});
describe('start process instance', () => {
const processDefId = '1234', processName = 'My process instance';
let startNewProcessInstance: jasmine.Spy;
beforeEach(() => {
startNewProcessInstance = spyOn(alfrescoApi.activiti.processApi, 'startNewProcessInstance')
.and
.returnValue(Promise.resolve(exampleProcess));
});
it('should call the API to create the process instance', () => {
service.startProcess(processDefId, processName);
expect(startNewProcessInstance).toHaveBeenCalledWith({
name: processName,
processDefinitionId: processDefId
});
});
it('should call the API to create the process instance with form parameters', () => {
let formParams = {
type: 'ford',
color: 'red'
};
service.startProcess(processDefId, processName, null, formParams);
expect(startNewProcessInstance).toHaveBeenCalledWith({
name: processName,
processDefinitionId: processDefId,
values: formParams
});
});
it('should return the created process instance', async(() => {
service.startProcess(processDefId, processName).subscribe((createdProcess) => {
expect(createdProcess.id).toBe(exampleProcess.id);
expect(createdProcess.name).toBe(exampleProcess.name);
expect(createdProcess.started).toBe(exampleProcess.started);
expect(createdProcess.startedBy.id).toBe(exampleProcess.startedBy.id);
});
}));
it('should pass on any error that is returned by the API', async(() => {
startNewProcessInstance = startNewProcessInstance.and.returnValue(Promise.reject(fakeError));
service.startProcess(processDefId, processName).subscribe(
() => {},
(res) => {
expect(res).toBe(fakeError);
}
);
}));
it('should return a default error if no data is returned by the API', async(() => {
startNewProcessInstance = startNewProcessInstance.and.returnValue(Promise.reject(null));
service.startProcess(processDefId, processName).subscribe(
() => {},
(res) => {
expect(res).toBe('Server error');
}
);
}));
});
describe('cancel process instance', () => {
const processInstanceId = '1234';
let deleteProcessInstance: jasmine.Spy;
beforeEach(() => {
deleteProcessInstance = spyOn(alfrescoApi.activiti.processApi, 'deleteProcessInstance')
.and
.returnValue(Promise.resolve());
});
it('should call service to delete process instances', () => {
service.cancelProcess(processInstanceId);
expect(deleteProcessInstance).toHaveBeenCalled();
});
it('should call service with supplied process ID', () => {
service.cancelProcess(processInstanceId);
expect(deleteProcessInstance).toHaveBeenCalledWith(processInstanceId);
});
it('should run the success callback', (done) => {
service.cancelProcess(processInstanceId).subscribe(() => {
done();
});
});
it('should pass on any error that is returned by the API', async(() => {
deleteProcessInstance = deleteProcessInstance.and.returnValue(Promise.reject(fakeError));
service.cancelProcess(null).subscribe(
() => {},
(res) => {
expect(res).toBe(fakeError);
}
);
}));
it('should return a default error if no data is returned by the API', async(() => {
deleteProcessInstance = deleteProcessInstance.and.returnValue(Promise.reject(null));
service.cancelProcess(null).subscribe(
() => {},
(res) => {
expect(res).toBe('Server error');
}
);
}));
});
describe('process definitions', () => {
let getProcessDefinitions: jasmine.Spy;
beforeEach(() => {
getProcessDefinitions = spyOn(alfrescoApi.activiti.processApi, 'getProcessDefinitions')
.and
.returnValue(Promise.resolve({ data: [ fakeProcessDef, fakeProcessDef ] }));
});
it('should return the correct number of process defs', async(() => {
service.getProcessDefinitions().subscribe((defs) => {
expect(defs.length).toBe(2);
});
}));
it('should return the correct process def data', async(() => {
service.getProcessDefinitions().subscribe((defs) => {
expect(defs[0].id).toBe(fakeProcessDef.id);
expect(defs[0].key).toBe(fakeProcessDef.key);
expect(defs[0].name).toBe(fakeProcessDef.name);
});
}));
it('should call API with correct parameters when no appId provided', () => {
service.getProcessDefinitions();
expect(getProcessDefinitions).toHaveBeenCalledWith({
latest: true
});
});
it('should call API with correct parameters when appId provided', () => {
const appId = 1;
service.getProcessDefinitions(appId);
expect(getProcessDefinitions).toHaveBeenCalledWith({
latest: true,
appDefinitionId: appId
});
});
it('should pass on any error that is returned by the API', async(() => {
getProcessDefinitions = getProcessDefinitions.and.returnValue(Promise.reject(fakeError));
service.getProcessDefinitions().subscribe(
() => {},
(res) => {
expect(res).toBe(fakeError);
}
);
}));
it('should return a default error if no data is returned by the API', async(() => {
getProcessDefinitions = getProcessDefinitions.and.returnValue(Promise.reject(null));
service.getProcessDefinitions().subscribe(
() => {},
(res) => {
expect(res).toBe('Server error');
}
);
}));
});
describe('process instance tasks', () => {
const processId = '1001';
let listTasks: jasmine.Spy;
beforeEach(() => {
listTasks = spyOn(alfrescoApi.activiti.taskApi, 'listTasks')
.and
.returnValue(Promise.resolve(fakeTasksList));
});
it('should return the correct number of tasks', async(() => {
service.getProcessTasks(processId).subscribe((tasks) => {
expect(tasks.length).toBe(2);
});
}));
it('should return the correct task data', async(() => {
let fakeTasks = fakeTasksList.data;
service.getProcessTasks(processId).subscribe((tasks) => {
let task = tasks[0];
expect(task.id).toBe(fakeTasks[0].id);
expect(task.name).toBe(fakeTasks[0].name);
expect(task.created).toEqual(moment(new Date('2016-11-10T00:00:00+00:00'), 'YYYY-MM-DD').format());
});
}));
it('should call service to fetch process instance tasks', () => {
service.getProcessTasks(processId);
expect(listTasks).toHaveBeenCalled();
});
it('should call service with processInstanceId parameter', () => {
service.getProcessTasks(processId);
expect(listTasks).toHaveBeenCalledWith({
processInstanceId: processId
});
});
it('should call service with processInstanceId and state parameters', () => {
service.getProcessTasks(processId, 'completed');
expect(listTasks).toHaveBeenCalledWith({
processInstanceId: processId,
state: 'completed'
});
});
it('should pass on any error that is returned by the API', async(() => {
listTasks = listTasks.and.returnValue(Promise.reject(fakeError));
service.getProcessTasks(processId).subscribe(
() => {},
(res) => {
expect(res).toBe(fakeError);
}
);
}));
it('should return a default error if no data is returned by the API', async(() => {
listTasks = listTasks.and.returnValue(Promise.reject(null));
service.getProcessTasks(processId).subscribe(
() => {},
(res) => {
expect(res).toBe('Server error');
}
);
}));
});
describe('process variables', () => {
let getVariablesSpy: jasmine.Spy;
let createOrUpdateProcessInstanceVariablesSpy: jasmine.Spy;
let deleteProcessInstanceVariableSpy: jasmine.Spy;
beforeEach(() => {
getVariablesSpy = spyOn(alfrescoApi.activiti.processInstanceVariablesApi, 'getProcessInstanceVariables').and.returnValue(Promise.resolve([{
name: 'var1',
value: 'Test1'
}, {
name: 'var3',
value: 'Test3'
}]));
createOrUpdateProcessInstanceVariablesSpy = spyOn(alfrescoApi.activiti.processInstanceVariablesApi,
'createOrUpdateProcessInstanceVariables').and.returnValue(Promise.resolve({}));
deleteProcessInstanceVariableSpy = spyOn(alfrescoApi.activiti.processInstanceVariablesApi,
'deleteProcessInstanceVariable').and.returnValue(Promise.resolve());
});
describe('get variables', () => {
it('should call service to fetch variables', () => {
service.getProcessInstanceVariables(null);
expect(getVariablesSpy).toHaveBeenCalled();
});
it('should pass on any error that is returned by the API', async(() => {
getVariablesSpy = getVariablesSpy.and.returnValue(Promise.reject(fakeError));
service.getProcessInstanceVariables(null).subscribe(
() => {},
(res) => {
expect(res).toBe(fakeError);
}
);
}));
it('should return a default error if no data is returned by the API', async(() => {
getVariablesSpy = getVariablesSpy.and.returnValue(Promise.reject(null));
service.getProcessInstanceVariables(null).subscribe(
() => {},
(res) => {
expect(res).toBe('Server error');
}
);
}));
});
describe('create or update variables', () => {
let updatedVariables = [new ProcessInstanceVariable({
name: 'var1',
value: 'Test1'
}), new ProcessInstanceVariable({
name: 'var3',
value: 'Test3'
})];
it('should call service to create or update variables', () => {
service.createOrUpdateProcessInstanceVariables('123', updatedVariables);
expect(createOrUpdateProcessInstanceVariablesSpy).toHaveBeenCalled();
});
it('should pass on any error that is returned by the API', async(() => {
createOrUpdateProcessInstanceVariablesSpy = createOrUpdateProcessInstanceVariablesSpy.and.returnValue(Promise.reject(fakeError));
service.createOrUpdateProcessInstanceVariables('123', updatedVariables).subscribe(
() => {},
(res) => {
expect(res).toBe(fakeError);
}
);
}));
it('should return a default error if no data is returned by the API', async(() => {
createOrUpdateProcessInstanceVariablesSpy = createOrUpdateProcessInstanceVariablesSpy.and.returnValue(Promise.reject(null));
service.createOrUpdateProcessInstanceVariables('123', updatedVariables).subscribe(
() => {},
(res) => {
expect(res).toBe('Server error');
}
);
}));
});
describe('delete variables', () => {
it('should call service to delete variables', () => {
service.deleteProcessInstanceVariable('123', 'myVar');
expect(deleteProcessInstanceVariableSpy).toHaveBeenCalled();
});
it('should pass on any error that is returned by the API', async(() => {
deleteProcessInstanceVariableSpy = deleteProcessInstanceVariableSpy.and.returnValue(Promise.reject(fakeError));
service.deleteProcessInstanceVariable('123', 'myVar').subscribe(
() => {},
(res) => {
expect(res).toBe(fakeError);
}
);
}));
it('should return a default error if no data is returned by the API', async(() => {
deleteProcessInstanceVariableSpy = deleteProcessInstanceVariableSpy.and.returnValue(Promise.reject(null));
service.deleteProcessInstanceVariable('123', 'myVar').subscribe(
() => {},
(res) => {
expect(res).toBe('Server error');
}
);
}));
});
});
});
@@ -0,0 +1,157 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AlfrescoApiService } from '@alfresco/core';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { TaskDetailsModel } from '../../task-list';
import { ProcessFilterParamRepresentationModel } from '../models/filter-process.model';
import { ProcessDefinitionRepresentation } from '../models/process-definition.model';
import { ProcessInstanceVariable } from '../models/process-instance-variable.model';
import { ProcessInstance } from '../models/process-instance.model';
declare let moment: any;
@Injectable()
export class ProcessService {
constructor(private alfrescoApiService: AlfrescoApiService) {
}
getProcessInstances(requestNode: ProcessFilterParamRepresentationModel, processDefinitionKey?: string): Observable<ProcessInstance[]> {
return Observable.fromPromise(this.alfrescoApiService.getInstance().activiti.processApi.getProcessInstances(requestNode))
.map((res: any) => {
if (processDefinitionKey) {
return res.data.filter(process => process.processDefinitionKey === processDefinitionKey);
} else {
return res.data;
}
}).catch(err => this.handleProcessError(err));
}
/**
* fetch the Process Audit information as a pdf
* @param processId - the process id
*/
fetchProcessAuditPdfById(processId: string): Observable<Blob> {
return Observable.fromPromise(this.alfrescoApiService.getInstance().activiti.processApi.getProcessAuditPdf(processId))
.catch(err => this.handleProcessError(err));
}
/**
* fetch the Process Audit information in a json format
* @param processId - the process id
*/
fetchProcessAuditJsonById(processId: string): Observable<any> {
return Observable.fromPromise(this.alfrescoApiService.getInstance().activiti.processApi.getProcessAuditJson(processId))
.catch(err => this.handleProcessError(err));
}
getProcess(processInstanceId: string): Observable<ProcessInstance> {
return Observable.fromPromise(this.alfrescoApiService.getInstance().activiti.processApi.getProcessInstance(processInstanceId))
.catch(err => this.handleProcessError(err));
}
getProcessTasks(processInstanceId: string, state?: string): Observable<TaskDetailsModel[]> {
let taskOpts = state ? {
processInstanceId: processInstanceId,
state: state
} : {
processInstanceId: processInstanceId
};
return Observable.fromPromise(this.alfrescoApiService.getInstance().activiti.taskApi.listTasks(taskOpts))
.map(this.extractData)
.map(tasks => tasks.map((task: any) => {
task.created = moment(task.created, 'YYYY-MM-DD').format();
return task;
}))
.catch(err => this.handleProcessError(err));
}
getProcessDefinitions(appId?: number): Observable<ProcessDefinitionRepresentation[]> {
let opts = appId ? {
latest: true,
appDefinitionId: appId
} : {
latest: true
};
return Observable.fromPromise(
this.alfrescoApiService.getInstance().activiti.processApi.getProcessDefinitions(opts)
)
.map(this.extractData)
.map(processDefs => processDefs.map((pd) => new ProcessDefinitionRepresentation(pd)))
.catch(err => this.handleProcessError(err));
}
startProcess(processDefinitionId: string, name: string, outcome?: string, startFormValues?: any, variables?: ProcessInstanceVariable[]): Observable<ProcessInstance> {
let startRequest: any = {
name: name,
processDefinitionId: processDefinitionId
};
if (outcome) {
startRequest.outcome = outcome;
}
if (startFormValues) {
startRequest.values = startFormValues;
}
if (variables) {
startRequest.variables = variables;
}
return Observable.fromPromise(
this.alfrescoApiService.getInstance().activiti.processApi.startNewProcessInstance(startRequest)
)
.map((pd) => new ProcessInstance(pd))
.catch(err => this.handleProcessError(err));
}
cancelProcess(processInstanceId: string): Observable<void> {
return Observable.fromPromise(
this.alfrescoApiService.getInstance().activiti.processApi.deleteProcessInstance(processInstanceId)
)
.catch(err => this.handleProcessError(err));
}
getProcessInstanceVariables(processDefinitionId: string): Observable<ProcessInstanceVariable[]> {
return Observable.fromPromise(
this.alfrescoApiService.getInstance().activiti.processInstanceVariablesApi.getProcessInstanceVariables(processDefinitionId)
)
.map((processVars: any[]) => processVars.map((pd) => new ProcessInstanceVariable(pd)))
.catch(err => this.handleProcessError(err));
}
createOrUpdateProcessInstanceVariables(processDefinitionId: string, variables: ProcessInstanceVariable[]): Observable<ProcessInstanceVariable[]> {
return Observable.fromPromise(
this.alfrescoApiService.getInstance().activiti.processInstanceVariablesApi.createOrUpdateProcessInstanceVariables(processDefinitionId, variables)
)
.catch(err => this.handleProcessError(err));
}
deleteProcessInstanceVariable(processDefinitionId: string, variableName: string): Observable<void> {
return Observable.fromPromise(
this.alfrescoApiService.getInstance().activiti.processInstanceVariablesApi.deleteProcessInstanceVariable(processDefinitionId, variableName)
)
.catch(err => this.handleProcessError(err));
}
private extractData(res: any) {
return res.data || {};
}
private handleProcessError(error: any) {
return Observable.throw(error || 'Server error');
}
}