move form to activity js api

This commit is contained in:
Eugenio Romano 2016-08-19 13:37:54 +01:00
parent 0110f486b6
commit 06c05ea9b5
14 changed files with 258 additions and 351 deletions

View File

@ -26,7 +26,7 @@
"label-undefined": true,
"max-line-length": [
true,
140
180
],
"member-ordering": [
true,

View File

@ -80,6 +80,7 @@
"karma-coverage": "1.0.0",
"karma-coveralls": "1.1.2",
"karma-jasmine": "1.0.2",
"karma-jasmine-ajax": "0.1.13",
"karma-jasmine-html-reporter": "0.2.0",
"karma-jasmine-ajax": "0.1.13",
"karma-mocha-reporter": "2.0.3",

View File

@ -15,81 +15,57 @@
* limitations under the License.
*/
import { it, describe, expect, beforeEach } from '@angular/core/testing';
import { Http, RequestOptionsArgs, Response, ResponseOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { it, inject, describe, expect, beforeEach, beforeEachProviders, afterEach } from '@angular/core/testing';
import { AlfrescoAuthenticationService, AlfrescoSettingsService } from 'ng2-alfresco-core';
import { Response, ResponseOptions } from '@angular/http';
import { FormService } from './form.service';
import { FormValues } from './../components/widgets/core/index';
declare let jasmine: any;
describe('FormService', () => {
let http: Http;
let responseBody: any;
let formService: FormService;
let authService: AlfrescoAuthenticationService;
let settingsService: AlfrescoSettingsService;
let responseBody: any, formService: FormService;
let createResponse = (url, body): Observable<Response> => {
return Observable.create(observer => {
let response = new Response(new ResponseOptions({
url: url,
body: body
beforeEachProviders(() => {
return [
FormService,
AlfrescoSettingsService,
AlfrescoAuthenticationService
];
});
beforeEach(inject([FormService], (service: FormService) => {
jasmine.Ajax.install();
formService = service;
}));
observer.next(response);
observer.complete();
});
};
beforeEach(() => {
http = <Http> {
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
return createResponse(url, responseBody);
},
post(url: string, body: any, options?: RequestOptionsArgs): Observable<Response> {
return createResponse(url, responseBody);
}
};
settingsService = new AlfrescoSettingsService();
settingsService.setProviders([]);
authService = new AlfrescoAuthenticationService(settingsService, null);
formService = new FormService(http, authService, settingsService);
});
it('should resolve host address via settings service', () => {
const url = '<url>';
settingsService.bpmHost = url;
expect(formService.getHostAddress()).toBe(url);
afterEach(() => {
jasmine.Ajax.uninstall();
});
it('should fetch and parse process definitions', (done) => {
spyOn(http, 'get').and.callThrough();
responseBody = {
data: [
{ id: '1' },
{ id: '2' }
{id: '1'},
{id: '2'}
]
};
formService.getProcessDefinitions().subscribe(result => {
expect(http.get).toHaveBeenCalled();
let args: any[] = (<any>http).get.calls.argsFor(0);
expect(args[0].endsWith('/process-definitions')).toBeTruthy();
expect(result).toEqual(responseBody.data);
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/process-definitions')).toBeTruthy();
expect(result).toEqual(JSON.parse(jasmine.Ajax.requests.mostRecent().response).data);
done();
});
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
});
});
it('should fetch and parse tasks', (done) => {
spyOn(http, 'post').and.callThrough();
responseBody = {
data: [
{ id: '1' },
@ -98,126 +74,130 @@ describe('FormService', () => {
};
formService.getTasks().subscribe(result => {
expect(http.post).toHaveBeenCalled();
let args: any[] = (<any>http).post.calls.argsFor(0);
expect(args[0].endsWith('/tasks/query')).toBeTruthy();
expect(result).toEqual(responseBody.data);
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/tasks/query')).toBeTruthy();
expect(result).toEqual(JSON.parse(jasmine.Ajax.requests.mostRecent().response).data);
done();
});
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
});
});
it('should fetch and parse the task by id', (done) => {
spyOn(http, 'get').and.callThrough();
responseBody = {
id: '1'
};
formService.getTask('1').subscribe(result => {
expect(http.get).toHaveBeenCalled();
let args: any[] = (<any>http).get.calls.argsFor(0);
expect(args[0].endsWith('/tasks/1')).toBeTruthy();
expect(result).toEqual(responseBody);
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/tasks/1')).toBeTruthy();
expect(result.id).toEqual(responseBody.id);
done();
});
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
});
});
it('should save task form', (done) => {
spyOn(http, 'post').and.callThrough();
let values = <FormValues> {
field1: 'one',
field2: 'two'
};
formService.saveTaskForm('1', values).subscribe(() => {
expect(http.post).toHaveBeenCalled();
let args: any[] = (<any>http).post.calls.argsFor(0);
expect(args[0].endsWith('/task-forms/1/save-form')).toBeTruthy();
expect(args[1]).toEqual(JSON.stringify({ values: values }));
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/task-forms/1/save-form')).toBeTruthy();
expect(JSON.parse(jasmine.Ajax.requests.mostRecent().params).values.field1).toEqual(values.field1);
expect(JSON.parse(jasmine.Ajax.requests.mostRecent().params).values.field2).toEqual(values.field2);
done();
});
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
});
});
it('should complete task form', (done) => {
spyOn(http, 'post').and.callThrough();
let values = <FormValues> {
field1: 'one',
field2: 'two'
};
formService.completeTaskForm('1', values).subscribe(() => {
expect(http.post).toHaveBeenCalled();
let args: any[] = (<any>http).post.calls.argsFor(0);
expect(args[0].endsWith('/task-forms/1')).toBeTruthy();
expect(args[1]).toEqual(JSON.stringify({ values: values }));
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/task-forms/1')).toBeTruthy();
expect(JSON.parse(jasmine.Ajax.requests.mostRecent().params).values.field1).toEqual(values.field1);
expect(JSON.parse(jasmine.Ajax.requests.mostRecent().params).values.field2).toEqual(values.field2);
done();
});
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
});
});
it('should complete task form with a specific outcome', (done) => {
spyOn(http, 'post').and.callThrough();
let values = <FormValues> {
field1: 'one',
field2: 'two'
};
formService.completeTaskForm('1', values, 'custom').subscribe(() => {
expect(http.post).toHaveBeenCalled();
let args: any[] = (<any>http).post.calls.argsFor(0);
expect(args[0].endsWith('/task-forms/1')).toBeTruthy();
expect(args[1]).toEqual(JSON.stringify({ values: values, outcome: 'custom' }));
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/task-forms/1')).toBeTruthy();
expect(JSON.parse(jasmine.Ajax.requests.mostRecent().params).values.field2).toEqual(values.field2);
expect(JSON.parse(jasmine.Ajax.requests.mostRecent().params).outcome).toEqual('custom' );
done();
});
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
});
});
it('should get task form by id', (done) => {
spyOn(http, 'get').and.callThrough();
responseBody = { id: '1' };
responseBody = { id: 1 };
formService.getTaskForm('1').subscribe(result => {
expect(http.get).toHaveBeenCalled();
let args: any[] = (<any>http).get.calls.argsFor(0);
expect(args[0].endsWith('/task-forms/1')).toBeTruthy();
expect(result).toEqual(responseBody);
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/task-forms/1')).toBeTruthy();
expect(result.id).toEqual(responseBody.id);
done();
});
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
});
});
it('should get form definition by id', (done) => {
spyOn(http, 'get').and.callThrough();
responseBody = { id: '1' };
responseBody = { id: 1 };
formService.getFormDefinitionById('1').subscribe(result => {
expect(http.get).toHaveBeenCalled();
let args: any[] = (<any>http).get.calls.argsFor(0);
expect(args[0].endsWith('/form-models/1')).toBeTruthy();
expect(result).toEqual(responseBody);
expect(jasmine.Ajax.requests.mostRecent().url.endsWith('/form-models/1')).toBeTruthy();
expect(result.id).toEqual(responseBody.id);
done();
});
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
});
});
it('should get form definition id by name', (done) => {
spyOn(http, 'get').and.callThrough();
const formName = 'form1';
const formId = 1;
responseBody = {
@ -227,14 +207,16 @@ describe('FormService', () => {
};
formService.getFormDefinitionByName(formName).subscribe(result => {
expect(http.get).toHaveBeenCalled();
let args: any[] = (<any>http).get.calls.argsFor(0);
expect(args[0].endsWith(`models?filter=myReusableForms&filterText=${formName}&modelType=2`)).toBeTruthy();
expect(jasmine.Ajax.requests.mostRecent().url.endsWith(`models?filter=myReusableForms&filterText=${formName}&modelType=2`)).toBeTruthy();
expect(result).toEqual(formId);
done();
});
http://localhost:9999/activiti-app/api/enterprise/models?filter=myReusableForms&modelType=2"
jasmine.Ajax.requests.mostRecent().respondWith({
'status': 200,
contentType: 'application/json',
responseText: JSON.stringify(responseBody)
});
});
it('should not get form id from response', () => {
@ -253,30 +235,6 @@ describe('FormService', () => {
expect(formService.getFormId(null)).toBeNull();
});
it('should convert response to json object', () => {
let data = { id: 1 };
let response = new Response(new ResponseOptions({ body: data }));
expect(formService.toJson(response)).toEqual(data);
});
it('should fallback to empty json object', () => {
let response = new Response(new ResponseOptions({ body: null }));
expect(formService.toJson(response)).toEqual({});
expect(formService.toJson(null)).toEqual({});
});
it('should convert response to json array', () => {
let payload = {
data: [
{ id: 1 }
]
};
let response = new Response(new ResponseOptions({ body: JSON.stringify(payload) }));
expect(formService.toJsonArray(response)).toEqual(payload.data);
});
it('should fallback to empty json array', () => {
expect(formService.toJsonArray(null)).toEqual([]);

View File

@ -15,12 +15,10 @@
* limitations under the License.
*/
import { Injectable } from '@angular/core';
import { Response, Http, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { AlfrescoAuthenticationService } from 'ng2-alfresco-core';
import { FormValues } from './../components/widgets/core/index';
import { AlfrescoSettingsService } from 'ng2-alfresco-core';
import {Injectable} from '@angular/core';
import {Observable} from 'rxjs/Rx';
import {AlfrescoAuthenticationService} from 'ng2-alfresco-core';
import {FormValues} from './../components/widgets/core/index';
@Injectable()
export class FormService {
@ -28,92 +26,60 @@ export class FormService {
static UNKNOWN_ERROR_MESSAGE: string = 'Unknown error';
static GENERIC_ERROR_MESSAGE: string = 'Server error';
constructor(private http: Http,
private authService: AlfrescoAuthenticationService,
private alfrescoSettingsService: AlfrescoSettingsService) {
}
getHostAddress(): string {
return this.alfrescoSettingsService.bpmHost;
constructor(private authService: AlfrescoAuthenticationService) {
}
getProcessDefinitions(): Observable<any> {
let url = `${this.getHostAddress()}/activiti-app/api/enterprise/process-definitions`;
let options = this.getRequestOptions();
return this.http
.get(url, options)
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.processApi.getProcessDefinitions({}))
.map(this.toJsonArray)
.catch(this.handleError);
}
getTasks(): Observable<any> {
let url = `${this.getHostAddress()}/activiti-app/api/enterprise/tasks/query`;
let body = JSON.stringify({});
let options = this.getRequestOptions();
return this.http
.post(url, body, options)
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.taskApi.listTasks({}))
.map(this.toJsonArray)
.catch(this.handleError);
}
getTask(id: string): Observable<any> {
let url = `${this.getHostAddress()}/activiti-app/api/enterprise/tasks/${id}`;
let options = this.getRequestOptions();
return this.http
.get(url, options)
getTask(taskId: string): Observable<any> {
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.taskApi.getTask(taskId))
.map(this.toJson)
.catch(this.handleError);
}
saveTaskForm(id: string, formValues: FormValues): Observable<Response> {
let url = `${this.getHostAddress()}/activiti-app/api/enterprise/task-forms/${id}/save-form`;
let body = JSON.stringify({ values: formValues });
let options = this.getRequestOptions();
saveTaskForm(taskId: string, formValues: FormValues): Observable<any> {
let body = JSON.stringify({values: formValues});
return this.http
.post(url, body, options)
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.taskApi.saveTaskForm(taskId, body))
.catch(this.handleError);
}
/**
* Complete Task Form
* @param id Task Id
* @param taskId Task Id
* @param formValues Form Values
* @param outcome Form Outcome
* @returns {any}
*/
completeTaskForm(id: string, formValues: FormValues, outcome?: string): Observable<Response> {
let url = `${this.getHostAddress()}/activiti-app/api/enterprise/task-forms/${id}`;
let data: any = { values: formValues };
completeTaskForm(taskId: string, formValues: FormValues, outcome?: string): Observable<any> {
let data: any = {values: formValues};
if (outcome) {
data.outcome = outcome;
}
let body = JSON.stringify(data);
let options = this.getRequestOptions();
return this.http
.post(url, body, options)
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.taskApi.completeTaskForm(taskId, body))
.catch(this.handleError);
}
getTaskForm(id: string): Observable<any> {
let url = `${this.getHostAddress()}/activiti-app/api/enterprise/task-forms/${id}`;
let options = this.getRequestOptions();
return this.http
.get(url, options)
getTaskForm(taskId: string): Observable<any> {
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.taskApi.getTaskForm(taskId))
.map(this.toJson)
.catch(this.handleError);
}
getFormDefinitionById(id: string): Observable<any> {
let url = `${this.getHostAddress()}/activiti-app/app/rest/form-models/${id}`;
let options = this.getRequestOptions();
return this.http
.get(url, options)
getFormDefinitionById(formId: string): Observable<any> {
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.editorApi.getForm(formId))
.map(this.toJson)
.catch(this.handleError);
}
@ -124,53 +90,37 @@ export class FormService {
* @returns {Promise<T>|Promise<ErrorObservable>}
*/
getFormDefinitionByName(name: string): Observable<any> {
let url = `${this.getHostAddress()}/activiti-app/app/rest/models?filter=myReusableForms&filterText=${name}&modelType=2`;
let options = this.getRequestOptions();
let opts = {
'filter': 'myReusableForms',
'filterText': name,
'modelType': 2
};
return this.http
.get(url, options)
return Observable.fromPromise(this.authService.getAlfrescoApi().activiti.modelsApi.getModels(opts))
.map(this.getFormId)
.catch(this.handleError);
}
private getHeaders(): Headers {
return new Headers({
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': this.authService.getTicket('BPM')
});
}
private getRequestOptions(): RequestOptions {
let headers = this.getHeaders();
return new RequestOptions({headers: headers});
}
getFormId(res: Response) {
getFormId(res: any) {
let result = null;
if (res) {
let body = res.json();
if (body && body.data && body.data.length > 0) {
result = body.data[0].id;
}
if (res && res.data && res.data.length > 0) {
result = res.data[0].id;
}
return result;
}
toJson(res: Response) {
toJson(res: any) {
if (res) {
let body = res.json();
return body || {};
return res || {};
}
return {};
}
toJsonArray(res: Response) {
toJsonArray(res: any) {
if (res) {
let body = res.json();
return body.data || [];
return res.data || [];
}
return [];
}
@ -184,5 +134,4 @@ export class FormService {
console.error(errMsg);
return Observable.throw(errMsg);
}
}

View File

@ -24,7 +24,7 @@
"label-undefined": true,
"max-line-length": [
true,
140
180
],
"member-ordering": [
true,

View File

@ -101,7 +101,6 @@ describe('ActivitiTaskListService', () => {
];
});
beforeEach(inject([ActivitiTaskListService], (activitiTaskListService: ActivitiTaskListService) => {
jasmine.Ajax.install();
service = activitiTaskListService;

View File

@ -24,7 +24,7 @@
"label-undefined": true,
"max-line-length": [
true,
140
180
],
"member-ordering": [
true,

View File

@ -24,7 +24,7 @@
"label-undefined": true,
"max-line-length": [
true,
140
180
],
"member-ordering": [
true,

View File

@ -24,7 +24,7 @@
"label-undefined": true,
"max-line-length": [
true,
140
180
],
"member-ordering": [
true,

View File

@ -24,7 +24,7 @@
"label-undefined": true,
"max-line-length": [
true,
140
180
],
"member-ordering": [
true,

View File

@ -24,7 +24,7 @@
"label-undefined": true,
"max-line-length": [
true,
140
180
],
"member-ordering": [
true,

View File

@ -24,7 +24,7 @@
"label-undefined": true,
"max-line-length": [
true,
140
180
],
"member-ordering": [
true,

View File

@ -24,7 +24,7 @@
"label-undefined": true,
"max-line-length": [
true,
140
180
],
"member-ordering": [
true,

View File

@ -24,7 +24,7 @@
"label-undefined": true,
"max-line-length": [
true,
140
180
],
"member-ordering": [
true,