process-reporting-ts
Version:
Process reporting with typescript
156 lines (155 loc) • 8.01 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const BpmnElement_1 = require("./BpmnElement");
const models_1 = require("./models");
const axios_1 = __importDefault(require("axios"));
jest.mock('axios');
const mockedAxios = axios_1.default;
describe('Intercept on class method', () => {
const events = [];
beforeEach(() => {
events.length = 0;
mockedAxios.post.mockImplementation((url, data) => {
events.push(data);
return Promise.resolve(data);
});
process.env.REPORTING_SERVER = 'http://localhost:8080';
process.env.REPORTING_PROCESS_ID = 'process-id';
process.env.REPORTING_PROCESS_VERSION = '1.0';
process.env.REPORTING_EVENT_CATEGORY = 'UnitTest';
});
afterEach(() => {
jest.resetAllMocks();
});
test('should be COMPLETED', () => __awaiter(void 0, void 0, void 0, function* () {
// Given
class Service {
execute(input) {
return { output: input };
}
}
__decorate([
(0, BpmnElement_1.BpmnElement)({
id: 'StartEvent_1',
startEvent: true,
instanceIdExpression: '{{ input }}',
keyExpression: '{{ input }}'
})
], Service.prototype, "execute", null);
const sut = new Service();
const input = 'foo';
// When
const answer = yield sut.execute(input);
expect(answer).toEqual({ output: input });
// Then
expect(events.length).toBe(2);
expect(mockedAxios.post).toHaveBeenCalledWith('http://localhost:8080/reporting-service/rest/api/report', events[0]);
expect(mockedAxios.post).toHaveBeenCalledWith('http://localhost:8080/reporting-service/rest/api/report', events[1]);
expect(events[0].status).toEqual(models_1.ReportStatus.STARTED);
expect(events[0].payload).toEqual(JSON.stringify({ input }));
expect(events[0].elementId).toEqual('StartEvent_1');
expect(events[0].transactionId).toEqual(input);
expect(events[0].referenceType).toEqual(process.env.REPORTING_EVENT_CATEGORY);
expect(events[0].referenceId).toEqual(input);
expect(events[0].startEvent).toEqual(true);
expect(events[0].endEvent).toEqual(false);
expect(events[0].multipleInstanceIndex).toEqual('0.0');
expect(events[0].errorMessage).not.toBeDefined();
expect(events[0].processId).toEqual(process.env.REPORTING_PROCESS_ID);
expect(events[0].processVersion).toEqual(process.env.REPORTING_PROCESS_VERSION);
expect(events[0].executionId).toBeDefined();
expect(events[0].eventTime).toBeDefined();
expect(events[0].startedBy).toEqual('system');
expect(events[1].status).toEqual(models_1.ReportStatus.COMPLETED);
expect(events[1].payload).toEqual(JSON.stringify({ output: input }));
expect(events[1].elementId).toEqual('StartEvent_1');
expect(events[1].transactionId).toEqual(input);
expect(events[1].referenceType).toEqual('UnitTest');
expect(events[1].referenceId).toEqual(input);
expect(events[1].startEvent).toEqual(true);
expect(events[1].endEvent).toEqual(false);
expect(events[1].multipleInstanceIndex).toEqual('0.0');
expect(events[1].errorMessage).not.toBeDefined();
expect(events[1].processId).toEqual(process.env.REPORTING_PROCESS_ID);
expect(events[1].processVersion).toEqual(process.env.REPORTING_PROCESS_VERSION);
expect(events[1].executionId).toBeDefined();
expect(events[1].eventTime).toBeDefined();
expect(events[1].startedBy).toEqual('system');
}));
test('should be ERROR', () => __awaiter(void 0, void 0, void 0, function* () {
// Given
class Service {
execute(message) {
throw new Error('Business Error');
}
}
__decorate([
(0, BpmnElement_1.BpmnElement)({
id: 'StartEvent_1',
endEvent: true,
instanceIdExpression: '{{ message }}',
startedByExpression: '{{ message }}',
keyExpression: '{{ message }}'
})
], Service.prototype, "execute", null);
const sut = new Service();
const message = 'foo';
// When
try {
yield sut.execute('foo');
}
catch (e) {
expect(e).toEqual(new Error('Business Error'));
}
// Then
expect(events.length).toEqual(2);
expect(mockedAxios.post).toHaveBeenCalledWith('http://localhost:8080/reporting-service/rest/api/report', events[0]);
expect(mockedAxios.post).toHaveBeenCalledWith('http://localhost:8080/reporting-service/rest/api/report', events[1]);
expect(events[0].status).toEqual(models_1.ReportStatus.STARTED);
expect(events[0].payload).toEqual(JSON.stringify({ message }));
expect(events[0].elementId).toEqual('StartEvent_1');
expect(events[0].transactionId).toEqual(message);
expect(events[0].referenceId).toEqual(message);
expect(events[0].startEvent).toEqual(false);
expect(events[0].endEvent).toEqual(true);
expect(events[0].multipleInstanceIndex).toEqual('0.0');
expect(events[0].errorMessage).not.toBeDefined();
expect(events[0].processId).toEqual(process.env.REPORTING_PROCESS_ID);
expect(events[0].processVersion).toEqual(process.env.REPORTING_PROCESS_VERSION);
expect(events[0].executionId).toBeDefined();
expect(events[0].eventTime).toBeDefined();
expect(events[0].startedBy).toEqual('foo');
expect(events[1].status).toEqual(models_1.ReportStatus.ERROR);
expect(events[1].payload).not.toBeDefined();
expect(events[1].elementId).toEqual('StartEvent_1');
expect(events[1].transactionId).toEqual(message);
expect(events[1].referenceId).toEqual(message);
expect(events[1].startEvent).toEqual(false);
expect(events[1].endEvent).toEqual(true);
expect(events[1].multipleInstanceIndex).toEqual('0.0');
expect(events[1].errorMessage).toEqual('Error: Business Error');
expect(events[1].processId).toEqual(process.env.REPORTING_PROCESS_ID);
expect(events[1].processVersion).toEqual(process.env.REPORTING_PROCESS_VERSION);
expect(events[1].executionId).toBeDefined();
expect(events[1].eventTime).toBeDefined();
expect(events[1].startedBy).toEqual('foo');
}));
});