-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add pending state to ContractNegotiation
and TransferProcess
#3321
Merged
ndr-brt
merged 2 commits into
eclipse-edc:main
from
Think-iT-Labs:3308-manual-interactions
Jul 27, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
90 changes: 90 additions & 0 deletions
90
core/common/state-machine/src/main/java/org/eclipse/edc/statemachine/ProcessorImpl.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,90 @@ | ||
/* | ||
* Copyright (c) 2022 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.statemachine; | ||
|
||
import java.util.Collection; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
import java.util.function.Supplier; | ||
|
||
import static java.util.function.Predicate.isEqual; | ||
|
||
/** | ||
* Describes the processing flow applied by a state machine. The entities are provided by a supplier. | ||
* A process is a function that returns a boolean that indicates if the entity has been processed or not in | ||
* the scope of the function. | ||
* The run method returns the processed state count, this is used by the state machine to decide | ||
* to apply the wait strategy or not. | ||
* <p> | ||
* An {@link Guard} can be registered, if its predicate is verified, the guard processor is executed instead of the standard one. | ||
* | ||
* @param <E> the entity that is processed | ||
*/ | ||
public class ProcessorImpl<E> implements Processor { | ||
|
||
private final Supplier<Collection<E>> entities; | ||
private Function<E, Boolean> process; | ||
private Guard<E> guard = Guard.noop(); | ||
|
||
private ProcessorImpl(Supplier<Collection<E>> entitiesSupplier) { | ||
entities = entitiesSupplier; | ||
} | ||
|
||
@Override | ||
public Long process() { | ||
return entities.get().stream() | ||
.map(entity -> guard.predicate().test(entity) | ||
? guard.process().apply(entity) : | ||
process.apply(entity)) | ||
.filter(isEqual(true)) | ||
.count(); | ||
} | ||
|
||
public static class Builder<E> { | ||
|
||
private final ProcessorImpl<E> processor; | ||
|
||
public Builder(Supplier<Collection<E>> entitiesSupplier) { | ||
processor = new ProcessorImpl<>(entitiesSupplier); | ||
} | ||
|
||
public static <E> Builder<E> newInstance(Supplier<Collection<E>> entitiesSupplier) { | ||
return new Builder<>(entitiesSupplier); | ||
} | ||
|
||
public Builder<E> process(Function<E, Boolean> process) { | ||
processor.process = process; | ||
return this; | ||
} | ||
|
||
public Builder<E> guard(Predicate<E> predicate, Function<E, Boolean> process) { | ||
processor.guard = new Guard<>(predicate, process); | ||
return this; | ||
} | ||
|
||
public ProcessorImpl<E> build() { | ||
Objects.requireNonNull(processor.process); | ||
|
||
return processor; | ||
} | ||
} | ||
|
||
private record Guard<E>(Predicate<E> predicate, Function<E, Boolean> process) { | ||
static <E> Guard<E> noop() { | ||
return new Guard<>(e -> false, e -> 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
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
91 changes: 91 additions & 0 deletions
91
core/common/state-machine/src/test/java/org/eclipse/edc/statemachine/ProcessorImplTest.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,91 @@ | ||
/* | ||
* Copyright (c) 2022 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.statemachine; | ||
|
||
import org.eclipse.edc.statemachine.retry.TestEntity; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
class ProcessorImplTest { | ||
|
||
@Test | ||
void shouldReturnTheProcessedCount() { | ||
var entity = TestEntity.Builder.newInstance().id("id").build(); | ||
var processor = ProcessorImpl.Builder.newInstance(() -> List.of(entity)) | ||
.process(e -> true) | ||
.build(); | ||
|
||
var count = processor.process(); | ||
|
||
assertThat(count).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
void shouldNotCountUnprocessedEntities() { | ||
var entity = TestEntity.Builder.newInstance().id("id").build(); | ||
var processor = ProcessorImpl.Builder.newInstance(() -> List.of(entity)) | ||
.process(e -> false) | ||
.build(); | ||
|
||
var count = processor.process(); | ||
|
||
assertThat(count).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
void shouldExecuteGuard_whenItsPredicateMatches() { | ||
var entity = TestEntity.Builder.newInstance().id("id").build(); | ||
Function<TestEntity, Boolean> process = mock(); | ||
Function<TestEntity, Boolean> guardProcess = mock(); | ||
when(guardProcess.apply(any())).thenReturn(true); | ||
var processor = ProcessorImpl.Builder.newInstance(() -> List.of(entity)) | ||
.guard(e -> true, guardProcess) | ||
.process(process) | ||
.build(); | ||
|
||
var count = processor.process(); | ||
|
||
assertThat(count).isEqualTo(1); | ||
verify(guardProcess).apply(entity); | ||
verifyNoInteractions(process); | ||
} | ||
|
||
@Test | ||
void shouldExecuteDefaultProcessor_whenGuardPredicateDoesNotMatch() { | ||
var entity = TestEntity.Builder.newInstance().id("id").build(); | ||
Function<TestEntity, Boolean> process = mock(); | ||
Function<TestEntity, Boolean> guardProcess = mock(); | ||
when(process.apply(any())).thenReturn(true); | ||
var processor = ProcessorImpl.Builder.newInstance(() -> List.of(entity)) | ||
.guard(e -> false, guardProcess) | ||
.process(process) | ||
.build(); | ||
|
||
var count = processor.process(); | ||
|
||
assertThat(count).isEqualTo(1); | ||
verify(process).apply(entity); | ||
verifyNoInteractions(guardProcess); | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there really any point to deprecating this? It's an internal class, not intended for public use
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least tractus-x is using this for the EDR state machine, I think this deprecation work helps a lot migrating from version to version (because the
@deprecated
tag in the javadoc text)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems this begs a broader discussion about SPIs, what we should and should not deprecate etc.