|
| 1 | +/* |
| 2 | + * Copyright 2025 ByteChef |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.bytechef.task.dispatcher.on.error; |
| 18 | + |
| 19 | +import com.bytechef.atlas.coordinator.task.completion.TaskCompletionHandlerFactory; |
| 20 | +import com.bytechef.atlas.coordinator.task.dispatcher.TaskDispatcherResolverFactory; |
| 21 | +import com.bytechef.atlas.execution.service.ContextService; |
| 22 | +import com.bytechef.atlas.execution.service.CounterService; |
| 23 | +import com.bytechef.atlas.execution.service.TaskExecutionService; |
| 24 | +import com.bytechef.atlas.file.storage.TaskFileStorage; |
| 25 | +import com.bytechef.atlas.worker.exception.TaskExecutionException; |
| 26 | +import com.bytechef.atlas.worker.task.handler.TaskHandler; |
| 27 | +import com.bytechef.commons.util.EncodingUtils; |
| 28 | +import com.bytechef.evaluator.SpelEvaluator; |
| 29 | +import com.bytechef.exception.ExecutionException; |
| 30 | +import com.bytechef.platform.workflow.task.dispatcher.test.annotation.TaskDispatcherIntTest; |
| 31 | +import com.bytechef.platform.workflow.task.dispatcher.test.task.handler.TestVarTaskHandler; |
| 32 | +import com.bytechef.platform.workflow.task.dispatcher.test.workflow.TaskDispatcherJobTestExecutor; |
| 33 | +import com.bytechef.task.dispatcher.loop.LoopTaskDispatcher; |
| 34 | +import com.bytechef.task.dispatcher.loop.completion.LoopTaskCompletionHandler; |
| 35 | +import com.bytechef.task.dispatcher.on.error.completition.OnErrorTaskCompletionHandler; |
| 36 | +import java.nio.charset.StandardCharsets; |
| 37 | +import java.util.Collections; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Map; |
| 40 | +import org.junit.jupiter.api.Assertions; |
| 41 | +import org.junit.jupiter.api.BeforeEach; |
| 42 | +import org.junit.jupiter.api.Test; |
| 43 | +import org.springframework.beans.factory.annotation.Autowired; |
| 44 | +import org.springframework.context.ApplicationEventPublisher; |
| 45 | + |
| 46 | +/** |
| 47 | + * @author Matija Petanjek |
| 48 | + */ |
| 49 | +@TaskDispatcherIntTest |
| 50 | +public class OnErrorTaskDispatcherIntTest { |
| 51 | + |
| 52 | + private TestVarTaskHandler<Object, Object> testVarTaskHandler; |
| 53 | + |
| 54 | + @Autowired |
| 55 | + protected ContextService contextService; |
| 56 | + |
| 57 | + @Autowired |
| 58 | + protected TaskExecutionService taskExecutionService; |
| 59 | + |
| 60 | + @Autowired |
| 61 | + private TaskDispatcherJobTestExecutor taskDispatcherJobTestExecutor; |
| 62 | + |
| 63 | + @Autowired |
| 64 | + private TaskFileStorage taskFileStorage; |
| 65 | + |
| 66 | + @BeforeEach |
| 67 | + void beforeEach() { |
| 68 | + testVarTaskHandler = new TestVarTaskHandler<>(Map::put); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testOnErrorTaskDispatcherWhenNoException() { |
| 73 | + taskDispatcherJobTestExecutor.execute( |
| 74 | + EncodingUtils.base64EncodeToString("on-error_v1-no-exception".getBytes(StandardCharsets.UTF_8)), |
| 75 | + Collections.emptyMap(), |
| 76 | + this::getTaskCompletionHandlerFactories, |
| 77 | + this::getTaskDispatcherResolverFactories, |
| 78 | + this::getTaskHandlerMap); |
| 79 | + |
| 80 | + Assertions.assertEquals("main branch", testVarTaskHandler.get("mainBranchVar")); |
| 81 | + Assertions.assertNull(testVarTaskHandler.get("errorBranchVar")); |
| 82 | + Assertions.assertEquals("end", testVarTaskHandler.get("endVar")); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testOnErrorTaskDispatcherWhenExceptionInMainBranch() { |
| 87 | + taskDispatcherJobTestExecutor.execute( |
| 88 | + EncodingUtils.base64EncodeToString("on-error_v1-exception-main-branch".getBytes(StandardCharsets.UTF_8)), |
| 89 | + Collections.emptyMap(), |
| 90 | + this::getTaskCompletionHandlerFactories, |
| 91 | + this::getTaskDispatcherResolverFactories, |
| 92 | + this::getTaskHandlerMap); |
| 93 | + |
| 94 | + Assertions.assertEquals( |
| 95 | + "main branch before exception", testVarTaskHandler.get("beforeExceptionMainBranchVar")); |
| 96 | + Assertions.assertNull(testVarTaskHandler.get("afterExceptionMainBranchVar")); |
| 97 | + Assertions.assertEquals("error branch", testVarTaskHandler.get("errorBranchVar")); |
| 98 | + Assertions.assertEquals("end", testVarTaskHandler.get("endVar")); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testOnErrorTaskDispatcherWhenExceptionInOnErrorBranch() { |
| 103 | + ExecutionException executionException = Assertions.assertThrows( |
| 104 | + ExecutionException.class, () -> taskDispatcherJobTestExecutor.execute( |
| 105 | + EncodingUtils |
| 106 | + .base64EncodeToString("on-error_v1-exception-error-branch".getBytes(StandardCharsets.UTF_8)), |
| 107 | + Collections.emptyMap(), |
| 108 | + this::getTaskCompletionHandlerFactories, |
| 109 | + this::getTaskDispatcherResolverFactories, |
| 110 | + this::getTaskHandlerMap)); |
| 111 | + |
| 112 | + Assertions.assertEquals("test exception", executionException.getMessage()); |
| 113 | + |
| 114 | + Assertions.assertEquals("main branch", testVarTaskHandler.get("mainBranchVar")); |
| 115 | + Assertions.assertEquals( |
| 116 | + "before exception in error branch", testVarTaskHandler.get("beforeExceptionErrorBranchVar")); |
| 117 | + Assertions.assertNull(testVarTaskHandler.get("afterExceptionErrorBranchVar")); |
| 118 | + Assertions.assertNull(testVarTaskHandler.get("endVar")); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void testOnErrorTaskDispatcherInLoopTaskDispatcherWhenExceptionInMainBranch() { |
| 123 | + taskDispatcherJobTestExecutor.execute( |
| 124 | + EncodingUtils |
| 125 | + .base64EncodeToString("on-error_v1-exception-main-branch-in-loop_v1".getBytes(StandardCharsets.UTF_8)), |
| 126 | + Collections.emptyMap(), |
| 127 | + this::getTaskCompletionHandlerFactories, |
| 128 | + this::getTaskDispatcherResolverFactories, |
| 129 | + this::getTaskHandlerMap); |
| 130 | + |
| 131 | + Assertions.assertEquals( |
| 132 | + "main branch before exception, iteration number 2", |
| 133 | + testVarTaskHandler.get("beforeExceptionMainBranchVar")); |
| 134 | + Assertions.assertNull(testVarTaskHandler.get("afterExceptionMainBranchVar")); |
| 135 | + Assertions.assertEquals("error branch iteration number 2", testVarTaskHandler.get("errorBranchVar")); |
| 136 | + Assertions.assertEquals("end", testVarTaskHandler.get("endVar")); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testOnErrorTaskDispatcherInLoopTaskDispatcherWhenExceptionInErrorBranch() { |
| 141 | + ExecutionException executionException = Assertions.assertThrows( |
| 142 | + ExecutionException.class, () -> taskDispatcherJobTestExecutor.execute( |
| 143 | + EncodingUtils.base64EncodeToString( |
| 144 | + "on-error_v1-exception-error-branch-in-loop_v1".getBytes(StandardCharsets.UTF_8)), |
| 145 | + Collections.emptyMap(), |
| 146 | + this::getTaskCompletionHandlerFactories, |
| 147 | + this::getTaskDispatcherResolverFactories, |
| 148 | + this::getTaskHandlerMap)); |
| 149 | + |
| 150 | + Assertions.assertEquals("test exception", executionException.getMessage()); |
| 151 | + |
| 152 | + Assertions.assertEquals( |
| 153 | + "main branch before exception, iteration number 1", |
| 154 | + testVarTaskHandler.get("beforeExceptionMainBranchVar")); |
| 155 | + Assertions.assertNull(testVarTaskHandler.get("afterExceptionMainBranchVar")); |
| 156 | + Assertions.assertEquals( |
| 157 | + "error branch before exception, iteration number 1", |
| 158 | + testVarTaskHandler.get("beforeExceptionErrorBranchVar")); |
| 159 | + Assertions.assertNull(testVarTaskHandler.get("afterExceptionErrorBranchVar")); |
| 160 | + Assertions.assertNull(testVarTaskHandler.get("endVar")); |
| 161 | + } |
| 162 | + |
| 163 | + @SuppressWarnings("PMD") |
| 164 | + private List<TaskCompletionHandlerFactory> getTaskCompletionHandlerFactories( |
| 165 | + CounterService counterService, TaskExecutionService taskExecutionService) { |
| 166 | + |
| 167 | + return List.of( |
| 168 | + (taskCompletionHandler, taskDispatcher) -> new LoopTaskCompletionHandler( |
| 169 | + contextService, SpelEvaluator.create(), taskCompletionHandler, taskDispatcher, taskExecutionService, |
| 170 | + taskFileStorage), |
| 171 | + (taskCompletionHandler, taskDispatcher) -> new OnErrorTaskCompletionHandler( |
| 172 | + contextService, SpelEvaluator.create(), taskCompletionHandler, taskDispatcher, taskExecutionService, |
| 173 | + taskFileStorage)); |
| 174 | + } |
| 175 | + |
| 176 | + @SuppressWarnings("PMD") |
| 177 | + private List<TaskDispatcherResolverFactory> getTaskDispatcherResolverFactories( |
| 178 | + ApplicationEventPublisher eventPublisher, ContextService contextService, |
| 179 | + CounterService counterService, TaskExecutionService taskExecutionService) { |
| 180 | + |
| 181 | + return List.of((taskDispatcher) -> new LoopTaskDispatcher( |
| 182 | + contextService, SpelEvaluator.create(), eventPublisher, taskDispatcher, taskExecutionService, |
| 183 | + taskFileStorage), |
| 184 | + (taskDispatcher) -> new OnErrorTaskDispatcher( |
| 185 | + contextService, SpelEvaluator.create(), eventPublisher, taskDispatcher, taskExecutionService, |
| 186 | + taskFileStorage)); |
| 187 | + } |
| 188 | + |
| 189 | + private Map<String, TaskHandler<?>> getTaskHandlerMap() { |
| 190 | + return Map.of( |
| 191 | + "var/v1/set", testVarTaskHandler, |
| 192 | + "httpClient/v1/get", taskExecution -> { |
| 193 | + throw new TaskExecutionException("test exception"); |
| 194 | + }); |
| 195 | + } |
| 196 | +} |
0 commit comments