|
| 1 | +/* Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | + * you may not use this file except in compliance with the License. |
| 3 | + * You may obtain a copy of the License at |
| 4 | + * |
| 5 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + */ |
| 13 | +package org.flowable.app.engine.test.event; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import org.flowable.app.api.repository.AppDefinition; |
| 22 | +import org.flowable.app.engine.test.BaseFlowableAppTest; |
| 23 | +import org.flowable.common.engine.api.delegate.event.AbstractFlowableEventListener; |
| 24 | +import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType; |
| 25 | +import org.flowable.common.engine.api.delegate.event.FlowableEntityEvent; |
| 26 | +import org.flowable.common.engine.api.delegate.event.FlowableEvent; |
| 27 | +import org.flowable.common.engine.api.delegate.event.FlowableEventType; |
| 28 | +import org.flowable.common.engine.api.repository.FlowableDefinition; |
| 29 | +import org.junit.jupiter.api.AfterEach; |
| 30 | +import org.junit.jupiter.api.BeforeEach; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +class DefinitionDeployedEventTest extends BaseFlowableAppTest { |
| 34 | + |
| 35 | + private static final String APP_RESOURCE = "definitionEvent.app"; |
| 36 | + |
| 37 | + private static final String APP_JSON = """ |
| 38 | + {"key":"definitionEventApp","name":"Definition Event App"}"""; |
| 39 | + |
| 40 | + private final CapturingListener listener = new CapturingListener(); |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void addListener() { |
| 44 | + appEngineConfiguration.getEventDispatcher().addEventListener(listener); |
| 45 | + } |
| 46 | + |
| 47 | + @AfterEach |
| 48 | + void removeListener() { |
| 49 | + appEngineConfiguration.getEventDispatcher().removeEventListener(listener); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void deployDispatchesDefinitionDeployedEvent() { |
| 54 | + String deploymentId = appRepositoryService.createDeployment() |
| 55 | + .name("definitionEventDeployment") |
| 56 | + .addString(APP_RESOURCE, APP_JSON) |
| 57 | + .deploy() |
| 58 | + .getId(); |
| 59 | + try { |
| 60 | + AppDefinition appDefinition = appRepositoryService.createAppDefinitionQuery() |
| 61 | + .deploymentId(deploymentId).singleResult(); |
| 62 | + |
| 63 | + assertThat(listener.deployed).hasSize(1); |
| 64 | + FlowableDefinition deployed = (FlowableDefinition) listener.deployed.get(0).getEntity(); |
| 65 | + assertThat(deployed.getKey()).isEqualTo(appDefinition.getKey()); |
| 66 | + assertThat(deployed.getVersion()).isEqualTo(appDefinition.getVersion()); |
| 67 | + assertThat(deployed.getDeploymentId()).isEqualTo(deploymentId); |
| 68 | + } finally { |
| 69 | + appRepositoryService.deleteDeployment(deploymentId, true); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void deleteDeploymentDispatchesDefinitionUndeployedEvent() { |
| 75 | + String deploymentId = appRepositoryService.createDeployment() |
| 76 | + .name("definitionEventDeployment") |
| 77 | + .addString(APP_RESOURCE, APP_JSON) |
| 78 | + .deploy() |
| 79 | + .getId(); |
| 80 | + AppDefinition appDefinition = appRepositoryService.createAppDefinitionQuery() |
| 81 | + .deploymentId(deploymentId).singleResult(); |
| 82 | + |
| 83 | + appRepositoryService.deleteDeployment(deploymentId, true); |
| 84 | + |
| 85 | + assertThat(listener.undeployed).hasSize(1); |
| 86 | + FlowableDefinition undeployed = (FlowableDefinition) listener.undeployed.get(0).getEntity(); |
| 87 | + assertThat(undeployed.getKey()).isEqualTo(appDefinition.getKey()); |
| 88 | + assertThat(undeployed.getVersion()).isEqualTo(appDefinition.getVersion()); |
| 89 | + assertThat(undeployed.getDeploymentId()).isEqualTo(deploymentId); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void duplicateDeploymentDoesNotDispatchDefinitionDeployedEvent() { |
| 94 | + String firstDeploymentId = appRepositoryService.createDeployment() |
| 95 | + .name("definitionEventDeployment") |
| 96 | + .addString(APP_RESOURCE, APP_JSON) |
| 97 | + .enableDuplicateFiltering() |
| 98 | + .deploy() |
| 99 | + .getId(); |
| 100 | + listener.deployed.clear(); |
| 101 | + |
| 102 | + String secondDeploymentId = appRepositoryService.createDeployment() |
| 103 | + .name("definitionEventDeployment") |
| 104 | + .addString(APP_RESOURCE, APP_JSON) |
| 105 | + .enableDuplicateFiltering() |
| 106 | + .deploy() |
| 107 | + .getId(); |
| 108 | + try { |
| 109 | + assertThat(secondDeploymentId).isEqualTo(firstDeploymentId); |
| 110 | + assertThat(listener.deployed).isEmpty(); |
| 111 | + } finally { |
| 112 | + appRepositoryService.deleteDeployment(firstDeploymentId, true); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + static class CapturingListener extends AbstractFlowableEventListener { |
| 117 | + |
| 118 | + final List<FlowableEntityEvent> deployed = new ArrayList<>(); |
| 119 | + final List<FlowableEntityEvent> undeployed = new ArrayList<>(); |
| 120 | + |
| 121 | + @Override |
| 122 | + public void onEvent(FlowableEvent event) { |
| 123 | + if (event.getType() == FlowableEngineEventType.DEFINITION_DEPLOYED) { |
| 124 | + deployed.add((FlowableEntityEvent) event); |
| 125 | + } else if (event.getType() == FlowableEngineEventType.DEFINITION_UNDEPLOYED) { |
| 126 | + undeployed.add((FlowableEntityEvent) event); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public boolean isFailOnException() { |
| 132 | + return true; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public Collection<? extends FlowableEventType> getTypes() { |
| 137 | + return List.of(FlowableEngineEventType.DEFINITION_DEPLOYED, FlowableEngineEventType.DEFINITION_UNDEPLOYED); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments