From fe0c0a779b1ff86e7172092fa6a09b7c2d8f445d Mon Sep 17 00:00:00 2001 From: Niklas Kiefer Date: Mon, 28 Feb 2022 14:55:07 +0100 Subject: [PATCH] feat(usage-statistics): add cloud-templates to diagram open Closes #2786 --- .../event-handlers/DiagramOpenEventHandler.js | 35 +++-- .../__tests__/DiagramOpenEventHandlerSpec.js | 142 +++++++++++++++++- 2 files changed, 151 insertions(+), 26 deletions(-) diff --git a/client/src/plugins/usage-statistics/event-handlers/DiagramOpenEventHandler.js b/client/src/plugins/usage-statistics/event-handlers/DiagramOpenEventHandler.js index ddf1d1fecd..709c0fc61f 100644 --- a/client/src/plugins/usage-statistics/event-handlers/DiagramOpenEventHandler.js +++ b/client/src/plugins/usage-statistics/event-handlers/DiagramOpenEventHandler.js @@ -16,6 +16,8 @@ import { getMetrics } from '../../../util'; import { getEngineProfile as parseEngineProfile } from '../../../util/parse'; +import { getCloudTemplates, getPlatformTemplates } from '../../../util/elementTemplates'; + import { ENGINES } from '../../../util/Engines'; const HTTP_STATUS_PAYLOAD_TOO_BIG = 413; @@ -57,11 +59,7 @@ export default class DiagramOpenEventHandler extends BaseEventHandler { type } = tab; - if (type === types.BPMN) { - return await this.onCamundaDiagramOpened(file); - } else { // , maybe more in the future - return await this.onBpmnDiagramOpened(file, type); - } + return await this.onBpmnDiagramOpened(file, type); }); subscribe('dmn.modeler.created', () => { @@ -192,28 +190,19 @@ export default class DiagramOpenEventHandler extends BaseEventHandler { onBpmnDiagramOpened = async (file, type, context = {}) => { const diagramMetrics = await this.generateMetrics(file, type); - const engineProfile = await this.getEngineProfile(file, type); + const elementTemplates = await this.getElementTemplates(file, type); return await this.onDiagramOpened(types.BPMN, { diagramMetrics, engineProfile, + elementTemplates, ...context }); } - onCamundaDiagramOpened = async (file) => { - - const elementTemplates = await this.getElementTemplates(file); - - return await this.onBpmnDiagramOpened(file, types.BPMN, { - elementTemplates - }); - - } - - getElementTemplates = async (file) => { + getElementTemplates = async (file, type) => { const config = this._config; @@ -223,7 +212,9 @@ export default class DiagramOpenEventHandler extends BaseEventHandler { return []; } - return elementTemplates.map((elementTemplate) => { + const elementTemplateFilter = getElementTemplatesFilter(type); + + return elementTemplateFilter(elementTemplates).map((elementTemplate) => { const { appliesTo, properties } = elementTemplate; const propertyCounts = properties.map((property) => { @@ -264,3 +255,11 @@ function getDefaultExecutionPlatform(type) { return ENGINES.PLATFORM; } + +function getElementTemplatesFilter(type) { + if (type === 'cloud-bpmn') { + return getCloudTemplates; + } + + return getPlatformTemplates; +} diff --git a/client/src/plugins/usage-statistics/event-handlers/__tests__/DiagramOpenEventHandlerSpec.js b/client/src/plugins/usage-statistics/event-handlers/__tests__/DiagramOpenEventHandlerSpec.js index 5825cdd05f..5b12bad6fe 100644 --- a/client/src/plugins/usage-statistics/event-handlers/__tests__/DiagramOpenEventHandlerSpec.js +++ b/client/src/plugins/usage-statistics/event-handlers/__tests__/DiagramOpenEventHandlerSpec.js @@ -222,6 +222,7 @@ describe('', () => { }); }); + it('should not send with broken file contents: form', async () => { // given @@ -255,7 +256,7 @@ describe('', () => { describe('element templates', () => { - it('should send element templates', async () => { + async function expectTemplatesSent(elementTemplates, type, expected) { // given const subscribe = sinon.spy(); @@ -267,12 +268,12 @@ describe('', () => { const config = { get: (key, file) => { configSpy(key, file); - return mockElementTemplates(); + return elementTemplates; } }; const tab = createTab({ file: { path: 'testPath' }, - type: 'bpmn' + type }); // when @@ -286,11 +287,81 @@ describe('', () => { const configArgs = configSpy.getCall(0).args; - const elementTemplates = onSend.getCall(0).args[0].elementTemplates; + const sentElementTemplates = onSend.getCall(0).args[0].elementTemplates; // then expect(configArgs).to.eql([ 'bpmn.elementTemplates', { path: 'testPath' } ]); - expect(elementTemplates).to.eql([ + expect(sentElementTemplates).to.eql(expected); + } + + + it('should send element templates - platform', async () => { + + // given + const elementTemplates = mockPlatformElementTemplates(); + + const type = 'bpmn'; + + const expected = [ + { + appliesTo: [ 'bpmn:ServiceTask' ], + properties: { + 'camunda:asyncBefore': 1, + 'camunda:class': 1, + 'camunda:inputParameter': 3, + 'camunda:outputParameter': 1 + } + } + ]; + + // then + await expectTemplatesSent( + elementTemplates, + type, + expected + ); + }); + + + it('should send element templates - cloud', async () => { + + // given + const elementTemplates = mockCloudElementTemplates(); + + const type = 'cloud-bpmn'; + + const expected = [ + { + appliesTo: [ 'bpmn:ServiceTask' ], + properties: { + 'zeebe:input': 3, + 'zeebe:output': 1, + 'zeebe:taskDefinition:type': 1, + 'zeebe:taskHeader': 1 + } + } + ]; + + // then + await expectTemplatesSent( + elementTemplates, + type, + expected + ); + }); + + + it('should ONLY send platform element templates (mixture)', async () => { + + // given + const elementTemplates = [ + ...mockPlatformElementTemplates(), + ...mockCloudElementTemplates() + ]; + + const type = 'bpmn'; + + const expected = [ { appliesTo: [ 'bpmn:ServiceTask' ], properties: { @@ -300,7 +371,45 @@ describe('', () => { 'camunda:outputParameter': 1 } } - ]); + ]; + + // then + await expectTemplatesSent( + elementTemplates, + type, + expected + ); + }); + + + it('should ONLY send cloud element templates (mixture)', async () => { + + // given + const elementTemplates = [ + ...mockPlatformElementTemplates(), + ...mockCloudElementTemplates() + ]; + + const type = 'cloud-bpmn'; + + const expected = [ + { + appliesTo: [ 'bpmn:ServiceTask' ], + properties: { + 'zeebe:input': 3, + 'zeebe:output': 1, + 'zeebe:taskDefinition:type': 1, + 'zeebe:taskHeader': 1 + } + } + ]; + + // then + await expectTemplatesSent( + elementTemplates, + type, + expected + ); }); @@ -309,7 +418,7 @@ describe('', () => { // given const subscribe = sinon.spy(); - const config = { get: () => mockElementTemplates() }; + const config = { get: () => mockPlatformElementTemplates() }; const onSendSpy = sinon.spy(); @@ -1604,7 +1713,7 @@ describe('', () => { // helpers ////// -function mockElementTemplates() { +function mockPlatformElementTemplates() { return [ { appliesTo: [ 'bpmn:ServiceTask' ], @@ -1620,6 +1729,23 @@ function mockElementTemplates() { ]; } +function mockCloudElementTemplates() { + return [ + { + $schema: 'https://example.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json', + appliesTo: [ 'bpmn:ServiceTask' ], + properties: [ + { binding: { type: 'zeebe:taskDefinition:type' } }, + { binding: { name: 'sender', type: 'zeebe:input' } }, + { binding: { name: 'receivers', type: 'zeebe:input' } }, + { binding: { name: 'messageBody', type: 'zeebe:input' } }, + { binding: { source: 'result', type: 'zeebe:output' } }, + { binding: { key: 'header', type: 'zeebe:taskHeader' } } + ] + } + ]; +} + function createTab(overrides = {}) { return { id: 42,