From db4be9d9d2108a9d6a8e3b133cb2b25fcf78a2da Mon Sep 17 00:00:00 2001 From: John Michael Luy Date: Sat, 29 Nov 2025 23:19:41 +0800 Subject: [PATCH] Allow 0 for retries and retryInterval for both UpdateFirmware and GetDiagnostics --- .../model/firmware/GetDiagnosticsRequest.java | 9 +++ .../model/firmware/UpdateFirmwareRequest.java | 8 +-- .../test/GetDiagnosticsRequestTest.java | 58 +++++++++++++++++++ .../test/UpdateFirmwareRequestTest.java | 40 ++++++------- 4 files changed, 90 insertions(+), 25 deletions(-) diff --git a/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/firmware/GetDiagnosticsRequest.java b/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/firmware/GetDiagnosticsRequest.java index 75c98993f..d1c8e67e7 100644 --- a/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/firmware/GetDiagnosticsRequest.java +++ b/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/firmware/GetDiagnosticsRequest.java @@ -26,6 +26,7 @@ of this software and associated documentation files (the "Software"), to deal SOFTWARE. */ +import eu.chargetime.ocpp.PropertyConstraintException; import eu.chargetime.ocpp.model.RequestWithId; import eu.chargetime.ocpp.utilities.MoreObjects; import java.time.ZonedDateTime; @@ -107,6 +108,10 @@ public Integer getRetries() { */ @XmlElement public void setRetries(Integer retries) { + if (retries < 0) { + throw new PropertyConstraintException(retries, "retries must be >= 0"); + } + this.retries = retries; } @@ -128,6 +133,10 @@ public Integer getRetryInterval() { */ @XmlElement public void setRetryInterval(Integer retryInterval) { + if (retryInterval < 0) { + throw new PropertyConstraintException(retryInterval, "retryInterval must be >= 0"); + } + this.retryInterval = retryInterval; } diff --git a/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/firmware/UpdateFirmwareRequest.java b/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/firmware/UpdateFirmwareRequest.java index 6191b3d17..f00e599d4 100644 --- a/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/firmware/UpdateFirmwareRequest.java +++ b/ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/firmware/UpdateFirmwareRequest.java @@ -112,8 +112,8 @@ public Integer getRetries() { */ @XmlElement public void setRetries(int retries) { - if (retries <= 0) { - throw new PropertyConstraintException(retries, "retries must be > 0"); + if (retries < 0) { + throw new PropertyConstraintException(retries, "retries must be >= 0"); } this.retries = retries; @@ -157,8 +157,8 @@ public Integer getRetryInterval() { */ @XmlElement public void setRetryInterval(int retryInterval) { - if (retryInterval <= 0) { - throw new PropertyConstraintException(retryInterval, "retryInterval must be > 0"); + if (retryInterval < 0) { + throw new PropertyConstraintException(retryInterval, "retryInterval must be >= 0"); } this.retryInterval = retryInterval; diff --git a/ocpp-v1_6/src/test/java/eu/chargetime/ocpp/model/firmware/test/GetDiagnosticsRequestTest.java b/ocpp-v1_6/src/test/java/eu/chargetime/ocpp/model/firmware/test/GetDiagnosticsRequestTest.java index 37a028f8c..0a05cd35f 100644 --- a/ocpp-v1_6/src/test/java/eu/chargetime/ocpp/model/firmware/test/GetDiagnosticsRequestTest.java +++ b/ocpp-v1_6/src/test/java/eu/chargetime/ocpp/model/firmware/test/GetDiagnosticsRequestTest.java @@ -25,15 +25,23 @@ of this software and associated documentation files (the "Software"), to deal SOFTWARE. */ +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import eu.chargetime.ocpp.PropertyConstraintException; import eu.chargetime.ocpp.model.firmware.GetDiagnosticsRequest; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; public class GetDiagnosticsRequestTest { + @Rule + public ExpectedException thrownException = ExpectedException.none(); + private GetDiagnosticsRequest request; @Before @@ -62,4 +70,54 @@ public void validate_locationIsSet_returnsTrue() { // Then assertThat(result, is(true)); } + + @Test + public void setRetries_asPositive_isAccepted() { + request.setRetries(42); + + assertThat(request.getRetries(), equalTo(42)); + } + + @Test + public void setRetries_asZero_isAccepted() { + request.setRetries(0); + + assertThat(request.getRetries(), equalTo(0)); + } + + @Test + public void setRetries_asNegative_throwsPropertyConstraintException() { + int retries = -42; + thrownException.expect(instanceOf(PropertyConstraintException.class)); + thrownException.expectMessage( + equalTo("Validation failed: [retries must be >= 0]. Current Value: [" + retries + "]")); + + request.setRetries(retries); + } + + @Test + public void setRetryInterval_asPositive_isAccepted() { + request.setRetryInterval(42); + + assertThat(request.getRetryInterval(), equalTo(42)); + } + + @Test + public void setRetryInterval_asZero_isAccepted() { + request.setRetryInterval(0); + + assertThat(request.getRetryInterval(), equalTo(0)); + } + + @Test + public void setRetryInterval_asNegative_throwsPropertyConstraintException() { + int retryInterval = -42; + thrownException.expect(instanceOf(PropertyConstraintException.class)); + thrownException.expectMessage( + equalTo("Validation failed: [retryInterval must be >= 0]. Current Value: [" + + retryInterval + + "]")); + + request.setRetryInterval(retryInterval); + } } diff --git a/ocpp-v1_6/src/test/java/eu/chargetime/ocpp/model/firmware/test/UpdateFirmwareRequestTest.java b/ocpp-v1_6/src/test/java/eu/chargetime/ocpp/model/firmware/test/UpdateFirmwareRequestTest.java index 75736b1f3..75705c506 100644 --- a/ocpp-v1_6/src/test/java/eu/chargetime/ocpp/model/firmware/test/UpdateFirmwareRequestTest.java +++ b/ocpp-v1_6/src/test/java/eu/chargetime/ocpp/model/firmware/test/UpdateFirmwareRequestTest.java @@ -89,19 +89,11 @@ public void validate_locationAndRetrieveDateIsSet_returnsTrue() { @Test public void setRetries_asNegative_throwsPropertyConstraintException() { - testInvalidRetries(-42); - } - - @Test - public void setRetries_asZero_throwsPropertyConstraintException() { - testInvalidRetries(0); - } - - private void testInvalidRetries(int retryInvalidRetries) { + int retries = -42; defineThrownException( - "Validation failed: [retries must be > 0]. Current Value: [" + retryInvalidRetries + "]"); + "Validation failed: [retries must be >= 0]. Current Value: [" + retries + "]"); - request.setRetries(retryInvalidRetries); + request.setRetries(retries); } private void defineThrownException(String expectedExceptionMessage) { @@ -117,22 +109,21 @@ public void setRetries_asPositive_isAccepted() { } @Test - public void setRetryInterval_asNegative_throwsPropertyConstraintException() { - testInvalidRetryInterval(-42); - } + public void setRetries_asZero_isAccepted() { + request.setRetries(0); - @Test - public void setRetryInterval_asZero_throwsPropertyConstraintException() { - testInvalidRetryInterval(0); + assertThat(request.getRetries(), equalTo(0)); } - private void testInvalidRetryInterval(int invalidRetryValue) { + @Test + public void setRetryInterval_asNegative_throwsPropertyConstraintException() { + int retryInterval = -42; defineThrownException( - "Validation failed: [retryInterval must be > 0]. Current Value: [" - + invalidRetryValue + "Validation failed: [retryInterval must be >= 0]. Current Value: [" + + retryInterval + "]"); - request.setRetryInterval(invalidRetryValue); + request.setRetryInterval(retryInterval); } @Test @@ -141,4 +132,11 @@ public void setRetryInterval_asPositive_isAccepted() { assertThat(request.getRetryInterval(), equalTo(42)); } + + @Test + public void setRetryInterval_asZero_isAccepted() { + request.setRetryInterval(0); + + assertThat(request.getRetryInterval(), equalTo(0)); + } }