Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias Oben committed Jun 22, 2022
1 parent ec79d05 commit ce6bad7
Show file tree
Hide file tree
Showing 14 changed files with 851 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Copyright (C) 2016-2018 Thomas Volden <tv@chargetime.eu>
Copyright (C) 2019 Kevin Raddatz <kevin.raddatz@valtech-mobility.com>
Copyright (C) 2022 Mathias Oben <mathias.oben@enervalis.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -29,6 +30,7 @@ of this software and associated documentation files (the "Software"), to deal
import eu.chargetime.ocpp.ServerEvents;
import eu.chargetime.ocpp.feature.profile.ServerCoreEventHandler;
import eu.chargetime.ocpp.feature.profile.ServerFirmwareManagementEventHandler;
import eu.chargetime.ocpp.feature.profile.securityext.ServerSecurityExtEventHandler;
import eu.chargetime.ocpp.model.Confirmation;
import eu.chargetime.ocpp.model.Request;
import eu.chargetime.ocpp.model.SessionInformation;
Expand All @@ -37,6 +39,9 @@ of this software and associated documentation files (the "Software"), to deal
import eu.chargetime.ocpp.model.firmware.DiagnosticsStatusNotificationRequest;
import eu.chargetime.ocpp.model.firmware.FirmwareStatusNotificationConfirmation;
import eu.chargetime.ocpp.model.firmware.FirmwareStatusNotificationRequest;
import eu.chargetime.ocpp.model.securityext.*;
import eu.chargetime.ocpp.model.securityext.types.GenericStatusEnumType;

import java.lang.reflect.Type;
import java.time.ZonedDateTime;
import java.util.UUID;
Expand Down Expand Up @@ -160,6 +165,39 @@ public FirmwareStatusNotificationConfirmation handleFirmwareStatusNotificationRe
};
}

public ServerSecurityExtEventHandler createServerSecurityExtEventHandler() {
return new ServerSecurityExtEventHandler() {

@Override
public LogStatusNotificationConfirmation handleLogStatusNotificationRequest(UUID sessionIndex, LogStatusNotificationRequest request) {
receivedRequest = request;
LogStatusNotificationConfirmation confirmation = new LogStatusNotificationConfirmation();
return failurePoint(confirmation);
}

@Override
public SecurityEventNotificationConfirmation handleSecurityEventNotificationRequest(UUID sessionIndex, SecurityEventNotificationRequest request) {
receivedRequest = request;
SecurityEventNotificationConfirmation confirmation = new SecurityEventNotificationConfirmation();
return failurePoint(confirmation);
}

@Override
public SignCertificateConfirmation handleSignCertificateRequest(UUID sessionIndex, SignCertificateRequest request) {
receivedRequest = request;
SignCertificateConfirmation confirmation = new SignCertificateConfirmation(GenericStatusEnumType.Accepted);
return failurePoint(confirmation);
}

@Override
public SignedFirmwareStatusNotificationConfirmation handleSignedFirmwareStatusNotificationRequest(UUID sessionIndex, SignedFirmwareStatusNotificationRequest request) {
receivedRequest = request;
SignedFirmwareStatusNotificationConfirmation confirmation = new SignedFirmwareStatusNotificationConfirmation();
return failurePoint(confirmation);
}
};
}

public ServerEvents generateServerEventsHandler() {
return new ServerEvents() {
@Override
Expand Down Expand Up @@ -201,9 +239,8 @@ public boolean wasLatestRequest(Type requestType) {
&& requestType.equals(receivedRequest.getClass());
}

public <T extends Request> T getReceivedRequest(T requestType) {
if (wasLatestRequest(requestType.getClass())) return (T) receivedRequest;
return null;
public <T extends Request> T getReceivedRequest(Class<T> clazz) {
return wasLatestRequest(clazz) ? (T) receivedRequest : null;
}

public boolean wasLatestConfirmation(Type confirmationType) {
Expand All @@ -212,9 +249,8 @@ public boolean wasLatestConfirmation(Type confirmationType) {
&& confirmationType.equals(receivedConfirmation.getClass());
}

public <T extends Confirmation> T getReceivedConfirmation(T confirmationType) {
if (wasLatestConfirmation(confirmationType.getClass())) return (T) receivedConfirmation;
return null;
public <T extends Confirmation> T getReceivedConfirmation(Class<T> clazz) {
return wasLatestConfirmation(clazz) ? (T) receivedConfirmation : null;
}

public void setRiggedToFail(boolean riggedToFail) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Copyright (C) 2016-2018 Thomas Volden
Copyright (C) 2019 Kevin Raddatz <kevin.raddatz@valtech-mobility.com>
Copyright (C) 2022 Mathias Oben <mathias.oben@enervalis.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -31,6 +32,7 @@ of this software and associated documentation files (the "Software"), to deal
import eu.chargetime.ocpp.JSONConfiguration;
import eu.chargetime.ocpp.PropertyConstraintException;
import eu.chargetime.ocpp.feature.profile.*;
import eu.chargetime.ocpp.feature.profile.securityext.ServerSecurityExtProfile;
import eu.chargetime.ocpp.model.Request;
import eu.chargetime.ocpp.model.core.*;
import eu.chargetime.ocpp.model.firmware.*;
Expand All @@ -41,6 +43,8 @@ of this software and associated documentation files (the "Software"), to deal
import eu.chargetime.ocpp.model.reservation.CancelReservationRequest;
import eu.chargetime.ocpp.model.reservation.ReserveNowConfirmation;
import eu.chargetime.ocpp.model.reservation.ReserveNowRequest;
import eu.chargetime.ocpp.model.securityext.*;
import eu.chargetime.ocpp.model.securityext.types.*;
import eu.chargetime.ocpp.model.smartcharging.SetChargingProfileConfirmation;
import eu.chargetime.ocpp.model.smartcharging.SetChargingProfileRequest;
import eu.chargetime.ocpp.test.FakeCentral.serverType;
Expand Down Expand Up @@ -91,6 +95,11 @@ private void initializeServer() {

ServerReservationProfile serverReservationProfile = new ServerReservationProfile();
server.addFeatureProfile(serverReservationProfile);

ServerSecurityExtProfile serverSecurityExtProfile = new ServerSecurityExtProfile(
dummyHandlers.createServerSecurityExtEventHandler()
);
server.addFeatureProfile(serverSecurityExtProfile);
}

public boolean isClosed() {
Expand Down Expand Up @@ -141,7 +150,7 @@ public boolean hasHandledAuthorizeRequest() {
public boolean hasHandledBootNotification(String vendor, String model) {
boolean result = false;
BootNotificationRequest request =
dummyHandlers.getReceivedRequest(new BootNotificationRequest());
dummyHandlers.getReceivedRequest(BootNotificationRequest.class);
if (request != null) {
result = request.getChargePointVendor().equals(vendor);
result &= request.getChargePointModel().equals(model);
Expand Down Expand Up @@ -188,7 +197,7 @@ public boolean hasReceivedSetChargingProfileConfirmation() {
public boolean hasReceivedChangeAvailabilityConfirmation(String status) {
boolean result = false;
ChangeAvailabilityConfirmation confirmation =
dummyHandlers.getReceivedConfirmation(new ChangeAvailabilityConfirmation());
dummyHandlers.getReceivedConfirmation(ChangeAvailabilityConfirmation.class);
if (confirmation != null) result = confirmation.getStatus().toString().equals(status);
return result;
}
Expand Down Expand Up @@ -279,7 +288,7 @@ public void sendRemoteStartTransactionWithProfileRequest(int connectorId, String
public boolean hasReceivedRemoteStartTransactionConfirmation(String status) {
boolean result = false;
RemoteStartTransactionConfirmation confirmation =
dummyHandlers.getReceivedConfirmation(new RemoteStartTransactionConfirmation());
dummyHandlers.getReceivedConfirmation(RemoteStartTransactionConfirmation.class);
if (confirmation != null) result = confirmation.getStatus().toString().equals(status);
return result;
}
Expand All @@ -293,7 +302,7 @@ public void sendRemoteStopTransactionRequest(int transactionId) throws Exception
public boolean hasReceivedRemoteStopTransactionConfirmation(String status) {
boolean result = false;
RemoteStopTransactionConfirmation confirmation =
dummyHandlers.getReceivedConfirmation(new RemoteStopTransactionConfirmation());
dummyHandlers.getReceivedConfirmation(RemoteStopTransactionConfirmation.class);
if (confirmation != null) result = confirmation.getStatus().toString().equals(status);
return result;
}
Expand Down Expand Up @@ -348,7 +357,7 @@ public void sendSetChargingProfileRequest(Integer connectorId, ChargingProfile c

public boolean hasReceivedResetConfirmation(String status) {
boolean result = false;
ResetConfirmation confirmation = dummyHandlers.getReceivedConfirmation(new ResetConfirmation());
ResetConfirmation confirmation = dummyHandlers.getReceivedConfirmation(ResetConfirmation.class);
if (confirmation != null) result = confirmation.getStatus().toString().equals(status);
return result;
}
Expand All @@ -374,11 +383,104 @@ public void sendUnlockConnectorRequest(int connectorId) throws Exception {
public boolean hasReceivedUnlockConnectorConfirmation(String status) {
boolean result = false;
UnlockConnectorConfirmation confirmation =
dummyHandlers.getReceivedConfirmation(new UnlockConnectorConfirmation());
dummyHandlers.getReceivedConfirmation(UnlockConnectorConfirmation.class);
if (confirmation != null) result = confirmation.getStatus().toString().equals(status);
return result;
}

public void sendCertificateSignedRequest(String certificateChain) throws Exception {
Request request = new CertificateSignedRequest(certificateChain);
send(request);
}

public boolean hasReceivedCertificateSignedConfirmation(String status) {
CertificateSignedConfirmation confirmation =
dummyHandlers.getReceivedConfirmation(CertificateSignedConfirmation.class);
return confirmation != null && confirmation.getStatus().toString().equals(status);
}

public void sendDeleteCertificateRequest(CertificateHashDataType certificateHashDataType) throws Exception {
Request request = new DeleteCertificateRequest(certificateHashDataType);
send(request);
}

public boolean hasReceivedDeleteCertificateConfirmation(String status) {
DeleteCertificateConfirmation confirmation =
dummyHandlers.getReceivedConfirmation(DeleteCertificateConfirmation.class);
return confirmation != null && confirmation.getStatus().toString().equals(status);
}

public void sendExtendedTriggerMessageRequest(MessageTriggerEnumType requestedMessage) throws Exception {
Request request = new ExtendedTriggerMessageRequest(requestedMessage);
send(request);
}

public boolean hasReceivedExtendedTriggerMessageConfirmation(String status) {
ExtendedTriggerMessageConfirmation confirmation =
dummyHandlers.getReceivedConfirmation(ExtendedTriggerMessageConfirmation.class);
return confirmation != null && confirmation.getStatus().toString().equals(status);
}

public void sendGetInstalledCertificateIdsRequest(CertificateUseEnumType certificateType) throws Exception {
Request request = new GetInstalledCertificateIdsRequest(certificateType);
send(request);
}

public boolean hasReceivedGetInstalledCertificateIdsConfirmation(String status) {
GetInstalledCertificateIdsConfirmation confirmation =
dummyHandlers.getReceivedConfirmation(GetInstalledCertificateIdsConfirmation.class);
return confirmation != null && confirmation.getStatus().toString().equals(status);
}

public void sendGetLogRequest(LogEnumType logType, Integer requestId, LogParametersType log) throws Exception {
Request request = new GetLogRequest(logType, requestId, log);
send(request);
}

public boolean hasReceivedGetLogConfirmation(String status) {
GetLogConfirmation confirmation =
dummyHandlers.getReceivedConfirmation(GetLogConfirmation.class);
return confirmation != null && confirmation.getStatus().toString().equals(status);
}

public void sendInstallCertificateRequest(CertificateUseEnumType certificateType, String certificate) throws Exception {
Request request = new InstallCertificateRequest(certificateType, certificate);
send(request);
}

public boolean hasReceivedInstallCertificateConfirmation(String status) {
InstallCertificateConfirmation confirmation =
dummyHandlers.getReceivedConfirmation(InstallCertificateConfirmation.class);
return confirmation != null && confirmation.getStatus().toString().equals(status);
}

public boolean hasHandledLogStatusNotificationRequest() {
return dummyHandlers.wasLatestRequest(LogStatusNotificationRequest.class);
}

public boolean hasHandledSecurityEventNotificationRequest() {
return dummyHandlers.wasLatestRequest(SecurityEventNotificationRequest.class);
}

public boolean hasHandledSignCertificateRequest() {
return dummyHandlers.wasLatestRequest(SignCertificateRequest.class);
}

public boolean hasHandledSignedFirmwareStatusNotificationRequest() {
return dummyHandlers.wasLatestRequest(SignedFirmwareStatusNotificationRequest.class);
}

public void sendSignedUpdateFirmwareRequest(Integer requestId, FirmwareType firmware) throws Exception {
Request request = new SignedUpdateFirmwareRequest(requestId, firmware);
send(request);
}

public boolean hasReceivedSignedUpdateFirmwareConfirmation(String status) {
SignedUpdateFirmwareConfirmation confirmation =
dummyHandlers.getReceivedConfirmation(SignedUpdateFirmwareConfirmation.class);
return confirmation != null && confirmation.getStatus().toString().equals(status);
}

public void rigNextRequestToFail() {
dummyHandlers.setRiggedToFail(true);
}
Expand Down
Loading

0 comments on commit ce6bad7

Please sign in to comment.