Skip to content

Commit

Permalink
Merge pull request #242 from mmauksch/validateMeterValues
Browse files Browse the repository at this point in the history
add stricter validation for Metervalues as described in  #241
  • Loading branch information
TVolden authored Mar 4, 2024
2 parents 2348f9f + 0ce7c80 commit caf3bb6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit caf3bb6

Please sign in to comment.