|
| 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.cmmn.test.event; |
| 14 | + |
| 15 | +import static org.assertj.core.api.Assertions.assertThat; |
| 16 | +import static org.flowable.common.engine.api.delegate.event.FlowableEngineEventType.ENTITY_UPDATED; |
| 17 | +import static org.flowable.common.engine.api.delegate.event.FlowableEngineEventType.JOB_RETRIES_DECREMENTED; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Set; |
| 23 | + |
| 24 | +import org.flowable.cmmn.api.runtime.CaseInstance; |
| 25 | +import org.flowable.cmmn.engine.test.CmmnDeployment; |
| 26 | +import org.flowable.cmmn.test.FlowableCmmnTestCase; |
| 27 | +import org.flowable.common.engine.api.delegate.event.AbstractFlowableEventListener; |
| 28 | +import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent; |
| 29 | +import org.flowable.common.engine.api.delegate.event.FlowableEvent; |
| 30 | +import org.flowable.common.engine.api.delegate.event.FlowableEventType; |
| 31 | +import org.flowable.job.api.Job; |
| 32 | +import org.flowable.task.api.Task; |
| 33 | +import org.junit.jupiter.api.AfterEach; |
| 34 | +import org.junit.jupiter.api.BeforeEach; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author martin.grofcik |
| 39 | + */ |
| 40 | +public class JobRetriesDecrementedEventTest extends FlowableCmmnTestCase { |
| 41 | + |
| 42 | + TestEventListener listener = new TestEventListener(); |
| 43 | + |
| 44 | + @BeforeEach |
| 45 | + void addListener() { |
| 46 | + listener.events.clear(); |
| 47 | + cmmnEngineConfiguration.getEventDispatcher().addEventListener(listener); |
| 48 | + } |
| 49 | + |
| 50 | + @AfterEach |
| 51 | + void removeListener() { |
| 52 | + cmmnEngineConfiguration.getEventDispatcher().removeEventListener(listener); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + @CmmnDeployment(resources = "org/flowable/cmmn/test/async/AsyncTaskTest.testAsyncServiceTask.cmmn") |
| 57 | + void noEventForSuccess() { |
| 58 | + CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder().caseDefinitionKey("testAsyncServiceTask").start(); |
| 59 | + Task task = cmmnTaskService.createTaskQuery() |
| 60 | + .caseInstanceId(caseInstance.getId()) |
| 61 | + .singleResult(); |
| 62 | + assertThat(task.getName()).isEqualTo("Task before service task"); |
| 63 | + cmmnTaskService.complete(task.getId()); |
| 64 | + |
| 65 | + waitForJobExecutorToProcessAllAsyncJobs(); |
| 66 | + |
| 67 | + assertThat(listener.events).isEmpty(); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + @CmmnDeployment(resources = "org/flowable/cmmn/test/async/AsyncTaskTest.testAsyncServiceTaskWithFailure.cmmn") |
| 72 | + void jobRetriesDecrementedOnFailure() { |
| 73 | + CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder().caseDefinitionKey("testAsyncServiceTask").start(); |
| 74 | + Task task = cmmnTaskService.createTaskQuery() |
| 75 | + .caseInstanceId(caseInstance.getId()) |
| 76 | + .singleResult(); |
| 77 | + assertThat(task.getName()).isEqualTo("Task before service task"); |
| 78 | + cmmnTaskService.complete(task.getId()); |
| 79 | + |
| 80 | + waitForJobExecutorToProcessAllAsyncJobs(); |
| 81 | + |
| 82 | + assertThat(listener.events) |
| 83 | + .filteredOn( e -> caseInstance.getId().equals(getScopeId(e))) |
| 84 | + .extracting(FlowableEvent::getType) |
| 85 | + .containsOnly(JOB_RETRIES_DECREMENTED, ENTITY_UPDATED); |
| 86 | + } |
| 87 | + |
| 88 | + private String getScopeId(FlowableEvent e) { |
| 89 | + return ((Job) ((FlowableEngineEntityEvent) e).getEntity()).getScopeId(); |
| 90 | + } |
| 91 | + |
| 92 | + private static class TestEventListener extends AbstractFlowableEventListener { |
| 93 | + private static final Collection<? extends FlowableEventType> SUPPORTED_TYPES = Set.of(JOB_RETRIES_DECREMENTED, ENTITY_UPDATED); |
| 94 | + List<FlowableEvent> events = new ArrayList<>(); |
| 95 | + |
| 96 | + @Override |
| 97 | + public void onEvent(FlowableEvent event) { |
| 98 | + events.add(event); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public boolean isFailOnException() { |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public Collection<? extends FlowableEventType> getTypes() { |
| 108 | + return SUPPORTED_TYPES; |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments