Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/v21/refactor smart charging tests #994

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/lib/ocpp/v201/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ target_include_directories(libocpp_unit_tests PUBLIC

target_sources(libocpp_unit_tests PRIVATE
device_model_test_helper.cpp
smart_charging_test_utils.cpp
test_charge_point.cpp
test_database_handler.cpp
test_database_migration_files.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <evse_manager_fake.hpp>
#include <message_dispatcher_mock.hpp>

#include <device_model_test_helper.hpp>
#include <ocpp/v201/functional_blocks/reservation.hpp>

#include <ocpp/v201/ctrlr_component_variables.hpp>
Expand All @@ -18,9 +19,6 @@
#include <ocpp/v201/messages/ReserveNow.hpp>
#include <ocpp/v201/messages/Reset.hpp>

const static std::string MIGRATION_FILES_PATH = "./resources/v201/device_model_migration_files";
const static std::string CONFIG_PATH = "./resources/example_config/v201/component_config";
const static std::string DEVICE_MODEL_DB_IN_MEMORY_PATH = "file::memory:?cache=shared";
const static uint32_t NR_OF_EVSES = 2;

using namespace ocpp::v201;
Expand Down
158 changes: 3 additions & 155 deletions tests/lib/ocpp/v201/functional_blocks/test_smart_charging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,6 @@ using ::testing::ReturnRef;

namespace ocpp::v201 {

static const int NR_OF_EVSES = 2;
static const int STATION_WIDE_ID = 0;
static const int DEFAULT_EVSE_ID = 1;
static const int DEFAULT_PROFILE_ID = 1;
static const int DEFAULT_STACK_LEVEL = 1;
static const int DEFAULT_REQUEST_ID = 1;
static const std::string DEFAULT_TX_ID = "10c75ff7-74f5-44f5-9d01-f649f3ac7b78";
const static std::string MIGRATION_FILES_PATH = "./resources/v201/device_model_migration_files";
const static std::string CONFIG_PATH = "./resources/example_config/v201/component_config";
const static std::string DEVICE_MODEL_DB_IN_MEMORY_PATH = "file::memory:?cache=shared";

class TestSmartCharging : public SmartCharging {
public:
using SmartCharging::add_profile;
using SmartCharging::clear_profiles;
using SmartCharging::get_reported_profiles;
using SmartCharging::get_valid_profiles;
using SmartCharging::validate_charging_station_max_profile;
using SmartCharging::validate_evse_exists;
using SmartCharging::validate_profile_schedules;
using SmartCharging::validate_tx_default_profile;
using SmartCharging::validate_tx_profile;
using SmartCharging::verify_no_conflicting_external_constraints_id;

using SmartCharging::SmartCharging;
};

class SmartChargingTest : public DatabaseTestingUtils {
protected:
void SetUp() override {
Expand All @@ -87,131 +60,6 @@ class SmartChargingTest : public DatabaseTestingUtils {
this->database_handler->clear_charging_profiles();
}

ChargingSchedule create_charge_schedule(ChargingRateUnitEnum charging_rate_unit) {
ChargingSchedule charging_schedule;
charging_schedule.chargingRateUnit = charging_rate_unit;
return charging_schedule;
}

ChargingSchedule create_charge_schedule(ChargingRateUnitEnum charging_rate_unit,
std::vector<ChargingSchedulePeriod> charging_schedule_period,
std::optional<ocpp::DateTime> start_schedule = std::nullopt) {
ChargingSchedule charging_schedule;
charging_schedule.chargingRateUnit = charging_rate_unit;
charging_schedule.chargingSchedulePeriod = charging_schedule_period;
charging_schedule.startSchedule = start_schedule;
return charging_schedule;
}

std::vector<ChargingSchedulePeriod>
create_charging_schedule_periods(int32_t start_period, std::optional<int32_t> number_phases = std::nullopt,
std::optional<int32_t> phase_to_use = std::nullopt) {
ChargingSchedulePeriod charging_schedule_period;
charging_schedule_period.startPeriod = start_period;
charging_schedule_period.numberPhases = number_phases;
charging_schedule_period.phaseToUse = phase_to_use;

return {charging_schedule_period};
}

std::vector<ChargingSchedulePeriod> create_charging_schedule_periods(std::vector<int32_t> start_periods) {
auto charging_schedule_periods = std::vector<ChargingSchedulePeriod>();
for (auto start_period : start_periods) {
ChargingSchedulePeriod charging_schedule_period;
charging_schedule_period.startPeriod = start_period;

charging_schedule_periods.push_back(charging_schedule_period);
}

return charging_schedule_periods;
}

std::vector<ChargingSchedulePeriod>
create_charging_schedule_periods_with_phases(int32_t start_period, int32_t numberPhases, int32_t phaseToUse) {
ChargingSchedulePeriod charging_schedule_period;
charging_schedule_period.startPeriod = start_period;
charging_schedule_period.numberPhases = numberPhases;
charging_schedule_period.phaseToUse = phaseToUse;

return {charging_schedule_period};
}

ChargingProfile
create_charging_profile(int32_t charging_profile_id, ChargingProfilePurposeEnum charging_profile_purpose,
std::vector<ChargingSchedule> charging_schedules,
std::optional<std::string> transaction_id = {},
ChargingProfileKindEnum charging_profile_kind = ChargingProfileKindEnum::Absolute,
int stack_level = DEFAULT_STACK_LEVEL, std::optional<ocpp::DateTime> validFrom = {},
std::optional<ocpp::DateTime> validTo = {}) {
auto recurrency_kind = RecurrencyKindEnum::Daily;
ChargingProfile charging_profile;
charging_profile.id = charging_profile_id;
charging_profile.stackLevel = stack_level;
charging_profile.chargingProfilePurpose = charging_profile_purpose;
charging_profile.chargingProfileKind = charging_profile_kind;
charging_profile.chargingSchedule = charging_schedules;
charging_profile.customData = {};
charging_profile.recurrencyKind = recurrency_kind;
charging_profile.validFrom = validFrom;
charging_profile.validTo = validTo;
charging_profile.transactionId = transaction_id;
return charging_profile;
}

ChargingProfile
create_charging_profile(int32_t charging_profile_id, ChargingProfilePurposeEnum charging_profile_purpose,
ChargingSchedule charging_schedule, std::optional<std::string> transaction_id = {},
ChargingProfileKindEnum charging_profile_kind = ChargingProfileKindEnum::Absolute,
int stack_level = DEFAULT_STACK_LEVEL, std::optional<ocpp::DateTime> validFrom = {},
std::optional<ocpp::DateTime> validTo = {}) {
return create_charging_profile(charging_profile_id, charging_profile_purpose,
std::vector<ChargingSchedule>{charging_schedule}, transaction_id,
charging_profile_kind, stack_level, validFrom, validTo);
}

ChargingProfileCriterion
create_charging_profile_criteria(std::optional<std::vector<ocpp::CiString<20>>> sources = std::nullopt,
std::optional<std::vector<int32_t>> ids = std::nullopt,
std::optional<ChargingProfilePurposeEnum> purpose = std::nullopt,
std::optional<int32_t> stack_level = std::nullopt) {
ChargingProfileCriterion criteria;
criteria.chargingLimitSource = sources;
criteria.chargingProfileId = ids;
criteria.chargingProfilePurpose = purpose;
criteria.stackLevel = stack_level;
return criteria;
}

GetChargingProfilesRequest create_get_charging_profile_request(int32_t request_id,
ChargingProfileCriterion criteria,
std::optional<int32_t> evse_id = std::nullopt) {
GetChargingProfilesRequest req;
req.requestId = request_id;
req.chargingProfile = criteria;
req.evseId = evse_id;
return req;
}

ClearChargingProfileRequest
create_clear_charging_profile_request(std::optional<int32_t> id = std::nullopt,
std::optional<ClearChargingProfile> criteria = std::nullopt) {
ClearChargingProfileRequest req;
req.chargingProfileId = id;
req.chargingProfileCriteria = criteria;
return req;
}

ClearChargingProfile create_clear_charging_profile(std::optional<int32_t> evse_id = std::nullopt,
std::optional<ChargingProfilePurposeEnum> purpose = std::nullopt,
std::optional<int32_t> stack_level = std::nullopt) {
ClearChargingProfile clear_charging_profile;
clear_charging_profile.customData = {};
clear_charging_profile.evseId = evse_id;
clear_charging_profile.chargingProfilePurpose = purpose;
clear_charging_profile.stackLevel = stack_level;
return clear_charging_profile;
}

template <class T> void call_to_json(json& j, const ocpp::Call<T>& call) {
j = json::array();
j.push_back(ocpp::MessageTypeId::CALL);
Expand Down Expand Up @@ -283,7 +131,7 @@ class SmartChargingTest : public DatabaseTestingUtils {
// Default values used within the tests
DeviceModelTestHelper device_model_test_helper;
MockMessageDispatcher mock_dispatcher;
std::unique_ptr<EvseManagerFake> evse_manager = std::make_unique<EvseManagerFake>(NR_OF_EVSES);
std::unique_ptr<EvseManagerFake> evse_manager = std::make_unique<EvseManagerFake>(NR_OF_TWO_EVSES);

sqlite3* db_handle;
std::shared_ptr<DatabaseHandler> database_handler;
Expand Down Expand Up @@ -606,7 +454,7 @@ TEST_F(SmartChargingTest, K01FR41_IfChargingProfileKindIsRelativeAndStartSchedul
}

TEST_F(SmartChargingTest, K01FR28_WhenEvseDoesNotExistThenReject) {
auto sut = smart_charging.validate_evse_exists(NR_OF_EVSES + 1);
auto sut = smart_charging.validate_evse_exists(NR_OF_TWO_EVSES + 1);
EXPECT_THAT(sut, testing::Eq(ProfileValidationResultEnum::EvseDoesNotExist));
}

Expand Down Expand Up @@ -940,7 +788,7 @@ TEST_F(SmartChargingTest, K01_ValidateProfile_IfEvseDoesNotExist_ThenProfileIsIn
auto profile = create_charging_profile(DEFAULT_PROFILE_ID, ChargingProfilePurposeEnum::TxProfile,
create_charge_schedule(ChargingRateUnitEnum::A), DEFAULT_TX_ID);

auto sut = smart_charging.conform_and_validate_profile(profile, NR_OF_EVSES + 1);
auto sut = smart_charging.conform_and_validate_profile(profile, NR_OF_TWO_EVSES + 1);

EXPECT_THAT(sut, testing::Eq(ProfileValidationResultEnum::EvseDoesNotExist));
}
Expand Down
Loading