|
1 |
| -import { WorkflowOverview } from '@janus-idp/backstage-plugin-orchestrator-common'; |
| 1 | +import moment from 'moment'; |
2 | 2 |
|
3 |
| -import { generateTestWorkflowOverview } from '../test-utils'; |
4 | 3 | import {
|
| 4 | + ProcessInstance, |
| 5 | + ProcessInstanceState, |
| 6 | + WorkflowOverview, |
| 7 | + WorkflowRunStatusDTO, |
| 8 | +} from '@janus-idp/backstage-plugin-orchestrator-common'; |
| 9 | + |
| 10 | +import { |
| 11 | + generateProcessInstance, |
| 12 | + generateTestExecuteWorkflowResponse, |
| 13 | + generateTestWorkflowOverview, |
| 14 | +} from '../test-utils'; |
| 15 | +import assessedProcessInstanceData from './__fixtures__/assessedProcessInstance.json'; |
| 16 | +import { |
| 17 | + getProcessInstancesDTOFromString, |
| 18 | + mapToExecuteWorkflowResponseDTO, |
| 19 | + mapToGetWorkflowInstanceResults, |
| 20 | + mapToProcessInstanceDTO, |
5 | 21 | mapToWorkflowOverviewDTO,
|
| 22 | + mapToWorkflowRunStatusDTO, |
6 | 23 | mapWorkflowCategoryDTOFromString,
|
7 | 24 | } from './V2Mappings';
|
8 | 25 |
|
| 26 | +describe('scenarios to verify mapToGetWorkflowInstanceResults', () => { |
| 27 | + it('correctly maps positive scenario response', async () => { |
| 28 | + const assessedProcessInstance = assessedProcessInstanceData; |
| 29 | + |
| 30 | + const mappedValue = mapToGetWorkflowInstanceResults( |
| 31 | + // @ts-ignore |
| 32 | + assessedProcessInstance.instance.variables, |
| 33 | + ); |
| 34 | + |
| 35 | + expect(mappedValue).toBeDefined(); |
| 36 | + expect(mappedValue.result).toBeDefined(); |
| 37 | + expect(mappedValue.preCheck).toBeDefined(); |
| 38 | + expect(mappedValue.workflowOptions).toBeDefined(); |
| 39 | + expect(mappedValue.repositoryUrl).toEqual('https://java.com'); |
| 40 | + expect(Object.keys(mappedValue).length).toBe(4); |
| 41 | + }); |
| 42 | + |
| 43 | + it('correctly maps string response', async () => { |
| 44 | + const testValue = 'string_value'; |
| 45 | + const mappedValue = mapToGetWorkflowInstanceResults(testValue); |
| 46 | + expect(mappedValue).toBeDefined(); |
| 47 | + expect(Object.keys(mappedValue).length).toBe(1); |
| 48 | + expect(mappedValue.variables).toBeDefined(); |
| 49 | + expect(mappedValue.variables).toEqual(testValue); |
| 50 | + }); |
| 51 | + |
| 52 | + it('correctly returns empty workflowoptions when variables property does not exist', async () => { |
| 53 | + const assessedProcessInstance = assessedProcessInstanceData; |
| 54 | + |
| 55 | + // @ts-ignore |
| 56 | + delete assessedProcessInstance.instance.variables; |
| 57 | + |
| 58 | + const mappedValue = mapToGetWorkflowInstanceResults( |
| 59 | + // @ts-ignore |
| 60 | + assessedProcessInstance.instance.variables, |
| 61 | + ); |
| 62 | + |
| 63 | + expect(mappedValue).toBeDefined(); |
| 64 | + expect(Object.keys(mappedValue).length).toBe(1); |
| 65 | + expect(mappedValue.workflowoptions).toBeDefined(); |
| 66 | + expect(mappedValue.workflowoptions?.length).toBe(0); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe('scenarios to verify executeWorkflowResponseDTO', () => { |
| 71 | + it('correctly maps positive scenario response', async () => { |
| 72 | + const execWorkflowResp = generateTestExecuteWorkflowResponse(); |
| 73 | + const mappedValue = mapToExecuteWorkflowResponseDTO( |
| 74 | + 'test_workflowId', |
| 75 | + execWorkflowResp, |
| 76 | + ); |
| 77 | + expect(mappedValue).toBeDefined(); |
| 78 | + expect(mappedValue.id).toBeDefined(); |
| 79 | + expect(Object.keys(mappedValue).length).toBe(1); |
| 80 | + }); |
| 81 | + |
| 82 | + it('throws error when no id attribute present in response', async () => { |
| 83 | + expect(() => { |
| 84 | + mapToExecuteWorkflowResponseDTO('workflowId', { id: '' }); |
| 85 | + }).toThrow( |
| 86 | + `Error while mapping ExecuteWorkflowResponse to ExecuteWorkflowResponseDTO for workflow with id`, |
| 87 | + ); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
9 | 91 | describe('scenarios to verify mapToWorkflowOverviewDTO', () => {
|
10 | 92 | it('correctly maps WorkflowOverview', () => {
|
11 | 93 | // Arrange
|
@@ -47,3 +129,82 @@ describe('scenarios to verify mapWorkflowCategoryDTOFromString', () => {
|
47 | 129 | expect(resultCategory).toBe(expected);
|
48 | 130 | });
|
49 | 131 | });
|
| 132 | + |
| 133 | +describe('scenarios to verify mapToProcessInstanceDTO', () => { |
| 134 | + it('correctly maps ProcessInstanceDTO for not completed workflow', () => { |
| 135 | + // Arrange |
| 136 | + const processIntanceV1: ProcessInstance = generateProcessInstance(1); |
| 137 | + processIntanceV1.end = undefined; |
| 138 | + |
| 139 | + // Act |
| 140 | + const result = mapToProcessInstanceDTO(processIntanceV1); |
| 141 | + |
| 142 | + // Assert |
| 143 | + expect(result).toBeDefined(); |
| 144 | + expect(result.id).toBeDefined(); |
| 145 | + expect(result.start).toBeDefined(); |
| 146 | + expect(result.start).toEqual(processIntanceV1.start?.toUTCString()); |
| 147 | + expect(result.end).toBeUndefined(); |
| 148 | + expect(result.duration).toBeUndefined(); |
| 149 | + expect(result.status).toEqual( |
| 150 | + getProcessInstancesDTOFromString(processIntanceV1.state), |
| 151 | + ); |
| 152 | + expect(result.description).toEqual(processIntanceV1.description); |
| 153 | + expect(result.category).toEqual('infrastructure'); |
| 154 | + expect(result.workflowdata).toEqual( |
| 155 | + // @ts-ignore |
| 156 | + processIntanceV1?.variables?.workflowdata, |
| 157 | + ); |
| 158 | + expect(result.workflow).toEqual( |
| 159 | + processIntanceV1.processName ?? processIntanceV1.processId, |
| 160 | + ); |
| 161 | + }); |
| 162 | + it('correctly maps ProcessInstanceDTO', () => { |
| 163 | + // Arrange |
| 164 | + const processIntanceV1: ProcessInstance = generateProcessInstance(1); |
| 165 | + |
| 166 | + const start = moment(processIntanceV1.start); |
| 167 | + const end = moment(processIntanceV1.end); |
| 168 | + const duration = moment.duration(start.diff(end)).humanize(); |
| 169 | + // Act |
| 170 | + const result = mapToProcessInstanceDTO(processIntanceV1); |
| 171 | + |
| 172 | + // Assert |
| 173 | + expect(result.id).toBeDefined(); |
| 174 | + expect(result.start).toEqual(processIntanceV1.start?.toUTCString()); |
| 175 | + expect(result.end).toBeDefined(); |
| 176 | + expect(result.end).toEqual(processIntanceV1.end!.toUTCString()); |
| 177 | + expect(result.duration).toEqual(duration); |
| 178 | + |
| 179 | + expect(result).toBeDefined(); |
| 180 | + expect(result.status).toEqual( |
| 181 | + getProcessInstancesDTOFromString(processIntanceV1.state), |
| 182 | + ); |
| 183 | + expect(result.end).toEqual(processIntanceV1.end?.toUTCString()); |
| 184 | + expect(result.duration).toEqual(duration); |
| 185 | + expect(result.duration).toEqual('an hour'); |
| 186 | + expect(result.description).toEqual(processIntanceV1.description); |
| 187 | + expect(result.category).toEqual('infrastructure'); |
| 188 | + expect(result.workflowdata).toEqual( |
| 189 | + // @ts-ignore |
| 190 | + processIntanceV1?.variables?.workflowdata, |
| 191 | + ); |
| 192 | + expect(result.workflow).toEqual( |
| 193 | + processIntanceV1.processName ?? processIntanceV1.processId, |
| 194 | + ); |
| 195 | + }); |
| 196 | +}); |
| 197 | + |
| 198 | +describe('scenarios to verify mapToWorkflowRunStatusDTO', () => { |
| 199 | + it('correctly maps ProcessInstanceState to WorkflowRunStatusDTO', async () => { |
| 200 | + const mappedValue: WorkflowRunStatusDTO = mapToWorkflowRunStatusDTO( |
| 201 | + ProcessInstanceState.Active, |
| 202 | + ); |
| 203 | + |
| 204 | + expect(mappedValue).toBeDefined(); |
| 205 | + expect(mappedValue.key).toBeDefined(); |
| 206 | + expect(mappedValue.value).toBeDefined(); |
| 207 | + expect(mappedValue.key).toEqual('Active'); |
| 208 | + expect(mappedValue.value).toEqual('ACTIVE'); |
| 209 | + }); |
| 210 | +}); |
0 commit comments