|
| 1 | +/* |
| 2 | + * Copyright 2023-2025 the original author or authors. |
| 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 org.springframework.ai.tool.execution; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | + |
| 22 | +import org.springframework.ai.tool.definition.DefaultToolDefinition; |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 25 | +import static org.assertj.core.api.InstanceOfAssertFactories.type; |
| 26 | + |
| 27 | +/** |
| 28 | + * Unit tests for {@link DefaultToolExecutionExceptionProcessor}. |
| 29 | + * |
| 30 | + * @author Daniel Garnier-Moiroux |
| 31 | + */ |
| 32 | +class DefaultToolExecutionExceptionProcessorTests { |
| 33 | + |
| 34 | + private final IllegalStateException toolException = new IllegalStateException("Inner exception"); |
| 35 | + |
| 36 | + private final DefaultToolDefinition toolDefinition = new DefaultToolDefinition("toolName", "toolDescription", |
| 37 | + "inputSchema"); |
| 38 | + |
| 39 | + private final ToolExecutionException toolExecutionException = new ToolExecutionException(toolDefinition, |
| 40 | + toolException); |
| 41 | + |
| 42 | + @Test |
| 43 | + void processReturnsMessage() { |
| 44 | + DefaultToolExecutionExceptionProcessor processor = DefaultToolExecutionExceptionProcessor.builder().build(); |
| 45 | + |
| 46 | + String result = processor.process(this.toolExecutionException); |
| 47 | + |
| 48 | + assertThat(result).isEqualTo(this.toolException.getMessage()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void processAlwaysThrows() { |
| 53 | + DefaultToolExecutionExceptionProcessor processor = DefaultToolExecutionExceptionProcessor.builder() |
| 54 | + .alwaysThrow(true) |
| 55 | + .build(); |
| 56 | + |
| 57 | + assertThatThrownBy(() -> processor.process(this.toolExecutionException)) |
| 58 | + .hasMessage(this.toolException.getMessage()) |
| 59 | + .hasCauseInstanceOf(this.toolException.getClass()) |
| 60 | + .asInstanceOf(type(ToolExecutionException.class)) |
| 61 | + .extracting(ToolExecutionException::getToolDefinition) |
| 62 | + .isEqualTo(this.toolDefinition); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void processRethrows() { |
| 67 | + DefaultToolExecutionExceptionProcessor processor = DefaultToolExecutionExceptionProcessor.builder() |
| 68 | + .alwaysThrow(false) |
| 69 | + .rethrowExceptions(List.of(IllegalStateException.class)) |
| 70 | + .build(); |
| 71 | + |
| 72 | + assertThatThrownBy(() -> processor.process(this.toolExecutionException)).isEqualTo(this.toolException); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void processRethrowsExceptionSubclasses() { |
| 77 | + DefaultToolExecutionExceptionProcessor processor = DefaultToolExecutionExceptionProcessor.builder() |
| 78 | + .alwaysThrow(false) |
| 79 | + .rethrowExceptions(List.of(RuntimeException.class)) |
| 80 | + .build(); |
| 81 | + |
| 82 | + assertThatThrownBy(() -> processor.process(this.toolExecutionException)).isEqualTo(this.toolException); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void processRethrowsOnlySelectExceptions() { |
| 87 | + DefaultToolExecutionExceptionProcessor processor = DefaultToolExecutionExceptionProcessor.builder() |
| 88 | + .alwaysThrow(false) |
| 89 | + .rethrowExceptions(List.of(IllegalStateException.class)) |
| 90 | + .build(); |
| 91 | + |
| 92 | + ToolExecutionException exception = new ToolExecutionException(this.toolDefinition, |
| 93 | + new RuntimeException("This exception was not rethrown")); |
| 94 | + String result = processor.process(exception); |
| 95 | + |
| 96 | + assertThat(result).isEqualTo("This exception was not rethrown"); |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments