/*! * @license * Copyright © 2005-2026 Hyland Software, Inc. and its affiliates. All rights reserved. * * 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 assert from 'assert'; import { resetGlobalMockAgent } from '../mockObjects/base.mock'; import { BpmAuthMock, ProcessInstanceVariablesMock } from '../mockObjects'; import { ProcessInstanceVariablesApi, AlfrescoApi } from '../../src'; import { describe, it, beforeEach, afterEach } from 'node:test'; describe('Activiti Process Instance Variables Api', () => { let authResponseBpmMock: BpmAuthMock; let variablesMock: ProcessInstanceVariablesMock; let alfrescoJsApi: AlfrescoApi; let processInstanceVariablesApi: ProcessInstanceVariablesApi; beforeEach(async () => { const BPM_HOST = 'https://127.0.0.1:9999'; authResponseBpmMock = new BpmAuthMock(BPM_HOST); variablesMock = new ProcessInstanceVariablesMock(BPM_HOST); authResponseBpmMock.get200Response(); alfrescoJsApi = new AlfrescoApi({ hostBpm: BPM_HOST, provider: 'BPM' }); processInstanceVariablesApi = new ProcessInstanceVariablesApi(alfrescoJsApi); await alfrescoJsApi.login('admin', 'admin'); }); afterEach(() => { resetGlobalMockAgent(); }); describe('get variables', () => { it('should return all variables for a process instance', async () => { const processInstanceId = '111'; variablesMock.addListProcessInstanceVariables200Response(processInstanceId); const data = await processInstanceVariablesApi.getProcessInstanceVariables(processInstanceId); assert.equal(data.length, 2); }); it('should emit an error when API returns an error response', async () => { const processInstanceId = '111'; variablesMock.addListProcessInstanceVariables500Response(processInstanceId); try { await processInstanceVariablesApi.getProcessInstanceVariables(processInstanceId); assert.fail('Expected getProcessInstanceVariables to throw error on 500 response'); } catch (error: any) { assert.equal(error.status, 500); assert.equal(error.message, '{"messageKey":"UNKNOWN","message":"Unknown error"}'); } }); }); describe('create or update variables', () => { it('should return all variables for a process instance', async () => { const processInstanceId = '111'; variablesMock.addPutProcessInstanceVariables200Response(processInstanceId); const data = await processInstanceVariablesApi.createOrUpdateProcessInstanceVariables(processInstanceId, []); assert.equal(data.length, 2); }); it('should emit an error when API returns an error response', async () => { const processInstanceId = '111'; variablesMock.addPutProcessInstanceVariables500Response(processInstanceId); try { await processInstanceVariablesApi.createOrUpdateProcessInstanceVariables(processInstanceId, []); assert.fail('Expected createOrUpdateProcessInstanceVariables to throw error on 500 response'); } catch (error: any) { assert.equal(error.status, 500); assert.equal(error.message, '{"messageKey":"UNKNOWN","message":"Unknown error"}'); } }); }); describe('get variable', () => { it('should call API to get variable', async () => { const processInstanceId = '111'; const variableName = 'var1'; variablesMock.addGetProcessInstanceVariable200Response(processInstanceId, variableName); const data = await processInstanceVariablesApi.getProcessInstanceVariable(processInstanceId, variableName); assert.equal(data.name, 'variable1'); assert.equal(data.value, 'Value 123'); }); it('should emit an error when API returns an error response', async () => { const processInstanceId = '111'; const variableName = 'var1'; variablesMock.addGetProcessInstanceVariable500Response(processInstanceId, variableName); try { await processInstanceVariablesApi.getProcessInstanceVariable(processInstanceId, variableName); assert.fail('Expected getProcessInstanceVariable to throw error on 500 response'); } catch (error: any) { assert.equal(error.status, 500); assert.equal(error.message, '{"messageKey":"UNKNOWN","message":"Unknown error"}'); } }); }); describe('update variable', () => { it('should call API to update variable', async () => { const processInstanceId = '111'; const variableName = 'var1'; variablesMock.addUpdateProcessInstanceVariable200Response(processInstanceId, variableName); const result = await processInstanceVariablesApi.updateProcessInstanceVariable(processInstanceId, variableName, {}); assert.ok(result !== undefined, 'updateProcessInstanceVariable should complete successfully'); }); it('should emit an error when API returns an error response', async () => { const processInstanceId = '111'; const variableName = 'var1'; variablesMock.addUpdateProcessInstanceVariable500Response(processInstanceId, variableName); try { await processInstanceVariablesApi.updateProcessInstanceVariable(processInstanceId, variableName, {}); assert.fail('Expected updateProcessInstanceVariable to throw error on 500 response'); } catch (error: any) { assert.equal(error.status, 500); assert.equal(error.message, '{"messageKey":"UNKNOWN","message":"Unknown error"}'); } }); }); describe('delete variable', () => { it('should call API to delete variables', async () => { const processInstanceId = '111'; const variableName = 'var1'; variablesMock.addDeleteProcessInstanceVariable200Response(processInstanceId, variableName); const result = await processInstanceVariablesApi.deleteProcessInstanceVariable(processInstanceId, variableName); assert.ok(result !== undefined, 'deleteProcessInstanceVariable should complete successfully'); }); it('should emit an error when API returns an error response', async () => { const processInstanceId = '111'; const variableName = 'var1'; variablesMock.addDeleteProcessInstanceVariable500Response(processInstanceId, variableName); try { await processInstanceVariablesApi.deleteProcessInstanceVariable(processInstanceId, variableName); assert.fail('Expected deleteProcessInstanceVariable to throw error on 500 response'); } catch (error: any) { assert.equal(error.status, 500); assert.equal(error.message, '{"messageKey":"UNKNOWN","message":"Unknown error"}'); } }); }); });