Skip to content

Commit

Permalink
Add tests for SmartCharging.
Browse files Browse the repository at this point in the history
  • Loading branch information
sumlin committed Apr 19, 2018
1 parent 3e7e923 commit 4ec5d13
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,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.smartcharging.SetChargingProfileConfirmation;
import eu.chargetime.ocpp.model.smartcharging.SetChargingProfileRequest;
import eu.chargetime.ocpp.test.FakeCentral.serverType;

import java.util.Calendar;
Expand All @@ -55,17 +57,13 @@ public class FakeCentralSystem {
dummyHandlers = new DummyHandlers();

ServerCoreProfile serverCoreProfile = new ServerCoreProfile(dummyHandlers.createServerCoreEventHandler());
ServerReservationProfile serverReservationProfile = new ServerReservationProfile();
ServerLocalAuthListProfile serverLocalAuthListProfile = new ServerLocalAuthListProfile();

if (type == serverType.JSON) {
server = new JSONServer(serverCoreProfile);
} else {
server = new SOAPServer(serverCoreProfile);
}

server.addFeatureProfile(serverReservationProfile);
server.addFeatureProfile(serverLocalAuthListProfile);
initializeServer();
isStarted = false;
}
Expand All @@ -82,6 +80,9 @@ private void initializeServer() {

ServerLocalAuthListProfile localAuthListProfile = new ServerLocalAuthListProfile();
server.addFeatureProfile(localAuthListProfile);

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

public boolean connected() {
Expand Down Expand Up @@ -161,6 +162,10 @@ public boolean hasReceivedUpdateFirmwareConfirmation() {
return dummyHandlers.wasLatestConfirmation(UpdateFirmwareConfirmation.class);
}

public boolean hasReceivedSetChargingProfileConfirmation() {
return dummyHandlers.wasLatestConfirmation(SetChargingProfileConfirmation.class);
}

public boolean hasReceivedChangeAvailabilityConfirmation(String status) {
boolean result = false;
ChangeAvailabilityConfirmation confirmation = dummyHandlers.getReceivedConfirmation(new ChangeAvailabilityConfirmation());
Expand Down Expand Up @@ -299,6 +304,11 @@ public void sendSendLocalListRequest(int listVersion, UpdateType updateType) thr
send(request);
}

public void sendSetChargingProfileRequest(Integer connectorId, ChargingProfile chargingProfile) throws Exception {
SetChargingProfileRequest request = new SetChargingProfileRequest(connectorId, chargingProfile);
send(request);
}

public boolean hasReceivedResetConfirmation(String status) {
boolean result = false;
ResetConfirmation confirmation = dummyHandlers.getReceivedConfirmation(new ResetConfirmation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ public boolean hasHandledGetLocalListVersionRequest() {
return receivedRequest instanceof GetLocalListVersionRequest;
}

public boolean hasHandledSetChargingProfileRequest() {
return receivedRequest instanceof SetChargingProfileRequest;
}

public boolean hasHandledChangeConfigurationRequest() {
return receivedRequest instanceof ChangeConfigurationRequest;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package eu.chargetime.ocpp.test.smartcharging.json

import eu.chargetime.ocpp.model.core.ChargingProfile
import eu.chargetime.ocpp.model.core.ChargingProfileKindType
import eu.chargetime.ocpp.model.core.ChargingProfilePurposeType
import eu.chargetime.ocpp.model.core.ChargingRateUnitType
import eu.chargetime.ocpp.model.core.ChargingSchedule
import eu.chargetime.ocpp.model.core.ChargingSchedulePeriod
import eu.chargetime.ocpp.test.FakeCentral
import eu.chargetime.ocpp.test.FakeCentralSystem
import eu.chargetime.ocpp.test.FakeChargePoint
import spock.lang.Shared
import spock.lang.Specification
import spock.util.concurrent.PollingConditions

/*
ChargeTime.eu - Java-OCA-OCPP
MIT License
Copyright (C) 2016-2018 Thomas Volden <[email protected]>
Copyright (C) 2018 Mikhail Kladkevich <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

class JSONSetChargingProfileSpec extends Specification {
@Shared
FakeCentralSystem centralSystem = FakeCentral.getSystem(FakeCentral.serverType.JSON)
@Shared
FakeChargePoint chargePoint = new FakeChargePoint()

def setupSpec() {
// When a Central System is running
centralSystem.started()
}

def setup() {
chargePoint.connect()
}

def cleanup() {
chargePoint.disconnect()
}

def "Central System sends a SetChargingProfile request and receives a response"() {
def conditions = new PollingConditions(timeout: 1)
given:
conditions.eventually {
assert centralSystem.connected()
}

when:
ChargingSchedulePeriod[] chargingSchedulePeriod = new ChargingSchedulePeriod[1]
chargingSchedulePeriod[0] = new ChargingSchedulePeriod(1, 2D)
centralSystem.sendSetChargingProfileRequest(1, new ChargingProfile(1, 2, ChargingProfilePurposeType.ChargePointMaxProfile, ChargingProfileKindType.Recurring,
new ChargingSchedule(ChargingRateUnitType.A, chargingSchedulePeriod)))

then:
conditions.eventually {
assert chargePoint.hasHandledSetChargingProfileRequest()
assert centralSystem.hasReceivedSetChargingProfileConfirmation()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,26 @@ public class ChargingProfile implements Validatable {
private Calendar validTo;
private ChargingSchedule chargingSchedule;

public ChargingProfile() { }

public ChargingProfile(Integer chargingProfileId, Integer stackLevel, ChargingProfilePurposeType chargingProfilePurpose, ChargingProfileKindType chargingProfileKind, ChargingSchedule chargingSchedule) {
this.chargingProfileId = chargingProfileId;
this.stackLevel = stackLevel;
this.chargingProfilePurpose = chargingProfilePurpose;
this.chargingProfileKind = chargingProfileKind;
this.chargingSchedule = chargingSchedule;
}

public ChargingProfile(Integer chargingProfileId, Integer stackLevel, ChargingProfilePurposeType chargingProfilePurpose, ChargingProfileKindType chargingProfileKind) {
this.chargingProfileId = chargingProfileId;
this.stackLevel = stackLevel;
this.chargingProfilePurpose = chargingProfilePurpose;
this.chargingProfileKind = chargingProfileKind;
}

@Override
public boolean validate() {
boolean valid = true;
valid &= chargingProfileId != null;
boolean valid = chargingProfileId != null;
valid &= stackLevel >= 0;
valid &= chargingProfilePurpose != null;
valid &= transactionId == null || chargingProfilePurpose == ChargingProfilePurposeType.TxProfile;
Expand All @@ -61,7 +77,7 @@ public boolean validate() {
@XmlElement
public void setChargingProfileId(Integer chargingProfileId) throws PropertyConstraintException {
if (chargingProfileId == null)
throw new PropertyConstraintException("chargingProfileId", chargingProfileId);
throw new PropertyConstraintException("chargingProfileId", null);

this.chargingProfileId = chargingProfileId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,16 @@ public class ChargingSchedule implements Validatable {
private ChargingSchedulePeriod[] chargingSchedulePeriod;
private Double minChargingRate;

public ChargingSchedule() { }

public ChargingSchedule(ChargingRateUnitType chargingRateUnit, ChargingSchedulePeriod[] chargingSchedulePeriod) {
this.chargingRateUnit = chargingRateUnit;
this.chargingSchedulePeriod = chargingSchedulePeriod;
}

@Override
public boolean validate() {
boolean valid = true;
valid &= chargingRateUnit != null;
boolean valid = chargingRateUnit != null;
if (valid &= chargingSchedulePeriod != null) {
for (ChargingSchedulePeriod period : chargingSchedulePeriod)
valid &= period.validate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public class ChargingSchedulePeriod implements Validatable {
private Double limit;
private Integer numberPhases = 3;

public ChargingSchedulePeriod() { }

public ChargingSchedulePeriod(Integer startPeriod, Double limit) {
this.startPeriod = startPeriod;
this.limit = limit;
}

@Override
public boolean validate() {
boolean valid = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public class SetChargingProfileRequest implements Request {
private Integer connectorId;
private ChargingProfile chargingProfile;

public SetChargingProfileRequest() { }

public SetChargingProfileRequest(Integer connectorId, ChargingProfile chargingProfile) {
this.connectorId = connectorId;
this.chargingProfile = chargingProfile;
}

/**
* This identifies which connector of the Charge Point is used.
*
Expand Down Expand Up @@ -77,7 +84,7 @@ public ChargingProfile getChargingProfile() {
*
* @param chargingProfile the {@link ChargingProfile}.
*/
@XmlElement
@XmlElement(name = "csChargingProfiles")
public void setChargingProfile(ChargingProfile chargingProfile) {
this.chargingProfile = chargingProfile;
}
Expand Down

0 comments on commit 4ec5d13

Please sign in to comment.