Skip to content

Commit 449f1cd

Browse files
committed
add #82
1 parent 25d0f32 commit 449f1cd

28 files changed

Lines changed: 3446 additions & 37 deletions
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.codingapi.flow.mock;
2+
3+
import com.codingapi.flow.gateway.FlowOperatorGateway;
4+
import com.codingapi.flow.service.FlowService;
5+
import com.codingapi.flow.utils.RandomUtils;
6+
import lombok.Getter;
7+
8+
import java.util.Map;
9+
import java.util.concurrent.ConcurrentHashMap;
10+
11+
public class FlowServiceMockFactory {
12+
13+
private final Map<String, FlowService> cache;
14+
15+
@Getter
16+
private final static FlowServiceMockFactory instance = new FlowServiceMockFactory();
17+
18+
private FlowServiceMockFactory() {
19+
this.cache = new ConcurrentHashMap<>();
20+
}
21+
22+
public String create(FlowOperatorGateway flowOperatorGateway) {
23+
String key = RandomUtils.generateStringId();
24+
MockRepositoryHolder mockRepositoryHolder = new MockRepositoryHolder(key, flowOperatorGateway);
25+
FlowService flowService = new FlowService(mockRepositoryHolder);
26+
this.cache.put(key, flowService);
27+
return key;
28+
}
29+
30+
public FlowService getFlowService(String key) {
31+
return this.cache.get(key);
32+
}
33+
34+
public void clear(String key) {
35+
this.cache.remove(key);
36+
}
37+
38+
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package com.codingapi.flow.mock;
2+
3+
import com.codingapi.flow.domain.DelayTask;
4+
import com.codingapi.flow.domain.UrgeInterval;
5+
import com.codingapi.flow.gateway.FlowOperatorGateway;
6+
import com.codingapi.flow.mock.repository.*;
7+
import com.codingapi.flow.operator.IFlowOperator;
8+
import com.codingapi.flow.record.FlowRecord;
9+
import com.codingapi.flow.repository.*;
10+
import com.codingapi.flow.service.FlowRecordService;
11+
import com.codingapi.flow.service.FlowService;
12+
import com.codingapi.flow.service.WorkflowService;
13+
import com.codingapi.flow.service.impl.FlowActionService;
14+
import com.codingapi.flow.service.impl.FlowDelayTriggerService;
15+
import com.codingapi.flow.session.FlowSession;
16+
import com.codingapi.flow.session.IRepositoryHolder;
17+
import lombok.Getter;
18+
19+
import java.util.List;
20+
21+
@Getter
22+
public class MockRepositoryHolder implements IRepositoryHolder {
23+
24+
private final DelayTaskRepository delayTaskRepository;
25+
private final ParallelBranchRepository parallelBranchRepository;
26+
private final UrgeIntervalRepository urgeIntervalRepository;
27+
private final FlowRecordRepository flowRecordRepository;
28+
private final FlowTodoMergeRepository flowTodoMergeRepository;
29+
private final FlowTodoRecordRepository flowTodoRecordRepository;
30+
private final WorkflowRepository workflowRepository;
31+
private final WorkflowRuntimeRepository workflowRuntimeRepository;
32+
private final WorkflowVersionRepository workflowVersionRepository;
33+
private final FlowRecordService flowRecordService;
34+
private final WorkflowService workflowService;
35+
private final FlowOperatorGateway flowOperatorGateway;
36+
private final String mockKey;
37+
38+
public MockRepositoryHolder(String mockKey, FlowOperatorGateway flowOperatorGateway) {
39+
this.mockKey = mockKey;
40+
this.flowOperatorGateway = flowOperatorGateway;
41+
this.delayTaskRepository = new DelayTaskRepositoryMockImpl();
42+
this.flowRecordRepository = new FlowRecordRepositoryMockImpl();
43+
this.flowTodoMergeRepository = new FlowTodoMergeRepositoryMockImpl();
44+
this.flowTodoRecordRepository = new FlowTodoRecordRepositoryMockImpl();
45+
this.parallelBranchRepository = new ParallelBranchRepositoryMockImpl();
46+
this.urgeIntervalRepository = new UrgeIntervalRepositoryMockImpl();
47+
this.workflowRepository = new WorkflowRepositoryMockImpl();
48+
this.workflowRuntimeRepository = new WorkflowRuntimeRepositoryMockImpl();
49+
this.workflowVersionRepository = new WorkflowVersionRepositoryMockImpl();
50+
this.flowRecordService = new FlowRecordService(flowTodoRecordRepository, flowTodoMergeRepository, flowRecordRepository);
51+
this.workflowService = new WorkflowService(workflowVersionRepository, workflowRepository, workflowRuntimeRepository);
52+
}
53+
54+
@Override
55+
public WorkflowService getWorkflowService() {
56+
return workflowService;
57+
}
58+
59+
@Override
60+
public FlowRecordService getFlowRecordService() {
61+
return flowRecordService;
62+
}
63+
64+
@Override
65+
public FlowOperatorGateway getFlowOperatorGateway() {
66+
return flowOperatorGateway;
67+
}
68+
69+
@Override
70+
public FlowDelayTriggerService createDelayTriggerService(DelayTask task) {
71+
return new FlowDelayTriggerService(task, this);
72+
}
73+
74+
@Override
75+
public FlowActionService createFlowActionService(FlowSession flowSession) {
76+
return new FlowActionService(flowSession.toActionRequest(), this);
77+
}
78+
79+
@Override
80+
public FlowService createFlowService() {
81+
return new FlowService(this);
82+
}
83+
84+
@Override
85+
public FlowRecord getRecordById(long recordId) {
86+
return this.flowRecordService.getFlowRecord(recordId);
87+
}
88+
89+
@Override
90+
public List<IFlowOperator> findOperatorByIds(List<Long> ids) {
91+
return this.flowOperatorGateway.findByIds(ids);
92+
}
93+
94+
@Override
95+
public IFlowOperator getOperatorById(long id) {
96+
return this.flowOperatorGateway.get(id);
97+
}
98+
99+
@Override
100+
public void saveDelayTask(DelayTask delayTask) {
101+
this.delayTaskRepository.save(delayTask);
102+
}
103+
104+
@Override
105+
public void deleteDelayTask(DelayTask delayTask) {
106+
this.delayTaskRepository.delete(delayTask);
107+
}
108+
109+
@Override
110+
public void saveRecords(List<FlowRecord> flowRecords) {
111+
this.flowRecordService.saveFlowRecords(flowRecords);
112+
}
113+
114+
@Override
115+
public void saveRecord(FlowRecord flowRecord) {
116+
this.flowRecordService.saveFlowRecord(flowRecord);
117+
}
118+
119+
@Override
120+
public List<FlowRecord> findCurrentNodeRecords(long fromId, String nodeId) {
121+
return this.flowRecordService.findFlowRecordCurrentNodeRecords(fromId, nodeId);
122+
}
123+
124+
@Override
125+
public List<FlowRecord> findProcessRecords(String processId) {
126+
return this.flowRecordService.findFlowRecordByProcessId(processId);
127+
}
128+
129+
@Override
130+
public List<FlowRecord> findAfterRecords(String processId, long currentId) {
131+
return this.flowRecordService.findFlowRecordAfterRecords(processId, currentId);
132+
}
133+
134+
@Override
135+
public int getParallelBranchTriggerCount(String parallelId) {
136+
return this.parallelBranchRepository.getTriggerCount(parallelId);
137+
}
138+
139+
@Override
140+
public void addParallelTriggerCount(String parallelId) {
141+
this.parallelBranchRepository.addTriggerCount(parallelId);
142+
}
143+
144+
@Override
145+
public void clearParallelTriggerCount(String parallelId) {
146+
this.parallelBranchRepository.clearTriggerCount(parallelId);
147+
}
148+
149+
@Override
150+
public void saveUrgeInterval(UrgeInterval interval) {
151+
this.urgeIntervalRepository.save(interval);
152+
}
153+
154+
@Override
155+
public UrgeInterval getLatestUrgeInterval(String processId, long recordId) {
156+
return this.urgeIntervalRepository.getLatest(processId, recordId);
157+
}
158+
159+
@Override
160+
public List<DelayTask> findDelayTasks() {
161+
return this.delayTaskRepository.findAll();
162+
}
163+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.codingapi.flow.mock.repository;
2+
3+
import com.codingapi.flow.domain.DelayTask;
4+
import com.codingapi.flow.repository.DelayTaskRepository;
5+
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
public class DelayTaskRepositoryMockImpl implements DelayTaskRepository {
11+
12+
private final Map<String, DelayTask> cache = new HashMap<>();
13+
14+
@Override
15+
public void save(DelayTask task) {
16+
cache.put(task.getId(), task);
17+
}
18+
19+
@Override
20+
public void delete(DelayTask delayTask) {
21+
cache.remove(delayTask.getId());
22+
}
23+
24+
@Override
25+
public List<DelayTask> findAll() {
26+
return cache.values().stream().toList();
27+
}
28+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.codingapi.flow.mock.repository;
2+
3+
import com.codingapi.flow.record.FlowRecord;
4+
import com.codingapi.flow.repository.FlowRecordRepository;
5+
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
public class FlowRecordRepositoryMockImpl implements FlowRecordRepository {
11+
12+
private final Map<Long, FlowRecord> cache = new HashMap<>();
13+
14+
@Override
15+
public FlowRecord get(long id) {
16+
return cache.get(id);
17+
}
18+
19+
@Override
20+
public List<FlowRecord> findByIds(List<Long> ids) {
21+
return ids.stream().map(cache::get).toList();
22+
}
23+
24+
public List<FlowRecord> findTodoByOperator(long operatorId) {
25+
return cache.values().stream().filter(flowRecord -> flowRecord.getCurrentOperatorId() == operatorId && flowRecord.isTodo()).toList();
26+
}
27+
28+
public List<FlowRecord> findDoneByOperator(long operatorId) {
29+
return cache.values().stream().filter(flowRecord -> flowRecord.getCurrentOperatorId() == operatorId && !flowRecord.isTodo()).toList();
30+
}
31+
32+
33+
@Override
34+
public void save(FlowRecord flowRecord) {
35+
if (flowRecord.getId() > 0) {
36+
cache.put(flowRecord.getId(), flowRecord);
37+
} else {
38+
long id = cache.size() + 1;
39+
flowRecord.setId(id);
40+
cache.put(id, flowRecord);
41+
}
42+
}
43+
44+
@Override
45+
public void saveAll(List<FlowRecord> flowRecords) {
46+
for (FlowRecord flowRecord : flowRecords) {
47+
this.save(flowRecord);
48+
}
49+
}
50+
51+
@Override
52+
public void delete(FlowRecord flowRecord) {
53+
cache.remove(flowRecord.getId());
54+
}
55+
56+
@Override
57+
public List<FlowRecord> findCurrentNodeRecords(long fromId, String nodeId) {
58+
return cache.values().stream().filter(flowRecord ->
59+
flowRecord.getFromId() == fromId
60+
&& flowRecord.getNodeId().equals(nodeId)
61+
&& !flowRecord.isRevoked()
62+
)
63+
.toList();
64+
}
65+
66+
@Override
67+
public List<FlowRecord> findProcessRecords(String processId) {
68+
return cache.values().stream().filter(flowRecord ->
69+
flowRecord.getProcessId().equals(processId))
70+
.toList();
71+
}
72+
73+
@Override
74+
public List<FlowRecord> findTodoRecords(String processId) {
75+
return cache.values().stream().filter(flowRecord ->
76+
flowRecord.getProcessId().equals(processId)
77+
&& flowRecord.isTodo()
78+
)
79+
.toList();
80+
}
81+
82+
@Override
83+
public List<FlowRecord> findAfterRecords(String processId, long fromId) {
84+
return cache.values().stream().filter(flowRecord ->
85+
flowRecord.getProcessId().equals(processId)
86+
&& flowRecord.getFromId() >= fromId
87+
&& !flowRecord.isRevoked()
88+
&& !flowRecord.isHidden()
89+
).toList();
90+
}
91+
92+
@Override
93+
public List<FlowRecord> findBeforeRecords(String processId, long id) {
94+
return cache.values().stream().filter(flowRecord ->
95+
flowRecord.getProcessId().equals(processId)
96+
&& flowRecord.getId() < id
97+
&& !flowRecord.isRevoked()
98+
&& !flowRecord.isHidden()
99+
).toList();
100+
}
101+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.codingapi.flow.mock.repository;
2+
3+
import com.codingapi.flow.record.FlowTodoMerge;
4+
import com.codingapi.flow.repository.FlowTodoMergeRepository;
5+
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
public class FlowTodoMergeRepositoryMockImpl implements FlowTodoMergeRepository {
11+
12+
private final Map<Long, FlowTodoMerge> cache = new HashMap<>();
13+
14+
private void save(FlowTodoMerge relation) {
15+
if (relation.getId() > 0) {
16+
cache.put(relation.getId(), relation);
17+
} else {
18+
long id = cache.size() + 1;
19+
relation.setId(id);
20+
cache.put(id, relation);
21+
}
22+
}
23+
24+
@Override
25+
public void delete(FlowTodoMerge todoMerge) {
26+
this.cache.remove(todoMerge.getId());
27+
}
28+
29+
@Override
30+
public void saveAll(List<FlowTodoMerge> list) {
31+
for (FlowTodoMerge relation : list){
32+
this.save(relation);
33+
}
34+
}
35+
36+
@Override
37+
public List<FlowTodoMerge> findByTodoId(long todoId) {
38+
return cache.values().stream().
39+
filter(relation -> relation.getTodoId() == todoId)
40+
.toList();
41+
}
42+
43+
44+
public List<FlowTodoMerge> findAll() {
45+
return cache.values().stream().toList();
46+
}
47+
}

0 commit comments

Comments
 (0)