Skip to content

Commit

Permalink
test(orchestrator): add unit tests for v2 endpoints (janus-idp#1300)
Browse files Browse the repository at this point in the history
* Unit Test: executeWorkflow (V2)

Signed-off-by: Kamlesh Panchal <[email protected]>

Add Helpers.ts mock

Signed-off-by: Gloria Ciavarrini <[email protected]>

* Unit Test: getWorkflowResults (v2)

Signed-off-by: Kamlesh Panchal <[email protected]>

* Unit Test: getInstances (v2)

Signed-off-by: Gloria Ciavarrini <[email protected]>

* Unit Test: getInstanceById (v2)

Signed-off-by: Gloria Ciavarrini <[email protected]>

* Unit Test: abortWorkflow (v2)

Signed-off-by: Kamlesh Panchal <[email protected]>

* refactor: introduce mapToWorkflowRunStatusDTO mapper

* Unit Test: mapToWorkflowRunStatusDTO and firstLetterToUppercase mapper methods

* Unit Test: getWorkflowStatuses

* fix unit test getWorkflowsOverview (v2)

Signed-off-by: Gloria Ciavarrini <[email protected]>

* fix: include annotation value in the test workflowDefinition obj

* fix: add pagination to unit tests

Signed-off-by: Gloria Ciavarrini <[email protected]>

* fix typos

Signed-off-by: Gloria Ciavarrini <[email protected]>

* Move StriingUtils from orchestrator to orchestrator-common

Signed-off-by: Gloria Ciavarrini <[email protected]>

* fix unit test abortWorkflow

Signed-off-by: Gloria Ciavarrini <[email protected]>

---------

Signed-off-by: Gloria Ciavarrini <[email protected]>
Signed-off-by: Kamlesh Panchal <[email protected]>
Co-authored-by: Kamlesh Panchal <[email protected]>
Co-authored-by: Jude Niroshan <[email protected]>
  • Loading branch information
3 people authored Mar 8, 2024
1 parent d16109a commit 9a13138
Show file tree
Hide file tree
Showing 16 changed files with 797 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,93 @@
import { WorkflowOverview } from '@janus-idp/backstage-plugin-orchestrator-common';
import moment from 'moment';

import { generateTestWorkflowOverview } from '../test-utils';
import {
ProcessInstance,
ProcessInstanceState,
WorkflowOverview,
WorkflowRunStatusDTO,
} from '@janus-idp/backstage-plugin-orchestrator-common';

import {
generateProcessInstance,
generateTestExecuteWorkflowResponse,
generateTestWorkflowOverview,
} from '../test-utils';
import assessedProcessInstanceData from './__fixtures__/assessedProcessInstance.json';
import {
getProcessInstancesDTOFromString,
mapToExecuteWorkflowResponseDTO,
mapToGetWorkflowInstanceResults,
mapToProcessInstanceDTO,
mapToWorkflowOverviewDTO,
mapToWorkflowRunStatusDTO,
mapWorkflowCategoryDTOFromString,
} from './V2Mappings';

describe('scenarios to verify mapToGetWorkflowInstanceResults', () => {
it('correctly maps positive scenario response', async () => {
const assessedProcessInstance = assessedProcessInstanceData;

const mappedValue = mapToGetWorkflowInstanceResults(
// @ts-ignore
assessedProcessInstance.instance.variables,
);

expect(mappedValue).toBeDefined();
expect(mappedValue.result).toBeDefined();
expect(mappedValue.preCheck).toBeDefined();
expect(mappedValue.workflowOptions).toBeDefined();
expect(mappedValue.repositoryUrl).toEqual('https://java.com');
expect(Object.keys(mappedValue).length).toBe(4);
});

it('correctly maps string response', async () => {
const testValue = 'string_value';
const mappedValue = mapToGetWorkflowInstanceResults(testValue);
expect(mappedValue).toBeDefined();
expect(Object.keys(mappedValue).length).toBe(1);
expect(mappedValue.variables).toBeDefined();
expect(mappedValue.variables).toEqual(testValue);
});

it('correctly returns empty workflowoptions when variables property does not exist', async () => {
const assessedProcessInstance = assessedProcessInstanceData;

// @ts-ignore
delete assessedProcessInstance.instance.variables;

const mappedValue = mapToGetWorkflowInstanceResults(
// @ts-ignore
assessedProcessInstance.instance.variables,
);

expect(mappedValue).toBeDefined();
expect(Object.keys(mappedValue).length).toBe(1);
expect(mappedValue.workflowoptions).toBeDefined();
expect(mappedValue.workflowoptions?.length).toBe(0);
});
});

describe('scenarios to verify executeWorkflowResponseDTO', () => {
it('correctly maps positive scenario response', async () => {
const execWorkflowResp = generateTestExecuteWorkflowResponse();
const mappedValue = mapToExecuteWorkflowResponseDTO(
'test_workflowId',
execWorkflowResp,
);
expect(mappedValue).toBeDefined();
expect(mappedValue.id).toBeDefined();
expect(Object.keys(mappedValue).length).toBe(1);
});

it('throws error when no id attribute present in response', async () => {
expect(() => {
mapToExecuteWorkflowResponseDTO('workflowId', { id: '' });
}).toThrow(
`Error while mapping ExecuteWorkflowResponse to ExecuteWorkflowResponseDTO for workflow with id`,
);
});
});

describe('scenarios to verify mapToWorkflowOverviewDTO', () => {
it('correctly maps WorkflowOverview', () => {
// Arrange
Expand Down Expand Up @@ -47,3 +129,82 @@ describe('scenarios to verify mapWorkflowCategoryDTOFromString', () => {
expect(resultCategory).toBe(expected);
});
});

describe('scenarios to verify mapToProcessInstanceDTO', () => {
it('correctly maps ProcessInstanceDTO for not completed workflow', () => {
// Arrange
const processIntanceV1: ProcessInstance = generateProcessInstance(1);
processIntanceV1.end = undefined;

// Act
const result = mapToProcessInstanceDTO(processIntanceV1);

// Assert
expect(result).toBeDefined();
expect(result.id).toBeDefined();
expect(result.start).toBeDefined();
expect(result.start).toEqual(processIntanceV1.start?.toUTCString());
expect(result.end).toBeUndefined();
expect(result.duration).toBeUndefined();
expect(result.status).toEqual(
getProcessInstancesDTOFromString(processIntanceV1.state),
);
expect(result.description).toEqual(processIntanceV1.description);
expect(result.category).toEqual('infrastructure');
expect(result.workflowdata).toEqual(
// @ts-ignore
processIntanceV1?.variables?.workflowdata,
);
expect(result.workflow).toEqual(
processIntanceV1.processName ?? processIntanceV1.processId,
);
});
it('correctly maps ProcessInstanceDTO', () => {
// Arrange
const processIntanceV1: ProcessInstance = generateProcessInstance(1);

const start = moment(processIntanceV1.start);
const end = moment(processIntanceV1.end);
const duration = moment.duration(start.diff(end)).humanize();
// Act
const result = mapToProcessInstanceDTO(processIntanceV1);

// Assert
expect(result.id).toBeDefined();
expect(result.start).toEqual(processIntanceV1.start?.toUTCString());
expect(result.end).toBeDefined();
expect(result.end).toEqual(processIntanceV1.end!.toUTCString());
expect(result.duration).toEqual(duration);

expect(result).toBeDefined();
expect(result.status).toEqual(
getProcessInstancesDTOFromString(processIntanceV1.state),
);
expect(result.end).toEqual(processIntanceV1.end?.toUTCString());
expect(result.duration).toEqual(duration);
expect(result.duration).toEqual('an hour');
expect(result.description).toEqual(processIntanceV1.description);
expect(result.category).toEqual('infrastructure');
expect(result.workflowdata).toEqual(
// @ts-ignore
processIntanceV1?.variables?.workflowdata,
);
expect(result.workflow).toEqual(
processIntanceV1.processName ?? processIntanceV1.processId,
);
});
});

describe('scenarios to verify mapToWorkflowRunStatusDTO', () => {
it('correctly maps ProcessInstanceState to WorkflowRunStatusDTO', async () => {
const mappedValue: WorkflowRunStatusDTO = mapToWorkflowRunStatusDTO(
ProcessInstanceState.Active,
);

expect(mappedValue).toBeDefined();
expect(mappedValue.key).toBeDefined();
expect(mappedValue.value).toBeDefined();
expect(mappedValue.key).toEqual('Active');
expect(mappedValue.value).toEqual('ACTIVE');
});
});
24 changes: 19 additions & 5 deletions plugins/orchestrator-backend/src/service/api/mapping/V2Mappings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import moment from 'moment';

import {
capitalize,
ExecuteWorkflowResponseDTO,
extractWorkflowFormat,
fromWorkflowSource,
Expand All @@ -18,6 +19,7 @@ import {
WorkflowFormatDTO,
WorkflowOverview,
WorkflowOverviewDTO,
WorkflowRunStatusDTO,
} from '@janus-idp/backstage-plugin-orchestrator-common';

// Mapping functions
Expand Down Expand Up @@ -95,9 +97,11 @@ export function getProcessInstancesDTOFromString(
export function mapToProcessInstanceDTO(
processInstance: ProcessInstance,
): ProcessInstanceDTO {
const start = moment(processInstance.start?.toString());
const end = moment(processInstance.end?.toString());
const duration = moment.duration(start.diff(end));
const start = moment(processInstance.start);
const end = moment(processInstance.end);
const duration = processInstance.end
? moment.duration(start.diff(end)).humanize()
: undefined;

let variables: Record<string, unknown> | undefined;
if (typeof processInstance?.variables === 'string') {
Expand All @@ -109,13 +113,14 @@ export function mapToProcessInstanceDTO(
return {
category: mapWorkflowCategoryDTO(processInstance.category),
description: processInstance.description,
duration: duration.humanize(),
id: processInstance.id,
name: processInstance.processName,
// To be fixed https://issues.redhat.com/browse/FLPATH-950
// @ts-ignore
workflowdata: variables?.workflowdata,
started: start.toDate().toLocaleString(),
start: processInstance.start?.toUTCString(),
end: processInstance.end?.toUTCString(),
duration: duration,
status: getProcessInstancesDTOFromString(processInstance.state),
workflow: processInstance.processName ?? processInstance.processId,
};
Expand Down Expand Up @@ -158,3 +163,12 @@ export function mapToGetWorkflowInstanceResults(

return returnObject;
}

export function mapToWorkflowRunStatusDTO(
status: ProcessInstanceState,
): WorkflowRunStatusDTO {
return {
key: capitalize(status),
value: status,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"instance": {
"id": "026f38fc-6121-46c7-9fa8-3f4b8207bab9",
"processName": "Assessment",
"processId": "assessment",
"businessKey": null,
"state": "COMPLETED",
"start": "2024-02-15T16:11:35.829Z",
"lastUpdate": "2024-02-15T16:11:35.829Z",
"end": "2024-02-15T16:11:35.822Z",
"nodes": [
{
"id": "3290d3c5-c7c0-4920-a1dc-7a37d98d22b7",
"nodeId": "_jbpm-unique-51",
"definitionId": "_jbpm-unique-51",
"type": "WorkItemNode",
"name": "execute",
"enter": "2024-02-15T16:11:33.485Z",
"exit": "2024-02-15T16:11:35.828Z"
},
{
"id": "2813e81e-0c38-424f-a505-639b1d33341b",
"nodeId": "_jbpm-unique-50",
"definitionId": "_jbpm-unique-50",
"type": "StartNode",
"name": "EmbeddedStart",
"enter": "2024-02-15T16:11:33.477Z",
"exit": "2024-02-15T16:11:35.829Z"
},
{
"id": "f15d5540-0df7-401c-991e-3c45755b1302",
"nodeId": "_jbpm-unique-47",
"definitionId": "_jbpm-unique-47",
"type": "StartNode",
"name": "Start",
"enter": "2024-02-15T16:11:33.472Z",
"exit": "2024-02-15T16:11:35.829Z"
},
{
"id": "142fcd3a-64e6-4539-a6b9-5ba2064623a1",
"nodeId": "_jbpm-unique-48",
"definitionId": "_jbpm-unique-48",
"type": "EndNode",
"name": "End",
"enter": "2024-02-15T16:11:35.821Z",
"exit": "2024-02-15T16:11:35.827Z"
},
{
"id": "97e209c4-ae0a-4d09-8b00-4bd9ca062126",
"nodeId": "_jbpm-unique-59",
"definitionId": "_jbpm-unique-59",
"type": "ActionNode",
"name": "Script",
"enter": "2024-02-15T16:11:35.812Z",
"exit": "2024-02-15T16:11:35.827Z"
},
{
"id": "f415ddd5-ba25-4e0a-be93-797b5f0ae6f3",
"nodeId": "_jbpm-unique-49",
"definitionId": "_jbpm-unique-49",
"type": "CompositeContextNode",
"name": "AssessRepository",
"enter": "2024-02-15T16:11:33.476Z",
"exit": "2024-02-15T16:11:35.827Z"
},
{
"id": "10879e09-12c0-4770-be2b-d2fd27858edc",
"nodeId": "_jbpm-unique-58",
"definitionId": "_jbpm-unique-58",
"type": "EndNode",
"name": "EmbeddedEnd",
"enter": "2024-02-15T16:11:35.812Z",
"exit": "2024-02-15T16:11:35.827Z"
},
{
"id": "96bb3b01-2a4a-44b2-b29c-da6dc7448450",
"nodeId": "_jbpm-unique-57",
"definitionId": "_jbpm-unique-57",
"type": "ActionNode",
"name": "Script",
"enter": "2024-02-15T16:11:35.811Z",
"exit": "2024-02-15T16:11:35.827Z"
},
{
"id": "491975f5-4d6a-4a43-ac00-1e305bfcd97a",
"nodeId": "_jbpm-unique-56",
"definitionId": "_jbpm-unique-56",
"type": "ActionNode",
"name": "logOuput",
"enter": "2024-02-15T16:11:35.809Z",
"exit": "2024-02-15T16:11:35.828Z"
},
{
"id": "bc4ddb8a-f551-442a-a0d9-7f96a8332a09",
"nodeId": "_jbpm-unique-55",
"definitionId": "_jbpm-unique-55",
"type": "ActionNode",
"name": "Script",
"enter": "2024-02-15T16:11:35.807Z",
"exit": "2024-02-15T16:11:35.828Z"
},
{
"id": "effbf2d7-8201-48de-8f81-3983fb26d591",
"nodeId": "_jbpm-unique-54",
"definitionId": "_jbpm-unique-54",
"type": "WorkItemNode",
"name": "preCheck",
"enter": "2024-02-15T16:11:33.896Z",
"exit": "2024-02-15T16:11:35.828Z"
},
{
"id": "54c7da48-de5c-4e62-868b-ad6c7ef25ad3",
"nodeId": "_jbpm-unique-53",
"definitionId": "_jbpm-unique-53",
"type": "ActionNode",
"name": "Script",
"enter": "2024-02-15T16:11:33.894Z",
"exit": "2024-02-15T16:11:35.828Z"
},
{
"id": "bfc5796c-8aa7-4c4f-aa11-dc91aee24ea2",
"nodeId": "_jbpm-unique-52",
"definitionId": "_jbpm-unique-52",
"type": "ActionNode",
"name": "Script",
"enter": "2024-02-15T16:11:33.826Z",
"exit": "2024-02-15T16:11:35.828Z"
}
],
"variables": {
"workflowdata": {
"result": "[Object]",
"preCheck": "[Object]",
"repositoryUrl": "https://java.com",
"workflowOptions": "[Object]"
}
},
"parentProcessInstance": null,
"error": null,
"category": "assessment",
"description": "undefined"
}
}
Loading

0 comments on commit 9a13138

Please sign in to comment.