-
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.
feat(test): ability to evaluate function locator fragments as groovy …
…scripts Closes: #64
- Loading branch information
1 parent
fb5ea75
commit 16422f4
Showing
4 changed files
with
115 additions
and
1 deletion.
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
92 changes: 92 additions & 0 deletions
92
src/main/java/ru/ewc/checklogic/testing/FunctionsLocator.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,92 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2024 Decision-Driven Development | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package ru.ewc.checklogic.testing; | ||
|
||
import groovy.lang.Binding; | ||
import groovy.util.GroovyScriptEngine; | ||
import java.net.URL; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
import lombok.SneakyThrows; | ||
import ru.ewc.decisions.api.ComputationContext; | ||
import ru.ewc.decisions.api.DecitaException; | ||
import ru.ewc.decisions.api.InMemoryLocator; | ||
import ru.ewc.decisions.api.Locator; | ||
|
||
/** | ||
* I am a special locator that can load functions from Groovy scripts. My main responsibility is to | ||
* run the Groovy script and return the result of the script execution. | ||
* | ||
* @since 0.4.1 | ||
*/ | ||
public final class FunctionsLocator implements Locator { | ||
/** | ||
* The locator's name. | ||
*/ | ||
private final String name; | ||
|
||
/** | ||
* The in-memory locator used to store overridden values. | ||
*/ | ||
private final InMemoryLocator locator; | ||
|
||
/** | ||
* Groovy script engine used to run the Groovy scripts. | ||
*/ | ||
private final GroovyScriptEngine engine; | ||
|
||
@SneakyThrows | ||
public FunctionsLocator(final String name, final Path path) { | ||
this.name = name; | ||
this.locator = new InMemoryLocator("locator", new HashMap<>()); | ||
this.engine = new GroovyScriptEngine(new URL[]{path.toUri().toURL()}); | ||
} | ||
|
||
@SneakyThrows | ||
@Override | ||
public String fragmentBy( | ||
final String fragment, | ||
final ComputationContext context | ||
) throws DecitaException { | ||
final String result; | ||
if (this.locator.state().containsKey(fragment)) { | ||
result = this.locator.fragmentBy(fragment, context); | ||
} else { | ||
final Binding binding = new Binding(); | ||
binding.setVariable("context", context); | ||
result = this.engine.run("%s.groovy".formatted(fragment), binding).toString(); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public void setFragmentValue(final String fragment, final String value) { | ||
this.locator.setFragmentValue(fragment, value); | ||
} | ||
|
||
@Override | ||
public String locatorName() { | ||
return this.name; | ||
} | ||
} |