Skip to content

Commit 2d01788

Browse files
committed
include root flow in the list
1 parent e2816c8 commit 2d01788

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

src/main/java/com/flowci/flow/FlowController.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.flowci.flow.model.CreateFlowParam;
1111
import com.flowci.flow.model.Flow;
1212
import com.flowci.flow.model.YamlTemplate;
13+
import io.swagger.v3.oas.annotations.tags.Tag;
1314
import jakarta.validation.Valid;
1415
import jakarta.validation.constraints.Min;
1516
import lombok.AllArgsConstructor;
@@ -25,6 +26,7 @@
2526
@RestController
2627
@RequestMapping("/v2/flows")
2728
@AllArgsConstructor
29+
@Tag(name = "flow")
2830
public class FlowController {
2931

3032
private final FetchTemplates fetchTemplates;
@@ -41,9 +43,17 @@ public Flow getFlow(@PathVariable("id") @Valid @ValidId String id) {
4143
}
4244

4345
@GetMapping
44-
public List<Flow> getFlows(@RequestParam(required = false, defaultValue = "10000") @Valid @ValidId String parentId,
45-
@RequestParam(required = false, defaultValue = "0") @Valid @Min(0) Integer page,
46-
@RequestParam(required = false, defaultValue = "20") @Valid @Min(20) Integer size) {
46+
public List<Flow> getFlows(@RequestParam(required = false, name = "parentId", defaultValue = "10000")
47+
@Valid
48+
@ValidId String parentId,
49+
50+
@RequestParam(required = false, name = "page", defaultValue = "0")
51+
@Valid
52+
@Min(0) Integer page,
53+
54+
@RequestParam(required = false, name = "size", defaultValue = "20")
55+
@Valid
56+
@Min(20) Integer size) {
4757
return listFlows.invoke(parseLong(parentId), PageRequest.of(page, size));
4858
}
4959

src/main/java/com/flowci/flow/business/impl/ListFlowsImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.flowci.flow.business.impl;
22

33
import com.flowci.common.RequestContextHolder;
4+
import com.flowci.flow.business.FetchFlow;
45
import com.flowci.flow.business.ListFlows;
56
import com.flowci.flow.model.Flow;
67
import com.flowci.flow.repo.FlowRepo;
@@ -10,6 +11,7 @@
1011
import org.springframework.data.domain.PageRequest;
1112
import org.springframework.stereotype.Component;
1213

14+
import java.util.ArrayList;
1315
import java.util.List;
1416

1517
@Slf4j
@@ -19,6 +21,8 @@ public class ListFlowsImpl implements ListFlows {
1921

2022
private final FlowRepo flowRepo;
2123

24+
private final FetchFlow fetchFlow;
25+
2226
private final RequestContextHolder requestContextHolder;
2327

2428
@Override
@@ -27,10 +31,15 @@ public List<Flow> invoke(@Nullable Long parentId, PageRequest pageRequest) {
2731
parentId = Flow.ROOT_ID;
2832
}
2933

30-
return flowRepo.findAllByParentIdAndUserIdOrderByCreatedAt(
34+
var list = flowRepo.findAllByParentIdAndUserIdOrderByCreatedAt(
3135
parentId,
3236
requestContextHolder.getUserId(),
3337
pageRequest
3438
);
39+
40+
var r = new ArrayList<Flow>(list.size() + 1);
41+
r.add(fetchFlow.invoke(Flow.ROOT_ID));
42+
r.addAll(list);
43+
return r;
3544
}
3645
}

src/test/java/com/flowci/flow/business/ListFlowsTest.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
import com.flowci.SpringTest;
44
import com.flowci.common.RequestContextHolder;
55
import com.flowci.flow.model.Flow;
6+
import org.junit.jupiter.api.Assertions;
67
import org.junit.jupiter.api.Test;
78
import org.mockito.ArgumentCaptor;
89
import org.springframework.beans.factory.annotation.Autowired;
910
import org.springframework.boot.test.mock.mockito.MockBean;
1011
import org.springframework.data.domain.PageRequest;
1112
import org.springframework.data.domain.Pageable;
1213

13-
import java.util.Collections;
14+
import java.util.List;
1415

16+
import static com.flowci.TestUtils.newDummyInstance;
1517
import static org.junit.jupiter.api.Assertions.assertEquals;
1618
import static org.mockito.ArgumentMatchers.any;
1719
import static org.mockito.Mockito.*;
@@ -24,27 +26,41 @@ class ListFlowsTest extends SpringTest {
2426
@Autowired
2527
private ListFlows listFlows;
2628

29+
@MockBean
30+
private FetchFlow fetchFlow;
31+
2732
@MockBean
2833
private RequestContextHolder requestContextHolder;
2934

3035
@Test
3136
void givenParentId_whenFetching_thenReturnAllFlowsUnderTheParent() {
37+
// mock flow repo
3238
var flowRepoMock = mockRepositoriesConfig.getFlowRepo();
3339
var parentIdCaptor = ArgumentCaptor.forClass(Long.class);
3440
var userIdCaptor = ArgumentCaptor.forClass(Long.class);
3541
when(flowRepoMock.findAllByParentIdAndUserIdOrderByCreatedAt(
3642
parentIdCaptor.capture(),
3743
userIdCaptor.capture(),
3844
any(Pageable.class))
39-
).thenReturn(Collections.emptyList());
45+
).thenReturn(List.of(newDummyInstance(Flow.class).create()));
46+
47+
// mock fetch root flow
48+
when(fetchFlow.invoke(eq(Flow.ROOT_ID)))
49+
.thenReturn(newDummyInstance(Flow.class).create());
4050

4151
var userIdMock = 1L;
4252
when(requestContextHolder.getUserId()).thenReturn(userIdMock);
4353

44-
listFlows.invoke(null, PageRequest.of(0, 1));
54+
// when fetching list flow without parent id
55+
var list = listFlows.invoke(null, PageRequest.of(0, 1));
56+
assertEquals(2, list.size());
4557

58+
// verify the parent should be root id as default
4659
assertEquals(Flow.ROOT_ID, parentIdCaptor.getValue());
4760
verify(flowRepoMock, times(1)).findAllByParentIdAndUserIdOrderByCreatedAt(
4861
parentIdCaptor.capture(), userIdCaptor.capture(), any(Pageable.class));
62+
63+
// verify the root flow also fetched
64+
verify(fetchFlow, times(1)).invoke(eq(Flow.ROOT_ID));
4965
}
5066
}

0 commit comments

Comments
 (0)