From bac2a45d18792ff5db07b38d1d6f1a2dc663f7b5 Mon Sep 17 00:00:00 2001 From: Infad Kachancheri Date: Tue, 9 May 2017 23:00:53 +0530 Subject: [PATCH] [ADF- 556] Analytics Service - Add a new method to get the report details by name (#1869) * added service to get report details by report name * analytics service tslit fix#1 * added test cases for getReportList and getReportByName --- .../src/assets/analytics.service.mock.ts | 28 ++++++ .../src/services/analytics.service.spec.ts | 91 +++++++++++++++++++ .../src/services/analytics.service.ts | 12 +++ 3 files changed, 131 insertions(+) create mode 100644 ng2-components/ng2-activiti-analytics/src/assets/analytics.service.mock.ts create mode 100644 ng2-components/ng2-activiti-analytics/src/services/analytics.service.spec.ts diff --git a/ng2-components/ng2-activiti-analytics/src/assets/analytics.service.mock.ts b/ng2-components/ng2-activiti-analytics/src/assets/analytics.service.mock.ts new file mode 100644 index 0000000000..574a81d38f --- /dev/null +++ b/ng2-components/ng2-activiti-analytics/src/assets/analytics.service.mock.ts @@ -0,0 +1,28 @@ +/*! + * @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. + */ + +export let fakeReportList = [ + { + id: '1', + name: 'Fake Report 1' + }, + { + id: '2', + name: 'Fake Report 2' + } +]; + diff --git a/ng2-components/ng2-activiti-analytics/src/services/analytics.service.spec.ts b/ng2-components/ng2-activiti-analytics/src/services/analytics.service.spec.ts new file mode 100644 index 0000000000..793d41c555 --- /dev/null +++ b/ng2-components/ng2-activiti-analytics/src/services/analytics.service.spec.ts @@ -0,0 +1,91 @@ +/*! + * @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 { ReflectiveInjector } from '@angular/core'; +import { TestBed } from '@angular/core/testing'; +import { async } from '@angular/core/testing'; +import { CoreModule, AlfrescoApiService, LogService } from 'ng2-alfresco-core'; +import { ReportParametersModel, ParameterValueModel } from '../models/report.model'; +import { AnalyticsService } from './analytics.service'; +import { fakeReportList } from '../assets/analytics.service.mock'; + +declare let jasmine: any; + +describe('Activiti Analytics Service', () => { + + let service: AnalyticsService; + let apiService: AlfrescoApiService; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + CoreModule.forRoot() + ], + providers: [ + AnalyticsService + ] + }); + service = TestBed.get(AnalyticsService); + apiService = TestBed.get(AlfrescoApiService); + }); + + beforeEach(() => { + jasmine.Ajax.install(); + }); + + afterEach(() => { + jasmine.Ajax.uninstall(); + }); + + describe('Content tests', () => { + + it('should return the report list by appId', (done) => { + service.getReportList('1').subscribe( + (reportList) => { + expect(reportList).toBeDefined(); + expect(reportList.length).toEqual(2); + expect(reportList[0].name).toEqual('Fake Report 1'); + expect(reportList[1].name).toEqual('Fake Report 2'); + done(); + } + ); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 200, + contentType: 'application/json', + responseText: JSON.stringify(fakeReportList) + }); + }); + + it('should return the report by report name', (done) => { + service.getReportByName('Fake Report 2').subscribe( + (report) => { + expect(report).toBeDefined(); + expect(report).not.toBeNull(); + expect(report.id).toEqual(2); + done(); + } + ); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 200, + contentType: 'application/json', + responseText: JSON.stringify(fakeReportList) + }); + }); + }); +}); diff --git a/ng2-components/ng2-activiti-analytics/src/services/analytics.service.ts b/ng2-components/ng2-activiti-analytics/src/services/analytics.service.ts index e0be6e33da..c5dcc7fdb4 100644 --- a/ng2-components/ng2-activiti-analytics/src/services/analytics.service.ts +++ b/ng2-components/ng2-activiti-analytics/src/services/analytics.service.ts @@ -55,6 +55,18 @@ export class AnalyticsService { }).catch(err => this.handleError(err)); } + /** + * Retrive Report by name + * @param reportName - string - The name of report + * @returns {Observable} + */ + getReportByName(reportName: string): Observable { + return Observable.fromPromise(this.apiService.getInstance().activiti.reportApi.getReportList()) + .map((response: any) => { + return response.find(report => report.name === reportName); + }).catch(err => this.handleError(err)); + } + private isReportValid(appId: string, report: ReportParametersModel) { let isValid: boolean = true; if (appId && appId !== '0' && report.name.includes('Process definition overview')) {