diff --git a/ali-agentic-adk-java/ali-agentic-adk-core/src/test/java/com/alibaba/agentic/core/utils/AssertUtilsTest.java b/ali-agentic-adk-java/ali-agentic-adk-core/src/test/java/com/alibaba/agentic/core/utils/AssertUtilsTest.java new file mode 100644 index 00000000..a334253c --- /dev/null +++ b/ali-agentic-adk-java/ali-agentic-adk-core/src/test/java/com/alibaba/agentic/core/utils/AssertUtilsTest.java @@ -0,0 +1,170 @@ +/** + * Copyright (C) 2024 AIDC-AI + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.agentic.core.utils; + +import com.alibaba.agentic.core.Application; +import com.alibaba.agentic.core.engine.constants.NodeIdConstant; +import com.alibaba.agentic.core.exceptions.BaseException; +import com.alibaba.agentic.core.executor.InvokeMode; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * DESCRIPTION + * + * @author xc1080 + * @date 2025/11/15 + */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = { Application.class }) +@ActiveProfiles("testing") +public class AssertUtilsTest { + + @Test + public void testAssertNotIn_Success() { + String nodeId = "customNode_123"; + String result = AssertUtils.assertNotIn(nodeId, NodeIdConstant.RESERVED_NODE_ID); + Assert.assertEquals("customNode_123", result); + } + + @Test(expected = BaseException.class) + public void testAssertNotIn_ThrowsWhenObjectInCollection() { + AssertUtils.assertNotIn("start", NodeIdConstant.RESERVED_NODE_ID); + } + + @Test(expected = BaseException.class) + public void testAssertNotIn_ThrowsWhenCollectionIsEmpty() { + List emptyList = Collections.emptyList(); + AssertUtils.assertNotIn("anyValue", emptyList); + } + + @Test + public void testAssertIn_Success() { + List allowedNodeIds = Arrays.asList("llmNode", "toolNode", "decisionNode"); + String result = AssertUtils.assertIn("llmNode", allowedNodeIds); + Assert.assertEquals("llmNode", result); + } + + @Test(expected = BaseException.class) + public void testAssertIn_ThrowsWhenObjectNotInCollection() { + List allowedTypes = Arrays.asList("llm", "tool", "delegation"); + AssertUtils.assertIn("unknown", allowedTypes); + } + + @Test(expected = BaseException.class) + public void testAssertIn_ThrowsWhenCollectionIsEmpty() { + List emptyList = Collections.emptyList(); + AssertUtils.assertIn("value", emptyList); + } + + @Test(expected = BaseException.class) + public void testAssertIn_ThrowsWhenObjectIsNull() { + List validValues = Arrays.asList("value1", "value2"); + AssertUtils.assertIn(null, validValues); + } + + @Test + public void testAssertNotNull_Success() { + InvokeMode invokeMode = InvokeMode.SYNC; + InvokeMode result = AssertUtils.assertNotNull(invokeMode); + Assert.assertEquals(InvokeMode.SYNC, result); + } + + @Test + public void testAssertNotNull_SuccessWithNonEnumObject() { + InvokeMode mode = InvokeMode.ASYNC; + InvokeMode result = AssertUtils.assertNotNull(mode); + Assert.assertEquals(InvokeMode.ASYNC, result); + } + + @Test(expected = BaseException.class) + public void testAssertNotNull_ThrowsWhenObjectIsNull() { + AssertUtils.assertNotNull(null); + } + + @Test + public void testAssertTrue_Success() { + Boolean result = AssertUtils.assertTrue(Boolean.TRUE); + Assert.assertTrue(result); + } + + @Test(expected = BaseException.class) + public void testAssertTrue_ThrowsWhenValueIsFalse() { + AssertUtils.assertTrue(Boolean.FALSE); + } + + @Test(expected = BaseException.class) + public void testAssertTrue_ThrowsWhenValueIsNull() { + AssertUtils.assertTrue(null); + } + + @Test + public void testAssertNotBlank_Success() { + String nodeId = "llmNode_001"; + String result = AssertUtils.assertNotBlank(nodeId); + Assert.assertEquals("llmNode_001", result); + } + + @Test + public void testAssertNotBlank_SuccessWithSpaces() { + // 包含空格的文本仍然通过验证 + String flowName = " Agent Flow Canvas "; + String result = AssertUtils.assertNotBlank(flowName); + Assert.assertEquals(" Agent Flow Canvas ", result); + } + + @Test(expected = BaseException.class) + public void testAssertNotBlank_ThrowsWhenStringIsNull() { + AssertUtils.assertNotBlank(null); + } + + @Test(expected = BaseException.class) + public void testAssertNotBlank_ThrowsWhenStringIsEmpty() { + AssertUtils.assertNotBlank(""); + } + + @Test(expected = BaseException.class) + public void testAssertNotBlank_ThrowsWhenStringIsWhitespace() { + AssertUtils.assertNotBlank(" "); + } + + @Test + public void testAssertNotBlank_WithCustomMessage_Success() { + String processId = "process_12345"; + String result = AssertUtils.assertNotBlank(processId, "Process ID is required"); + Assert.assertEquals("process_12345", result); + } + + @Test + public void testAssertNotBlank_WithCustomMessage_VerifyMessage() { + // 验证自定义错误消息 + String customMessage = "Node ID cannot be blank"; + try { + AssertUtils.assertNotBlank("", customMessage); + Assert.fail("Should have thrown BaseException"); + } catch (BaseException e) { + Assert.assertEquals(customMessage, e.getMessage()); + } + } +}