Skip to content

Commit

Permalink
include root flow in the list
Browse files Browse the repository at this point in the history
  • Loading branch information
gy2006 committed Dec 19, 2024
1 parent e2816c8 commit 2d01788
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/main/java/com/flowci/flow/FlowController.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.flowci.flow.model.CreateFlowParam;
import com.flowci.flow.model.Flow;
import com.flowci.flow.model.YamlTemplate;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import lombok.AllArgsConstructor;
Expand All @@ -25,6 +26,7 @@
@RestController
@RequestMapping("/v2/flows")
@AllArgsConstructor
@Tag(name = "flow")
public class FlowController {

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

@GetMapping
public List<Flow> getFlows(@RequestParam(required = false, defaultValue = "10000") @Valid @ValidId String parentId,
@RequestParam(required = false, defaultValue = "0") @Valid @Min(0) Integer page,
@RequestParam(required = false, defaultValue = "20") @Valid @Min(20) Integer size) {
public List<Flow> getFlows(@RequestParam(required = false, name = "parentId", defaultValue = "10000")
@Valid
@ValidId String parentId,

@RequestParam(required = false, name = "page", defaultValue = "0")
@Valid
@Min(0) Integer page,

@RequestParam(required = false, name = "size", defaultValue = "20")
@Valid
@Min(20) Integer size) {
return listFlows.invoke(parseLong(parentId), PageRequest.of(page, size));
}

Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/flowci/flow/business/impl/ListFlowsImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.flowci.flow.business.impl;

import com.flowci.common.RequestContextHolder;
import com.flowci.flow.business.FetchFlow;
import com.flowci.flow.business.ListFlows;
import com.flowci.flow.model.Flow;
import com.flowci.flow.repo.FlowRepo;
Expand All @@ -10,6 +11,7 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
Expand All @@ -19,6 +21,8 @@ public class ListFlowsImpl implements ListFlows {

private final FlowRepo flowRepo;

private final FetchFlow fetchFlow;

private final RequestContextHolder requestContextHolder;

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

return flowRepo.findAllByParentIdAndUserIdOrderByCreatedAt(
var list = flowRepo.findAllByParentIdAndUserIdOrderByCreatedAt(
parentId,
requestContextHolder.getUserId(),
pageRequest
);

var r = new ArrayList<Flow>(list.size() + 1);
r.add(fetchFlow.invoke(Flow.ROOT_ID));
r.addAll(list);
return r;
}
}
22 changes: 19 additions & 3 deletions src/test/java/com/flowci/flow/business/ListFlowsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import com.flowci.SpringTest;
import com.flowci.common.RequestContextHolder;
import com.flowci.flow.model.Flow;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.Collections;
import java.util.List;

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

@MockBean
private FetchFlow fetchFlow;

@MockBean
private RequestContextHolder requestContextHolder;

@Test
void givenParentId_whenFetching_thenReturnAllFlowsUnderTheParent() {
// mock flow repo
var flowRepoMock = mockRepositoriesConfig.getFlowRepo();
var parentIdCaptor = ArgumentCaptor.forClass(Long.class);
var userIdCaptor = ArgumentCaptor.forClass(Long.class);
when(flowRepoMock.findAllByParentIdAndUserIdOrderByCreatedAt(
parentIdCaptor.capture(),
userIdCaptor.capture(),
any(Pageable.class))
).thenReturn(Collections.emptyList());
).thenReturn(List.of(newDummyInstance(Flow.class).create()));

// mock fetch root flow
when(fetchFlow.invoke(eq(Flow.ROOT_ID)))
.thenReturn(newDummyInstance(Flow.class).create());

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

listFlows.invoke(null, PageRequest.of(0, 1));
// when fetching list flow without parent id
var list = listFlows.invoke(null, PageRequest.of(0, 1));
assertEquals(2, list.size());

// verify the parent should be root id as default
assertEquals(Flow.ROOT_ID, parentIdCaptor.getValue());
verify(flowRepoMock, times(1)).findAllByParentIdAndUserIdOrderByCreatedAt(
parentIdCaptor.capture(), userIdCaptor.capture(), any(Pageable.class));

// verify the root flow also fetched
verify(fetchFlow, times(1)).invoke(eq(Flow.ROOT_ID));
}
}

0 comments on commit 2d01788

Please sign in to comment.