Skip to content

Commit

Permalink
Merge pull request #62 from IZIVIA/feat/add-response-classes-to-actio…
Browse files Browse the repository at this point in the history
…ns-and-new-exception-to-parser

fix: add a more meaningful exception in parseAnyFromString and add classResponse to the Actions enum
  • Loading branch information
jggrimal authored Mar 13, 2024
2 parents 9797f76 + b8df08d commit f504d1f
Show file tree
Hide file tree
Showing 12 changed files with 794 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -1,73 +1,182 @@
package com.izivia.ocpp.core15.model.common.enumeration

import com.izivia.ocpp.core15.model.authorize.AuthorizeReq
import com.izivia.ocpp.core15.model.authorize.AuthorizeResp
import com.izivia.ocpp.core15.model.bootnotification.BootNotificationReq
import com.izivia.ocpp.core15.model.bootnotification.BootNotificationResp
import com.izivia.ocpp.core15.model.cancelreservation.CancelReservationReq
import com.izivia.ocpp.core15.model.cancelreservation.CancelReservationResp
import com.izivia.ocpp.core15.model.changeavailability.ChangeAvailabilityReq
import com.izivia.ocpp.core15.model.changeavailability.ChangeAvailabilityResp
import com.izivia.ocpp.core15.model.changeconfiguration.ChangeConfigurationReq
import com.izivia.ocpp.core15.model.changeconfiguration.ChangeConfigurationResp
import com.izivia.ocpp.core15.model.clearcache.ClearCacheReq
import com.izivia.ocpp.core15.model.clearcache.ClearCacheResp
import com.izivia.ocpp.core15.model.datatransfer.DataTransferReq
import com.izivia.ocpp.core15.model.datatransfer.DataTransferResp
import com.izivia.ocpp.core15.model.diagnosticsstatusnotification.DiagnosticsStatusNotificationReq
import com.izivia.ocpp.core15.model.diagnosticsstatusnotification.DiagnosticsStatusNotificationResp
import com.izivia.ocpp.core15.model.firmwarestatusnotification.FirmwareStatusNotificationReq
import com.izivia.ocpp.core15.model.firmwarestatusnotification.FirmwareStatusNotificationResp
import com.izivia.ocpp.core15.model.getconfiguration.GetConfigurationReq
import com.izivia.ocpp.core15.model.getconfiguration.GetConfigurationResp
import com.izivia.ocpp.core15.model.getdiagnostics.GetDiagnosticsReq
import com.izivia.ocpp.core15.model.getdiagnostics.GetDiagnosticsResp
import com.izivia.ocpp.core15.model.getlocallistversion.GetLocalListVersionReq
import com.izivia.ocpp.core15.model.getlocallistversion.GetLocalListVersionResp
import com.izivia.ocpp.core15.model.heartbeat.HeartbeatReq
import com.izivia.ocpp.core15.model.heartbeat.HeartbeatResp
import com.izivia.ocpp.core15.model.metervalues.MeterValuesReq
import com.izivia.ocpp.core15.model.metervalues.MeterValuesResp
import com.izivia.ocpp.core15.model.remotestart.RemoteStartTransactionReq
import com.izivia.ocpp.core15.model.remotestart.RemoteStartTransactionResp
import com.izivia.ocpp.core15.model.remotestop.RemoteStopTransactionReq
import com.izivia.ocpp.core15.model.remotestop.RemoteStopTransactionResp
import com.izivia.ocpp.core15.model.reservenow.ReserveNowReq
import com.izivia.ocpp.core15.model.reservenow.ReserveNowResp
import com.izivia.ocpp.core15.model.reset.ResetReq
import com.izivia.ocpp.core15.model.reset.ResetResp
import com.izivia.ocpp.core15.model.sendlocallist.SendLocalListReq
import com.izivia.ocpp.core15.model.sendlocallist.SendLocalListResp
import com.izivia.ocpp.core15.model.starttransaction.StartTransactionReq
import com.izivia.ocpp.core15.model.starttransaction.StartTransactionResp
import com.izivia.ocpp.core15.model.statusnotification.StatusNotificationReq
import com.izivia.ocpp.core15.model.statusnotification.StatusNotificationResp
import com.izivia.ocpp.core15.model.stoptransaction.StopTransactionReq
import com.izivia.ocpp.core15.model.stoptransaction.StopTransactionResp
import com.izivia.ocpp.core15.model.unlockconnector.UnlockConnectorReq
import com.izivia.ocpp.core15.model.unlockconnector.UnlockConnectorResp
import com.izivia.ocpp.core15.model.updatefirmware.UpdateFirmwareReq
import com.izivia.ocpp.core15.model.updatefirmware.UpdateFirmwareResp
import com.izivia.ocpp.utils.IActions
import com.izivia.ocpp.utils.OcppInitiator

enum class Actions(
override val value: String, override val classRequest: Class<*>,
override val value: String,
override val classRequest: Class<*>,
override val classResponse: Class<*>,
override val initiatedBy: OcppInitiator
) : IActions {
AUTHORIZE("authorize", AuthorizeReq::class.java, OcppInitiator.CHARGING_STATION),
BOOTNOTIFICATION("bootNotification", BootNotificationReq::class.java, OcppInitiator.CHARGING_STATION),
CANCELRESERVATION("cancelReservation", CancelReservationReq::class.java, OcppInitiator.CENTRAL_SYSTEM),
CHANGEAVAILABILITY("changeAvailability", ChangeAvailabilityReq::class.java, OcppInitiator.CENTRAL_SYSTEM),
CHANGECONFIGURATION("changeConfiguration", ChangeConfigurationReq::class.java, OcppInitiator.CENTRAL_SYSTEM),
CLEARCACHE("clearCache", ClearCacheReq::class.java, OcppInitiator.CENTRAL_SYSTEM),
DATATRANSFER("dataTransfer", DataTransferReq::class.java, OcppInitiator.ALL),
AUTHORIZE("authorize", AuthorizeReq::class.java, AuthorizeResp::class.java, OcppInitiator.CHARGING_STATION),
BOOTNOTIFICATION(
"bootNotification",
BootNotificationReq::class.java,
BootNotificationResp::class.java,
OcppInitiator.CHARGING_STATION
),
CANCELRESERVATION(
"cancelReservation",
CancelReservationReq::class.java,
CancelReservationResp::class.java,
OcppInitiator.CENTRAL_SYSTEM
),
CHANGEAVAILABILITY(
"changeAvailability",
ChangeAvailabilityReq::class.java,
ChangeAvailabilityResp::class.java,
OcppInitiator.CENTRAL_SYSTEM
),
CHANGECONFIGURATION(
"changeConfiguration",
ChangeConfigurationReq::class.java,
ChangeConfigurationResp::class.java,
OcppInitiator.CENTRAL_SYSTEM
),
CLEARCACHE("clearCache", ClearCacheReq::class.java, ClearCacheResp::class.java, OcppInitiator.CENTRAL_SYSTEM),
DATATRANSFER("dataTransfer", DataTransferReq::class.java, DataTransferResp::class.java, OcppInitiator.ALL),
DIAGNOSTICSSTATUSNOTIFICATION(
"diagnosticsStatusNotification",
DiagnosticsStatusNotificationReq::class.java,
DiagnosticsStatusNotificationResp::class.java,
OcppInitiator.CHARGING_STATION
),
FIRMWARESTATUSNOTIFICATION(
"firmwareStatusNotification",
FirmwareStatusNotificationReq::class.java,
FirmwareStatusNotificationResp::class.java,
OcppInitiator.CHARGING_STATION
),
GETCONFIGURATION(
"getConfiguration",
GetConfigurationReq::class.java,
GetConfigurationResp::class.java,
OcppInitiator.CENTRAL_SYSTEM
),
GETDIAGNOSTICS(
"getDiagnostics",
GetDiagnosticsReq::class.java,
GetDiagnosticsResp::class.java,
OcppInitiator.CENTRAL_SYSTEM
),
GETLOCALLISTVERSION(
"getLocalListVersion",
GetLocalListVersionReq::class.java,
GetLocalListVersionResp::class.java,
OcppInitiator.CENTRAL_SYSTEM
),
HEARTBEAT(
"heartbeat",
HeartbeatReq::class.java,
HeartbeatResp::class.java,
OcppInitiator.CHARGING_STATION
),
METERVALUES(
"meterValues",
MeterValuesReq::class.java,
MeterValuesResp::class.java,
OcppInitiator.CHARGING_STATION
),
GETCONFIGURATION("getConfiguration", GetConfigurationReq::class.java, OcppInitiator.CENTRAL_SYSTEM),
GETDIAGNOSTICS("getDiagnostics", GetDiagnosticsReq::class.java, OcppInitiator.CENTRAL_SYSTEM),
GETLOCALLISTVERSION("getLocalListVersion", GetLocalListVersionReq::class.java, OcppInitiator.CENTRAL_SYSTEM),
HEARTBEAT("heartbeat", HeartbeatReq::class.java, OcppInitiator.CHARGING_STATION),
METERVALUES("meterValues", MeterValuesReq::class.java, OcppInitiator.CHARGING_STATION),
REMOTESTARTTRANSACTION(
"remoteStartTransaction",
RemoteStartTransactionReq::class.java,
RemoteStartTransactionResp::class.java,
OcppInitiator.CENTRAL_SYSTEM
),
REMOTESTOPTRANSACTION(
"remoteStopTransaction",
RemoteStopTransactionReq::class.java,
RemoteStopTransactionResp::class.java,
OcppInitiator.CENTRAL_SYSTEM
),
RESERVENOW("reserveNow", ReserveNowReq::class.java, ReserveNowResp::class.java, OcppInitiator.CENTRAL_SYSTEM),
RESET("reset", ResetReq::class.java, ResetResp::class.java, OcppInitiator.CENTRAL_SYSTEM),
SENDLOCALLIST(
"sendLocalList",
SendLocalListReq::class.java,
SendLocalListResp::class.java,
OcppInitiator.CENTRAL_SYSTEM
),
STARTTRANSACTION(
"startTransaction",
StartTransactionReq::class.java,
StartTransactionResp::class.java,
OcppInitiator.CHARGING_STATION
),
STATUSNOTIFICATION(
"statusNotification",
StatusNotificationReq::class.java,
StatusNotificationResp::class.java,
OcppInitiator.CHARGING_STATION
),
STOPTRANSACTION(
"stopTransaction",
StopTransactionReq::class.java,
StopTransactionResp::class.java,
OcppInitiator.CHARGING_STATION
),
UNLOCKCONNECTOR(
"unlockConnector",
UnlockConnectorReq::class.java,
UnlockConnectorResp::class.java,
OcppInitiator.CENTRAL_SYSTEM
),
REMOTESTOPTRANSACTION("remoteStopTransaction", RemoteStopTransactionReq::class.java,
OcppInitiator.CENTRAL_SYSTEM),
RESERVENOW("reserveNow", ReserveNowReq::class.java, OcppInitiator.CENTRAL_SYSTEM),
RESET("reset", ResetReq::class.java, OcppInitiator.CENTRAL_SYSTEM),
SENDLOCALLIST("sendLocalList", SendLocalListReq::class.java, OcppInitiator.CENTRAL_SYSTEM),
STARTTRANSACTION("startTransaction", StartTransactionReq::class.java, OcppInitiator.CHARGING_STATION),
STATUSNOTIFICATION("statusNotification", StatusNotificationReq::class.java, OcppInitiator.CHARGING_STATION),
STOPTRANSACTION("stopTransaction", StopTransactionReq::class.java, OcppInitiator.CHARGING_STATION),
UNLOCKCONNECTOR("unlockConnector", UnlockConnectorReq::class.java, OcppInitiator.CENTRAL_SYSTEM),
UPDATEFIRMWARE("updateFirmware", UpdateFirmwareReq::class.java, OcppInitiator.CENTRAL_SYSTEM);
UPDATEFIRMWARE(
"updateFirmware",
UpdateFirmwareReq::class.java,
UpdateFirmwareResp::class.java,
OcppInitiator.CENTRAL_SYSTEM
);


fun lowercase() = value.lowercase()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class Ocpp15JsonParser(
throw errorHandler(e)
}

override fun getResponsePayloadClass(action: String, errorHandler: (e: Exception) -> Throwable): Class<out Any> =
try {
Actions.valueOf(action.uppercase()).classResponse
} catch (e: Exception) {
throw errorHandler(e)
}

override fun getActionFromClass(className: String): String =
Actions.valueOf(getActionFromClassName(className)).value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,36 @@ class Ocpp15JsonParserErrorTest {
// expectThat(percent).isGreaterThan(0.0)
}

@Test
fun `should parse to Fault missing usedClazz`() {
val response = """[3,"messageId",{}]"""

val res = parser.parseAnyFromString(response)
expectThat(res)
.and {
get { action }.isEqualTo("Fault")
get { errorCode }.isEqualTo(MessageErrorCode.NOT_IMPLEMENTED)
get { payload }.isA<Fault>()
.and {
get { errorCode }.isEqualTo(MessageErrorCode.NOT_IMPLEMENTED.errorCode)
get { errorDescription }.isEqualTo(MessageErrorCode.NOT_IMPLEMENTED.description)
get { errorDetails }.hasSize(2)
.and {
get { get(0) }
.and {
get { code }.isEqualTo(ErrorDetailCode.ACTION.value)
get { detail }.contains("Cannot parse message, class used to retrieve the response action is not defined")
}
get { get(1) }
.and {
get { code }.isEqualTo(ErrorDetailCode.PAYLOAD.value)
get { detail }.isEqualTo(response)
}
}
}
}
}

@OptIn(ExperimentalTime::class)
fun timeit(requestList: List<String>, parser: Ocpp15JsonParser): Duration {
return measureTime {
Expand Down
Loading

0 comments on commit f504d1f

Please sign in to comment.