Skip to content

Commit 6cfb83b

Browse files
committed
using flow entity to manage group
1 parent ba083c3 commit 6cfb83b

File tree

9 files changed

+75
-95
lines changed

9 files changed

+75
-95
lines changed

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

Lines changed: 3 additions & 0 deletions
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.common.model.Variables;
45
import com.flowci.flow.business.CreateFlow;
56
import com.flowci.flow.business.FetchTemplateContent;
67
import com.flowci.flow.model.CreateFlowParam;
@@ -38,6 +39,8 @@ public Flow invoke(CreateFlowParam param) {
3839
private Flow toObject(CreateFlowParam param) {
3940
var flow = new Flow();
4041
flow.setName(param.name());
42+
flow.setType(Flow.Type.FLOW);
43+
flow.setVariables(Variables.EMPTY);
4144
flow.setCreatedBy(requestContextHolder.getUser());
4245
flow.setUpdatedBy(requestContextHolder.getUser());
4346

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.flowci.common.model.Variables;
44
import com.flowci.flow.business.InitRootGroup;
5-
import com.flowci.flow.model.Group;
6-
import com.flowci.flow.repo.GroupRepo;
5+
import com.flowci.flow.model.Flow;
6+
import com.flowci.flow.repo.FlowRepo;
77
import lombok.AllArgsConstructor;
88
import lombok.extern.slf4j.Slf4j;
99
import org.springframework.stereotype.Component;
@@ -13,23 +13,24 @@
1313
@AllArgsConstructor
1414
public class InitRootGroupImpl implements InitRootGroup {
1515

16-
private final GroupRepo groupRepo;
16+
private final FlowRepo flowRepo;
1717

1818
@Override
1919
public void invoke() {
20-
var optional = groupRepo.findById(Group.ROOT_ID);
20+
var optional = flowRepo.findById(Flow.ROOT_ID);
2121
if (optional.isPresent()) {
22-
log.info("Root group '{}' already exists", Group.ROOT_NAME);
22+
log.info("Root flow group '{}' already exists", Flow.ROOT_NAME);
2323
return;
2424
}
2525

26-
var root = new Group();
27-
root.setName(Group.ROOT_NAME);
28-
root.setParentId(Group.ROOT_ID);
26+
var root = new Flow();
27+
root.setType(Flow.Type.GROUP);
28+
root.setName(Flow.ROOT_NAME);
29+
root.setParentId(Flow.ROOT_ID);
2930
root.setVariables(Variables.EMPTY);
3031
root.setCreatedBy("system");
3132
root.setUpdatedBy("system");
32-
groupRepo.save(root);
33-
log.info("Root group '{}' is created", root.getName());
33+
flowRepo.save(root);
34+
log.info("Root flow group '{}' is created", root.getName());
3435
}
3536
}

src/main/java/com/flowci/flow/model/Flow.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,29 @@
1111
@EqualsAndHashCode(callSuper = false, of = "id")
1212
public final class Flow extends EntityBase {
1313

14+
public static final Long ROOT_ID = 10000L;
15+
public static final String ROOT_NAME = "flows";
16+
17+
public enum Type {
18+
GROUP,
19+
FLOW,
20+
}
21+
1422
@Id
1523
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "flows_id_gen")
1624
@SequenceGenerator(name = "flows_id_gen", sequenceName = "flows_id_sequence", allocationSize = 1)
1725
private Long id;
1826

1927
private String name;
2028

29+
@Enumerated(EnumType.STRING)
30+
private Type type;
31+
2132
// parent group id
22-
private Long parentId = Group.ROOT_ID;
33+
private Long parentId = ROOT_ID;
2334

2435
@Convert(converter = Variables.AttributeConverter.class)
25-
private Variables variables = new Variables();
36+
private Variables variables;
2637

2738
@Convert(converter = GitLink.Converter.class)
2839
private GitLink gitLink;

src/main/java/com/flowci/flow/model/Group.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/main/java/com/flowci/flow/repo/GroupRepo.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/resources/db/migration/V1__Init.sql

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,10 @@
1-
DROP SEQUENCE IF EXISTS groups_id_sequence;
2-
CREATE SEQUENCE groups_id_sequence START 10000 INCREMENT 1;
3-
4-
CREATE TABLE groups
5-
(
6-
id BIGSERIAL PRIMARY KEY,
7-
parent_id BIGINT NOT NULL,
8-
name VARCHAR(100) UNIQUE NOT NULL,
9-
variables TEXT NOT NULL,
10-
created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
11-
created_by VARCHAR(200),
12-
updated_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
13-
updated_by VARCHAR(200)
14-
);
15-
161
DROP SEQUENCE IF EXISTS flows_id_sequence;
172
CREATE SEQUENCE flows_id_sequence START 10000 INCREMENT 1;
183

194
CREATE TABLE flows
205
(
216
id BIGSERIAL PRIMARY KEY,
7+
type VARCHAR(10) NOT NULL,
228
parent_id BIGINT NOT NULL,
239
name VARCHAR(100) UNIQUE NOT NULL,
2410
variables TEXT NOT NULL,

src/test/java/com/flowci/SpringTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.flowci.flow.repo.FlowRepo;
44
import com.flowci.flow.repo.FlowYamlRepo;
5-
import com.flowci.flow.repo.GroupRepo;
65
import lombok.Getter;
76
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
87
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
@@ -40,9 +39,6 @@ public static class MockRepositoriesConfig {
4039

4140
@MockBean
4241
private FlowYamlRepo flowYamlRepo;
43-
44-
@MockBean
45-
private GroupRepo groupRepo;
4642
}
4743

4844
@DynamicPropertySource

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

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,73 @@
11
package com.flowci.flow.business;
22

3-
import com.flowci.SpringTestWithDB;
3+
import com.flowci.SpringTest;
4+
import com.flowci.common.model.Variables;
45
import com.flowci.flow.model.CreateFlowParam;
6+
import com.flowci.flow.model.Flow;
7+
import com.flowci.flow.model.FlowYaml;
58
import org.junit.jupiter.api.BeforeEach;
69
import org.junit.jupiter.api.Test;
10+
import org.mockito.ArgumentCaptor;
711
import org.springframework.beans.factory.annotation.Autowired;
812
import org.springframework.boot.test.mock.mockito.MockBean;
913
import org.springframework.dao.DataIntegrityViolationException;
1014

1115
import static org.junit.jupiter.api.Assertions.*;
1216
import static org.mockito.ArgumentMatchers.anyString;
13-
import static org.mockito.Mockito.when;
17+
import static org.mockito.Mockito.*;
1418

15-
class CreateAndFetchFlowTest extends SpringTestWithDB {
19+
class CreateAndFetchFlowTest extends SpringTest {
1620

1721
@Autowired
18-
private CreateFlow createFlow;
19-
20-
@Autowired
21-
private FetchFlow fetchFlow;
22+
private MockRepositoriesConfig mockRepositoriesConfig;
2223

2324
@Autowired
24-
private FetchFlowYamlContent fetchFlowYamlContent;
25+
private CreateFlow createFlow;
2526

2627
@MockBean
2728
private FetchTemplateContent fetchTemplateContent;
2829

2930
@BeforeEach
3031
void mockFetchTemplateContent() {
31-
when(fetchTemplateContent.invoke(anyString()))
32-
.thenReturn("helloworld yaml");
32+
when(fetchTemplateContent.invoke(anyString())).thenReturn("helloworld yaml");
3333
}
3434

3535
@Test
3636
void whenCreateFlowWithTemplate_thenFlowCreatedWithTemplateYamlContent() {
37-
var flow = createFlow.invoke(new CreateFlowParam("test flow", "helloworld", null));
37+
var flowRepoMock = mockRepositoriesConfig.getFlowRepo();
38+
var flowArgCaptor = ArgumentCaptor.forClass(Flow.class);
39+
when(flowRepoMock.save(flowArgCaptor.capture())).thenAnswer(i -> i.getArgument(0));
40+
41+
var flowYamlRepoMock = mockRepositoriesConfig.getFlowYamlRepo();
42+
var flowYamlArgCaptor = ArgumentCaptor.forClass(FlowYaml.class);
43+
when(flowYamlRepoMock.save(flowYamlArgCaptor.capture())).thenAnswer(i -> i.getArgument(0));
44+
45+
var param = new CreateFlowParam("test flow", "helloworld", null);
46+
createFlow.invoke(param);
47+
48+
// verify saved flow
49+
verify(flowRepoMock, times(1)).save(flowArgCaptor.capture());
50+
51+
var savedFlow = flowArgCaptor.getValue();
52+
assertEquals(param.name(), savedFlow.getName());
53+
assertEquals(Flow.Type.FLOW, savedFlow.getType());
54+
assertEquals(Variables.EMPTY, savedFlow.getVariables());
55+
assertNotNull(savedFlow.getCreatedBy());
56+
assertNotNull(savedFlow.getUpdatedBy());
3857

39-
var f = fetchFlow.invoke(flow.getId());
40-
assertNotNull(f);
58+
// verify saved flow yaml
59+
verify(flowYamlRepoMock, times(1)).save(flowYamlArgCaptor.capture());
4160

42-
var yaml = fetchFlowYamlContent.invoke(flow.getId());
43-
assertEquals("helloworld yaml", yaml);
44-
assertNotNull(f.getCreatedBy());
45-
assertNotNull(f.getUpdatedBy());
61+
var savedFlowYaml = flowYamlArgCaptor.getValue();
62+
assertEquals("helloworld yaml", savedFlowYaml.getYaml());
63+
assertEquals(savedFlow.getCreatedBy(), savedFlowYaml.getCreatedBy());
64+
assertEquals(savedFlow.getUpdatedBy(), savedFlowYaml.getUpdatedBy());
4665
}
4766

4867
@Test
4968
void whenCreateFlowWithDuplicatedName_thenThrowException() {
50-
createFlow.invoke(new CreateFlowParam("flow A", null, null));
69+
var flowRepoMock = mockRepositoriesConfig.getFlowRepo();
70+
when(flowRepoMock.save(any())).thenThrow(DataIntegrityViolationException.class);
5171

5272
assertThrows(DataIntegrityViolationException.class, () ->
5373
createFlow.invoke(new CreateFlowParam("flow A", null, null)));

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.flowci.flow.business;
22

33
import com.flowci.SpringTest;
4-
import com.flowci.flow.model.Group;
4+
import com.flowci.flow.model.Flow;
55
import org.junit.jupiter.api.Test;
66
import org.mockito.ArgumentCaptor;
77
import org.springframework.beans.factory.annotation.Autowired;
@@ -22,25 +22,26 @@ class InitRootGroupTest extends SpringTest {
2222

2323
@Test
2424
void whenEmptyDatabase_thenDefaultRootGroupShouldBeCreated() {
25-
var groupRepoMock = repositoriesConfig.getGroupRepo();
25+
var groupRepoMock = repositoriesConfig.getFlowRepo();
2626
when(groupRepoMock.findById(any())).thenReturn(Optional.empty());
2727

28-
var argCaptor = ArgumentCaptor.forClass(Group.class);
29-
when(groupRepoMock.save(argCaptor.capture())).thenReturn(mock(Group.class));
28+
var argCaptor = ArgumentCaptor.forClass(Flow.class);
29+
when(groupRepoMock.save(argCaptor.capture())).thenReturn(mock(Flow.class));
3030

3131
initRootGroup.invoke();
3232
verify(groupRepoMock, times(1)).save(argCaptor.capture());
3333

3434
var groupParam = argCaptor.getValue();
35-
assertEquals(Group.ROOT_NAME, groupParam.getName());
35+
assertEquals(Flow.ROOT_NAME, groupParam.getName());
36+
assertEquals(Flow.Type.GROUP, groupParam.getType());
3637
}
3738

3839
@Test
3940
void whenRootGroupExisted_thenSkipToCreateIt() {
40-
var groupRepoMock = repositoriesConfig.getGroupRepo();
41-
when(groupRepoMock.findById(any())).thenReturn(Optional.of(mock(Group.class)));
41+
var groupRepoMock = repositoriesConfig.getFlowRepo();
42+
when(groupRepoMock.findById(any())).thenReturn(Optional.of(mock(Flow.class)));
4243

4344
initRootGroup.invoke();
44-
verify(groupRepoMock, times(0)).save(any(Group.class));
45+
verify(groupRepoMock, times(0)).save(any(Flow.class));
4546
}
4647
}

0 commit comments

Comments
 (0)