Skip to content

Commit

Permalink
feat: remove zeebe:subscription when no properties are left
Browse files Browse the repository at this point in the history
  • Loading branch information
barmac committed Oct 17, 2023
1 parent 428a5c7 commit b82ce6b
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 0 deletions.
85 changes: 85 additions & 0 deletions lib/camunda-cloud/CleanUpSubscriptionBehavior.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {
getBusinessObject,
is
} from 'bpmn-js/lib/util/ModelUtil';

import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';

import { getExtensionElementsList, removeExtensionElements } from '../util/ExtensionElementsUtil';

/**
* Zeebe BPMN behavior ensuring that zeebe:subscription is removed from bpmn:Message
* when it has no properties anymore.
*/
export default class CleanUpSubscriptionBehavior extends CommandInterceptor {
constructor(eventBus, commandStack) {
super(eventBus);

this.postExecuted([
'element.updateProperties',
'element.updateModdleProperties'
], context => {
const element = context.shape || context.newShape || context.element;

if (element.labelTarget) {
return;
}

if (!is(element, 'bpmn:Event')) {
return;
}

const messageEventDefinition = getMessageEventDefinition(element);

if (!messageEventDefinition) {
return;
}

const message = messageEventDefinition.get('messageRef');

if (!message) {
return;

Check warning on line 41 in lib/camunda-cloud/CleanUpSubscriptionBehavior.js

View check run for this annotation

Codecov / codecov/patch

lib/camunda-cloud/CleanUpSubscriptionBehavior.js#L41

Added line #L41 was not covered by tests
}

const subscription = getSubscription(message);

if (!subscription) {
return;
}

if (!hasNoProperties(subscription)) {
return;

Check warning on line 51 in lib/camunda-cloud/CleanUpSubscriptionBehavior.js

View check run for this annotation

Codecov / codecov/patch

lib/camunda-cloud/CleanUpSubscriptionBehavior.js#L51

Added line #L51 was not covered by tests
}

removeExtensionElements(element, message, subscription, commandStack);
}, true);
}
}

CleanUpSubscriptionBehavior.$inject = [
'eventBus',
'commandStack'
];


// helpers //////////

function getMessageEventDefinition(event) {
const businessObject = getBusinessObject(event);

return businessObject.get('eventDefinitions').find(eventDefinition => {
return is(eventDefinition, 'bpmn:MessageEventDefinition');
});
}

function getSubscription(message) {
return getExtensionElementsList(message, 'zeebe:Subscription')[ 0 ];
}

function hasNoProperties(element) {
const descriptor = element.$descriptor;

return descriptor.properties.every(property => {
return element.get(property.name) === undefined;
});
}
3 changes: 3 additions & 0 deletions lib/camunda-cloud/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import CleanUpBusinessRuleTaskBehavior from './CleanUpBusinessRuleTaskBehavior';
import CleanUpEndEventBehavior from './CleanUpEndEventBehavior';
import CleanUpSubscriptionBehavior from './CleanUpSubscriptionBehavior';
import CleanUpTimerExpressionBehavior from './CleanUpTimerExpressionBehavior';
import CopyPasteBehavior from './CopyPasteBehavior';
import CreateZeebeCallActivityBehavior from './CreateZeebeCallActivityBehavior';
Expand All @@ -13,6 +14,7 @@ export default {
__init__: [
'cleanUpBusinessRuleTaskBehavior',
'cleanUpEndEventBehavior',
'cleanUpSubscriptionBehavior',
'cleanUpTimerExpressionBehavior',
'copyPasteBehavior',
'createZeebeCallActivityBehavior',
Expand All @@ -24,6 +26,7 @@ export default {
],
cleanUpBusinessRuleTaskBehavior: [ 'type', CleanUpBusinessRuleTaskBehavior ],
cleanUpEndEventBehavior: [ 'type', CleanUpEndEventBehavior ],
cleanUpSubscriptionBehavior: [ 'type', CleanUpSubscriptionBehavior ],
cleanUpTimerExpressionBehavior: [ 'type', CleanUpTimerExpressionBehavior ],
copyPasteBehavior: [ 'type', CopyPasteBehavior ],
createZeebeCallActivityBehavior: [ 'type', CreateZeebeCallActivityBehavior ],
Expand Down
81 changes: 81 additions & 0 deletions test/camunda-cloud/CleanUpSubscriptionBehaviorSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {
bootstrapCamundaCloudModeler,
inject
} from 'test/TestHelper';

import { getBusinessObject } from 'bpmn-js/lib/util/ModelUtil';

import { getExtensionElementsList } from 'lib/util/ExtensionElementsUtil';

import diagramXML from './message-subscription.bpmn';


describe('camunda-cloud/features/modeling - CleanUpSubscriptionBehavior', function() {

beforeEach(bootstrapCamundaCloudModeler(diagramXML));


describe('removing zeebe:subscription when correlationKey is removed', function() {

let element;

beforeEach(inject(function(elementRegistry, modeling) {

// given
element = elementRegistry.get('Event');

const subscription = getSubscription(element);

// when
modeling.updateModdleProperties(element, subscription, {
correlationKey: undefined
});
}));


it('should execute', function() {

// then
const subscription = getSubscription(element);

expect(subscription).not.to.exist;
});


it('should undo', inject(function(commandStack) {

// when
commandStack.undo();

// then
const subscription = getSubscription(element);

expect(subscription).to.exist;
expect(subscription.get('correlationKey')).to.equal('=abc');
}));


it('should undo/redo', inject(function(commandStack) {

// when
commandStack.undo();
commandStack.redo();

// then
const subscription = getSubscription(element);

expect(subscription).not.to.exist;
}));

});
});

// helpers //////////

function getSubscription(element) {
const businessObject = getBusinessObject(element);
const eventDefiniton = businessObject.get('eventDefinitions')[ 0 ];
const message = eventDefiniton.get('messageRef');

return getExtensionElementsList(message, 'zeebe:Subscription')[ 0 ];
}
20 changes: 20 additions & 0 deletions test/camunda-cloud/message-subscription.bpmn
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:zeebe="http://camunda.org/schema/zeebe/1.0" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_0nxh8y3" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="5.17.0-nightly.20231015" modeler:executionPlatform="Camunda Cloud" modeler:executionPlatformVersion="8.3.0">
<bpmn:process id="Process_1qnptza" isExecutable="true">
<bpmn:intermediateCatchEvent id="Event">
<bpmn:messageEventDefinition id="MessageEventDefinition_0bwaev1" messageRef="Message_1lnjcbt" />
</bpmn:intermediateCatchEvent>
</bpmn:process>
<bpmn:message id="Message_1lnjcbt" name="Message">
<bpmn:extensionElements>
<zeebe:subscription correlationKey="=abc" />
</bpmn:extensionElements>
</bpmn:message>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1qnptza">
<bpmndi:BPMNShape id="Event_101rckl_di" bpmnElement="Event">
<dc:Bounds x="152" y="79" width="36" height="36" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>

0 comments on commit b82ce6b

Please sign in to comment.