Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Adding unittests for parsing StatusNotification messages with … #14

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions server/src/main/kotlin/com/monta/ocpp/MontaApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import io.ktor.serialization.jackson.*
import io.ktor.server.application.*
import io.ktor.server.netty.*
import io.ktor.server.plugins.callloging.*
import io.ktor.server.plugins.calllogging.*

Check warning on line 25 in server/src/main/kotlin/com/monta/ocpp/MontaApplication.kt

View workflow job for this annotation

GitHub Actions / detekt

[detekt] server/src/main/kotlin/com/monta/ocpp/MontaApplication.kt#L25 <detekt.NoWildcardImports>

Wildcard import
Raw output
/github/workspace/server/src/main/kotlin/com/monta/ocpp/MontaApplication.kt:25:1: warning: Wildcard import (detekt.NoWildcardImports)

Check warning on line 25 in server/src/main/kotlin/com/monta/ocpp/MontaApplication.kt

View workflow job for this annotation

GitHub Actions / detekt

[detekt] server/src/main/kotlin/com/monta/ocpp/MontaApplication.kt#L25 <detekt.WildcardImport>

io.ktor.server.plugins.calllogging.* is a wildcard import. Replace it with fully qualified imports.
Raw output
/github/workspace/server/src/main/kotlin/com/monta/ocpp/MontaApplication.kt:25:1: warning: io.ktor.server.plugins.calllogging.* is a wildcard import. Replace it with fully qualified imports. (detekt.WildcardImport)
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.routing.*
import io.ktor.server.util.*
Expand All @@ -34,6 +34,7 @@
import java.time.ZonedDateTime
import java.util.TimeZone
import java.util.zip.Deflater
import kotlin.time.toKotlinDuration

fun main(
args: Array<String>
Expand All @@ -59,8 +60,8 @@
objectmapper = MontaSerialization.getDefaultMapper()
)

pingPeriod = Duration.ofSeconds(15)
timeout = Duration.ofSeconds(15)
pingPeriod = Duration.ofSeconds(15).toKotlinDuration()
timeout = Duration.ofSeconds(15).toKotlinDuration()

maxFrameSize = Long.MAX_VALUE
masking = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.monta.library.ocpp.common.serialization.Message
import com.monta.library.ocpp.common.serialization.MessageSerializer
import com.monta.library.ocpp.common.serialization.ParsingResult
import com.monta.library.ocpp.common.serialization.SerializationMode
import com.monta.library.ocpp.v16.core.StatusNotificationRequest
import com.monta.library.ocpp.v16.error.OcppErrorResponderV16
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
Expand All @@ -25,6 +26,34 @@ class StatusNotificationSerializationTest : StringSpec({
ocppMessage.payload shouldBe TestUtils.toJsonNode("{\"connectorId\":1,\"errorCode\":\"NoError\",\"status\":\"Available\"}")
}

"parse a valid status notification request with a timestamp" {
val jsonString = TestUtils.getFileAsString("status_notification/req-timestamp.json")
val parsingResult = messageSerializer.parse(jsonString)

parsingResult.shouldBeInstanceOf<ParsingResult.Success<Message.Request>>()
val ocppMessage = parsingResult.value
ocppMessage.shouldBeInstanceOf<Message.Request>()
val expected = messageSerializer.deserializePayload(ocppMessage, StatusNotificationRequest::class.java)
expected.shouldBeInstanceOf<ParsingResult.Success<StatusNotificationRequest>>()
ocppMessage.uniqueId shouldBe "2024120922534800000049"
ocppMessage.action shouldBe "StatusNotification"
ocppMessage.payload shouldBe TestUtils.toJsonNode("{\"connectorId\":1,\"status\":\"Available\",\"errorCode\":\"NoError\",\"info\":\"eCp_12V\",\"timestamp\":\"2024-12-09T22:53:49.000Z\"}")
}

"parse a valid status notification request with a timestamp without a timezone should fail" {
val jsonString = TestUtils.getFileAsString("status_notification/req-timestamp-no-timezone.json")
val parsingResult = messageSerializer.parse(jsonString)

parsingResult.shouldBeInstanceOf<ParsingResult.Success<Message.Request>>()
val ocppMessage = parsingResult.value
ocppMessage.shouldBeInstanceOf<Message.Request>()
val expected = messageSerializer.deserializePayload(ocppMessage, StatusNotificationRequest::class.java)
expected.shouldBeInstanceOf<ParsingResult.Failure<StatusNotificationRequest>>()
ocppMessage.uniqueId shouldBe "2024120922534800000049"
ocppMessage.action shouldBe "StatusNotification"
ocppMessage.payload shouldBe TestUtils.toJsonNode("{\"connectorId\":1,\"status\":\"Available\",\"errorCode\":\"NoError\",\"info\":\"eCp_12V\",\"timestamp\":\"2024-12-09T22:53:49.000\"}")
}

"parse a valid status notification response" {
val jsonString = TestUtils.getFileAsString("status_notification/res.json")
val parsingResult = messageSerializer.parse(jsonString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PlugAndChargeSerializationTest : StringSpec({
val json = this.javaClass.getResourceAsStream(fileName)?.bufferedReader().use { it?.readText() }!!
val result = messageSerializer.parse(json)
result should beInstanceOf<ParsingResult.Success<DataTransferRequest>>()
val resp = (result as ParsingResult.Success<Message.Request>).value
val resp = (result as ParsingResult.Success<*>).value as Message.Request
val parseResult = messageSerializer.deserializePayload(resp, DataTransferRequest::class.java)
when (parseResult) {
is ParsingResult.Success<DataTransferRequest> -> {
Expand All @@ -40,7 +40,7 @@ class PlugAndChargeSerializationTest : StringSpec({
val json = this.javaClass.getResourceAsStream(fileName)?.bufferedReader().use { it?.readText() }!!
val result = messageSerializer.parse(json)
result should beInstanceOf<ParsingResult.Success<DataTransferConfirmation>>()
val resp = (result as ParsingResult.Success<Message.Response>).value
val resp = (result as ParsingResult.Success<*>).value as Message.Response
val parseResult = messageSerializer.deserializePayload(resp, DataTransferConfirmation::class.java)
when (parseResult) {
is ParsingResult.Success<DataTransferConfirmation> -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[2, "2024120922534800000049", "StatusNotification", {
"connectorId": 1,
"status": "Available",
"errorCode": "NoError",
"info": "eCp_12V",
"timestamp": "2024-12-09T22:53:49.000"
}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[2, "2024120922534800000049", "StatusNotification", {
"connectorId": 1,
"status": "Available",
"errorCode": "NoError",
"info": "eCp_12V",
"timestamp": "2024-12-09T22:53:49.000Z"
}]
Loading