Junjie
2026-04-27 cd04aa8b887e82ec664e42f0bc353c079be1d2c5
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
134
135
136
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.StationFlowCapacity;
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;
import static org.junit.jupiter.api.Assertions.assertNotNull;
 
class FlowTopologySnapshotServiceImplTest {
 
    @Test
    void buildTopologyItemUsesExplicitDirectionAndTargetStation() {
        FlowTopologySnapshotServiceImpl service = new FlowTopologySnapshotServiceImpl();
        StationFlowCapacity capacity = capacity(101, "OUT", 3);
        Map<Integer, List<Integer>> adjacency = new LinkedHashMap<>();
        adjacency.put(101, Arrays.asList(103, 102));
 
        AutoTuneFlowTopologyItem item = service.buildTopologyItem(
                capacity,
                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();
        StationFlowCapacity capacity = capacity(201, "IN", 4);
        Map<Integer, List<Integer>> adjacency = new LinkedHashMap<>();
        adjacency.put(201, Arrays.asList(203, 202));
 
        AutoTuneFlowTopologyItem item = service.buildTopologyItem(
                capacity,
                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 findCapacityUsesStationIdAndDirectionCode() {
        FlowTopologySnapshotServiceImpl service = new FlowTopologySnapshotServiceImpl();
        List<StationFlowCapacity> capacities = Arrays.asList(
                capacity(101, "IN", 2),
                capacity(101, "OUT", 4),
                capacity(102, "OUT", 8)
        );
 
        StationFlowCapacity result = service.findCapacity(capacities, 101, "OUT");
 
        assertNotNull(result);
        assertEquals(4, result.getBufferCapacity());
        assertEquals("OUT", result.getDirectionCode());
    }
 
    @Test
    void flowStationIdsAndCountsAreNotTruncatedByBufferCapacity() {
        FlowTopologySnapshotServiceImpl service = new FlowTopologySnapshotServiceImpl();
        StationFlowCapacity capacity = capacity(101, "OUT", 1);
        Map<Integer, List<Integer>> adjacency = new LinkedHashMap<>();
        adjacency.put(101, Arrays.asList(104, 102, 103));
 
        AutoTuneFlowTopologyItem item = service.buildTopologyItem(
                capacity,
                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());
    }
 
    private StationFlowCapacity capacity(Integer stationId, String directionCode, Integer bufferCapacity) {
        StationFlowCapacity capacity = new StationFlowCapacity();
        capacity.setStationId(stationId);
        capacity.setDirectionCode(directionCode);
        capacity.setBufferCapacity(bufferCapacity);
        return capacity;
    }
 
    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;
    }
}