|
| 1 | +package eu.chargetime.ocpp.feature.profile.test.securityext; |
| 2 | + |
| 3 | +import eu.chargetime.ocpp.feature.Feature; |
| 4 | +import eu.chargetime.ocpp.feature.profile.securityext.ClientSecurityExtEventHandler; |
| 5 | +import eu.chargetime.ocpp.feature.profile.securityext.ClientSecurityExtProfile; |
| 6 | +import eu.chargetime.ocpp.feature.profile.test.ProfileTest; |
| 7 | +import eu.chargetime.ocpp.feature.securityext.CertificateSignedFeature; |
| 8 | +import eu.chargetime.ocpp.feature.securityext.DeleteCertificateFeature; |
| 9 | +import eu.chargetime.ocpp.feature.securityext.ExtendedTriggerMessageFeature; |
| 10 | +import eu.chargetime.ocpp.feature.securityext.GetInstalledCertificateIdsFeature; |
| 11 | +import eu.chargetime.ocpp.feature.securityext.GetLogFeature; |
| 12 | +import eu.chargetime.ocpp.feature.securityext.InstallCertificateFeature; |
| 13 | +import eu.chargetime.ocpp.feature.securityext.LogStatusNotificationFeature; |
| 14 | +import eu.chargetime.ocpp.feature.securityext.SecurityEventNotificationFeature; |
| 15 | +import eu.chargetime.ocpp.feature.securityext.SignCertificateFeature; |
| 16 | +import eu.chargetime.ocpp.feature.securityext.SignedFirmwareStatusNotificationFeature; |
| 17 | +import eu.chargetime.ocpp.feature.securityext.SignedUpdateFirmwareFeature; |
| 18 | +import eu.chargetime.ocpp.model.Confirmation; |
| 19 | +import eu.chargetime.ocpp.model.securityext.CertificateSignedConfirmation; |
| 20 | +import eu.chargetime.ocpp.model.securityext.CertificateSignedRequest; |
| 21 | +import eu.chargetime.ocpp.model.securityext.DeleteCertificateConfirmation; |
| 22 | +import eu.chargetime.ocpp.model.securityext.DeleteCertificateRequest; |
| 23 | +import eu.chargetime.ocpp.model.securityext.ExtendedTriggerMessageConfirmation; |
| 24 | +import eu.chargetime.ocpp.model.securityext.ExtendedTriggerMessageRequest; |
| 25 | +import eu.chargetime.ocpp.model.securityext.GetInstalledCertificateIdsConfirmation; |
| 26 | +import eu.chargetime.ocpp.model.securityext.GetInstalledCertificateIdsRequest; |
| 27 | +import eu.chargetime.ocpp.model.securityext.GetLogConfirmation; |
| 28 | +import eu.chargetime.ocpp.model.securityext.GetLogRequest; |
| 29 | +import eu.chargetime.ocpp.model.securityext.InstallCertificateConfirmation; |
| 30 | +import eu.chargetime.ocpp.model.securityext.InstallCertificateRequest; |
| 31 | +import eu.chargetime.ocpp.model.securityext.LogStatusNotificationRequest; |
| 32 | +import eu.chargetime.ocpp.model.securityext.SecurityEventNotificationRequest; |
| 33 | +import eu.chargetime.ocpp.model.securityext.SignCertificateRequest; |
| 34 | +import eu.chargetime.ocpp.model.securityext.SignedFirmwareStatusNotificationRequest; |
| 35 | +import eu.chargetime.ocpp.model.securityext.SignedUpdateFirmwareConfirmation; |
| 36 | +import eu.chargetime.ocpp.model.securityext.SignedUpdateFirmwareRequest; |
| 37 | +import eu.chargetime.ocpp.model.securityext.types.FirmwareStatusEnumType; |
| 38 | +import eu.chargetime.ocpp.model.securityext.types.UploadLogStatusEnumType; |
| 39 | +import org.junit.Before; |
| 40 | +import org.junit.Test; |
| 41 | +import org.junit.runner.RunWith; |
| 42 | +import org.mockito.Mock; |
| 43 | +import org.mockito.runners.MockitoJUnitRunner; |
| 44 | + |
| 45 | +import java.time.ZonedDateTime; |
| 46 | +import java.util.UUID; |
| 47 | + |
| 48 | +import static org.junit.Assert.assertEquals; |
| 49 | +import static org.junit.Assert.assertNull; |
| 50 | +import static org.junit.Assert.assertTrue; |
| 51 | +import static org.mockito.Mockito.mock; |
| 52 | +import static org.mockito.Mockito.when; |
| 53 | + |
| 54 | +/* |
| 55 | + ChargeTime.eu - Java-OCA-OCPP |
| 56 | +
|
| 57 | + MIT License |
| 58 | +
|
| 59 | + Copyright (C) 2022 Mathias Oben <[email protected]> |
| 60 | +
|
| 61 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 62 | + of this software and associated documentation files (the "Software"), to deal |
| 63 | + in the Software without restriction, including without limitation the rights |
| 64 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 65 | + copies of the Software, and to permit persons to whom the Software is |
| 66 | + furnished to do so, subject to the following conditions: |
| 67 | +
|
| 68 | + The above copyright notice and this permission notice shall be included in all |
| 69 | + copies or substantial portions of the Software. |
| 70 | +
|
| 71 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 72 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 73 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 74 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 75 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 76 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 77 | + SOFTWARE. |
| 78 | +*/ |
| 79 | + |
| 80 | +@RunWith(MockitoJUnitRunner.class) |
| 81 | +public class ClientSecurityExtProfileTest extends ProfileTest { |
| 82 | + private static final UUID SESSION_ID = null; |
| 83 | + |
| 84 | + private ClientSecurityExtProfile profile; |
| 85 | + |
| 86 | + @Mock |
| 87 | + private ClientSecurityExtEventHandler handler; |
| 88 | + |
| 89 | + @Before |
| 90 | + public void setup() { |
| 91 | + profile = new ClientSecurityExtProfile(handler); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void getFeatureList_containsAllFeature() { |
| 96 | + // When |
| 97 | + Feature[] features = profile.getFeatureList(); |
| 98 | + |
| 99 | + // then |
| 100 | + assertTrue(findFeature(features, "CertificateSigned") instanceof CertificateSignedFeature); |
| 101 | + assertTrue(findFeature(features, "DeleteCertificate") instanceof DeleteCertificateFeature); |
| 102 | + assertTrue(findFeature(features, "ExtendedTriggerMessage") instanceof ExtendedTriggerMessageFeature); |
| 103 | + assertTrue(findFeature(features, "GetInstalledCertificateIds") instanceof GetInstalledCertificateIdsFeature); |
| 104 | + assertTrue(findFeature(features, "GetLog") instanceof GetLogFeature); |
| 105 | + assertTrue(findFeature(features, "InstallCertificate") instanceof InstallCertificateFeature); |
| 106 | + assertTrue(findFeature(features, "LogStatusNotification") instanceof LogStatusNotificationFeature); |
| 107 | + assertTrue(findFeature(features, "SecurityEventNotification") instanceof SecurityEventNotificationFeature); |
| 108 | + assertTrue(findFeature(features, "SignCertificate") instanceof SignCertificateFeature); |
| 109 | + assertTrue(findFeature(features, "SignedFirmwareStatusNotification") instanceof SignedFirmwareStatusNotificationFeature); |
| 110 | + assertTrue(findFeature(features, "SignedUpdateFirmware") instanceof SignedUpdateFirmwareFeature); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void createLogStatusNotificationRequest_withParams_returnsCompleteRequest() { |
| 115 | + // Given |
| 116 | + UploadLogStatusEnumType status = UploadLogStatusEnumType.Uploaded; |
| 117 | + |
| 118 | + // When |
| 119 | + LogStatusNotificationRequest actual = profile.createLogStatusNotificationRequest(status); |
| 120 | + |
| 121 | + // Then |
| 122 | + assertEquals(status, actual.getStatus()); |
| 123 | + assertNull(actual.getRequestId()); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void createSecurityEventNotificationRequest_withParams_returnsCompleteRequest() { |
| 128 | + // Given |
| 129 | + String type = "type"; |
| 130 | + ZonedDateTime timestamp = ZonedDateTime.now(); |
| 131 | + |
| 132 | + // When |
| 133 | + SecurityEventNotificationRequest actual = profile.createSecurityEventNotificationRequest(type, timestamp); |
| 134 | + |
| 135 | + // Then |
| 136 | + assertEquals(type, actual.getType()); |
| 137 | + assertEquals(timestamp, actual.getTimestamp()); |
| 138 | + assertNull(actual.getTechInfo()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void createSignCertificateRequest_withParams_returnsCompleteRequest() { |
| 143 | + // Given |
| 144 | + String csr = "csr"; |
| 145 | + |
| 146 | + // When |
| 147 | + SignCertificateRequest actual = profile.createSignCertificateRequest(csr); |
| 148 | + |
| 149 | + // Then |
| 150 | + assertEquals(csr, actual.getCsr()); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void createSignedFirmwareStatusNotificationRequest_withParams_returnsCompleteRequest() { |
| 155 | + // Given |
| 156 | + FirmwareStatusEnumType status = FirmwareStatusEnumType.Downloaded; |
| 157 | + |
| 158 | + // When |
| 159 | + SignedFirmwareStatusNotificationRequest actual = profile.createSignedFirmwareStatusNotificationRequest(status); |
| 160 | + |
| 161 | + // Then |
| 162 | + assertEquals(status, actual.getStatus()); |
| 163 | + assertNull(actual.getRequestId()); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void handleRequest_whenCertificateSignedRequest_callsHandleCertificateSignedRequest() { |
| 168 | + // Given |
| 169 | + CertificateSignedRequest request = mock(CertificateSignedRequest.class); |
| 170 | + CertificateSignedConfirmation confirmation = mock(CertificateSignedConfirmation.class); |
| 171 | + when(handler.handleCertificateSignedRequest(request)).thenReturn(confirmation); |
| 172 | + |
| 173 | + // When |
| 174 | + Confirmation actual = profile.handleRequest(SESSION_ID, request); |
| 175 | + |
| 176 | + // Then |
| 177 | + assertEquals(confirmation, actual); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void handleRequest_whenDeleteCertificateRequest_callsHandleDeleteCertificateRequest() { |
| 182 | + // Given |
| 183 | + DeleteCertificateRequest request = mock(DeleteCertificateRequest.class); |
| 184 | + DeleteCertificateConfirmation confirmation = mock(DeleteCertificateConfirmation.class); |
| 185 | + when(handler.handleDeleteCertificateRequest(request)).thenReturn(confirmation); |
| 186 | + |
| 187 | + // When |
| 188 | + Confirmation actual = profile.handleRequest(SESSION_ID, request); |
| 189 | + |
| 190 | + // Then |
| 191 | + assertEquals(confirmation, actual); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + public void handleRequest_whenExtendedTriggerMessageRequest_callsHandleExtendedTriggerMessageRequest() { |
| 196 | + // Given |
| 197 | + ExtendedTriggerMessageRequest request = mock(ExtendedTriggerMessageRequest.class); |
| 198 | + ExtendedTriggerMessageConfirmation confirmation = mock(ExtendedTriggerMessageConfirmation.class); |
| 199 | + when(handler.handleExtendedTriggerMessageRequest(request)).thenReturn(confirmation); |
| 200 | + |
| 201 | + // When |
| 202 | + Confirmation actual = profile.handleRequest(SESSION_ID, request); |
| 203 | + |
| 204 | + // Then |
| 205 | + assertEquals(confirmation, actual); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + public void handleRequest_whenGetInstalledCertificateIdsRequest_callsHandleGetInstalledCertificateIdsRequest() { |
| 210 | + // Given |
| 211 | + GetInstalledCertificateIdsRequest request = mock(GetInstalledCertificateIdsRequest.class); |
| 212 | + GetInstalledCertificateIdsConfirmation confirmation = mock(GetInstalledCertificateIdsConfirmation.class); |
| 213 | + when(handler.handleGetInstalledCertificateIdsRequest(request)).thenReturn(confirmation); |
| 214 | + |
| 215 | + // When |
| 216 | + Confirmation actual = profile.handleRequest(SESSION_ID, request); |
| 217 | + |
| 218 | + // Then |
| 219 | + assertEquals(confirmation, actual); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + public void handleRequest_whenGetLogRequest_callsHandleGetLogRequest() { |
| 224 | + // Given |
| 225 | + GetLogRequest request = mock(GetLogRequest.class); |
| 226 | + GetLogConfirmation confirmation = mock(GetLogConfirmation.class); |
| 227 | + when(handler.handleGetLogRequest(request)).thenReturn(confirmation); |
| 228 | + |
| 229 | + // When |
| 230 | + Confirmation actual = profile.handleRequest(SESSION_ID, request); |
| 231 | + |
| 232 | + // Then |
| 233 | + assertEquals(confirmation, actual); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + public void handleRequest_whenInstallCertificateRequest_callsHandleInstallCertificateRequest() { |
| 238 | + // Given |
| 239 | + InstallCertificateRequest request = mock(InstallCertificateRequest.class); |
| 240 | + InstallCertificateConfirmation confirmation = mock(InstallCertificateConfirmation.class); |
| 241 | + when(handler.handleInstallCertificateRequest(request)).thenReturn(confirmation); |
| 242 | + |
| 243 | + // When |
| 244 | + Confirmation actual = profile.handleRequest(SESSION_ID, request); |
| 245 | + |
| 246 | + // Then |
| 247 | + assertEquals(confirmation, actual); |
| 248 | + } |
| 249 | + |
| 250 | + @Test |
| 251 | + public void handleRequest_whenSignedUpdateFirmwareRequest_callsHandleSignedUpdateFirmwareRequest() { |
| 252 | + // Given |
| 253 | + SignedUpdateFirmwareRequest request = mock(SignedUpdateFirmwareRequest.class); |
| 254 | + SignedUpdateFirmwareConfirmation confirmation = mock(SignedUpdateFirmwareConfirmation.class); |
| 255 | + when(handler.handleSignedUpdateFirmwareRequest(request)).thenReturn(confirmation); |
| 256 | + |
| 257 | + // When |
| 258 | + Confirmation actual = profile.handleRequest(SESSION_ID, request); |
| 259 | + |
| 260 | + // Then |
| 261 | + assertEquals(confirmation, actual); |
| 262 | + } |
| 263 | + |
| 264 | +} |
0 commit comments