Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.rocketmq.studio.settings;

import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -26,7 +27,9 @@
@NoArgsConstructor
@AllArgsConstructor
public class DataSourceTestDTO {
@NotBlank(message = "url is required")
private String url;
@NotBlank(message = "type is required")
private String type;
private String auth;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.rocketmq.studio.settings;

import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -27,8 +28,11 @@
@AllArgsConstructor
public class DataSourceVO {
private String key;
@NotBlank(message = "name is required")
private String name;
@NotBlank(message = "type is required")
private String type;
@NotBlank(message = "url is required")
private String url;
private String auth;
private String status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public Result<List<DataSourceVO>> listDataSources() {
}

@PostMapping("/datasources/create")
public Result<DataSourceVO> createDataSource(@RequestBody DataSourceVO dataSource) {
public Result<DataSourceVO> createDataSource(@Valid @RequestBody DataSourceVO dataSource) {
return Result.ok(settingsService.createDataSource(dataSource));
}

@PostMapping("/datasources/update")
public Result<DataSourceVO> updateDataSource(@RequestBody DataSourceVO dataSource) {
public Result<DataSourceVO> updateDataSource(@Valid @RequestBody DataSourceVO dataSource) {
return Result.ok(settingsService.updateDataSource(dataSource));
}

Expand All @@ -68,7 +68,7 @@ public Result<Void> deleteDataSource(@RequestParam String key) {
}

@PostMapping("/datasources/test")
public Result<DataSourceTestResultVO> testDataSource(@RequestBody DataSourceTestDTO request) {
public Result<DataSourceTestResultVO> testDataSource(@Valid @RequestBody DataSourceTestDTO request) {
return Result.ok(settingsService.testDataSource(request));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,23 @@ void createDataSourceShouldReturnCreatedSource() throws Exception {
.andExpect(jsonPath("$.data.status", is("connected")));
}

@Test
void createDataSourceShouldRejectMissingUrl() throws Exception {
mockMvc.perform(post("/api/settings/datasources/create")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"name": "New DS",
"type": "prometheus"
}
"""))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code", is(400)))
.andExpect(jsonPath("$.message", is("url is required")));

verifyNoInteractions(settingsService);
}

@Test
void updateDataSourceShouldReturnUpdatedSource() throws Exception {
DataSourceVO input = DataSourceVO.builder().key("ds-1").name("Updated DS").type("rocketmq")
Expand All @@ -218,6 +235,24 @@ void updateDataSourceShouldReturnUpdatedSource() throws Exception {
.andExpect(jsonPath("$.data.name", is("Updated DS")));
}

@Test
void updateDataSourceShouldRejectMissingName() throws Exception {
mockMvc.perform(post("/api/settings/datasources/update")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"key": "ds-1",
"type": "rocketmq",
"url": "updated:9876"
}
"""))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code", is(400)))
.andExpect(jsonPath("$.message", is("name is required")));

verifyNoInteractions(settingsService);
}

@Test
void deleteDataSourceShouldReturnSuccess() throws Exception {
doNothing().when(settingsService).deleteDataSource("ds-1");
Expand Down Expand Up @@ -250,4 +285,20 @@ void testDataSourceShouldReturnTestResult() throws Exception {
.andExpect(jsonPath("$.data.success", is(true)))
.andExpect(jsonPath("$.data.message", is("Connection successful")));
}

@Test
void testDataSourceShouldRejectMissingType() throws Exception {
mockMvc.perform(post("/api/settings/datasources/test")
.contentType(MediaType.APPLICATION_JSON)
.content("""
{
"url": "localhost:9876"
}
"""))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code", is(400)))
.andExpect(jsonPath("$.message", is("type is required")));

verifyNoInteractions(settingsService);
}
}
Loading