Remove mso-oof-adapter from SO
[so.git] / so-monitoring / so-monitoring-ui / src / main / frontend / src / app / details / details.component.spec.ts
1 /**
2 ============LICENSE_START=======================================================
3  Copyright (C) 2018 Ericsson. All rights reserved.
4 ================================================================================
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15  limitations under the License.
16
17 SPDX-License-Identifier: Apache-2.0
18 ============LICENSE_END=========================================================
19
20 @authors: ronan.kenny@ericsson.com, waqas.ikram@ericsson.com, andrei.barcovschi@ericsson.com
21 */
22
23 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
24
25 import { DetailsComponent } from './details.component';
26 import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
27 import { RouterTestingModule } from '@angular/router/testing';
28 import { MatTableModule } from '@angular/material';
29 import { inject } from '@angular/core/testing';
30 import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
31 import { HttpClientModule } from '@angular/common/http';
32 import { HttpClient } from '@angular/common/http';
33 import { RouterModule } from '@angular/router';
34 import { APP_BASE_HREF } from '@angular/common';
35 import { ToastrNotificationService } from '../toastr-notification-service.service';
36 import { DataService } from '../data.service';
37 import { Observable, of, throwError } from 'rxjs';
38 import { ActivityInstance } from '../model/activityInstance.model';
39 import { ProcessDefinitionDetail } from '../model/processDefinition.model';
40 import { ProcessInstanceDetail } from '../model/processInstance.model';
41 import { VariableInstance } from '../model/variableInstance.model';
42 import { ActivatedRoute } from '@angular/router';
43 import { Params } from '@angular/router';
44
45 // Generate stub for toastr popup notifications
46 class StubbedToastrNotificationService extends ToastrNotificationService {
47   toastrSettings() {}
48   info() {}
49   error() {}
50 }
51
52 const startActivity: ActivityInstance = {
53   activityId: "StartEvent_1",
54   processInstanceId: "processInstanceId-val-1234",
55   calledProcessInstanceId: "",
56   activityName: "",
57   activityType: "",
58   durationInMillis: "1",
59   endTime: "",
60   startTime: ""
61 };
62
63 const subProcessActivity: ActivityInstance = {
64   activityId: "CallActivity_14h26ae",
65   processInstanceId: "processInstanceId-val-1234",
66   calledProcessInstanceId: "1234",
67   activityName: "",
68   activityType: "",
69   durationInMillis: "1",
70   endTime: "",
71   startTime: ""
72 };
73
74 const processDefinition: ProcessDefinitionDetail = {
75   processDefinitionId: "processDefinitionId-val-1234",
76   processDefinitionXml: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
77     "<bpmn:definitions xmlns:bpmn=\"http://www.omg.org/spec/BPMN/20100524/MODEL\" xmlns:bpmndi=\"http://www.omg.org/spec/BPMN/20100524/DI\" xmlns:di=\"http://www.omg.org/spec/DD/20100524/DI\" xmlns:dc=\"http://www.omg.org/spec/DD/20100524/DC\" xmlns:camunda=\"http://camunda.org/schema/1.0/bpmn\" id=\"Definitions_01lwydo\" targetNamespace=\"http://bpmn.io/schema/bpmn\" exporter=\"Camunda Modeler\" exporterVersion=\"2.2.4\">" +
78     "  <bpmn:process id=\"Process_1\" isExecutable=\"true\">" +
79     "    <bpmn:startEvent id=\"StartEvent_1\">" +
80     "      <bpmn:outgoing>SequenceFlow_1rrp6qt</bpmn:outgoing>" +
81     "    </bpmn:startEvent>" +
82     "    <bpmn:callActivity id=\"CallActivity_14h26ae\" name=\"Sub Process\" calledElement=\"DecomposeService\">" +
83     "      <bpmn:incoming>SequenceFlow_1rrp6qt</bpmn:incoming>" +
84     "      <bpmn:outgoing>SequenceFlow_0yvdjct</bpmn:outgoing>" +
85     "    </bpmn:callActivity>" +
86     "    <bpmn:sequenceFlow id=\"SequenceFlow_1rrp6qt\" sourceRef=\"StartEvent_1\" targetRef=\"CallActivity_14h26ae\" />" +
87     "    <bpmn:endEvent id=\"EndEvent_039q5o1\">" +
88     "      <bpmn:incoming>SequenceFlow_0yvdjct</bpmn:incoming>" +
89     "    </bpmn:endEvent>" +
90     "    <bpmn:sequenceFlow id=\"SequenceFlow_0yvdjct\" sourceRef=\"CallActivity_14h26ae\" targetRef=\"EndEvent_039q5o1\" />" +
91     "  </bpmn:process>" +
92     "  <bpmndi:BPMNDiagram id=\"BPMNDiagram_1\">" +
93     "    <bpmndi:BPMNPlane id=\"BPMNPlane_1\" bpmnElement=\"Process_1\">" +
94     "      <bpmndi:BPMNShape id=\"_BPMNShape_StartEvent_2\" bpmnElement=\"StartEvent_1\">" +
95     "        <dc:Bounds x=\"173\" y=\"102\" width=\"36\" height=\"36\" />" +
96     "      </bpmndi:BPMNShape>" +
97     "      <bpmndi:BPMNShape id=\"CallActivity_14h26ae_di\" bpmnElement=\"CallActivity_14h26ae\">" +
98     "        <dc:Bounds x=\"267\" y=\"80\" width=\"100\" height=\"80\" />" +
99     "      </bpmndi:BPMNShape>" +
100     "      <bpmndi:BPMNEdge id=\"SequenceFlow_1rrp6qt_di\" bpmnElement=\"SequenceFlow_1rrp6qt\">" +
101     "        <di:waypoint x=\"209\" y=\"120\" />" +
102     "        <di:waypoint x=\"267\" y=\"120\" />" +
103     "      </bpmndi:BPMNEdge>" +
104     "      <bpmndi:BPMNShape id=\"EndEvent_039q5o1_di\" bpmnElement=\"EndEvent_039q5o1\">" +
105     "        <dc:Bounds x=\"451\" y=\"102\" width=\"36\" height=\"36\" />" +
106     "      </bpmndi:BPMNShape>" +
107     "      <bpmndi:BPMNEdge id=\"SequenceFlow_0yvdjct_di\" bpmnElement=\"SequenceFlow_0yvdjct\">" +
108     "        <di:waypoint x=\"367\" y=\"120\" />" +
109     "        <di:waypoint x=\"451\" y=\"120\" />" +
110     "      </bpmndi:BPMNEdge>" +
111     "    </bpmndi:BPMNPlane>" +
112     "  </bpmndi:BPMNDiagram>" +
113     "</bpmn:definitions>"
114 };
115
116 const emptyProcessDefinition: ProcessDefinitionDetail = {
117   processDefinitionId: "processDefinitionId-val",
118   processDefinitionXml: ""
119 };
120
121 const processInstance: ProcessInstanceDetail = {
122   processInstanceId: "processInstanceId-val-1234",
123   processDefinitionId: "1",
124   processDefinitionName: "test",
125   superProcessInstanceId: "1"
126 };
127
128 const varInstanceObj: VariableInstance = {
129   name: 'ABC',
130   type: 'Object',
131   value: '{value: 1234}'
132 };
133
134 const varInstanceStr: VariableInstance = {
135   name: 'NameStr',
136   type: 'String',
137   value: 'valOfStr'
138 };
139
140 describe('DetailsComponent', (displayCamundaflow = {}) => {
141   // Create SPY Object for Jasmine tests to mock DataService
142   let spyDataService: jasmine.SpyObj<DataService>;
143   let component: DetailsComponent;
144   let fixture: ComponentFixture<DetailsComponent>;
145
146   beforeEach(async(() => {
147     spyDataService = jasmine.createSpyObj('DataService', ['getActivityInstance', 'getVariableInstance', 'getProcessDefinition', 'getProcessInstance']);
148
149     TestBed.configureTestingModule({
150       providers: [DetailsComponent, HttpClient, HttpTestingController,
151         { provide: APP_BASE_HREF, useValue: '/' },
152         { provide: ToastrNotificationService, useClass: StubbedToastrNotificationService },
153         { provide: DataService, useValue: spyDataService },
154         { provide: ActivatedRoute, useValue: { params: of({ id: '1234' }) } }],
155       imports: [RouterTestingModule, MatTableModule, HttpClientModule, RouterModule.forRoot([])],
156       declarations: [DetailsComponent],
157       schemas: [
158         CUSTOM_ELEMENTS_SCHEMA
159       ]
160     });
161     fixture = TestBed.createComponent(DetailsComponent);
162     component = fixture.componentInstance;
163   }));
164
165   // Ensure creation of DetailsComponent component
166   it('component should be created', inject([DetailsComponent],
167     (detailsComponent: DetailsComponent) => {
168       expect(detailsComponent).toBeTruthy();
169     }));
170
171   // Create a processInstance and ensure it is defined
172   it('processInstance should be defined if PII populated', async(() => {
173     spyDataService.getActivityInstance.and.returnValue(Promise.resolve([startActivity, subProcessActivity]));
174     spyDataService.getProcessDefinition.and.returnValue(of(processDefinition));
175     spyDataService.getProcessInstance.and.returnValue(Promise.resolve(processInstance));
176     spyDataService.getVariableInstance.and.returnValue(of([varInstanceObj]));
177     component.ngOnInit();
178
179     fixture.whenStable().then(() => {
180       expect(component.processInstance).toBeDefined();
181     });
182   }));
183
184   it('should handle bpmnViewer.importXML error', () => {
185     spyDataService.getActivityInstance.and.returnValue(Promise.resolve([startActivity, subProcessActivity]));
186     spyDataService.getProcessDefinition.and.returnValue(of(emptyProcessDefinition));
187     spyDataService.getProcessInstance.and.returnValue(Promise.resolve(processInstance));
188     spyDataService.getVariableInstance.and.returnValue(of([varInstanceObj]));
189     component.ngOnInit();
190   });
191
192   it('should handle error when dataService.getProcessInstance returns an error', () => {
193     spyDataService.getVariableInstance.and.returnValue(of([varInstanceObj]));
194     spyDataService.getProcessInstance.and.returnValue(Promise.reject(new Error('getProcessInstance Promise should not be resolved')));
195     component.ngOnInit();
196   });
197
198   it('should handle error when data.getVariableInstance returns an error', () => {
199     spyDataService.getActivityInstance.and.returnValue(Promise.resolve([startActivity, subProcessActivity]));
200     spyDataService.getProcessDefinition.and.returnValue(of(processDefinition));
201     spyDataService.getProcessInstance.and.returnValue(Promise.resolve(processInstance));
202     spyDataService.getVariableInstance.and.callFake(() => {
203       return throwError(new Error('getVariableInstance error'));
204     });
205     component.ngOnInit();
206   });
207
208   it('should handle error when data.getActivityInstance and data.getProcessDefinition return errors', () => {
209     spyDataService.getProcessInstance.and.returnValue(Promise.resolve(processInstance));
210     spyDataService.getVariableInstance.and.returnValue(of([varInstanceObj]));
211     spyDataService.getProcessDefinition.and.returnValue(of(processDefinition));
212     spyDataService.getActivityInstance.and.returnValue(Promise.reject(new Error('getActivityInstance Promise should not be resolved')));
213     spyDataService.getProcessDefinition.and.callFake(() => {
214       return throwError(new Error('getProcessDefinition error'));
215     });
216     component.ngOnInit();
217   });
218 });