diff --git a/build.gradle.kts b/build.gradle.kts index d7249fe..4ea4b69 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "com.github.doip-sim-ecu" -version = "0.5.0" +version = "0.5.1" repositories { mavenCentral() diff --git a/src/main/kotlin/SimDsl.kt b/src/main/kotlin/SimDsl.kt index a67c424..78e2a72 100644 --- a/src/main/kotlin/SimDsl.kt +++ b/src/main/kotlin/SimDsl.kt @@ -305,10 +305,15 @@ open class RequestsData( * The loglevel used to log when the request matches and its responses */ loglevel: LogLevel = LogLevel.DEBUG, + /** + * Insert at top + */ + insertAtTop: Boolean = false, /** * Handler that is called when the request is matched */ - response: RequestResponseHandler = {}): RequestMatcher { + response: RequestResponseHandler = {} + ): RequestMatcher { val req = RequestMatcher( name = name, requestBytes = request, @@ -316,7 +321,11 @@ open class RequestsData( loglevel = loglevel, responseHandler = response ) - requests.add(req) + if (insertAtTop) { + requests.add(0, req) + } else { + requests.add(req) + } return req } @@ -344,10 +353,15 @@ open class RequestsData( * The loglevel used to log when the request matches and its responses */ loglevel: LogLevel = LogLevel.DEBUG, + /** + * Insert at top + */ + insertAtTop: Boolean = false, /** * Handler that is called when the request is matched */ - response: RequestResponseHandler = {}): RequestMatcher { + response: RequestResponseHandler = {} + ): RequestMatcher { val req = RequestMatcher( name = name, requestBytes = null, @@ -355,7 +369,11 @@ open class RequestsData( loglevel = loglevel, responseHandler = response ) - requests.add(req) + if (insertAtTop) { + requests.add(0, req) + } else { + requests.add(req) + } return req } @@ -383,14 +401,19 @@ open class RequestsData( * The loglevel used to log when the request matches and its responses */ loglevel: LogLevel = LogLevel.DEBUG, + /** + * Insert at top + */ + insertAtTop: Boolean = false, /** * Handler that is called when the request is matched */ - response: RequestResponseHandler = {}) { + response: RequestResponseHandler = {} + ) { if (isRegex(reqHex)) { - request(regexifyRequestHex(reqHex), name, loglevel, response) + request(regexifyRequestHex(reqHex), name, loglevel, insertAtTop, response) } else { - request(reqHex.decodeHex(), name, loglevel, response) + request(reqHex.decodeHex(), name, loglevel, insertAtTop, response) } } diff --git a/src/test/kotlin/SimDslTest.kt b/src/test/kotlin/SimDslTest.kt index 811ad87..d8d22af 100644 --- a/src/test/kotlin/SimDslTest.kt +++ b/src/test/kotlin/SimDslTest.kt @@ -53,6 +53,17 @@ class SimDslTest { assertThat(gatewayInstances.size).isEqualTo(0) } + @Test + fun `test dsl insertAtTop`() { + gateway("GW") { + request(byteArrayOf(0x10), "REQ1") { respond(byteArrayOf(0x50)) } + request("10", "REQ2") { respond("50") } + request("10 []", "REQ3", insertAtTop = true) { ack() } + } + assertThat(gateways.size).isEqualTo(1) + assertThat(gateways[0].requests[0].requestRegex?.pattern).isEqualTo("10.*") + } + @Test fun `test multibyte ack`() { gateway("GW") {