Skip to content

Commit 6c6494e

Browse files
committed
change path parameter to flow name
1 parent c49a0a8 commit 6c6494e

File tree

4 files changed

+38
-33
lines changed

4 files changed

+38
-33
lines changed

src/main/java/com/flowci/build/BuildController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import com.flowci.build.business.ListBuilds;
44
import com.flowci.build.model.Build;
55
import io.swagger.v3.oas.annotations.Operation;
6-
import io.swagger.v3.oas.annotations.Parameter;
76
import io.swagger.v3.oas.annotations.tags.Tag;
87
import jakarta.validation.Valid;
98
import jakarta.validation.constraints.Min;
109
import lombok.AllArgsConstructor;
1110
import org.springframework.data.domain.PageRequest;
12-
import org.springframework.web.bind.annotation.*;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestParam;
14+
import org.springframework.web.bind.annotation.RestController;
1315

1416
import java.util.List;
1517

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.flowci.common.exception.DuplicateException;
44
import com.flowci.common.exception.ExceptionUtils;
55
import com.flowci.common.validator.ValidId;
6+
import com.flowci.common.validator.ValidName;
67
import com.flowci.flow.business.*;
78
import com.flowci.flow.model.CreateFlowParam;
89
import com.flowci.flow.model.Flow;
@@ -37,9 +38,9 @@ public class FlowController {
3738
private final FetchFlowYamlContent fetchFlowYamlContent;
3839
private final UpdateFlowYamlContent updateFlowYamlContent;
3940

40-
@GetMapping("/{id}")
41-
public Flow getFlow(@PathVariable("id") @Valid @ValidId String id) {
42-
return fetchFlow.invoke(parseLong(id));
41+
@GetMapping("/{name}")
42+
public Flow getFlow(@PathVariable("name") @Valid @ValidName String name) {
43+
return fetchFlow.invoke(name);
4344
}
4445

4546
@Operation(description = "get flow list by page")
@@ -81,9 +82,10 @@ public List<YamlTemplate> getTemplates() {
8182
)
8283
)
8384
)
84-
@GetMapping(value = "/{id}/yaml", produces = MediaType.TEXT_PLAIN_VALUE)
85-
public String getYaml(@PathVariable("id") @Valid @ValidId String id) {
86-
return fetchFlowYamlContent.invoke(parseLong(id), true);
85+
@GetMapping(value = "/{name}/yaml", produces = MediaType.TEXT_PLAIN_VALUE)
86+
public String getYaml(@PathVariable("name") @Valid @ValidName String name) {
87+
var flow = fetchFlow.invoke(name);
88+
return fetchFlowYamlContent.invoke(flow.getId(), true);
8789
}
8890

8991
@Operation(
@@ -99,9 +101,10 @@ public String getYaml(@PathVariable("id") @Valid @ValidId String id) {
99101
)
100102
)
101103
)
102-
@PostMapping(value = "/{id}/yaml", consumes = MediaType.TEXT_PLAIN_VALUE)
103-
public void updateYaml(@PathVariable("id") @Valid @ValidId String id,
104+
@PostMapping(value = "/{name}/yaml", consumes = MediaType.TEXT_PLAIN_VALUE)
105+
public void updateYaml(@PathVariable("name") @Valid @ValidName String name,
104106
@RequestBody String b64Yaml) {
105-
updateFlowYamlContent.invoke(parseLong(id), b64Yaml);
107+
var flow = fetchFlow.invoke(name);
108+
updateFlowYamlContent.invoke(flow.getId(), b64Yaml);
106109
}
107110
}

src/test/java/com/flowci/flow/FlowControllerTest.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Base64;
2222

2323
import static com.flowci.TestUtils.newDummyInstance;
24+
import static org.instancio.Select.field;
2425
import static org.junit.jupiter.api.Assertions.assertEquals;
2526
import static org.mockito.ArgumentMatchers.any;
2627
import static org.mockito.Mockito.*;
@@ -123,11 +124,14 @@ void givenCreateFlowParameter_whenCreateFlowWithUnexpectedError_thenReturnError(
123124
}
124125

125126
@Test
126-
void givenFlowId_whenFetching_thenReturnFlow() throws Exception {
127-
var mockFlow = newDummyInstance(Flow.class).create();
128-
when(fetchFlow.invoke(anyLong())).thenReturn(mockFlow);
127+
void givenFlowName_whenFetching_thenReturnFlow() throws Exception {
128+
var mockFlow = newDummyInstance(Flow.class)
129+
.set(field(Flow::getName), "hello_world")
130+
.create();
129131

130-
var r = mvc.perform(get("/v2/flows/" + mockFlow.getId()))
132+
when(fetchFlow.invoke(anyString())).thenReturn(mockFlow);
133+
134+
var r = mvc.perform(get(String.format("/v2/flows/%s", mockFlow.getName())))
131135
.andExpect(status().is2xxSuccessful())
132136
.andReturn();
133137

@@ -136,22 +140,16 @@ void givenFlowId_whenFetching_thenReturnFlow() throws Exception {
136140
}
137141

138142
@Test
139-
void givenInvalidFlowId_whenFetching_thenReturnError() throws Exception {
140-
var r = mvc.perform(get("/v2/flows/-1"))
141-
.andExpect(status().is4xxClientError())
142-
.andReturn();
143+
void givenFlowName_whenFetchingYaml_thenReturnBase64EncodedYaml() throws Exception {
144+
var mockFlow = newDummyInstance(Flow.class)
145+
.set(field(Flow::getName), "some_name")
146+
.create();
143147

144-
var error = objectMapper.readValue(r.getResponse().getContentAsString(), ErrorResponse.class);
145-
assertEquals(400, error.code());
146-
assertEquals("invalid id", error.message());
147-
}
148-
149-
@Test
150-
void givenFlowId_whenFetchingYaml_thenReturnBase64EncodedYaml() throws Exception {
151148
var expectedYaml = Base64.getEncoder().encodeToString("some yaml".getBytes());
149+
when(fetchFlow.invoke(anyString())).thenReturn(mockFlow);
152150
when(fetchFlowYamlContent.invoke(any(), eq(true))).thenReturn(expectedYaml);
153151

154-
var r = mvc.perform(get("/v2/flows/1001/yaml"))
152+
var r = mvc.perform(get(String.format("/v2/flows/%s/yaml", mockFlow.getName())))
155153
.andExpectAll(
156154
status().is2xxSuccessful(),
157155
header().string("Content-Type", "text/plain;charset=UTF-8"))
@@ -161,13 +159,18 @@ void givenFlowId_whenFetchingYaml_thenReturnBase64EncodedYaml() throws Exception
161159
}
162160

163161
@Test
164-
void givenFlowIdAndYaml_whenUpdating_thenYamlIsUpdated() throws Exception {
162+
void givenFlowNameAndYaml_whenUpdating_thenYamlIsUpdated() throws Exception {
163+
var mockFlow = newDummyInstance(Flow.class)
164+
.set(field(Flow::getName), "some_name")
165+
.create();
166+
when(fetchFlow.invoke(anyString())).thenReturn(mockFlow);
167+
165168
var expectedYaml = Base64.getEncoder().encodeToString("some yaml".getBytes());
166169

167170
var yamlCaptor = ArgumentCaptor.forClass(String.class);
168171
doNothing().when(updateFlowYamlContent).invoke(any(), yamlCaptor.capture());
169172

170-
var r = mvc.perform(post("/v2/flows/1001/yaml")
173+
mvc.perform(post(String.format("/v2/flows/%s/yaml", mockFlow.getName()))
171174
.contentType(MediaType.TEXT_PLAIN)
172175
.content(expectedYaml))
173176
.andExpect(status().is2xxSuccessful())

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,11 @@
77
import org.mockito.ArgumentCaptor;
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.boot.test.mock.mockito.MockBean;
10-
import org.springframework.data.domain.PageRequest;
11-
import org.springframework.data.domain.Pageable;
1210

1311
import java.util.List;
1412

1513
import static com.flowci.TestUtils.newDummyInstance;
1614
import static org.junit.jupiter.api.Assertions.assertEquals;
17-
import static org.mockito.ArgumentMatchers.any;
1815
import static org.mockito.Mockito.*;
1916

2017
class ListFlowsTest extends SpringTest {
@@ -50,7 +47,7 @@ void givenParentId_whenFetching_thenReturnAllFlowsUnderTheParent() {
5047
when(requestContextHolder.getUserId()).thenReturn(userIdMock);
5148

5249
// when fetching list flow without parent id
53-
var list = listFlows.invoke(null, PageRequest.of(0, 1));
50+
var list = listFlows.invoke(null);
5451
assertEquals(2, list.size());
5552

5653
// verify the parent should be root id as default

0 commit comments

Comments
 (0)