Skip to content

Commit 54d6d88

Browse files
committed
feat: adds system expression evaluator to config evaluator list.
1 parent e4cdb2a commit 54d6d88

File tree

12 files changed

+45
-10
lines changed

12 files changed

+45
-10
lines changed

cmd/src/main/java/io/gatehill/imposter/cmd/ImposterLauncher.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@
4242
*/
4343
package io.gatehill.imposter.cmd
4444

45+
import io.gatehill.imposter.config.ConfigHolder
4546
import io.gatehill.imposter.config.util.ConfigUtil
4647
import io.gatehill.imposter.config.util.MetaUtil.readVersion
4748
import io.gatehill.imposter.plugin.DynamicPluginDiscoveryStrategyImpl
4849
import io.gatehill.imposter.plugin.internal.MetaInfPluginDetectorImpl
49-
import io.gatehill.imposter.server.ConfigHolder
5050
import io.gatehill.imposter.server.vertxweb.VertxWebServerFactoryImpl
5151
import io.gatehill.imposter.util.CryptoUtil.DEFAULT_KEYSTORE_PASSWORD
5252
import io.gatehill.imposter.util.CryptoUtil.DEFAULT_KEYSTORE_PATH

server/src/main/java/io/gatehill/imposter/server/ConfigHolder.kt core/config/src/main/java/io/gatehill/imposter/config/ConfigHolder.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* You should have received a copy of the GNU Lesser General Public License
4141
* along with Imposter. If not, see <https://www.gnu.org/licenses/>.
4242
*/
43-
package io.gatehill.imposter.server
43+
package io.gatehill.imposter.config
4444

4545
import io.gatehill.imposter.ImposterConfig
4646

core/engine/src/main/java/io/gatehill/imposter/placeholder/SystemEvaluator.kt core/config/src/main/java/io/gatehill/imposter/config/expression/SystemEvaluator.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@
4141
* along with Imposter. If not, see <https://www.gnu.org/licenses/>.
4242
*/
4343

44-
package io.gatehill.imposter.placeholder
44+
package io.gatehill.imposter.config.expression
4545

4646
import io.gatehill.imposter.ImposterConfig
47+
import io.gatehill.imposter.config.ConfigHolder
4748
import io.gatehill.imposter.expression.eval.ExpressionEvaluator
48-
import io.gatehill.imposter.util.InjectorUtil
4949
import org.apache.logging.log4j.LogManager
5050

5151
/**
@@ -55,7 +55,7 @@ import org.apache.logging.log4j.LogManager
5555
* ```
5656
*/
5757
object SystemEvaluator : ExpressionEvaluator<String> {
58-
private val impl by lazy { SystemEvaluatorImpl(InjectorUtil.getInstance()) }
58+
private val impl by lazy { SystemEvaluatorImpl(ConfigHolder.config) }
5959

6060
override val name: String
6161
get() = impl.name

core/config/src/main/java/io/gatehill/imposter/config/util/ConfigUtil.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import io.gatehill.imposter.ImposterConfig
4848
import io.gatehill.imposter.config.ConfigReference
4949
import io.gatehill.imposter.config.LoadedConfig
5050
import io.gatehill.imposter.config.expression.EnvEvaluator
51+
import io.gatehill.imposter.config.expression.SystemEvaluator
5152
import io.gatehill.imposter.config.model.LightweightConfig
5253
import io.gatehill.imposter.config.resolver.ConfigResolver
5354
import io.gatehill.imposter.expression.eval.ExpressionEvaluator
@@ -136,7 +137,10 @@ object ConfigUtil {
136137

137138
fun initInterpolators(environment: Map<String, String>) {
138139
// reset the environment used by the expression evaluator
139-
expressionEvaluators = mapOf("env" to EnvEvaluator(environment))
140+
expressionEvaluators = mapOf(
141+
"env" to EnvEvaluator(environment),
142+
"system" to SystemEvaluator,
143+
)
140144
}
141145

142146
/**

core/config/src/test/java/io/gatehill/imposter/config/ConfigUtilTest.kt

+13
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ import io.gatehill.imposter.config.util.ConfigUtil
4848
import io.gatehill.imposter.config.util.EnvVars
4949
import io.gatehill.imposter.http.HttpMethod
5050
import org.hamcrest.MatcherAssert.assertThat
51+
import org.hamcrest.Matchers.containsString
5152
import org.hamcrest.Matchers.equalTo
5253
import org.hamcrest.Matchers.not
5354
import org.hamcrest.Matchers.nullValue
5455
import org.hamcrest.Matchers.startsWith
56+
import org.junit.AfterClass
5557
import org.junit.Assert.assertEquals
5658
import org.junit.Assert.assertNotNull
5759
import org.junit.Assert.assertTrue
@@ -64,8 +66,18 @@ import java.io.File
6466
* @author Pete Cornish
6567
*/
6668
class ConfigUtilTest {
69+
companion object {
70+
@AfterClass
71+
@JvmStatic
72+
fun afterClass() {
73+
ConfigHolder.config.listenPort = 0
74+
}
75+
}
76+
6777
@Test
6878
fun testReadInterpolatedPluginConfig() {
79+
ConfigHolder.config.listenPort = 9090
80+
6981
// override environment variables in string interpolators
7082
val environment: Map<String, String> = mapOf(
7183
"EXAMPLE_PLUGIN" to "example-plugin"
@@ -79,6 +91,7 @@ class ConfigUtilTest {
7991
)
8092
val loadedConfig = ConfigUtil.readPluginConfig(configRef)
8193
assertEquals("example-plugin", loadedConfig.plugin)
94+
assertThat(loadedConfig.serialised, containsString("port 9090"))
8295
}
8396

8497
/**

core/engine/src/test/java/io/gatehill/imposter/placeholder/SystemEvaluatorTest.kt core/config/src/test/java/io/gatehill/imposter/config/expression/SystemEvaluatorTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.gatehill.imposter.placeholder
1+
package io.gatehill.imposter.config.expression
22

33
import io.gatehill.imposter.ImposterConfig
44
import org.hamcrest.MatcherAssert.assertThat

core/config/src/test/resources/interpolated/test-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ plugin: "${env.EXAMPLE_PLUGIN}"
44
path: "/test"
55
contentType: text/plain
66
response:
7-
content: Hello, world!
7+
content: Hello, world, listening on port 9090!

core/engine/src/main/java/io/gatehill/imposter/util/PlaceholderUtil.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
*/
4343
package io.gatehill.imposter.util
4444

45+
import io.gatehill.imposter.config.expression.SystemEvaluator
4546
import io.gatehill.imposter.expression.eval.ExpressionEvaluator
4647
import io.gatehill.imposter.expression.eval.RandomEvaluator
4748
import io.gatehill.imposter.expression.util.ExpressionUtil
@@ -50,7 +51,6 @@ import io.gatehill.imposter.placeholder.ContextEvaluator
5051
import io.gatehill.imposter.placeholder.DateTimeEvaluator
5152
import io.gatehill.imposter.placeholder.HttpExpressionEvaluator
5253
import io.gatehill.imposter.placeholder.QueryProviderImpl
53-
import io.gatehill.imposter.placeholder.SystemEvaluator
5454

5555
/**
5656
* Replaces expression placeholders during the lifecycle of a request/response exchange.

docs/templates.md

+16
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,22 @@ Valid random functions:
257257

258258
> Note: the syntax for `random` is slightly different to the other placeholder types. These are functions, not properties, so are called with parentheses.
259259

260+
#### Customise length
261+
262+
Customise the length of the generated value by passing `length=NUM` argument into the function.
263+
264+
For example: `${random.alphabetic(length=5)}`
265+
266+
This applies to the following functions:
267+
268+
- alphabetic
269+
- alphanumeric
270+
- numeric
271+
272+
#### Uppercase
273+
274+
Convert the generated value to uppercase by passing `uppercase=true` argument
275+
260276
### Items in a Store
261277

262278
You can use items from a [Store](./stores.md), including data [captured](./data_capture.md) previously, or set by a script.

embedded/core/src/main/java/io/gatehill/imposter/embedded/ImposterBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
package io.gatehill.imposter.embedded;
4545

4646
import io.gatehill.imposter.ImposterConfig;
47+
import io.gatehill.imposter.config.ConfigHolder;
4748
import io.gatehill.imposter.plugin.Plugin;
4849
import io.gatehill.imposter.script.listener.ScriptListener;
49-
import io.gatehill.imposter.server.ConfigHolder;
5050
import io.gatehill.imposter.server.ImposterVerticle;
5151
import io.gatehill.imposter.service.script.EmbeddedScriptService;
5252
import io.gatehill.imposter.util.FeatureUtil;

server/src/main/java/io/gatehill/imposter/server/ImposterVerticle.kt

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ package io.gatehill.imposter.server
4545
import com.google.inject.Module
4646
import io.gatehill.imposter.EngineBuilder
4747
import io.gatehill.imposter.Imposter
48+
import io.gatehill.imposter.config.ConfigHolder
4849
import io.gatehill.imposter.scripting.common.CommonScriptingModule
4950
import io.gatehill.imposter.scripting.groovy.GroovyScriptingModule
5051
import io.gatehill.imposter.store.StoreModule

test/test-utils/src/main/java/io/gatehill/imposter/server/BaseVerticleTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
package io.gatehill.imposter.server
4444

4545
import io.gatehill.imposter.ImposterConfig
46+
import io.gatehill.imposter.config.ConfigHolder
4647
import io.gatehill.imposter.config.util.EnvVars
4748
import io.gatehill.imposter.plugin.DynamicPluginDiscoveryStrategyImpl
4849
import io.gatehill.imposter.plugin.Plugin

0 commit comments

Comments
 (0)