Skip to content

Commit

Permalink
add insertAtTop parameter for requests, to put often called services …
Browse files Browse the repository at this point in the history
…(like transferdata) at the top
  • Loading branch information
froks committed Feb 14, 2022
1 parent b939ee5 commit 15a89e6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.github.doip-sim-ecu"
version = "0.5.0"
version = "0.5.1"

repositories {
mavenCentral()
Expand Down
37 changes: 30 additions & 7 deletions src/main/kotlin/SimDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -305,18 +305,27 @@ 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,
requestRegex = null,
loglevel = loglevel,
responseHandler = response
)
requests.add(req)
if (insertAtTop) {
requests.add(0, req)
} else {
requests.add(req)
}
return req
}

Expand Down Expand Up @@ -344,18 +353,27 @@ 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,
requestRegex = reqRegex,
loglevel = loglevel,
responseHandler = response
)
requests.add(req)
if (insertAtTop) {
requests.add(0, req)
} else {
requests.add(req)
}
return req
}

Expand Down Expand Up @@ -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)
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/kotlin/SimDslTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down

0 comments on commit 15a89e6

Please sign in to comment.