-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from ppi-ag/feature-persistence-matcher
Feature persistence matcher + Post Processor
- Loading branch information
Showing
17 changed files
with
667 additions
and
59 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
deepsampler-core/src/main/java/de/ppi/deepsampler/core/api/Execution.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,39 @@ | ||
/* | ||
* Copyright 2020 PPI AG (Hamburg, Germany) | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
|
||
package de.ppi.deepsampler.core.api; | ||
|
||
import de.ppi.deepsampler.core.model.ExecutionRepository; | ||
import de.ppi.deepsampler.core.model.SampleRepository; | ||
|
||
/** | ||
* Provides functionality for influencing the execution phase of the stubbing done by deepsampler. | ||
* | ||
* @author Rico Schrage | ||
*/ | ||
public class Execution { | ||
|
||
private Execution() { | ||
// static only | ||
} | ||
|
||
/** | ||
* Makes deepsampler use a provided {@link SampleReturnProcessor} when returning arbitrary stubbed data. | ||
* | ||
* @param sampleReturnProcessor the sampleReturnProcessor | ||
*/ | ||
public static void useGlobal(SampleReturnProcessor sampleReturnProcessor) { | ||
ExecutionRepository.getInstance().addGlobalSampleReturnProcessor(sampleReturnProcessor); | ||
} | ||
|
||
/** | ||
* Makes deepsampler use a provided {@link SampleReturnProcessor} when returning stubbed data defined by the last created sample. | ||
* | ||
* @param sampleReturnProcessor the sampleReturnProcessor | ||
*/ | ||
public static void useForLastSample(SampleReturnProcessor sampleReturnProcessor) { | ||
ExecutionRepository.getInstance().addSampleReturnProcessor(SampleRepository.getInstance().getLastSampleDefinition(), sampleReturnProcessor); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
deepsampler-core/src/main/java/de/ppi/deepsampler/core/api/SampleReturnProcessor.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,28 @@ | ||
/* | ||
* Copyright 2020 PPI AG (Hamburg, Germany) | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
|
||
package de.ppi.deepsampler.core.api; | ||
|
||
import de.ppi.deepsampler.core.model.SampleDefinition; | ||
import de.ppi.deepsampler.core.model.StubMethodInvocation; | ||
|
||
/** | ||
* Hook-in processor for influencing the stubbing process. | ||
* | ||
* @author Rico Schrage | ||
*/ | ||
@FunctionalInterface | ||
public interface SampleReturnProcessor { | ||
/** | ||
* Will run after a stubbed method has been called. | ||
* | ||
* @param sampleDefinition the sampleDefinition which is responsible for the stubbing of the method call | ||
* @param stubMethodInvocation the actual method call | ||
* @param returnValue the real return value of the method call | ||
* | ||
* @return the new return value of the method call | ||
*/ | ||
Object onReturn(final SampleDefinition sampleDefinition, final StubMethodInvocation stubMethodInvocation, final Object returnValue); | ||
} |
36 changes: 36 additions & 0 deletions
36
deepsampler-core/src/main/java/de/ppi/deepsampler/core/internal/SampleHandling.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,36 @@ | ||
/* | ||
* Copyright 2020 PPI AG (Hamburg, Germany) | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
|
||
package de.ppi.deepsampler.core.internal; | ||
|
||
import de.ppi.deepsampler.core.model.ParameterMatcher; | ||
import de.ppi.deepsampler.core.model.SampleDefinition; | ||
|
||
import java.util.List; | ||
|
||
public class SampleHandling { | ||
|
||
private SampleHandling() { | ||
//static only | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public static boolean argumentsMatch(final SampleDefinition sampleDefinition, final Object[] arguments) { | ||
final List<ParameterMatcher<?>> parameterMatchers = sampleDefinition.getParameterMatchers(); | ||
|
||
if (parameterMatchers.size() != arguments.length) { | ||
return false; | ||
} | ||
|
||
for (int i = 0; i < arguments.length; i++) { | ||
final ParameterMatcher<Object> parameterMatcher = (ParameterMatcher<Object>) parameterMatchers.get(i); | ||
if (!parameterMatcher.matches(arguments[i])) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
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
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
37 changes: 37 additions & 0 deletions
37
deepsampler-core/src/test/java/de/ppi/deepsampler/core/api/ExecutionTest.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,37 @@ | ||
package de.ppi.deepsampler.core.api; | ||
|
||
import de.ppi.deepsampler.core.model.ExecutionRepository; | ||
import de.ppi.deepsampler.core.model.SampleDefinition; | ||
import de.ppi.deepsampler.core.model.SampleRepository; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class ExecutionTest { | ||
|
||
@Test | ||
void useGlobal() { | ||
// GIVEN | ||
SampleReturnProcessor sampleReturnProcessor = (a, b, c) -> null; | ||
|
||
// WHEN | ||
Execution.useGlobal(sampleReturnProcessor); | ||
|
||
// THEN | ||
assertEquals(sampleReturnProcessor, ExecutionRepository.getInstance().getGlobalProcessors().get(0)); | ||
} | ||
|
||
@Test | ||
void useForLastSample() { | ||
// GIVEN | ||
final SampleDefinition sdSampler = Sampler.prepare(SampleDefinition.class); | ||
Sample.of(sdSampler.getSampleId()).is(""); | ||
final SampleReturnProcessor sampleReturnProcessor = (a, b, c) -> null; | ||
|
||
// WHEN | ||
Execution.useForLastSample(sampleReturnProcessor); | ||
|
||
// THEN | ||
assertEquals(sampleReturnProcessor, ExecutionRepository.getInstance().getSampleReturnProcessorsFor(SampleRepository.getInstance().getLastSampleDefinition()).get(0)); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
deepsampler-persistence/src/main/java/de/ppi/deepsampler/persistence/api/ComboMatcher.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,56 @@ | ||
/* | ||
* Copyright 2020 PPI AG (Hamburg, Germany) | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
|
||
package de.ppi.deepsampler.persistence.api; | ||
|
||
import de.ppi.deepsampler.core.model.ParameterMatcher; | ||
|
||
/** | ||
* Special matcher to apply a {@link ParameterMatcher} <b>and</b> a {@link PersistentMatcher} to a single argument when defining a sample. | ||
* <br> | ||
* This class will basically behave exactly like the given {@link ParameterMatcher}, but additionally it will hold a {@link PersistentMatcher} to | ||
* retrieve it later in the loading process of persistent samples. | ||
* <br> | ||
* <b>Never create {@link ComboMatcher} yourself! Always use {@link PersistentMatchers#combo(Object, PersistentMatcher)} for this.</b> | ||
* | ||
* @param <T> type to get matched in some sense | ||
* @author Rico Schrage | ||
*/ | ||
public class ComboMatcher<T> implements ParameterMatcher<T> { | ||
|
||
private final PersistentMatcher<T> persistentMatcher; | ||
private final ParameterMatcher<T> parameterMatcher; | ||
|
||
/** | ||
* Create a ComboMatcher with the parameterMatcher to be imitated and the persistentMatcher to hold for the later creating of a real matcher | ||
* in the proces of loading persistent samples. | ||
* | ||
* @param parameterMatcher the {@link ParameterMatcher} to imitate | ||
* @param persistentMatcher the {@link PersistentMatcher} to hold | ||
*/ | ||
ComboMatcher(ParameterMatcher<T> parameterMatcher, PersistentMatcher<T> persistentMatcher) { | ||
this.parameterMatcher = parameterMatcher; | ||
this.persistentMatcher = persistentMatcher; | ||
} | ||
|
||
/** | ||
* @return the hold {@link PersistentMatcher} | ||
*/ | ||
public PersistentMatcher<T> getPersistentMatcher() { | ||
return persistentMatcher; | ||
} | ||
|
||
/** | ||
* @return the imitated {@link ParameterMatcher} | ||
*/ | ||
public ParameterMatcher<T> getParameterMatcher() { | ||
return parameterMatcher; | ||
} | ||
|
||
@Override | ||
public boolean matches(T parameter) { | ||
return parameterMatcher.matches(parameter); | ||
} | ||
} |
Oops, something went wrong.