forked from eclipse-edc/Connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add STOPPING state on TransferProcess
- Loading branch information
Showing
15 changed files
with
341 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
.../java/org/eclipse/edc/connector/transfer/command/handlers/StopTransferCommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.connector.transfer.command.handlers; | ||
|
||
import org.eclipse.edc.connector.transfer.spi.store.TransferProcessStore; | ||
import org.eclipse.edc.connector.transfer.spi.types.TransferProcess; | ||
import org.eclipse.edc.connector.transfer.spi.types.TransferProcessStates; | ||
import org.eclipse.edc.connector.transfer.spi.types.command.StopTransferCommand; | ||
import org.eclipse.edc.connector.transfer.spi.types.command.TerminateTransferCommand; | ||
import org.eclipse.edc.spi.command.EntityCommandHandler; | ||
|
||
import java.util.List; | ||
|
||
import static org.eclipse.edc.connector.transfer.spi.types.TransferProcessStates.COMPLETING; | ||
import static org.eclipse.edc.connector.transfer.spi.types.TransferProcessStates.SUSPENDING; | ||
import static org.eclipse.edc.connector.transfer.spi.types.TransferProcessStates.TERMINATING; | ||
|
||
/** | ||
* Transition a TransferProcess to the {@link TransferProcessStates#STOPPING} state. | ||
* Only possible on PROVIDER side. | ||
*/ | ||
public class StopTransferCommandHandler extends EntityCommandHandler<StopTransferCommand, TransferProcess> { | ||
|
||
public StopTransferCommandHandler(TransferProcessStore store) { | ||
super(store); | ||
} | ||
|
||
@Override | ||
public Class<StopTransferCommand> getType() { | ||
return StopTransferCommand.class; | ||
} | ||
|
||
@Override | ||
protected boolean modify(TransferProcess process, StopTransferCommand command) { | ||
if (process.canBeStopped() && List.of(COMPLETING, TERMINATING, SUSPENDING).contains(command.getSubsequentState())) { | ||
process.transitionStopping(command.getReason(), command.getSubsequentState()); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...a/org/eclipse/edc/connector/transfer/command/handlers/StopTransferCommandHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (c) 2023 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Contributors: | ||
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - Initial implementation | ||
* | ||
*/ | ||
|
||
package org.eclipse.edc.connector.transfer.command.handlers; | ||
|
||
import org.eclipse.edc.connector.transfer.spi.store.TransferProcessStore; | ||
import org.eclipse.edc.connector.transfer.spi.types.TransferProcess; | ||
import org.eclipse.edc.connector.transfer.spi.types.command.StopTransferCommand; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.eclipse.edc.connector.transfer.spi.types.TransferProcess.Type.CONSUMER; | ||
import static org.eclipse.edc.connector.transfer.spi.types.TransferProcess.Type.PROVIDER; | ||
import static org.eclipse.edc.connector.transfer.spi.types.TransferProcessStates.COMPLETED; | ||
import static org.eclipse.edc.connector.transfer.spi.types.TransferProcessStates.COMPLETING; | ||
import static org.eclipse.edc.connector.transfer.spi.types.TransferProcessStates.STARTED; | ||
import static org.eclipse.edc.connector.transfer.spi.types.TransferProcessStates.STARTING; | ||
import static org.eclipse.edc.connector.transfer.spi.types.TransferProcessStates.STOPPING; | ||
import static org.mockito.Mockito.mock; | ||
|
||
class StopTransferCommandHandlerTest { | ||
|
||
private final TransferProcessStore store = mock(); | ||
private final StopTransferCommandHandler handler = new StopTransferCommandHandler(store); | ||
|
||
@Test | ||
void verifyCorrectType() { | ||
assertThat(handler.getType()).isEqualTo(StopTransferCommand.class); | ||
} | ||
|
||
@Test | ||
void shouldModify_ifItCanBeStopped() { | ||
var command = new StopTransferCommand("test-id", "a reason", COMPLETING); | ||
var entity = TransferProcess.Builder.newInstance().type(PROVIDER).state(STARTED.code()).build(); | ||
|
||
var result = handler.modify(entity, command); | ||
|
||
assertThat(result).isTrue(); | ||
assertThat(entity.getState()).isEqualTo(STOPPING.code()); | ||
assertThat(entity.getErrorDetail()).isEqualTo("a reason"); | ||
assertThat(entity.stoppingSubsequentState()).isEqualTo(COMPLETING); | ||
} | ||
|
||
@Test | ||
void shouldNotModify_ifItCannotBeStopped() { | ||
var command = new StopTransferCommand("test-id", "a reason", STARTING); | ||
var entity = TransferProcess.Builder.newInstance().type(PROVIDER).state(COMPLETED.code()).build(); | ||
|
||
var result = handler.modify(entity, command); | ||
|
||
assertThat(result).isFalse(); | ||
assertThat(entity.getState()).isEqualTo(COMPLETED.code()); | ||
assertThat(entity.getErrorDetail()).isNull(); | ||
} | ||
|
||
@Test | ||
void shouldNotModify_ifItIsConsumer() { | ||
var command = new StopTransferCommand("test-id", "a reason", COMPLETING); | ||
var entity = TransferProcess.Builder.newInstance().type(CONSUMER).state(STARTED.code()).build(); | ||
|
||
var result = handler.modify(entity, command); | ||
|
||
assertThat(result).isFalse(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.