Skip to content

Commit 88a40e4

Browse files
authored
Allow zero retries in OCPP 1.6 UpdateFirmwareRequest
The OCPP 1.6 UpdateFirmwareRequest was incorrectly rejecting zero retries. Fix this and add missing checks for retryInterval and the same two fields in the OCPP 1.6 GetDiagnosticsRequest. Fixes #362.
2 parents 8f2ef21 + db4be9d commit 88a40e4

4 files changed

Lines changed: 90 additions & 25 deletions

File tree

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/firmware/GetDiagnosticsRequest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ of this software and associated documentation files (the "Software"), to deal
2626
SOFTWARE.
2727
*/
2828

29+
import eu.chargetime.ocpp.PropertyConstraintException;
2930
import eu.chargetime.ocpp.model.RequestWithId;
3031
import eu.chargetime.ocpp.utilities.MoreObjects;
3132
import java.time.ZonedDateTime;
@@ -107,6 +108,10 @@ public Integer getRetries() {
107108
*/
108109
@XmlElement
109110
public void setRetries(Integer retries) {
111+
if (retries < 0) {
112+
throw new PropertyConstraintException(retries, "retries must be >= 0");
113+
}
114+
110115
this.retries = retries;
111116
}
112117

@@ -128,6 +133,10 @@ public Integer getRetryInterval() {
128133
*/
129134
@XmlElement
130135
public void setRetryInterval(Integer retryInterval) {
136+
if (retryInterval < 0) {
137+
throw new PropertyConstraintException(retryInterval, "retryInterval must be >= 0");
138+
}
139+
131140
this.retryInterval = retryInterval;
132141
}
133142

ocpp-v1_6/src/main/java/eu/chargetime/ocpp/model/firmware/UpdateFirmwareRequest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ public Integer getRetries() {
112112
*/
113113
@XmlElement
114114
public void setRetries(int retries) {
115-
if (retries <= 0) {
116-
throw new PropertyConstraintException(retries, "retries must be > 0");
115+
if (retries < 0) {
116+
throw new PropertyConstraintException(retries, "retries must be >= 0");
117117
}
118118

119119
this.retries = retries;
@@ -157,8 +157,8 @@ public Integer getRetryInterval() {
157157
*/
158158
@XmlElement
159159
public void setRetryInterval(int retryInterval) {
160-
if (retryInterval <= 0) {
161-
throw new PropertyConstraintException(retryInterval, "retryInterval must be > 0");
160+
if (retryInterval < 0) {
161+
throw new PropertyConstraintException(retryInterval, "retryInterval must be >= 0");
162162
}
163163

164164
this.retryInterval = retryInterval;

ocpp-v1_6/src/test/java/eu/chargetime/ocpp/model/firmware/test/GetDiagnosticsRequestTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,23 @@ of this software and associated documentation files (the "Software"), to deal
2525
SOFTWARE.
2626
*/
2727

28+
import static org.hamcrest.CoreMatchers.equalTo;
29+
import static org.hamcrest.CoreMatchers.instanceOf;
2830
import static org.hamcrest.CoreMatchers.is;
2931
import static org.junit.Assert.assertThat;
3032

33+
import eu.chargetime.ocpp.PropertyConstraintException;
3134
import eu.chargetime.ocpp.model.firmware.GetDiagnosticsRequest;
3235
import org.junit.Before;
36+
import org.junit.Rule;
3337
import org.junit.Test;
38+
import org.junit.rules.ExpectedException;
3439

3540
public class GetDiagnosticsRequestTest {
3641

42+
@Rule
43+
public ExpectedException thrownException = ExpectedException.none();
44+
3745
private GetDiagnosticsRequest request;
3846

3947
@Before
@@ -62,4 +70,54 @@ public void validate_locationIsSet_returnsTrue() {
6270
// Then
6371
assertThat(result, is(true));
6472
}
73+
74+
@Test
75+
public void setRetries_asPositive_isAccepted() {
76+
request.setRetries(42);
77+
78+
assertThat(request.getRetries(), equalTo(42));
79+
}
80+
81+
@Test
82+
public void setRetries_asZero_isAccepted() {
83+
request.setRetries(0);
84+
85+
assertThat(request.getRetries(), equalTo(0));
86+
}
87+
88+
@Test
89+
public void setRetries_asNegative_throwsPropertyConstraintException() {
90+
int retries = -42;
91+
thrownException.expect(instanceOf(PropertyConstraintException.class));
92+
thrownException.expectMessage(
93+
equalTo("Validation failed: [retries must be >= 0]. Current Value: [" + retries + "]"));
94+
95+
request.setRetries(retries);
96+
}
97+
98+
@Test
99+
public void setRetryInterval_asPositive_isAccepted() {
100+
request.setRetryInterval(42);
101+
102+
assertThat(request.getRetryInterval(), equalTo(42));
103+
}
104+
105+
@Test
106+
public void setRetryInterval_asZero_isAccepted() {
107+
request.setRetryInterval(0);
108+
109+
assertThat(request.getRetryInterval(), equalTo(0));
110+
}
111+
112+
@Test
113+
public void setRetryInterval_asNegative_throwsPropertyConstraintException() {
114+
int retryInterval = -42;
115+
thrownException.expect(instanceOf(PropertyConstraintException.class));
116+
thrownException.expectMessage(
117+
equalTo("Validation failed: [retryInterval must be >= 0]. Current Value: ["
118+
+ retryInterval
119+
+ "]"));
120+
121+
request.setRetryInterval(retryInterval);
122+
}
65123
}

ocpp-v1_6/src/test/java/eu/chargetime/ocpp/model/firmware/test/UpdateFirmwareRequestTest.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,11 @@ public void validate_locationAndRetrieveDateIsSet_returnsTrue() {
8989

9090
@Test
9191
public void setRetries_asNegative_throwsPropertyConstraintException() {
92-
testInvalidRetries(-42);
93-
}
94-
95-
@Test
96-
public void setRetries_asZero_throwsPropertyConstraintException() {
97-
testInvalidRetries(0);
98-
}
99-
100-
private void testInvalidRetries(int retryInvalidRetries) {
92+
int retries = -42;
10193
defineThrownException(
102-
"Validation failed: [retries must be > 0]. Current Value: [" + retryInvalidRetries + "]");
94+
"Validation failed: [retries must be >= 0]. Current Value: [" + retries + "]");
10395

104-
request.setRetries(retryInvalidRetries);
96+
request.setRetries(retries);
10597
}
10698

10799
private void defineThrownException(String expectedExceptionMessage) {
@@ -117,22 +109,21 @@ public void setRetries_asPositive_isAccepted() {
117109
}
118110

119111
@Test
120-
public void setRetryInterval_asNegative_throwsPropertyConstraintException() {
121-
testInvalidRetryInterval(-42);
122-
}
112+
public void setRetries_asZero_isAccepted() {
113+
request.setRetries(0);
123114

124-
@Test
125-
public void setRetryInterval_asZero_throwsPropertyConstraintException() {
126-
testInvalidRetryInterval(0);
115+
assertThat(request.getRetries(), equalTo(0));
127116
}
128117

129-
private void testInvalidRetryInterval(int invalidRetryValue) {
118+
@Test
119+
public void setRetryInterval_asNegative_throwsPropertyConstraintException() {
120+
int retryInterval = -42;
130121
defineThrownException(
131-
"Validation failed: [retryInterval must be > 0]. Current Value: ["
132-
+ invalidRetryValue
122+
"Validation failed: [retryInterval must be >= 0]. Current Value: ["
123+
+ retryInterval
133124
+ "]");
134125

135-
request.setRetryInterval(invalidRetryValue);
126+
request.setRetryInterval(retryInterval);
136127
}
137128

138129
@Test
@@ -141,4 +132,11 @@ public void setRetryInterval_asPositive_isAccepted() {
141132

142133
assertThat(request.getRetryInterval(), equalTo(42));
143134
}
135+
136+
@Test
137+
public void setRetryInterval_asZero_isAccepted() {
138+
request.setRetryInterval(0);
139+
140+
assertThat(request.getRetryInterval(), equalTo(0));
141+
}
144142
}

0 commit comments

Comments
 (0)