Skip to content
Merged
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 @@ -65,6 +65,11 @@ public DataSourceVO saveDataSource(DataSourceVO dataSource) {
return dataSource;
}

@Override
public boolean replaceDataSource(DataSourceVO dataSource) {
return dataSources.replace(dataSource.getKey(), dataSource) != null;
}

@Override
public void deleteDataSource(String key) {
dataSources.remove(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public interface SettingsRepository {

DataSourceVO saveDataSource(DataSourceVO dataSource);

boolean replaceDataSource(DataSourceVO dataSource);

void deleteDataSource(String key);

Optional<DataSourceVO> findDataSourceByKey(String key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.studio.common.exception.BusinessException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
Expand Down Expand Up @@ -101,7 +102,10 @@ public DataSourceVO createDataSource(DataSourceVO dataSource) {

public DataSourceVO updateDataSource(DataSourceVO dataSource) {
log.info("Updating data source: {}", dataSource.getKey());
return settingsRepository.saveDataSource(dataSource);
if (!settingsRepository.replaceDataSource(dataSource)) {
throw new BusinessException(404, "Data source not found: " + dataSource.getKey());
}
return dataSource;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class InMemorySettingsRepositoryTest {
private final InMemorySettingsRepository repository = new InMemorySettingsRepository();

@Test
void saveDataSourceShouldSupportCreateUpdateAndDeleteByKey() {
void saveDataSourceShouldSupportCreateAndDeleteByKey() {
DataSourceVO dataSource = DataSourceVO.builder()
.key("source-1")
.name("Prometheus")
Expand All @@ -38,17 +38,31 @@ void saveDataSourceShouldSupportCreateUpdateAndDeleteByKey() {
assertThat(repository.findDataSourceByKey("source-1")).containsSame(dataSource);
assertThat(repository.findAllDataSources()).containsExactly(dataSource);

dataSource.setName("Updated Prometheus");
repository.saveDataSource(dataSource);

assertThat(repository.findDataSourceByKey("source-1"))
.get()
.extracting(DataSourceVO::getName)
.isEqualTo("Updated Prometheus");

repository.deleteDataSource("source-1");

assertThat(repository.findDataSourceByKey("source-1")).isEmpty();
assertThat(repository.findAllDataSources()).isEmpty();
}

@Test
void replaceDataSourceShouldUpdateExistingEntry() {
DataSourceVO existing = DataSourceVO.builder().key("source-1").name("Prometheus").build();
DataSourceVO replacement = DataSourceVO.builder().key("source-1").name("Updated Prometheus").build();
repository.saveDataSource(existing);

boolean replaced = repository.replaceDataSource(replacement);

assertThat(replaced).isTrue();
assertThat(repository.findAllDataSources()).containsExactly(replacement);
}

@Test
void replaceDataSourceShouldNotInsertUnknownEntry() {
DataSourceVO replacement = DataSourceVO.builder().key("missing").name("Unexpected DS").build();

boolean replaced = repository.replaceDataSource(replacement);

assertThat(replaced).isFalse();
assertThat(repository.findAllDataSources()).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpServer;
import org.apache.rocketmq.studio.common.exception.BusinessException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -37,6 +38,7 @@
import java.util.concurrent.atomic.AtomicReference;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -213,13 +215,27 @@ void createDataSourceShouldReplaceClientProvidedKey() {
void updateDataSourceShouldDelegateToRepository() {
DataSourceVO input = DataSourceVO.builder().key("ds-1").name("Updated DS").type("rocketmq")
.url("updated-host:9876").build();
when(settingsRepository.saveDataSource(any(DataSourceVO.class))).thenReturn(input);
when(settingsRepository.replaceDataSource(input)).thenReturn(true);

DataSourceVO result = settingsService.updateDataSource(input);

assertThat(result.getKey()).isEqualTo("ds-1");
assertThat(result.getName()).isEqualTo("Updated DS");
verify(settingsRepository).saveDataSource(input);
verify(settingsRepository).replaceDataSource(input);
}

@Test
void updateDataSourceShouldRejectUnknownKey() {
SettingsService service = new SettingsService(new InMemorySettingsRepository(), RestClient.builder(), new ObjectMapper());
DataSourceVO input = DataSourceVO.builder().key("missing").name("Unexpected DS").type("rocketmq")
.url("unexpected-host:9876").build();

assertThatThrownBy(() -> service.updateDataSource(input))
.isInstanceOf(BusinessException.class)
.hasMessage("Data source not found: missing")
.extracting("code")
.isEqualTo(404);
assertThat(service.listDataSources()).isEmpty();
}

@Test
Expand Down
Loading