From 0ce7c80b93603d51435cb22a4edc635b404ecf43 Mon Sep 17 00:00:00 2001 From: mmauksch Date: Wed, 16 Aug 2023 18:12:13 +0200 Subject: [PATCH] add stricter validation for Metervalues as described in #241 --- .../ocpp/model/core/MeterValue.java | 2 +- .../ocpp/model/core/MeterValuesRequest.java | 2 +- .../model/core/MeterValuesRequestTest.java | 32 +++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 ocpp-v1_6/src/test/java/eu/chargetime/ocpp/model/core/MeterValuesRequestTest.java diff --git a/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/core/MeterValue.java b/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/core/MeterValue.java index d8e59f5ca..2fc671123 100644 --- a/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/core/MeterValue.java +++ b/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/core/MeterValue.java @@ -67,7 +67,7 @@ public MeterValue(ZonedDateTime timestamp, SampledValue[] sampledValue) { @Override public boolean validate() { - boolean valid = timestamp != null && sampledValue != null; + boolean valid = timestamp != null && sampledValue != null && sampledValue.length > 0; if (valid) { for (SampledValue value : sampledValue) { diff --git a/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/core/MeterValuesRequest.java b/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/core/MeterValuesRequest.java index c28e5da2c..fd4a35d4f 100644 --- a/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/core/MeterValuesRequest.java +++ b/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/core/MeterValuesRequest.java @@ -64,7 +64,7 @@ public MeterValuesRequest(Integer connectorId) { @Override public boolean validate() { - boolean valid = this.connectorId != null && this.connectorId >= 0 && this.meterValue != null; + boolean valid = this.connectorId != null && this.connectorId >= 0 && this.meterValue != null && meterValue.length > 0; if (valid) { for (MeterValue current : this.meterValue) { diff --git a/ocpp-v1_6/src/test/java/eu/chargetime/ocpp/model/core/MeterValuesRequestTest.java b/ocpp-v1_6/src/test/java/eu/chargetime/ocpp/model/core/MeterValuesRequestTest.java new file mode 100644 index 000000000..cc3d341cb --- /dev/null +++ b/ocpp-v1_6/src/test/java/eu/chargetime/ocpp/model/core/MeterValuesRequestTest.java @@ -0,0 +1,32 @@ +package eu.chargetime.ocpp.model.core; + +import junit.framework.TestCase; + +import java.time.ZonedDateTime; + +public class MeterValuesRequestTest extends TestCase { + + public void testEmptyMeterValuesArray_FailsValidation() { + MeterValuesRequest request = new MeterValuesRequest(1); + request.setTransactionId(2); + request.setMeterValue(new MeterValue[]{}); + assertFalse(request.validate()); + } + + public void testEmptySampledValuesArray_failsValidation() { + MeterValuesRequest request = new MeterValuesRequest(1); + MeterValue measured = new MeterValue(ZonedDateTime.now(), new SampledValue[]{}); + request.setTransactionId(2); + request.setMeterValue(new MeterValue[]{measured}); + assertFalse(request.validate()); + } + + public void testMeterValuesWithAtLeastOneMeasurementPassesValidation() { + MeterValuesRequest request = new MeterValuesRequest(1); + SampledValue sample = new SampledValue("5"); + MeterValue measured = new MeterValue(ZonedDateTime.now(), new SampledValue[]{sample}); + request.setTransactionId(2); + request.setMeterValue(new MeterValue[]{measured}); + assertTrue(request.validate()); + } +} \ No newline at end of file