Skip to content

Commit

Permalink
fix: consider terminated a non existing DataFlow (#4420)
Browse files Browse the repository at this point in the history
fix: consider terminated a DataFlow that cannot be terminated because it does not exist
  • Loading branch information
ndr-brt authored Aug 21, 2024
1 parent 3353b62 commit a01be82
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

Expand Down Expand Up @@ -114,7 +115,10 @@ public boolean canHandle(TransferProcess transferProcess) {

@Override
public StatusResult<Void> suspend(TransferProcess transferProcess) {
return getClientForDataplane(transferProcess.getDataPlaneId())
return Optional.ofNullable(transferProcess.getDataPlaneId())
.map(StatusResult::success)
.orElse(StatusResult.failure(FATAL_ERROR, "DataPlane id is null"))
.compose(this::getClientForDataplane)
.map(client -> client.suspend(transferProcess.getId()))
.orElse(f -> {
var message = "Failed to select the data plane for suspending the transfer process %s. %s"
Expand All @@ -125,7 +129,12 @@ public StatusResult<Void> suspend(TransferProcess transferProcess) {

@Override
public StatusResult<Void> terminate(TransferProcess transferProcess) {
return getClientForDataplane(transferProcess.getDataPlaneId())
var dataPlaneId = transferProcess.getDataPlaneId();
if (dataPlaneId == null) {
return StatusResult.success();
}

return getClientForDataplane(dataPlaneId)
.map(client -> client.terminate(transferProcess.getId()))
.orElse(f -> {
var message = "Failed to select the data plane for terminating the transfer process %s. %s"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

public class DataPlaneSignalingFlowControllerTest {
Expand Down Expand Up @@ -256,6 +257,20 @@ void shouldFail_whenDataPlaneNotFound() {

assertThat(result).isFailed().detail().contains("Failed to select the data plane for terminating the transfer process");
}

@Test // a null dataPlaneId means that the flow has not been started so it can be considered as already terminated
void shouldReturnSuccess_whenDataPlaneIdIsNull() {
var transferProcess = transferProcessBuilder()
.id("transferProcessId")
.contentDataAddress(testDataAddress())
.dataPlaneId(null)
.build();

var result = flowController.terminate(transferProcess);

assertThat(result).isSucceeded();
verifyNoInteractions(dataPlaneClient, dataPlaneClientFactory, selectorService);
}
}

@Nested
Expand Down Expand Up @@ -287,13 +302,26 @@ void shouldFail_whenDataPlaneDoesNotExist() {
.contentDataAddress(testDataAddress())
.dataPlaneId("invalid")
.build();
when(dataPlaneClient.suspend(any())).thenReturn(StatusResult.success());
when(dataPlaneClientFactory.createClient(any())).thenReturn(dataPlaneClient);
when(selectorService.findById(any())).thenReturn(ServiceResult.notFound("not found"));

var result = flowController.suspend(transferProcess);

assertThat(result).isFailed().detail().contains("Failed to select the data plane for suspending the transfer process");
verifyNoInteractions(dataPlaneClient, dataPlaneClientFactory);
}

@Test
void shouldFail_whenDataPlaneIdIsNull() {
var transferProcess = TransferProcess.Builder.newInstance()
.id("transferProcessId")
.contentDataAddress(testDataAddress())
.dataPlaneId(null)
.build();

var result = flowController.suspend(transferProcess);

assertThat(result).isFailed().detail().contains("Failed to select the data plane for suspending the transfer process");
verifyNoInteractions(dataPlaneClient, dataPlaneClientFactory, selectorService);
}

}
Expand Down

0 comments on commit a01be82

Please sign in to comment.