|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.nageoffer.ai.ragent.ingestion.engine; |
| 19 | + |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | +import com.nageoffer.ai.ragent.framework.exception.ClientException; |
| 22 | +import com.nageoffer.ai.ragent.ingestion.domain.context.IngestionContext; |
| 23 | +import com.nageoffer.ai.ragent.ingestion.domain.enums.IngestionStatus; |
| 24 | +import com.nageoffer.ai.ragent.ingestion.domain.pipeline.NodeConfig; |
| 25 | +import com.nageoffer.ai.ragent.ingestion.domain.pipeline.PipelineDefinition; |
| 26 | +import com.nageoffer.ai.ragent.ingestion.domain.result.NodeResult; |
| 27 | +import com.nageoffer.ai.ragent.ingestion.node.IngestionNode; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 35 | + |
| 36 | +class IngestionEngineTest { |
| 37 | + |
| 38 | + @Test |
| 39 | + void multipleStartNodesFailBeforeAnyNodeExecutes() { |
| 40 | + List<String> executedNodeIds = new ArrayList<>(); |
| 41 | + IngestionEngine engine = engine(executedNodeIds); |
| 42 | + PipelineDefinition pipeline = PipelineDefinition.builder() |
| 43 | + .nodes(List.of( |
| 44 | + node("z-root", "z-leaf"), |
| 45 | + node("a-leaf", null), |
| 46 | + node("z-leaf", null), |
| 47 | + node("a-root", "a-leaf"))) |
| 48 | + .build(); |
| 49 | + |
| 50 | + ClientException exception = assertThrows( |
| 51 | + ClientException.class, |
| 52 | + () -> engine.execute(pipeline, IngestionContext.builder().build())); |
| 53 | + |
| 54 | + assertEquals("流水线存在多个起始节点: a-root, z-root", exception.getMessage()); |
| 55 | + assertEquals(List.of(), executedNodeIds); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void nullStartNodeIdAlongsideNormalRootFailsWithoutNpe() { |
| 60 | + List<String> executedNodeIds = new ArrayList<>(); |
| 61 | + IngestionEngine engine = engine(executedNodeIds); |
| 62 | + PipelineDefinition pipeline = PipelineDefinition.builder() |
| 63 | + .nodes(List.of( |
| 64 | + node("a-root", null), |
| 65 | + node(null, null))) |
| 66 | + .build(); |
| 67 | + |
| 68 | + ClientException exception = assertThrows( |
| 69 | + ClientException.class, |
| 70 | + () -> engine.execute(pipeline, IngestionContext.builder().build())); |
| 71 | + |
| 72 | + assertEquals("流水线存在多个起始节点: null, a-root", exception.getMessage()); |
| 73 | + assertEquals(List.of(), executedNodeIds); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void blankStartNodeIdIsRejectedBeforeExecution() { |
| 78 | + List<String> executedNodeIds = new ArrayList<>(); |
| 79 | + IngestionEngine engine = engine(executedNodeIds); |
| 80 | + PipelineDefinition pipeline = PipelineDefinition.builder() |
| 81 | + .nodes(List.of(node(" ", null))) |
| 82 | + .build(); |
| 83 | + |
| 84 | + ClientException exception = assertThrows( |
| 85 | + ClientException.class, |
| 86 | + () -> engine.execute(pipeline, IngestionContext.builder().build())); |
| 87 | + |
| 88 | + assertEquals("流水线未找到起始节点", exception.getMessage()); |
| 89 | + assertEquals(List.of(), executedNodeIds); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void singleChainStillExecutesEveryNodeInOrder() { |
| 94 | + List<String> executedNodeIds = new ArrayList<>(); |
| 95 | + IngestionEngine engine = engine(executedNodeIds); |
| 96 | + PipelineDefinition pipeline = PipelineDefinition.builder() |
| 97 | + .nodes(List.of( |
| 98 | + node("middle", "leaf"), |
| 99 | + node("leaf", null), |
| 100 | + node("root", "middle"))) |
| 101 | + .build(); |
| 102 | + IngestionContext context = IngestionContext.builder().build(); |
| 103 | + |
| 104 | + engine.execute(pipeline, context); |
| 105 | + |
| 106 | + assertEquals(List.of("root", "middle", "leaf"), executedNodeIds); |
| 107 | + assertEquals(IngestionStatus.COMPLETED, context.getStatus()); |
| 108 | + } |
| 109 | + |
| 110 | + private IngestionEngine engine(List<String> executedNodeIds) { |
| 111 | + IngestionNode node = new IngestionNode() { |
| 112 | + @Override |
| 113 | + public String getNodeType() { |
| 114 | + return "test"; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public NodeResult execute(IngestionContext context, NodeConfig config) { |
| 119 | + executedNodeIds.add(config.getNodeId()); |
| 120 | + return NodeResult.ok(); |
| 121 | + } |
| 122 | + }; |
| 123 | + return new IngestionEngine( |
| 124 | + List.of(node), |
| 125 | + new ConditionEvaluator(new ObjectMapper()), |
| 126 | + new NodeOutputExtractor()); |
| 127 | + } |
| 128 | + |
| 129 | + private NodeConfig node(String nodeId, String nextNodeId) { |
| 130 | + return NodeConfig.builder() |
| 131 | + .nodeId(nodeId) |
| 132 | + .nodeType("test") |
| 133 | + .nextNodeId(nextNodeId) |
| 134 | + .build(); |
| 135 | + } |
| 136 | +} |
0 commit comments