Skip to content

Commit 1c4bbe6

Browse files
committed
fix unit test failure since the way of creating flow has been changed
1 parent 75e4057 commit 1c4bbe6

File tree

4 files changed

+44
-22
lines changed

4 files changed

+44
-22
lines changed

core/src/main/java/com/flowci/core/flow/service/FlowServiceImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ public List<Flow> listByCredential(String secretName) {
132132

133133
@Override
134134
public Flow create(String name, CreateOption option) {
135+
Objects.requireNonNull(option, "CreateOption is missing");
135136
Flow.validateName(name);
137+
136138
var yamlContent = getYmlContent(option);
137139
var flow = new Flow(name);
138140
flow.getVars().put(Variables.Flow.Name, VarValue.of(flow.getName(), VarType.STRING, false));

core/src/test/java/com/flowci/core/test/api/OpenRestServiceTest.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.flowci.core.test.MockLoggedInScenario;
3030
import com.flowci.core.test.SpringScenario;
3131
import com.flowci.core.user.domain.User;
32+
import com.flowci.util.StringHelper;
3233
import org.junit.Assert;
3334
import org.junit.Before;
3435
import org.junit.Test;
@@ -37,6 +38,7 @@
3738
import org.springframework.boot.test.mock.mockito.MockBean;
3839
import org.springframework.mock.web.MockMultipartFile;
3940

41+
import java.io.IOException;
4042
import java.util.List;
4143
import java.util.Optional;
4244

@@ -54,10 +56,17 @@ public class OpenRestServiceTest extends MockLoggedInScenario {
5456
@Autowired
5557
private OpenRestService openRestService;
5658

59+
private Flow flow;
60+
61+
@Before
62+
public void init() throws IOException {
63+
var yaml = StringHelper.toString(load("flow.yml"));
64+
var option = new CreateOption().setRawYaml(StringHelper.toBase64(yaml));
65+
flow = flowService.create("user-test", option);
66+
}
67+
5768
@Test
5869
public void should_list_all_flow_users() {
59-
Flow flow = flowService.create("user-test", new CreateOption());
60-
6170
List<User> users = openRestService.users(flow.getName());
6271
Assert.assertEquals(1, users.size());
6372

@@ -74,10 +83,8 @@ public void should_list_all_flow_users() {
7483
}
7584

7685
@Test
77-
public void should_save_job_report() {
86+
public void should_save_job_report() throws IOException {
7887
// given:
79-
Flow flow = flowService.create("user-test", null);
80-
8188
Job job = new Job();
8289
job.setId("12345");
8390
job.setFlowId(flow.getId());

core/src/test/java/com/flowci/core/test/job/CmdManagerTest.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,14 @@ public class CmdManagerTest extends MockLoggedInScenario {
7272
@Test
7373
public void should_apply_flow_level_docker_option() throws IOException {
7474
// given: flow and job
75-
Flow flow = flowService.create("hello", new CreateOption());
76-
String yaml = StringHelper.toString(load("flow-with-root-docker.yml"));
77-
Yml ymlObj = ymlService.saveYml(flow, Yml.DEFAULT_NAME, yaml);
78-
Job job = jobService.create(flow, ymlObj.getRaw(), Job.Trigger.MANUAL, new StringVars());
75+
var yaml = StringHelper.toString(load("flow-with-root-docker.yml"));
76+
var option = new CreateOption().setRawYaml(StringHelper.toBase64(yaml));
77+
Flow flow = flowService.create("hello", option);
78+
79+
Job job = jobService.create(flow, yaml, Job.Trigger.MANUAL, new StringVars());
7980
Assert.assertNotNull(job);
8081

81-
FlowNode root = YmlParser.load(ymlObj.getRaw());
82+
FlowNode root = YmlParser.load(yaml);
8283
NodeTree tree = NodeTree.create(root);
8384

8485
// when: create first shell cmd
@@ -111,15 +112,15 @@ public void should_create_cmd_in_with_default_plugin_value() throws IOException
111112
Mockito.when(eventManager.publish(Mockito.any())).thenReturn(event);
112113

113114
// given: flow and job
114-
Flow flow = flowService.create("hello", new CreateOption());
115-
String yaml = StringHelper.toString(load("flow-with-plugin.yml"));
116-
Yml yml = ymlService.saveYml(flow, Yml.DEFAULT_NAME, yaml);
115+
var yaml = StringHelper.toString(load("flow-with-plugin.yml"));
116+
var option = new CreateOption().setRawYaml(StringHelper.toBase64(yaml));
117+
var flow = flowService.create("hello", option);
117118

118-
Job job = jobService.create(flow, yml.getRaw(), Job.Trigger.MANUAL, new StringVars());
119+
Job job = jobService.create(flow, yaml, Job.Trigger.MANUAL, new StringVars());
119120
Assert.assertNotNull(job);
120121

121122
// when: create shell cmd
122-
FlowNode root = YmlParser.load(yml.getRaw());
123+
FlowNode root = YmlParser.load(yaml);
123124
NodeTree tree = NodeTree.create(root);
124125
Node node = tree.get(NodePath.create(DEFAULT_ROOT_NAME, "plugin-test"));
125126
Step step = stepService.get(job.getId(), node.getPath().getPathInStr());
@@ -149,15 +150,15 @@ public void should_create_cmd_in_with_default_plugin_value() throws IOException
149150
@Test
150151
public void should_handle_step_in_step() throws IOException {
151152
// given: flow and job
152-
Flow flow = flowService.create("hello", new CreateOption());
153-
String yaml = StringHelper.toString(load("step-in-step.yml"));
154-
Yml yml = ymlService.saveYml(flow, Yml.DEFAULT_NAME, yaml);
153+
var yaml = StringHelper.toString(load("step-in-step.yml"));
154+
var option = new CreateOption().setRawYaml(StringHelper.toBase64(yaml));
155+
var flow = flowService.create("hello", option);
155156

156-
Job job = jobService.create(flow, yml.getRaw(), Job.Trigger.MANUAL, new StringVars());
157+
Job job = jobService.create(flow, yaml, Job.Trigger.MANUAL, new StringVars());
157158
Assert.assertNotNull(job);
158159

159160
// when: create shell cmd
160-
FlowNode root = YmlParser.load(yml.getRaw());
161+
FlowNode root = YmlParser.load(yaml);
161162
NodeTree tree = NodeTree.create(root);
162163

163164
Node step2_1 = tree.get(NodePath.create(DEFAULT_ROOT_NAME, "step2", "step-2-1"));

core/src/test/java/com/flowci/core/test/user/UserControllerTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.fasterxml.jackson.core.type.TypeReference;
2121
import com.fasterxml.jackson.databind.ObjectMapper;
2222
import com.flowci.core.common.domain.StatusCode;
23+
import com.flowci.core.common.manager.SessionManager;
2324
import com.flowci.core.test.MockLoggedInScenario;
2425
import com.flowci.core.test.MockMvcHelper;
2526
import com.flowci.core.test.SpringScenario;
@@ -30,6 +31,7 @@
3031
import com.flowci.exception.ErrorCode;
3132
import com.flowci.exception.NotFoundException;
3233
import com.flowci.util.HashingHelper;
34+
import com.flowci.util.StringHelper;
3335
import org.junit.Assert;
3436
import org.junit.Before;
3537
import org.junit.Test;
@@ -43,7 +45,7 @@
4345
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
4446
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
4547

46-
public class UserControllerTest extends MockLoggedInScenario {
48+
public class UserControllerTest extends SpringScenario {
4749

4850
private static final TypeReference<ResponseMessage<User>> UserType =
4951
new TypeReference<>() {
@@ -55,8 +57,18 @@ public class UserControllerTest extends MockLoggedInScenario {
5557
@Autowired
5658
private ObjectMapper objectMapper;
5759

60+
@Autowired
61+
private SessionManager sessionManager;
62+
5863
@Test(expected = AuthenticationException.class)
5964
public void should_change_password_for_current_user_successfully() throws Exception {
65+
var dummy = new User();
66+
dummy.setId("1111");
67+
dummy.setPasswordOnMd5(HashingHelper.md5("22222"));
68+
dummy.setRole(User.Role.Admin);
69+
dummy.setEmail("[email protected]");
70+
sessionManager.set(dummy);
71+
6072
User user = sessionManager.get();
6173

6274
String newPwOnMd5 = HashingHelper.md5("11111");
@@ -66,7 +78,7 @@ public void should_change_password_for_current_user_successfully() throws Except
6678
body.setConfirm(newPwOnMd5);
6779

6880
// when:
69-
ResponseMessage message = mockMvcHelper.expectSuccessAndReturnClass(
81+
var message = mockMvcHelper.expectSuccessAndReturnClass(
7082
post("/users/change/password")
7183
.content(objectMapper.writeValueAsBytes(body))
7284
.contentType(MediaType.APPLICATION_JSON), ResponseMessage.class);

0 commit comments

Comments
 (0)