Skip to content

Commit e158b3c

Browse files
committed
add tests
1 parent 53adf6c commit e158b3c

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

ocpp-jaxb/src/test/java/de/rwth/idsg/ocpp/jaxb/BeanDeserializerValidationTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package de.rwth.idsg.ocpp.jaxb;
22

33
import de.rwth.idsg.ocpp.jaxb.validation.BeanValidationModule;
4+
import ocpp._2020._03.CustomData;
5+
import ocpp._2020._03.SecurityEventNotificationRequest;
6+
import ocpp.cs._2015._10.AuthorizeResponse;
7+
import ocpp.cs._2015._10.IdTagInfo;
48
import ocpp.cs._2015._10.StartTransactionRequest;
59
import org.joda.time.DateTime;
610
import org.junit.jupiter.api.Assertions;
711
import org.junit.jupiter.api.BeforeAll;
812
import org.junit.jupiter.api.Test;
13+
import tools.jackson.databind.DatabindException;
914
import tools.jackson.databind.ObjectMapper;
1015
import tools.jackson.databind.json.JsonMapper;
1116
import tools.jackson.datatype.joda.JodaModule;
@@ -99,7 +104,38 @@ public void startTransactionIdTagTooLong() {
99104

100105
Assertions.assertEquals("idTag", violation.getPropertyPath().toString());
101106
Assertions.assertEquals("size must be between 0 and 20", violation.getMessage());
107+
}
108+
109+
@Test
110+
public void embeddedCustomDataEmpty() {
111+
var req = new SecurityEventNotificationRequest()
112+
.withType("type")
113+
.withTimestamp(DateTime.now())
114+
.withCustomData(new CustomData());
115+
116+
String input = mapper.writeValueAsString(req);
117+
118+
var exception = assertThrows(DatabindException.class, () -> mapper.readValue(input, SecurityEventNotificationRequest.class));
119+
120+
Throwable cause = exception.getCause();
121+
Assertions.assertInstanceOf(ConstraintViolationException.class, cause);
122+
123+
Assertions.assertEquals("vendorId: must not be null", cause.getMessage());
124+
}
125+
126+
@Test
127+
public void embeddedIdTagInfoEmpty() {
128+
var req = new AuthorizeResponse()
129+
.withIdTagInfo(new IdTagInfo());
130+
131+
String input = mapper.writeValueAsString(req);
132+
133+
var exception = assertThrows(DatabindException.class, () -> mapper.readValue(input, AuthorizeResponse.class));
134+
135+
Throwable cause = exception.getCause();
136+
Assertions.assertInstanceOf(ConstraintViolationException.class, cause);
102137

138+
Assertions.assertEquals("status: must not be null", cause.getMessage());
103139
}
104140

105141
private static void checkViolatingNullFields(Set<String> expected, Set<ConstraintViolation<?>> violations) {

ocpp-jaxb/src/test/java/de/rwth/idsg/ocpp/jaxb/BeanSerializerValidationTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package de.rwth.idsg.ocpp.jaxb;
22

33
import de.rwth.idsg.ocpp.jaxb.validation.BeanValidationModule;
4+
import ocpp._2020._03.CustomData;
5+
import ocpp._2020._03.SecurityEventNotificationRequest;
6+
import ocpp.cs._2015._10.AuthorizeResponse;
7+
import ocpp.cs._2015._10.IdTagInfo;
48
import ocpp.cs._2015._10.StartTransactionRequest;
59
import org.joda.time.DateTime;
610
import org.junit.jupiter.api.Assertions;
@@ -12,7 +16,6 @@
1216

1317
import jakarta.validation.ConstraintViolation;
1418
import jakarta.validation.ConstraintViolationException;
15-
1619
import java.util.Set;
1720
import java.util.stream.Collectors;
1821

@@ -89,6 +92,30 @@ public void startTransactionIdTagTooLong() {
8992
Assertions.assertEquals("size must be between 0 and 20", violation.getMessage());
9093
}
9194

95+
@Test
96+
public void embeddedCustomDataEmpty() {
97+
var req = new SecurityEventNotificationRequest()
98+
.withType("type")
99+
.withTimestamp(DateTime.now())
100+
.withCustomData(new CustomData());
101+
102+
var exception = assertThrows(ConstraintViolationException.class, () -> mapper.writeValueAsString(req));
103+
104+
var violations = exception.getConstraintViolations();
105+
checkViolatingNullFields(Set.of("customData.vendorId"), violations);
106+
}
107+
108+
@Test
109+
public void embeddedIdTagInfoEmpty() {
110+
var req = new AuthorizeResponse()
111+
.withIdTagInfo(new IdTagInfo());
112+
113+
var exception = assertThrows(ConstraintViolationException.class, () -> mapper.writeValueAsString(req));
114+
115+
var violations = exception.getConstraintViolations();
116+
checkViolatingNullFields(Set.of("idTagInfo.status"), violations);
117+
}
118+
92119
private static void checkViolatingNullFields(Set<String> expected, Set<ConstraintViolation<?>> violations) {
93120
Assertions.assertEquals(expected.size(), violations.size());
94121

0 commit comments

Comments
 (0)