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 22, 2018
1 parent 1976df1 commit b7855e7
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 19 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 <tv@chargetime.eu>
Copyright (C) 2018 Mikhail Kladkevich <kladmv@ecp-share.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
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 @@ -5,6 +5,7 @@
MIT License
Copyright (C) 2016-2018 Thomas Volden <tv@chargetime.eu>
Copyright (C) 2018 Mikhail Kladkevich <kladmv@ecp-share.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 Down
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 @@ -30,6 +30,7 @@
import eu.chargetime.ocpp.model.Confirmation;

import javax.xml.bind.annotation.XmlRootElement;
import java.util.Objects;

/**
* Sent by the Charge Point to the Central System in response to an {@link DiagnosticsStatusNotificationRequest}.
Expand All @@ -52,7 +53,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return 5;
return Objects.hash(DiagnosticsStatusNotificationConfirmation.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Objects;

/**
* Sent by the Charge Point to the Central System in response to an {@link FirmwareStatusNotificationRequest}.
Expand All @@ -53,7 +54,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return 6;
return Objects.hash(FirmwareStatusNotificationConfirmation.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Objects;

/**
* Sent by the Charge Point to the Central System in response to an {@link UpdateFirmwareRequest}.
Expand All @@ -53,7 +54,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return 7;
return Objects.hash(UpdateFirmwareConfirmation.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class UpdateFirmwareRequest implements Request {
private String location;
private Integer retries;
private Calendar retrieveDate;
private int retryInterval;
private Integer retryInterval;

public UpdateFirmwareRequest() {}

Expand Down Expand Up @@ -131,7 +131,7 @@ public void setRetrieveDate(Calendar retrieveDate) {
*
* @return int, retry interval.
*/
public int getRetryInterval() {
public Integer getRetryInterval() {
return retryInterval;
}

Expand All @@ -145,7 +145,7 @@ public int getRetryInterval() {
*
*/
@XmlElement
public void setRetryInterval(int retryInterval) throws PropertyConstraintException {
public void setRetryInterval(Integer retryInterval) throws PropertyConstraintException {
if (retryInterval <= 0)
throw new PropertyConstraintException("retryInterval", retryInterval);

Expand All @@ -162,7 +162,7 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UpdateFirmwareRequest that = (UpdateFirmwareRequest) o;
return retryInterval == that.retryInterval &&
return retryInterval.equals(that.retryInterval) &&
Objects.equals(location, that.location) &&
Objects.equals(retries, that.retries) &&
Objects.equals(retrieveDate, that.retrieveDate);
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void setup() {
}

@Test
public void validate_locationIsNotSet_returnsFalse() {
public void validate_returnFalse() {
// When
boolean result = request.validate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,33 @@ public void setup() {
}

@Test
public void validate_locationIsNotSet_returnsFalse() {
public void validate_returnFalse() {
// When
boolean result = request.validate();

// Then
assertThat(result, is(false));
}

@Test
public void validate_locationOnlyIsSet_returnsFalse() {
// Given
String aLocation = "/";
request.setLocation(aLocation);

// When
boolean result = request.validate();

// Then
assertThat(result, is(false));
}

@Test
public void validate_retrieveDateOnlyIsSet_returnsFalse() {
// Given
Calendar aRetrieveDate = Calendar.getInstance();
request.setRetrieveDate(aRetrieveDate);

// When
boolean result = request.validate();

Expand Down

0 comments on commit b7855e7

Please sign in to comment.