Junjie
2026-04-27 0c336d5c5c0596691c9b33c08643c03486d47d5f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.zy.ai.service;
 
import com.zy.ai.domain.autotune.AutoTuneFlowTopologyItem;
import com.zy.ai.domain.autotune.AutoTuneStationRuntimeItem;
import com.zy.ai.service.impl.FlowTopologySnapshotServiceImpl;
import com.zy.asrs.entity.BasStation;
import org.junit.jupiter.api.Test;
 
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
class FlowTopologySnapshotServiceImplTest {
 
    @Test
    void buildTopologyItemUsesExplicitDirectionAndTargetStation() {
        FlowTopologySnapshotServiceImpl service = new FlowTopologySnapshotServiceImpl();
        BasStation station = station(101, 3);
        Map<Integer, List<Integer>> adjacency = new LinkedHashMap<>();
        adjacency.put(101, Arrays.asList(103, 102));
 
        AutoTuneFlowTopologyItem item = service.buildTopologyItem(
                station,
                adjacency,
                Arrays.asList(runtime(101, 1, 0, 0), runtime(102, 1, 1, 0))
        );
 
        assertEquals(101, item.getTargetStationId());
        assertEquals("OUT", item.getDirection());
        assertEquals(Arrays.asList(102, 103), item.getAdjacentStationIds());
        assertEquals(Arrays.asList(), item.getUpstreamStationIds());
        assertEquals(Arrays.asList(), item.getDownstreamStationIds());
        assertEquals(Arrays.asList(101, 102, 103), item.getFlowStationIds());
    }
 
    @Test
    void undirectedGraphOnlyPopulatesAdjacentAndFlowStations() {
        FlowTopologySnapshotServiceImpl service = new FlowTopologySnapshotServiceImpl();
        BasStation station = station(201, 4);
        Map<Integer, List<Integer>> adjacency = new LinkedHashMap<>();
        adjacency.put(201, Arrays.asList(203, 202));
 
        AutoTuneFlowTopologyItem item = service.buildTopologyItem(
                station,
                adjacency,
                Arrays.asList(runtime(202, 1, 1, 0), runtime(203, 1, 0, 8001))
        );
 
        assertEquals(Arrays.asList(202, 203), item.getAdjacentStationIds());
        assertEquals(Arrays.asList(), item.getUpstreamStationIds());
        assertEquals(Arrays.asList(), item.getDownstreamStationIds());
        assertEquals(Arrays.asList(201, 202, 203), item.getFlowStationIds());
        assertEquals(2, item.getOccupiedCount());
    }
 
    @Test
    void calculateRuntimeCountsUsesOnlyAutoingLoadingAndTaskNo() {
        FlowTopologySnapshotServiceImpl service = new FlowTopologySnapshotServiceImpl();
        List<AutoTuneStationRuntimeItem> runtimeItems = Arrays.asList(
                runtime(101, 0, 0, 0),
                runtime(102, 1, 1, 0),
                runtime(103, 1, 0, 9001),
                runtime(104, 0, 1, 9002)
        );
 
        FlowTopologySnapshotServiceImpl.RuntimeCount count = service.calculateRuntimeCount(runtimeItems);
 
        assertEquals(3, count.getOccupiedCount());
        assertEquals(2, count.getNonAutoingCount());
        assertEquals(2, count.getLoadingCount());
        assertEquals(2, count.getTaskHoldingCount());
    }
 
    @Test
    void flowStationIdsAndCountsAreNotTruncatedByBufferCapacity() {
        FlowTopologySnapshotServiceImpl service = new FlowTopologySnapshotServiceImpl();
        BasStation station = station(101, 1);
        Map<Integer, List<Integer>> adjacency = new LinkedHashMap<>();
        adjacency.put(101, Arrays.asList(104, 102, 103));
 
        AutoTuneFlowTopologyItem item = service.buildTopologyItem(
                station,
                adjacency,
                Arrays.asList(
                        runtime(101, 1, 0, 0),
                        runtime(102, 1, 1, 0),
                        runtime(103, 1, 0, 9001),
                        runtime(104, 0, 0, 0)
                )
        );
 
        assertEquals(Arrays.asList(101, 102, 103, 104), item.getFlowStationIds());
        assertEquals(2, item.getOccupiedCount());
        assertEquals(1, item.getLoadingCount());
        assertEquals(1, item.getTaskHoldingCount());
        assertEquals(1, item.getNonAutoingCount());
        assertEquals(0, item.getFreeCount());
    }
 
    @Test
    void missingOutBufferCapacityKeepsFreeCountUnknown() {
        FlowTopologySnapshotServiceImpl service = new FlowTopologySnapshotServiceImpl();
        BasStation station = station(101, null);
 
        AutoTuneFlowTopologyItem item = service.buildTopologyItem(
                station,
                new LinkedHashMap<>(),
                Arrays.asList(runtime(101, 1, 1, 0))
        );
 
        assertEquals(null, item.getBufferCapacity());
        assertEquals(null, item.getFreeCount());
        assertEquals(1, item.getOccupiedCount());
    }
 
    private BasStation station(Integer stationId, Integer outBufferCapacity) {
        BasStation station = new BasStation();
        station.setStationId(stationId);
        station.setOutBufferCapacity(outBufferCapacity);
        return station;
    }
 
    private AutoTuneStationRuntimeItem runtime(Integer stationId, Integer autoing, Integer loading, Integer taskNo) {
        AutoTuneStationRuntimeItem item = new AutoTuneStationRuntimeItem();
        item.setStationId(stationId);
        item.setAutoing(autoing);
        item.setLoading(loading);
        item.setTaskNo(taskNo);
        return item;
    }
}