Skip to content

Commit 1f871db

Browse files
committed
Refactored smart charging tests:
* seperated smart_charging_test_utils into hpp and cpp * moved redundant definitions of test cpp files into smart_charging_test_utils * implemented a couple operator== overloads for v21 types Signed-off-by: Piet Gömpel <pietgoempel@gmail.com>
1 parent 562096d commit 1f871db

7 files changed

+605
-586
lines changed

tests/lib/ocpp/v201/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ target_include_directories(libocpp_unit_tests PUBLIC
44

55
target_sources(libocpp_unit_tests PRIVATE
66
device_model_test_helper.cpp
7+
smart_charging_test_utils.cpp
78
test_charge_point.cpp
89
test_database_handler.cpp
910
test_database_migration_files.cpp

tests/lib/ocpp/v201/functional_blocks/test_reservation.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <evse_manager_fake.hpp>
88
#include <message_dispatcher_mock.hpp>
99

10+
#include <device_model_test_helper.hpp>
1011
#include <ocpp/v201/functional_blocks/reservation.hpp>
1112

1213
#include <ocpp/v201/ctrlr_component_variables.hpp>
@@ -18,9 +19,6 @@
1819
#include <ocpp/v201/messages/ReserveNow.hpp>
1920
#include <ocpp/v201/messages/Reset.hpp>
2021

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

2624
using namespace ocpp::v201;

tests/lib/ocpp/v201/functional_blocks/test_smart_charging.cpp

+3-155
Original file line numberDiff line numberDiff line change
@@ -50,33 +50,6 @@ using ::testing::ReturnRef;
5050

5151
namespace ocpp::v201 {
5252

53-
static const int NR_OF_EVSES = 2;
54-
static const int STATION_WIDE_ID = 0;
55-
static const int DEFAULT_EVSE_ID = 1;
56-
static const int DEFAULT_PROFILE_ID = 1;
57-
static const int DEFAULT_STACK_LEVEL = 1;
58-
static const int DEFAULT_REQUEST_ID = 1;
59-
static const std::string DEFAULT_TX_ID = "10c75ff7-74f5-44f5-9d01-f649f3ac7b78";
60-
const static std::string MIGRATION_FILES_PATH = "./resources/v201/device_model_migration_files";
61-
const static std::string CONFIG_PATH = "./resources/example_config/v201/component_config";
62-
const static std::string DEVICE_MODEL_DB_IN_MEMORY_PATH = "file::memory:?cache=shared";
63-
64-
class TestSmartCharging : public SmartCharging {
65-
public:
66-
using SmartCharging::add_profile;
67-
using SmartCharging::clear_profiles;
68-
using SmartCharging::get_reported_profiles;
69-
using SmartCharging::get_valid_profiles;
70-
using SmartCharging::validate_charging_station_max_profile;
71-
using SmartCharging::validate_evse_exists;
72-
using SmartCharging::validate_profile_schedules;
73-
using SmartCharging::validate_tx_default_profile;
74-
using SmartCharging::validate_tx_profile;
75-
using SmartCharging::verify_no_conflicting_external_constraints_id;
76-
77-
using SmartCharging::SmartCharging;
78-
};
79-
8053
class SmartChargingTest : public DatabaseTestingUtils {
8154
protected:
8255
void SetUp() override {
@@ -87,131 +60,6 @@ class SmartChargingTest : public DatabaseTestingUtils {
8760
this->database_handler->clear_charging_profiles();
8861
}
8962

90-
ChargingSchedule create_charge_schedule(ChargingRateUnitEnum charging_rate_unit) {
91-
ChargingSchedule charging_schedule;
92-
charging_schedule.chargingRateUnit = charging_rate_unit;
93-
return charging_schedule;
94-
}
95-
96-
ChargingSchedule create_charge_schedule(ChargingRateUnitEnum charging_rate_unit,
97-
std::vector<ChargingSchedulePeriod> charging_schedule_period,
98-
std::optional<ocpp::DateTime> start_schedule = std::nullopt) {
99-
ChargingSchedule charging_schedule;
100-
charging_schedule.chargingRateUnit = charging_rate_unit;
101-
charging_schedule.chargingSchedulePeriod = charging_schedule_period;
102-
charging_schedule.startSchedule = start_schedule;
103-
return charging_schedule;
104-
}
105-
106-
std::vector<ChargingSchedulePeriod>
107-
create_charging_schedule_periods(int32_t start_period, std::optional<int32_t> number_phases = std::nullopt,
108-
std::optional<int32_t> phase_to_use = std::nullopt) {
109-
ChargingSchedulePeriod charging_schedule_period;
110-
charging_schedule_period.startPeriod = start_period;
111-
charging_schedule_period.numberPhases = number_phases;
112-
charging_schedule_period.phaseToUse = phase_to_use;
113-
114-
return {charging_schedule_period};
115-
}
116-
117-
std::vector<ChargingSchedulePeriod> create_charging_schedule_periods(std::vector<int32_t> start_periods) {
118-
auto charging_schedule_periods = std::vector<ChargingSchedulePeriod>();
119-
for (auto start_period : start_periods) {
120-
ChargingSchedulePeriod charging_schedule_period;
121-
charging_schedule_period.startPeriod = start_period;
122-
123-
charging_schedule_periods.push_back(charging_schedule_period);
124-
}
125-
126-
return charging_schedule_periods;
127-
}
128-
129-
std::vector<ChargingSchedulePeriod>
130-
create_charging_schedule_periods_with_phases(int32_t start_period, int32_t numberPhases, int32_t phaseToUse) {
131-
ChargingSchedulePeriod charging_schedule_period;
132-
charging_schedule_period.startPeriod = start_period;
133-
charging_schedule_period.numberPhases = numberPhases;
134-
charging_schedule_period.phaseToUse = phaseToUse;
135-
136-
return {charging_schedule_period};
137-
}
138-
139-
ChargingProfile
140-
create_charging_profile(int32_t charging_profile_id, ChargingProfilePurposeEnum charging_profile_purpose,
141-
std::vector<ChargingSchedule> charging_schedules,
142-
std::optional<std::string> transaction_id = {},
143-
ChargingProfileKindEnum charging_profile_kind = ChargingProfileKindEnum::Absolute,
144-
int stack_level = DEFAULT_STACK_LEVEL, std::optional<ocpp::DateTime> validFrom = {},
145-
std::optional<ocpp::DateTime> validTo = {}) {
146-
auto recurrency_kind = RecurrencyKindEnum::Daily;
147-
ChargingProfile charging_profile;
148-
charging_profile.id = charging_profile_id;
149-
charging_profile.stackLevel = stack_level;
150-
charging_profile.chargingProfilePurpose = charging_profile_purpose;
151-
charging_profile.chargingProfileKind = charging_profile_kind;
152-
charging_profile.chargingSchedule = charging_schedules;
153-
charging_profile.customData = {};
154-
charging_profile.recurrencyKind = recurrency_kind;
155-
charging_profile.validFrom = validFrom;
156-
charging_profile.validTo = validTo;
157-
charging_profile.transactionId = transaction_id;
158-
return charging_profile;
159-
}
160-
161-
ChargingProfile
162-
create_charging_profile(int32_t charging_profile_id, ChargingProfilePurposeEnum charging_profile_purpose,
163-
ChargingSchedule charging_schedule, std::optional<std::string> transaction_id = {},
164-
ChargingProfileKindEnum charging_profile_kind = ChargingProfileKindEnum::Absolute,
165-
int stack_level = DEFAULT_STACK_LEVEL, std::optional<ocpp::DateTime> validFrom = {},
166-
std::optional<ocpp::DateTime> validTo = {}) {
167-
return create_charging_profile(charging_profile_id, charging_profile_purpose,
168-
std::vector<ChargingSchedule>{charging_schedule}, transaction_id,
169-
charging_profile_kind, stack_level, validFrom, validTo);
170-
}
171-
172-
ChargingProfileCriterion
173-
create_charging_profile_criteria(std::optional<std::vector<ocpp::CiString<20>>> sources = std::nullopt,
174-
std::optional<std::vector<int32_t>> ids = std::nullopt,
175-
std::optional<ChargingProfilePurposeEnum> purpose = std::nullopt,
176-
std::optional<int32_t> stack_level = std::nullopt) {
177-
ChargingProfileCriterion criteria;
178-
criteria.chargingLimitSource = sources;
179-
criteria.chargingProfileId = ids;
180-
criteria.chargingProfilePurpose = purpose;
181-
criteria.stackLevel = stack_level;
182-
return criteria;
183-
}
184-
185-
GetChargingProfilesRequest create_get_charging_profile_request(int32_t request_id,
186-
ChargingProfileCriterion criteria,
187-
std::optional<int32_t> evse_id = std::nullopt) {
188-
GetChargingProfilesRequest req;
189-
req.requestId = request_id;
190-
req.chargingProfile = criteria;
191-
req.evseId = evse_id;
192-
return req;
193-
}
194-
195-
ClearChargingProfileRequest
196-
create_clear_charging_profile_request(std::optional<int32_t> id = std::nullopt,
197-
std::optional<ClearChargingProfile> criteria = std::nullopt) {
198-
ClearChargingProfileRequest req;
199-
req.chargingProfileId = id;
200-
req.chargingProfileCriteria = criteria;
201-
return req;
202-
}
203-
204-
ClearChargingProfile create_clear_charging_profile(std::optional<int32_t> evse_id = std::nullopt,
205-
std::optional<ChargingProfilePurposeEnum> purpose = std::nullopt,
206-
std::optional<int32_t> stack_level = std::nullopt) {
207-
ClearChargingProfile clear_charging_profile;
208-
clear_charging_profile.customData = {};
209-
clear_charging_profile.evseId = evse_id;
210-
clear_charging_profile.chargingProfilePurpose = purpose;
211-
clear_charging_profile.stackLevel = stack_level;
212-
return clear_charging_profile;
213-
}
214-
21563
template <class T> void call_to_json(json& j, const ocpp::Call<T>& call) {
21664
j = json::array();
21765
j.push_back(ocpp::MessageTypeId::CALL);
@@ -283,7 +131,7 @@ class SmartChargingTest : public DatabaseTestingUtils {
283131
// Default values used within the tests
284132
DeviceModelTestHelper device_model_test_helper;
285133
MockMessageDispatcher mock_dispatcher;
286-
std::unique_ptr<EvseManagerFake> evse_manager = std::make_unique<EvseManagerFake>(NR_OF_EVSES);
134+
std::unique_ptr<EvseManagerFake> evse_manager = std::make_unique<EvseManagerFake>(NR_OF_TWO_EVSES);
287135

288136
sqlite3* db_handle;
289137
std::shared_ptr<DatabaseHandler> database_handler;
@@ -606,7 +454,7 @@ TEST_F(SmartChargingTest, K01FR41_IfChargingProfileKindIsRelativeAndStartSchedul
606454
}
607455

608456
TEST_F(SmartChargingTest, K01FR28_WhenEvseDoesNotExistThenReject) {
609-
auto sut = smart_charging.validate_evse_exists(NR_OF_EVSES + 1);
457+
auto sut = smart_charging.validate_evse_exists(NR_OF_TWO_EVSES + 1);
610458
EXPECT_THAT(sut, testing::Eq(ProfileValidationResultEnum::EvseDoesNotExist));
611459
}
612460

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

943-
auto sut = smart_charging.conform_and_validate_profile(profile, NR_OF_EVSES + 1);
791+
auto sut = smart_charging.conform_and_validate_profile(profile, NR_OF_TWO_EVSES + 1);
944792

945793
EXPECT_THAT(sut, testing::Eq(ProfileValidationResultEnum::EvseDoesNotExist));
946794
}

0 commit comments

Comments
 (0)