|
| 1 | +package de.rwth.idsg.ocpp.jaxb; |
| 2 | + |
| 3 | +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; |
| 8 | +import ocpp.cs._2015._10.StartTransactionRequest; |
| 9 | +import org.joda.time.DateTime; |
| 10 | +import org.junit.jupiter.api.Assertions; |
| 11 | +import org.junit.jupiter.api.BeforeAll; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import tools.jackson.databind.DatabindException; |
| 14 | +import tools.jackson.databind.ObjectMapper; |
| 15 | +import tools.jackson.databind.json.JsonMapper; |
| 16 | +import tools.jackson.datatype.joda.JodaModule; |
| 17 | + |
| 18 | +import jakarta.validation.ConstraintViolation; |
| 19 | +import jakarta.validation.ConstraintViolationException; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.stream.Collectors; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 24 | + |
| 25 | +public class BeanDeserializerValidationTest { |
| 26 | + |
| 27 | + private static ObjectMapper mapper; |
| 28 | + |
| 29 | + @BeforeAll |
| 30 | + public static void setup() { |
| 31 | + mapper = JsonMapper.builder() |
| 32 | + .addModule(new JodaModule()) |
| 33 | + .addModule(BeanValidationModule.forReading(null)) |
| 34 | + .build(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void nullFieldsOcpp12() { |
| 39 | + String input = mapper.writeValueAsString(new ocpp.cs._2010._08.StartTransactionRequest()); |
| 40 | + |
| 41 | + var exception = assertThrows(ConstraintViolationException.class, () -> mapper.readValue(input, ocpp.cs._2010._08.StartTransactionRequest.class)); |
| 42 | + |
| 43 | + var violations = exception.getConstraintViolations(); |
| 44 | + checkViolatingNullFields(Set.of("connectorId", "idTag", "timestamp", "meterStart"), violations); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void nullFieldsOcpp15() { |
| 49 | + String input = mapper.writeValueAsString(new ocpp.cs._2012._06.StartTransactionRequest()); |
| 50 | + |
| 51 | + var exception = assertThrows(ConstraintViolationException.class, () -> mapper.readValue(input, ocpp.cs._2012._06.StartTransactionRequest.class)); |
| 52 | + |
| 53 | + var violations = exception.getConstraintViolations(); |
| 54 | + checkViolatingNullFields(Set.of("connectorId", "idTag", "timestamp", "meterStart"), violations); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void nullFieldsOcpp16() { |
| 59 | + String input = mapper.writeValueAsString(new ocpp.cs._2015._10.StartTransactionRequest()); |
| 60 | + |
| 61 | + var exception = assertThrows(ConstraintViolationException.class, () -> mapper.readValue(input, ocpp.cs._2015._10.StartTransactionRequest.class)); |
| 62 | + |
| 63 | + var violations = exception.getConstraintViolations(); |
| 64 | + checkViolatingNullFields(Set.of("connectorId", "idTag", "timestamp", "meterStart"), violations); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void nullFieldsOcpp16Security() { |
| 69 | + String input = mapper.writeValueAsString(new ocpp._2022._02.security.SecurityEventNotification()); |
| 70 | + |
| 71 | + var exception = assertThrows(ConstraintViolationException.class, () -> mapper.readValue(input, ocpp._2022._02.security.SecurityEventNotification.class)); |
| 72 | + |
| 73 | + var violations = exception.getConstraintViolations(); |
| 74 | + checkViolatingNullFields(Set.of("type", "timestamp"), violations); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void nullFieldsOcpp2() { |
| 79 | + String input = mapper.writeValueAsString(new ocpp._2020._03.SecurityEventNotificationRequest()); |
| 80 | + |
| 81 | + var exception = assertThrows(ConstraintViolationException.class, () -> mapper.readValue(input, ocpp._2020._03.SecurityEventNotificationRequest.class)); |
| 82 | + |
| 83 | + var violations = exception.getConstraintViolations(); |
| 84 | + checkViolatingNullFields(Set.of("type", "timestamp"), violations); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void startTransactionIdTagTooLong() { |
| 89 | + StartTransactionRequest request = new StartTransactionRequest() |
| 90 | + .withConnectorId(1) |
| 91 | + .withMeterStart(0) |
| 92 | + .withTimestamp(DateTime.now()) |
| 93 | + .withIdTag("ABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABC"); |
| 94 | + |
| 95 | + String input = mapper.writeValueAsString(request); |
| 96 | + System.out.println(input); |
| 97 | + |
| 98 | + var exception = assertThrows(ConstraintViolationException.class, () -> mapper.readValue(input, StartTransactionRequest.class)); |
| 99 | + |
| 100 | + var violations = exception.getConstraintViolations(); |
| 101 | + Assertions.assertEquals(1, violations.size()); |
| 102 | + |
| 103 | + ConstraintViolation<?> violation = violations.iterator().next(); |
| 104 | + |
| 105 | + Assertions.assertEquals("idTag", violation.getPropertyPath().toString()); |
| 106 | + 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); |
| 137 | + |
| 138 | + Assertions.assertEquals("status: must not be null", cause.getMessage()); |
| 139 | + } |
| 140 | + |
| 141 | + private static void checkViolatingNullFields(Set<String> expected, Set<ConstraintViolation<?>> violations) { |
| 142 | + Assertions.assertEquals(expected.size(), violations.size()); |
| 143 | + |
| 144 | + var violatingFields = violations.stream() |
| 145 | + .map(it -> it.getPropertyPath().toString()) |
| 146 | + .collect(Collectors.toSet()); |
| 147 | + |
| 148 | + Assertions.assertEquals(expected, violatingFields); |
| 149 | + |
| 150 | + var violationReason = violations.stream() |
| 151 | + .map(ConstraintViolation::getMessage) |
| 152 | + .collect(Collectors.toSet()); |
| 153 | + |
| 154 | + Assertions.assertEquals(violationReason, Set.of("must not be null")); |
| 155 | + } |
| 156 | +} |
0 commit comments