|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved. |
| 3 | + * This file is a part of the ModelEngine Project. |
| 4 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 5 | + *--------------------------------------------------------------------------------------------*/ |
| 6 | + |
| 7 | +package modelengine.fit.jade.aipp.variable.updater; |
| 8 | + |
| 9 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 10 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 11 | + |
| 12 | +import modelengine.fit.jober.aipp.common.exception.AippParamException; |
| 13 | +import modelengine.fitframework.util.MapBuilder; |
| 14 | +import modelengine.fitframework.util.ObjectUtils; |
| 15 | + |
| 16 | +import org.junit.jupiter.api.DisplayName; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +/** |
| 24 | + * {@link AippVariableUpdater}的测试方法。 |
| 25 | + * |
| 26 | + * @author 鲁为 |
| 27 | + * @since 2025-09-28 |
| 28 | + */ |
| 29 | +@DisplayName("变量更新节点测试。") |
| 30 | +public class AippVariableUpdaterTest { |
| 31 | + private static final String UPDATE_VARIABLES = "updateVariables"; |
| 32 | + private static final String INTERNAL = "_internal"; |
| 33 | + private static final String OUTPUT_SCOPE = "outputScope"; |
| 34 | + private static final String KEY = "key"; |
| 35 | + private static final String VALUE = "value"; |
| 36 | + |
| 37 | + private final AippVariableUpdater aippVariableUpdater = new AippVariableUpdater(); |
| 38 | + |
| 39 | + @Test |
| 40 | + @DisplayName("修改 businessData 中指定路径的值。") |
| 41 | + void shouldModifySpecificPathValue() { |
| 42 | + Map<String, Object> flowDataInner = |
| 43 | + this.getFlowData(this.getContextData(List.of("trace1"), "contextId"), this.buildBusinessData()); |
| 44 | + List<Map<String, Object>> flowData = List.of(flowDataInner); |
| 45 | + this.aippVariableUpdater.handleTask(flowData); |
| 46 | + assertEquals(ObjectUtils.<String>cast(ObjectUtils.<Map<String, Object>>cast(ObjectUtils.<Map<String, Object>>cast( |
| 47 | + ObjectUtils.<Map<String, Object>>cast(ObjectUtils.<Map<String, Object>>cast(ObjectUtils.<Map<String, Object>>cast( |
| 48 | + flowData.get(0).get("businessData")).get(INTERNAL)).get(OUTPUT_SCOPE)).get( |
| 49 | + "jade8xvdq4")) |
| 50 | + .get("output")).get("a")), "after"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + @DisplayName("当 path 不存在时,抛出异常。") |
| 55 | + void shouldThrowExceptionWhenPathDoesNotExist() { |
| 56 | + Map<String, Object> flowDataInner = |
| 57 | + this.getFlowData(this.getContextData(List.of("trace1"), "contextId"), this.buildInvalidBusinessData()); |
| 58 | + List<Map<String, Object>> flowData = List.of(flowDataInner); |
| 59 | + assertThrows(AippParamException.class, () -> this.aippVariableUpdater.handleTask(flowData)); |
| 60 | + } |
| 61 | + |
| 62 | + private Map<String, Object> getFlowData(Map<String, Object> contextData, Map<String, Object> businessData) { |
| 63 | + return MapBuilder.get(() -> new HashMap<String, Object>()) |
| 64 | + .put("contextData", contextData) |
| 65 | + .put("businessData", businessData) |
| 66 | + .build(); |
| 67 | + } |
| 68 | + |
| 69 | + private Map<String, Object> getContextData(List<String> traceIds, String contextId) { |
| 70 | + return MapBuilder.get(() -> new HashMap<String, Object>()) |
| 71 | + .put("flowTraceIds", traceIds) |
| 72 | + .put("contextId", contextId) |
| 73 | + .build(); |
| 74 | + } |
| 75 | + |
| 76 | + private Map<String, Object> buildBusinessData() { |
| 77 | + Map<Object, Object> output = MapBuilder.get().put("a", "before").build(); |
| 78 | + Map<Object, Object> node = MapBuilder.get().put("output", output).build(); |
| 79 | + Map<Object, Object> outputScope = MapBuilder.get().put("jade8xvdq4", node).build(); |
| 80 | + return MapBuilder.<String, Object>get() |
| 81 | + .put(UPDATE_VARIABLES, |
| 82 | + List.of(MapBuilder.get() |
| 83 | + .put(KEY, List.of("jade8xvdq4", "output", "a")) |
| 84 | + .put(VALUE, "after") |
| 85 | + .build())) |
| 86 | + .put(INTERNAL, MapBuilder.get().put(OUTPUT_SCOPE, outputScope).build()) |
| 87 | + .build(); |
| 88 | + } |
| 89 | + |
| 90 | + private Map<String, Object> buildInvalidBusinessData() { |
| 91 | + List<Object> output = List.of("a", "before"); |
| 92 | + Map<Object, Object> node = MapBuilder.get().put("output", output).build(); |
| 93 | + Map<Object, Object> outputScope = MapBuilder.get().put("jade8xvdq4", node).build(); |
| 94 | + return MapBuilder.<String, Object>get() |
| 95 | + .put(UPDATE_VARIABLES, |
| 96 | + List.of(MapBuilder.get() |
| 97 | + .put(KEY, List.of("jade8xvdq4", "output", "a")) |
| 98 | + .put(VALUE, "after") |
| 99 | + .build())) |
| 100 | + .put(INTERNAL, MapBuilder.get().put(OUTPUT_SCOPE, outputScope).build()) |
| 101 | + .build(); |
| 102 | + } |
| 103 | +} |
0 commit comments