From 2ebf1c7b77b7693aa96dc787ca79f85f82a2a665 Mon Sep 17 00:00:00 2001 From: Christoph <367712+folkengine@users.noreply.github.com> Date: Wed, 24 Jan 2024 07:25:35 -0800 Subject: [PATCH 01/13] Added unit testing for v1.6 Smart Charging Validate Profile Signed-off-by: Christoph <367712+folkengine@users.noreply.github.com> --- tests/CMakeLists.txt | 1 + tests/database_handler_mock.hpp | 20 + tests/lib/ocpp/v16/CMakeLists.txt | 2 + .../ocpp/v16/test_smart_charging_handler.cpp | 509 ++++++++++++++++++ 4 files changed, 532 insertions(+) create mode 100644 tests/database_handler_mock.hpp create mode 100644 tests/lib/ocpp/v16/CMakeLists.txt create mode 100644 tests/lib/ocpp/v16/test_smart_charging_handler.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1cf16a0d5..15fa536fb 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -38,6 +38,7 @@ target_link_libraries(libocpp_unit_tests PRIVATE target_sources(libocpp_unit_tests PRIVATE comparators.cpp) + add_subdirectory(lib/ocpp/v16) add_subdirectory(lib/ocpp/v201) add_subdirectory(lib/ocpp/common) diff --git a/tests/database_handler_mock.hpp b/tests/database_handler_mock.hpp new file mode 100644 index 000000000..f8a873358 --- /dev/null +++ b/tests/database_handler_mock.hpp @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: Apache-2.0 + +#ifndef OCPP_DATABASE_HANDLE_MOCK_H +#define OCPP_DATABASE_HANDLE_MOCK_H + +#include +#include + +#include + +namespace ocpp { + +class DatabaseHandlerMock : public DatabaseHandler { +public: + +}; + +} // namespace ocpp + +#endif // DATABASE_HANDLE_MOCK_H \ No newline at end of file diff --git a/tests/lib/ocpp/v16/CMakeLists.txt b/tests/lib/ocpp/v16/CMakeLists.txt new file mode 100644 index 000000000..04cfe5e13 --- /dev/null +++ b/tests/lib/ocpp/v16/CMakeLists.txt @@ -0,0 +1,2 @@ +target_sources(libocpp_unit_tests PRIVATE + test_smart_charging_handler.cpp) \ No newline at end of file diff --git a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp new file mode 100644 index 000000000..d4c0b93df --- /dev/null +++ b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp @@ -0,0 +1,509 @@ +#include +#include +#include +namespace fs = std::filesystem; + +#include +#include +#include +#include + +namespace ocpp { +namespace v16 { + +/** + * Chargepoint Test Fixture + * + * Test Matrix: + * + * Positive Boundary Conditions: + * - PB01 Valid Profile + * - PB02 Valid Profile No startSchedule & handler allows no startSchedule & profile.chargingProfileKind == Absolute + * - PB03 Valid Profile No startSchedule & handler allows no startSchedule & profile.chargingProfileKind == Relative + * - PB04 Absolute ChargePointMaxProfile Profile with connector id 0 + * - PB05 Absolute TxDefaultProfile + * - PB06 Absolute TxProfile && connector transaction != nullptr && transaction_id matches SKIPPED: was not able to test + * + * Negative Boundary Conditions: + * - NB01 Valid Profile, ConnectorID gt this->connectors.size() + * - NB02 Valid Profile, ConnectorID lt 0 + * - NB03 profile.stackLevel lt 0 + * - NB04 profile.stackLevel gt profile_max_stack_level + * - NB05 profile.chargingProfileKind == Absolute && !profile.chargingSchedule.startSchedule + * - NB06 Number of installed Profiles is > max_charging_profiles_installed + * - NB07 Invalid ChargingSchedule + * - NB08 profile.chargingProfileKind == Recurring && !profile.recurrencyKind + * - NB09 profile.chargingProfileKind == Recurring && !startSchedule + * - NB10 profile.chargingProfileKind == Recurring && !startSchedule && !allow_charging_profile_without_start_schedule + * - NB11 Absolute ChargePointMaxProfile Profile with connector id not 0 + * - NB12 Absolute TxProfile connector_id == 0 + */ +class ChargepointTestFixture : public testing::Test { +protected: + void SetUp() override { + } + + ChargingSchedule createChargeSchedule() { + return ChargingSchedule{{}}; + } + + ChargingSchedule createChargeSchedule(ChargingRateUnit chargingRateUnit) { + std::vector chargingSchedulePeriod; + std::optional duration; + std::optional startSchedule; + std::optional minChargingRate; + + return ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; + } + + ChargingProfile createChargingProfile(ChargingSchedule chargingSchedule) { + auto chargingProfileId = 1; + auto stackLevel = 1; + auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; + auto chargingProfileKind = ChargingProfileKindType::Absolute; + auto recurrencyKind = RecurrencyKindType::Daily; + return ChargingProfile{ + chargingProfileId, + stackLevel, + chargingProfilePurpose, + chargingProfileKind, + chargingSchedule, + {}, // transactionId + recurrencyKind, + {}, // validFrom + {} // validTo + }; + } + + /** + * TxDefaultProfile, stack #1: time-of-day limitation to 2 kW, recurring every day from 17:00h to 20:00h. + * + * This profile is Example #1 taken from the OCPP 2.0.1 Spec Part 2, page 241. + */ + ChargingProfile createChargingProfile_Example1() { + auto chargingRateUnit = ChargingRateUnit::W; + auto chargingSchedulePeriod = std::vector{ChargingSchedulePeriod{0, 2000, 1}}; + auto duration = 1080; + auto startSchedule = ocpp::DateTime("2024-01-17T17:00:00"); + float minChargingRate = 0; + auto chargingSchedule = ChargingSchedule{ + chargingRateUnit, + chargingSchedulePeriod, + duration, + startSchedule, + minChargingRate + }; + + auto chargingProfileId = 1; + auto stackLevel = 1; + auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; + auto chargingProfileKind = ChargingProfileKindType::Absolute; + auto recurrencyKind = RecurrencyKindType::Daily; + return ChargingProfile{ + chargingProfileId, + stackLevel, + chargingProfilePurpose, + chargingProfileKind, + chargingSchedule, + {}, // transactionId + recurrencyKind, + {}, // validFrom + {} // validTo + }; + } + + /** + * TxDefaultProfile, stack #2: overruling Sundays to no limit, recurring every week starting 2020-01-05. + * + * This profile is Example #2 taken from the OCPP 2.0.1 Spec Part 2, page 241. + */ + ChargingProfile createChargingProfile_Example2() { + auto chargingRateUnit = ChargingRateUnit::W; + auto chargingSchedulePeriod = std::vector{ChargingSchedulePeriod{0, 999999, 1}}; + auto duration = 0; + auto startSchedule = ocpp::DateTime("2020-01-19T00:00:00"); + float minChargingRate = 0; + auto chargingSchedule = ChargingSchedule{ + chargingRateUnit, + chargingSchedulePeriod, + duration, + startSchedule, + minChargingRate + }; + + auto chargingProfileId = 11; + auto stackLevel = 2; + auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; + auto chargingProfileKind = ChargingProfileKindType::Recurring; + auto recurrencyKind = RecurrencyKindType::Weekly; + return ChargingProfile{ + chargingProfileId, + stackLevel, + chargingProfilePurpose, + chargingProfileKind, + chargingSchedule, + {}, // transactionId + recurrencyKind, + {}, + {} + }; + } + + SmartChargingHandler* createSmartChargingHandler() { + auto c1 = std::make_shared(Connector{1}); + connectors[1] = c1; + auto handler = new SmartChargingHandler(connectors, database_handler, true); + return handler; + } + + // Default values used within the tests + std::map> connectors; + std::shared_ptr database_handler; + + const int connector_id = 1; + bool ignore_no_transaction = true; + const int profile_max_stack_level = 1; + const int max_charging_profiles_installed = 1; + const int charging_schedule_max_periods = 1; +}; + +/** + * PB01 Valid Profile + */ +TEST_F(ChargepointTestFixture, ValidateProfile) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(); + + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_TRUE(sut); +} + +/** + * PB01 Valid Profile: Example 1 + * + * This example is taken from the OCPP 2.0.1 Spec page. 241 + */ +TEST_F(ChargepointTestFixture, ValidateProfile__example1) { + auto profile = createChargingProfile_Example1(); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::W}; + auto handler = createSmartChargingHandler(); + + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_TRUE(sut); +} + +/** + * NB01 Valid Profile, ConnectorID gt this->connectors.size() + */ +TEST_F(ChargepointTestFixture, ValidateProfile__ConnectorIdGreaterThanConnectorsSize__ReturnsFalse) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(); + + const int connector_id = INT_MAX; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_FALSE(sut); +} + +/** + * NB02 Valid Profile, ConnectorID lt 0 + */ +TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NegativeConnectorIdTest__ReturnsFalse) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(); + + const int connector_id = -1; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_FALSE(sut); +} + +/** + * NB03 profile.stackLevel lt 0 + */ +TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ConnectorIdZero__ReturnsFalse) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(); + + profile.stackLevel = -1; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_FALSE(sut); +} + +/** + * NB04 profile.stackLevel gt this->profile_max_stack_level + */ +TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_StackLevelGreaterThanMaxStackLevel__ReturnsFalse) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(); + + profile.stackLevel = profile_max_stack_level + 1; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_FALSE(sut); +} + +/** + * NB05 profile.chargingProfileKind == Absolute && !profile.chargingSchedule.startSchedule +*/ +TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ChargingProfileKindAbsoluteNoStartSchedule__ReturnsFalse) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + // Create a SmartChargingHandler where allow_charging_profile_without_start_schedule is set to false + auto c1 = std::make_shared(Connector{1}); + connectors[1] = c1; + auto allow_charging_profile_without_start_schedule = false; + auto handler = new SmartChargingHandler(connectors, database_handler, allow_charging_profile_without_start_schedule); + + profile.chargingProfileKind = ChargingProfileKindType::Absolute; + profile.chargingSchedule.startSchedule = std::nullopt; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_FALSE(sut); +} + +/** + * PB02 Valid Profile No startSchedule & handler allows no startSchedule +*/ +TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_AllowsNoStartSchedule__ReturnsTrue) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + // Create a SmartChargingHandler where allow_charging_profile_without_start_schedule is set to false + auto handler = createSmartChargingHandler(); + + // Configure to have no start schedule + profile.chargingProfileKind = ChargingProfileKindType::Absolute; + profile.chargingSchedule.startSchedule = std::nullopt; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_TRUE(sut); +} + +/** + * NB06 Number of installed Profiles is > max_charging_profiles_installed + */ +TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_InstalledProfilesGreaterThanMaxInstalledProfiles__ReturnsFalse) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(); + + const int max_charging_profiles_installed = 0; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_FALSE(sut); +} + +/** + * NB07 Invalid ChargingSchedule + * + * Creating a ChargingProfile with a different ChargingRateUnit + */ +TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_InvalidChargingSchedule__ReturnsFalse) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + profile.chargingSchedule.chargingSchedulePeriod = std::vector{}; + auto handler = createSmartChargingHandler(); + + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::W}; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_FALSE(sut); +} + +/** + * NB08 profile.chargingProfileKind == Recurring && !profile.recurrencyKind + */ +TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ChargingProfileKindRecurringNoRecurrencyKind__ReturnsFalse) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(); + + profile.chargingProfileKind = ChargingProfileKindType::Recurring; + profile.recurrencyKind = std::nullopt; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_FALSE(sut); +} + +/** + * NB09 profile.chargingProfileKind == Recurring && !profile.chargingSchedule.startSchedule + */ +TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ChargingProfileKindRecurringNoStartSchedule__ReturnsFalse) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + // Create a SmartChargingHandler where allow_charging_profile_without_start_schedule is set to false + auto c1 = std::make_shared(Connector{1}); + connectors[1] = c1; + auto allow_charging_profile_without_start_schedule = false; + auto handler = new SmartChargingHandler(connectors, database_handler, allow_charging_profile_without_start_schedule); + + profile.chargingProfileKind = ChargingProfileKindType::Recurring; + profile.chargingSchedule.startSchedule = std::nullopt; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_FALSE(sut); +} + +/** + * PB03 Valid Profile No startSchedule & handler allows no startSchedule & profile.chargingProfileKind == Relative + */ +TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NoStartScheduleAllowedRelative__ReturnsTrue) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(); + + profile.chargingProfileKind = ChargingProfileKindType::Recurring; + profile.chargingSchedule.startSchedule = std::nullopt; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_TRUE(sut); +} + +/** + * NB10 profile.chargingProfileKind == Recurring && !startSchedule && !allow_charging_profile_without_start_schedule + */ +TEST_F(ChargepointTestFixture, ValidateProfile__RecurringNoStartScheduleNotAllowed__ReturnsFalse) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto c1 = std::make_shared(Connector{1}); + connectors[1] = c1; + auto allow_charging_profile_without_start_schedule = false; + auto handler = new SmartChargingHandler(connectors, database_handler, allow_charging_profile_without_start_schedule); + + profile.chargingProfileKind = ChargingProfileKindType::Recurring; + profile.chargingSchedule.startSchedule = std::nullopt; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_FALSE(sut); +} + +/** + * PB04 Absolute ChargePointMaxProfile Profile with connector id 0 + * + * Absolute ChargePointMaxProfile Profile need a connector id of 0 + */ +TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NotRecurrencyKindConnectorId0__ReturnsFalse) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(); + + profile.chargingProfilePurpose = ChargingProfilePurposeType::ChargePointMaxProfile; + profile.chargingProfileKind = ChargingProfileKindType::Absolute; + const int connector_id = 0; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_TRUE(sut); +} + +/** + * NB11 Absolute ChargePointMaxProfile Profile with connector id not 0 + * + * ChargePointMaxProfile Profiles where chargingProfileKind == Absolute need a connector id of 0 and not had a + * ChargingProfileKindType of Relative + */ +TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NotRecurrencyKindConnectorIdNot0__ReturnsFalse) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(); + + profile.chargingProfilePurpose = ChargingProfilePurposeType::ChargePointMaxProfile; + profile.chargingProfileKind = ChargingProfileKindType::Absolute; + const int connector_id = 1; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_FALSE(sut); +} + +/** + * PB05 Absolute TxDefaultProfile + */ +TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfileTxDefaultProfile__ReturnsTrue) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(); + + profile.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; + profile.chargingProfileKind = ChargingProfileKindType::Absolute; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_TRUE(sut); +} + +/** + * PB05 Absolute TxProfile ignore_no_transaction == true + */ +TEST_F(ChargepointTestFixture, ValidateProfile__AbsoluteTxProfileIgnoreNoTransaction__ReturnsTrue) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(); + + profile.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; + profile.chargingProfileKind = ChargingProfileKindType::Absolute; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_TRUE(sut); +} + +/** + * NB12 Absolute TxProfile connector_id == 0 + */ +TEST_F(ChargepointTestFixture, ValidateProfile__AbsoluteTxProfileConnectorId0__ReturnsFalse) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(); + + profile.chargingProfileKind = ChargingProfileKindType::Absolute; + profile.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; + const int connector_id = 0; + bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + + ASSERT_FALSE(sut); +} + +} // namespace v16 +} // namespace ocpp \ No newline at end of file From fab5233304b6cff1d61e5894d822daa9a21620c1 Mon Sep 17 00:00:00 2001 From: Christoph <367712+folkengine@users.noreply.github.com> Date: Wed, 24 Jan 2024 07:52:31 -0800 Subject: [PATCH 02/13] Cleaned up test names Signed-off-by: Christoph <367712+folkengine@users.noreply.github.com> --- tests/lib/ocpp/v16/test_smart_charging_handler.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp index d4c0b93df..8694df5fa 100644 --- a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp +++ b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp @@ -22,7 +22,8 @@ namespace v16 { * - PB03 Valid Profile No startSchedule & handler allows no startSchedule & profile.chargingProfileKind == Relative * - PB04 Absolute ChargePointMaxProfile Profile with connector id 0 * - PB05 Absolute TxDefaultProfile - * - PB06 Absolute TxProfile && connector transaction != nullptr && transaction_id matches SKIPPED: was not able to test + * - PB06 Absolute TxProfile ignore_no_transaction == true + * - PB07 Absolute TxProfile && connector transaction != nullptr && transaction_id matches SKIPPED: was not able to test * * Negative Boundary Conditions: * - NB01 Valid Profile, ConnectorID gt this->connectors.size() @@ -169,6 +170,8 @@ class ChargepointTestFixture : public testing::Test { /** * PB01 Valid Profile + * + * Happy path simple test */ TEST_F(ChargepointTestFixture, ValidateProfile) { auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); @@ -417,7 +420,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__RecurringNoStartScheduleNotAllow * * Absolute ChargePointMaxProfile Profile need a connector id of 0 */ -TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NotRecurrencyKindConnectorId0__ReturnsFalse) { +TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NotRecurrencyKindConnectorId0__ReturnsTrue) { auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); @@ -471,7 +474,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfileTxDefaultProfile__Re } /** - * PB05 Absolute TxProfile ignore_no_transaction == true + * PB06 Absolute TxProfile ignore_no_transaction == true */ TEST_F(ChargepointTestFixture, ValidateProfile__AbsoluteTxProfileIgnoreNoTransaction__ReturnsTrue) { auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); From 1aa9f96bb4f652e0af7f8160dad503fd9ad83d73 Mon Sep 17 00:00:00 2001 From: Christoph <367712+folkengine@users.noreply.github.com> Date: Wed, 24 Jan 2024 07:58:57 -0800 Subject: [PATCH 03/13] Cleaned up tests/CMakeLists.txt Signed-off-by: Christoph <367712+folkengine@users.noreply.github.com> --- tests/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 15fa536fb..e4c3bd672 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -38,7 +38,7 @@ target_link_libraries(libocpp_unit_tests PRIVATE target_sources(libocpp_unit_tests PRIVATE comparators.cpp) - add_subdirectory(lib/ocpp/v16) +add_subdirectory(lib/ocpp/v16) add_subdirectory(lib/ocpp/v201) add_subdirectory(lib/ocpp/common) From 5613133f5cb6728082dfecf28f9915c1652e3611 Mon Sep 17 00:00:00 2001 From: Coury Richards <146002925+couryrr-afs@users.noreply.github.com> Date: Wed, 24 Jan 2024 12:32:55 -0500 Subject: [PATCH 04/13] Inital commit with database_handler_mock Signed-off-by: Coury Richards <146002925+couryrr-afs@users.noreply.github.com> Signed-off-by: Christoph <367712+folkengine@users.noreply.github.com> --- include/ocpp/v16/database_handler.hpp | 4 +- tests/database_handler_mock.hpp | 8 +- .../ocpp/v16/test_smart_charging_handler.cpp | 215 +++++++++++------- 3 files changed, 142 insertions(+), 85 deletions(-) diff --git a/include/ocpp/v16/database_handler.hpp b/include/ocpp/v16/database_handler.hpp index fc64755a8..a819a6c0b 100644 --- a/include/ocpp/v16/database_handler.hpp +++ b/include/ocpp/v16/database_handler.hpp @@ -133,10 +133,10 @@ class DatabaseHandler : public ocpp::common::DatabaseHandlerBase { bool clear_local_authorization_list(); /// \brief Inserts or updates the given \p profile to CHARGING_PROFILES table - void insert_or_update_charging_profile(const int connector_id, const v16::ChargingProfile& profile); + virtual void insert_or_update_charging_profile(const int connector_id, const v16::ChargingProfile& profile); /// \brief Deletes the profile with the given \p profile_id - void delete_charging_profile(const int profile_id); + virtual void delete_charging_profile(const int profile_id); /// \brief Deletes all profiles from table CHARGING_PROFILES void delete_charging_profiles(); diff --git a/tests/database_handler_mock.hpp b/tests/database_handler_mock.hpp index f8a873358..8d7039968 100644 --- a/tests/database_handler_mock.hpp +++ b/tests/database_handler_mock.hpp @@ -10,9 +10,13 @@ namespace ocpp { -class DatabaseHandlerMock : public DatabaseHandler { +class DatabaseHandlerMock : public v16::DatabaseHandler { public: - + DatabaseHandlerMock(const std::string& chargepoint_id, const fs::path& database_path, + const fs::path& init_script_path) : + DatabaseHandler(chargepoint_id, database_path, init_script_path){}; + MOCK_METHOD(void, insert_or_update_charging_profile, (const int, const v16::ChargingProfile&), (override)); + MOCK_METHOD(void, delete_charging_profile, (const int profile_id), (override)); }; } // namespace ocpp diff --git a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp index 8694df5fa..3d73f4766 100644 --- a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp +++ b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp @@ -3,28 +3,30 @@ #include namespace fs = std::filesystem; +#include #include #include #include #include + namespace ocpp { namespace v16 { /** - * Chargepoint Test Fixture + * Chargepoint Test Fixture * * Test Matrix: - * + * * Positive Boundary Conditions: * - PB01 Valid Profile * - PB02 Valid Profile No startSchedule & handler allows no startSchedule & profile.chargingProfileKind == Absolute * - PB03 Valid Profile No startSchedule & handler allows no startSchedule & profile.chargingProfileKind == Relative * - PB04 Absolute ChargePointMaxProfile Profile with connector id 0 - * - PB05 Absolute TxDefaultProfile + * - PB05 Absolute TxDefaultProfile * - PB06 Absolute TxProfile ignore_no_transaction == true * - PB07 Absolute TxProfile && connector transaction != nullptr && transaction_id matches SKIPPED: was not able to test - * + * * Negative Boundary Conditions: * - NB01 Valid Profile, ConnectorID gt this->connectors.size() * - NB02 Valid Profile, ConnectorID lt 0 @@ -37,7 +39,7 @@ namespace v16 { * - NB09 profile.chargingProfileKind == Recurring && !startSchedule * - NB10 profile.chargingProfileKind == Recurring && !startSchedule && !allow_charging_profile_without_start_schedule * - NB11 Absolute ChargePointMaxProfile Profile with connector id not 0 - * - NB12 Absolute TxProfile connector_id == 0 + * - NB12 Absolute TxProfile connector_id == 0 */ class ChargepointTestFixture : public testing::Test { protected: @@ -69,16 +71,16 @@ class ChargepointTestFixture : public testing::Test { chargingProfilePurpose, chargingProfileKind, chargingSchedule, - {}, // transactionId + {}, // transactionId recurrencyKind, - {}, // validFrom - {} // validTo + {}, // validFrom + {} // validTo }; } /** * TxDefaultProfile, stack #1: time-of-day limitation to 2 kW, recurring every day from 17:00h to 20:00h. - * + * * This profile is Example #1 taken from the OCPP 2.0.1 Spec Part 2, page 241. */ ChargingProfile createChargingProfile_Example1() { @@ -87,13 +89,8 @@ class ChargepointTestFixture : public testing::Test { auto duration = 1080; auto startSchedule = ocpp::DateTime("2024-01-17T17:00:00"); float minChargingRate = 0; - auto chargingSchedule = ChargingSchedule{ - chargingRateUnit, - chargingSchedulePeriod, - duration, - startSchedule, - minChargingRate - }; + auto chargingSchedule = + ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; auto chargingProfileId = 1; auto stackLevel = 1; @@ -106,16 +103,16 @@ class ChargepointTestFixture : public testing::Test { chargingProfilePurpose, chargingProfileKind, chargingSchedule, - {}, // transactionId + {}, // transactionId recurrencyKind, - {}, // validFrom - {} // validTo + {}, // validFrom + {} // validTo }; } /** * TxDefaultProfile, stack #2: overruling Sundays to no limit, recurring every week starting 2020-01-05. - * + * * This profile is Example #2 taken from the OCPP 2.0.1 Spec Part 2, page 241. */ ChargingProfile createChargingProfile_Example2() { @@ -124,36 +121,37 @@ class ChargepointTestFixture : public testing::Test { auto duration = 0; auto startSchedule = ocpp::DateTime("2020-01-19T00:00:00"); float minChargingRate = 0; - auto chargingSchedule = ChargingSchedule{ - chargingRateUnit, - chargingSchedulePeriod, - duration, - startSchedule, - minChargingRate - }; + auto chargingSchedule = + ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; auto chargingProfileId = 11; auto stackLevel = 2; auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; auto chargingProfileKind = ChargingProfileKindType::Recurring; auto recurrencyKind = RecurrencyKindType::Weekly; - return ChargingProfile{ - chargingProfileId, - stackLevel, - chargingProfilePurpose, - chargingProfileKind, - chargingSchedule, - {}, // transactionId - recurrencyKind, - {}, - {} - }; + return ChargingProfile{chargingProfileId, + stackLevel, + chargingProfilePurpose, + chargingProfileKind, + chargingSchedule, + {}, // transactionId + recurrencyKind, + {}, + {}}; } SmartChargingHandler* createSmartChargingHandler() { - auto c1 = std::make_shared(Connector{1}); - connectors[1] = c1; + connectors[0] = std::make_shared(Connector{1}); + + const std::string chargepoint_id = "1"; + const fs::path database_path = "na"; + const fs::path init_script_path = "na"; + + std::shared_ptr database_handler = + std::make_shared(chargepoint_id, database_path, init_script_path); + auto handler = new SmartChargingHandler(connectors, database_handler, true); + return handler; } @@ -187,7 +185,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile) { /** * PB01 Valid Profile: Example 1 - * + * * This example is taken from the OCPP 2.0.1 Spec page. 241 */ TEST_F(ChargepointTestFixture, ValidateProfile__example1) { @@ -225,7 +223,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NegativeConnectorId auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); - + const int connector_id = -1; bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, max_charging_profiles_installed, charging_schedule_max_periods, @@ -267,8 +265,8 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_StackLevelGreaterTh } /** - * NB05 profile.chargingProfileKind == Absolute && !profile.chargingSchedule.startSchedule -*/ + * NB05 profile.chargingProfileKind == Absolute && !profile.chargingSchedule.startSchedule + */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ChargingProfileKindAbsoluteNoStartSchedule__ReturnsFalse) { auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; @@ -276,20 +274,21 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ChargingProfileKind auto c1 = std::make_shared(Connector{1}); connectors[1] = c1; auto allow_charging_profile_without_start_schedule = false; - auto handler = new SmartChargingHandler(connectors, database_handler, allow_charging_profile_without_start_schedule); + auto handler = + new SmartChargingHandler(connectors, database_handler, allow_charging_profile_without_start_schedule); profile.chargingProfileKind = ChargingProfileKindType::Absolute; profile.chargingSchedule.startSchedule = std::nullopt; bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, max_charging_profiles_installed, charging_schedule_max_periods, charging_schedule_allowed_charging_rate_units); - + ASSERT_FALSE(sut); } /** * PB02 Valid Profile No startSchedule & handler allows no startSchedule -*/ + */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_AllowsNoStartSchedule__ReturnsTrue) { auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; @@ -302,14 +301,15 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_AllowsNoStartSchedu bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, max_charging_profiles_installed, charging_schedule_max_periods, charging_schedule_allowed_charging_rate_units); - + ASSERT_TRUE(sut); } /** * NB06 Number of installed Profiles is > max_charging_profiles_installed */ -TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_InstalledProfilesGreaterThanMaxInstalledProfiles__ReturnsFalse) { +TEST_F(ChargepointTestFixture, + ValidateProfile__ValidProfile_InstalledProfilesGreaterThanMaxInstalledProfiles__ReturnsFalse) { auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); @@ -318,13 +318,13 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_InstalledProfilesGr bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, max_charging_profiles_installed, charging_schedule_max_periods, charging_schedule_allowed_charging_rate_units); - + ASSERT_FALSE(sut); } /** * NB07 Invalid ChargingSchedule - * + * * Creating a ChargingProfile with a different ChargingRateUnit */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_InvalidChargingSchedule__ReturnsFalse) { @@ -334,16 +334,17 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_InvalidChargingSche const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::W}; bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, + max_charging_profiles_installed, charging_schedule_max_periods, charging_schedule_allowed_charging_rate_units); - + ASSERT_FALSE(sut); } /** - * NB08 profile.chargingProfileKind == Recurring && !profile.recurrencyKind + * NB08 profile.chargingProfileKind == Recurring && !profile.recurrencyKind */ -TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ChargingProfileKindRecurringNoRecurrencyKind__ReturnsFalse) { +TEST_F(ChargepointTestFixture, + ValidateProfile__ValidProfile_ChargingProfileKindRecurringNoRecurrencyKind__ReturnsFalse) { auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); @@ -351,30 +352,32 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ChargingProfileKind profile.chargingProfileKind = ChargingProfileKindType::Recurring; profile.recurrencyKind = std::nullopt; bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, + max_charging_profiles_installed, charging_schedule_max_periods, charging_schedule_allowed_charging_rate_units); - + ASSERT_FALSE(sut); } /** * NB09 profile.chargingProfileKind == Recurring && !profile.chargingSchedule.startSchedule */ -TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ChargingProfileKindRecurringNoStartSchedule__ReturnsFalse) { +TEST_F(ChargepointTestFixture, + ValidateProfile__ValidProfile_ChargingProfileKindRecurringNoStartSchedule__ReturnsFalse) { auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; // Create a SmartChargingHandler where allow_charging_profile_without_start_schedule is set to false auto c1 = std::make_shared(Connector{1}); connectors[1] = c1; auto allow_charging_profile_without_start_schedule = false; - auto handler = new SmartChargingHandler(connectors, database_handler, allow_charging_profile_without_start_schedule); + auto handler = + new SmartChargingHandler(connectors, database_handler, allow_charging_profile_without_start_schedule); profile.chargingProfileKind = ChargingProfileKindType::Recurring; profile.chargingSchedule.startSchedule = std::nullopt; bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, + max_charging_profiles_installed, charging_schedule_max_periods, charging_schedule_allowed_charging_rate_units); - + ASSERT_FALSE(sut); } @@ -389,9 +392,9 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NoStartScheduleAllo profile.chargingProfileKind = ChargingProfileKindType::Recurring; profile.chargingSchedule.startSchedule = std::nullopt; bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, + max_charging_profiles_installed, charging_schedule_max_periods, charging_schedule_allowed_charging_rate_units); - + ASSERT_TRUE(sut); } @@ -404,20 +407,21 @@ TEST_F(ChargepointTestFixture, ValidateProfile__RecurringNoStartScheduleNotAllow auto c1 = std::make_shared(Connector{1}); connectors[1] = c1; auto allow_charging_profile_without_start_schedule = false; - auto handler = new SmartChargingHandler(connectors, database_handler, allow_charging_profile_without_start_schedule); + auto handler = + new SmartChargingHandler(connectors, database_handler, allow_charging_profile_without_start_schedule); profile.chargingProfileKind = ChargingProfileKindType::Recurring; profile.chargingSchedule.startSchedule = std::nullopt; bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, + max_charging_profiles_installed, charging_schedule_max_periods, charging_schedule_allowed_charging_rate_units); - + ASSERT_FALSE(sut); } /** * PB04 Absolute ChargePointMaxProfile Profile with connector id 0 - * + * * Absolute ChargePointMaxProfile Profile need a connector id of 0 */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NotRecurrencyKindConnectorId0__ReturnsTrue) { @@ -429,16 +433,16 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NotRecurrencyKindCo profile.chargingProfileKind = ChargingProfileKindType::Absolute; const int connector_id = 0; bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, + max_charging_profiles_installed, charging_schedule_max_periods, charging_schedule_allowed_charging_rate_units); - + ASSERT_TRUE(sut); } /** * NB11 Absolute ChargePointMaxProfile Profile with connector id not 0 - * - * ChargePointMaxProfile Profiles where chargingProfileKind == Absolute need a connector id of 0 and not had a + * + * ChargePointMaxProfile Profiles where chargingProfileKind == Absolute need a connector id of 0 and not had a * ChargingProfileKindType of Relative */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NotRecurrencyKindConnectorIdNot0__ReturnsFalse) { @@ -450,14 +454,14 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NotRecurrencyKindCo profile.chargingProfileKind = ChargingProfileKindType::Absolute; const int connector_id = 1; bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, + max_charging_profiles_installed, charging_schedule_max_periods, charging_schedule_allowed_charging_rate_units); - + ASSERT_FALSE(sut); } /** - * PB05 Absolute TxDefaultProfile + * PB05 Absolute TxDefaultProfile */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfileTxDefaultProfile__ReturnsTrue) { auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); @@ -467,9 +471,9 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfileTxDefaultProfile__Re profile.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; profile.chargingProfileKind = ChargingProfileKindType::Absolute; bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, + max_charging_profiles_installed, charging_schedule_max_periods, charging_schedule_allowed_charging_rate_units); - + ASSERT_TRUE(sut); } @@ -484,29 +488,78 @@ TEST_F(ChargepointTestFixture, ValidateProfile__AbsoluteTxProfileIgnoreNoTransac profile.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; profile.chargingProfileKind = ChargingProfileKindType::Absolute; bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, + max_charging_profiles_installed, charging_schedule_max_periods, charging_schedule_allowed_charging_rate_units); - + ASSERT_TRUE(sut); } /** - * NB12 Absolute TxProfile connector_id == 0 + * NB12 Absolute TxProfile connector_id == 0 */ TEST_F(ChargepointTestFixture, ValidateProfile__AbsoluteTxProfileConnectorId0__ReturnsFalse) { auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); - + profile.chargingProfileKind = ChargingProfileKindType::Absolute; profile.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; const int connector_id = 0; bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, + max_charging_profiles_installed, charging_schedule_max_periods, charging_schedule_allowed_charging_rate_units); ASSERT_FALSE(sut); } +/** + * + * 2. Testing the branches within clear_all_profiles_with_filter ClearAllProfilesWithFilter + * + */ + +/** + * NB + */ +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__AllOptionalsEmpty_DoNotCheckIdOnly__ReturnsFalse) { + auto handler = createSmartChargingHandler(); + + bool sut = handler->clear_all_profiles_with_filter({}, {}, {}, {}, false); + + ASSERT_FALSE(sut); +} + +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__AllOptionalsEmpty_CheckIdOnly__ReturnsFalse) { + auto handler = createSmartChargingHandler(); + + bool sut = handler->clear_all_profiles_with_filter({}, {}, {}, {}, true); + + ASSERT_FALSE(sut); +} + +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__OnlyOneMatchingProfileId_CheckIdOnly__ReturnsTrue) { + + auto handler = createSmartChargingHandler(); + + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + + handler->add_charge_point_max_profile(profile); + + bool sut = handler->clear_all_profiles_with_filter(1, {}, {}, {}, true); + + ASSERT_TRUE(sut); +} + +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__NoMatchingProfileId_CheckIdOnly__ReturnsFalse) { + auto handler = createSmartChargingHandler(); + + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + + handler->add_charge_point_max_profile(profile); + + bool sut = handler->clear_all_profiles_with_filter(2, {}, {}, {}, true); + + ASSERT_FALSE(sut); +} } // namespace v16 } // namespace ocpp \ No newline at end of file From b19fcc347d3e3fe1cab9abb15d346cec92e91737 Mon Sep 17 00:00:00 2001 From: Christoph <367712+folkengine@users.noreply.github.com> Date: Wed, 24 Jan 2024 16:26:35 -0800 Subject: [PATCH 05/13] init Signed-off-by: Christoph <367712+folkengine@users.noreply.github.com> --- tests/lib/ocpp/v16/test_smart_charging_handler.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp index 3d73f4766..1b036f3be 100644 --- a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp +++ b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp @@ -561,5 +561,9 @@ TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__NoMatchingProfileId_C ASSERT_FALSE(sut); } +/** + * SmartChargingHandler::add_charge_point_max_profile tests + */ + } // namespace v16 } // namespace ocpp \ No newline at end of file From 4b2f25619d763cfe07c41d936b77846d7006d7cd Mon Sep 17 00:00:00 2001 From: Christoph <367712+folkengine@users.noreply.github.com> Date: Thu, 25 Jan 2024 10:26:25 -0800 Subject: [PATCH 06/13] Added AddChargePointMaxProfile test Signed-off-by: Christoph <367712+folkengine@users.noreply.github.com> --- .../ocpp/v16/test_smart_charging_handler.cpp | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp index 1b036f3be..1e7468524 100644 --- a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp +++ b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp @@ -564,6 +564,26 @@ TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__NoMatchingProfileId_C /** * SmartChargingHandler::add_charge_point_max_profile tests */ + TEST_F(ChargepointTestFixture, AddChargePointMaxProfile) { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(); + profile.chargingProfilePurpose = ChargingProfilePurposeType::ChargePointMaxProfile; + profile.chargingProfileKind = ChargingProfileKindType::Absolute; + const int connector_id = 0; + bool is_profile_valid = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + ASSERT_TRUE(is_profile_valid); + + handler->add_charge_point_max_profile(profile); + auto valid_profiles = handler->get_valid_profiles({}, {}, 0); + auto retrieved = valid_profiles[0]; + + ASSERT_EQ(1, valid_profiles.size()); + ASSERT_EQ(ChargingProfilePurposeType::ChargePointMaxProfile, retrieved.chargingProfilePurpose); + ASSERT_EQ(ChargingProfileKindType::Absolute, retrieved.chargingProfileKind); +} } // namespace v16 } // namespace ocpp \ No newline at end of file From 8ae6ed3ac59fb3ffebfd09fed81a8bf0fc71ccb9 Mon Sep 17 00:00:00 2001 From: Christoph <367712+folkengine@users.noreply.github.com> Date: Wed, 31 Jan 2024 11:44:56 -0800 Subject: [PATCH 07/13] Added AddTxDefaultProfile tests Signed-off-by: Christoph <367712+folkengine@users.noreply.github.com> --- .../ocpp/v16/test_smart_charging_handler.cpp | 170 +++++++++++++++++- 1 file changed, 161 insertions(+), 9 deletions(-) diff --git a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp index 1e7468524..c6613a409 100644 --- a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp +++ b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp @@ -46,6 +46,16 @@ class ChargepointTestFixture : public testing::Test { void SetUp() override { } + // Adding transactiosn to the connectors because Profiles other than ChargePointMaxProfile need it in order to be valid. + void addConnector(int id) { + auto connector = Connector{id}; + + auto timer = std::unique_ptr(); + + connector.transaction = std::make_shared(id, "test", "test", 1, std::nullopt, ocpp::DateTime(), std::move(timer)); + connectors[id] = std::make_shared(connector); + } + ChargingSchedule createChargeSchedule() { return ChargingSchedule{{}}; } @@ -141,7 +151,27 @@ class ChargepointTestFixture : public testing::Test { } SmartChargingHandler* createSmartChargingHandler() { - connectors[0] = std::make_shared(Connector{1}); + // connectors[0] = std::make_shared(Connector{0}); + // connectors[1] = std::make_shared(Connector{1}); + + // const std::string chargepoint_id = "1"; + // const fs::path database_path = "na"; + // const fs::path init_script_path = "na"; + + // std::shared_ptr database_handler = + // std::make_shared(chargepoint_id, database_path, init_script_path); + + // auto handler = new SmartChargingHandler(connectors, database_handler, true); + + return createSmartChargingHandler(0); + } + + // + SmartChargingHandler* createSmartChargingHandler(const int number_of_connectors) { + for (int i = 0; i <= number_of_connectors; i++) { + // connectors[i] = std::make_shared(Connector{i}); + addConnector(i); + } const std::string chargepoint_id = "1"; const fs::path database_path = "na"; @@ -155,15 +185,33 @@ class ChargepointTestFixture : public testing::Test { return handler; } + + SmartChargingHandler* createSmartChargingHandlerWithChargePointMaxProfile() { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(10); + + profile.chargingProfilePurpose = ChargingProfilePurposeType::ChargePointMaxProfile; + profile.chargingProfileKind = ChargingProfileKindType::Absolute; + const int connector_id = 0; + const int max_charging_profiles_installed = 10; + bool is_profile_valid = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + handler->add_charge_point_max_profile(profile); + + return handler; + } + // Default values used within the tests std::map> connectors; std::shared_ptr database_handler; const int connector_id = 1; bool ignore_no_transaction = true; - const int profile_max_stack_level = 1; - const int max_charging_profiles_installed = 1; - const int charging_schedule_max_periods = 1; + const int profile_max_stack_level = 10; + const int max_charging_profiles_installed = 20; + const int charging_schedule_max_periods = 10; }; /** @@ -564,13 +612,26 @@ TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__NoMatchingProfileId_C /** * SmartChargingHandler::add_charge_point_max_profile tests */ - TEST_F(ChargepointTestFixture, AddChargePointMaxProfile) { +TEST_F(ChargepointTestFixture, AddChargePointMaxProfile) { + auto handler = createSmartChargingHandlerWithChargePointMaxProfile(); + + auto valid_profiles = handler->get_valid_profiles({}, {}, 0); + auto retrieved = valid_profiles[0]; + + ASSERT_EQ(1, valid_profiles.size()); + ASSERT_EQ(ChargingProfilePurposeType::ChargePointMaxProfile, retrieved.chargingProfilePurpose); + ASSERT_EQ(ChargingProfileKindType::Absolute, retrieved.chargingProfileKind); +} + +/** + * SmartChargingHandler::add_charge_point_max_profile tests + * + * The add_charge_point_max_profile method accepts a profile that is not a ChargePointMaxProfile. + */ + TEST_F(ChargepointTestFixture, AddChargePointMaxProfile__InvalidProfileType__ShouldNotWorkButDoes) { auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); - profile.chargingProfilePurpose = ChargingProfilePurposeType::ChargePointMaxProfile; - profile.chargingProfileKind = ChargingProfileKindType::Absolute; - const int connector_id = 0; bool is_profile_valid = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, max_charging_profiles_installed, charging_schedule_max_periods, charging_schedule_allowed_charging_rate_units); @@ -581,9 +642,100 @@ TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__NoMatchingProfileId_C auto retrieved = valid_profiles[0]; ASSERT_EQ(1, valid_profiles.size()); - ASSERT_EQ(ChargingProfilePurposeType::ChargePointMaxProfile, retrieved.chargingProfilePurpose); + ASSERT_EQ(ChargingProfilePurposeType::TxDefaultProfile, retrieved.chargingProfilePurpose); + ASSERT_EQ(ChargingProfileKindType::Absolute, retrieved.chargingProfileKind); +} + +TEST_F(ChargepointTestFixture, AddTxDefaultProfile_ConnectorId_eq_0) { + // GTEST_SKIP(); + std::cout << ">>> AddTxDefaultProfile_ConnectorId_gt_0\n"; + auto handler = createSmartChargingHandler(1); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + bool is_profile_valid = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + ASSERT_TRUE(is_profile_valid); + + const int connector_id = 0; + handler->add_tx_default_profile(profile, connector_id); + // While the connector id is 0 when it is added, it is retrieved with a connector id of 1 + // See AddTxDefaultProfile_ConnectorId_eq_0_Retrieved_at_0__NoProfilesReturned for a demonstration of this behavior + const int retrieved_connector_id = 1; + auto valid_profiles = handler->get_valid_profiles({}, {}, retrieved_connector_id); + auto retrieved = valid_profiles[0]; + + ASSERT_EQ(1, valid_profiles.size()); ASSERT_EQ(ChargingProfileKindType::Absolute, retrieved.chargingProfileKind); + ASSERT_EQ(ChargingProfilePurposeType::TxDefaultProfile, retrieved.chargingProfilePurpose); + } + + TEST_F(ChargepointTestFixture, AddTxDefaultProfile_ConnectorId_eq_0_Retrieved_at_0__NoProfilesReturned) { + // GTEST_SKIP(); + std::cout << ">>> AddTxDefaultProfile_ConnectorId_eq_0_Retrieved_at_0__ThrowsException\n"; + auto handler = createSmartChargingHandler(1); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + bool is_profile_valid = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + ASSERT_TRUE(is_profile_valid); + + const int connector_id = 0; + handler->add_tx_default_profile(profile, connector_id); + // When profiles are retrieved with the same connector id of 0, nothing is returned + // See AddTxDefaultProfile_ConnectorId_eq_0 for a demonstration of how to retrieve the profile + auto valid_profiles = handler->get_valid_profiles({}, {}, connector_id); + + ASSERT_EQ(0, valid_profiles.size()); + } + +/** + * SmartChargingHandler::add_tx_default_profile test + */ +TEST_F(ChargepointTestFixture, AddTxDefaultProfile__ConnectorId_gt_0) { + // GTEST_SKIP(); + auto handler = createSmartChargingHandlerWithChargePointMaxProfile(); + auto valid_profiles = handler->get_valid_profiles({}, {}, 0); + ASSERT_EQ(1, valid_profiles.size()); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + + const int connector_id = 2; + handler->add_tx_default_profile(profile, connector_id); + + valid_profiles = handler->get_valid_profiles({}, {}, connector_id); + ASSERT_EQ(2, valid_profiles.size()); + auto chargepoint_max_profile = valid_profiles[0]; + ASSERT_EQ(ChargingProfilePurposeType::ChargePointMaxProfile, chargepoint_max_profile.chargingProfilePurpose); + ASSERT_EQ(ChargingProfileKindType::Absolute, chargepoint_max_profile.chargingProfileKind); + auto tx_default_profile = valid_profiles[1]; + ASSERT_EQ(ChargingProfilePurposeType::TxDefaultProfile, tx_default_profile.chargingProfilePurpose); + ASSERT_EQ(ChargingProfileKindType::Absolute, tx_default_profile.chargingProfileKind); } +/** + * SmartChargingHandler::add_tx_default_profile() will throw an std::out_of_range Exception if the connector_id is + * greater than the number of connectors in the SmartChargingHandler's connectors map. + */ +TEST_F(ChargepointTestFixture, AddTxDefaultProfile__ConnectorIdOverMax__ThrowsException) { + // GTEST_SKIP(); + auto handler = createSmartChargingHandler(); + + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + + try { + handler->add_tx_default_profile(profile, connector_id); + FAIL() << "Expected std::out_of_range"; + } catch (std::out_of_range const& err) { + ASSERT_TRUE(true); + } catch (...) { + FAIL() << "Expected std::out_of_range"; + } + + ASSERT_FALSE(false); + } + } // namespace v16 } // namespace ocpp \ No newline at end of file From d0de60bc0d32b0a9b055b2d0fdec1174870159ae Mon Sep 17 00:00:00 2001 From: Christoph <367712+folkengine@users.noreply.github.com> Date: Wed, 31 Jan 2024 12:32:27 -0800 Subject: [PATCH 08/13] Added AddTxProfile test Signed-off-by: Christoph <367712+folkengine@users.noreply.github.com> --- .../ocpp/v16/test_smart_charging_handler.cpp | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp index c6613a409..bcc972ec1 100644 --- a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp +++ b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp @@ -70,9 +70,12 @@ class ChargepointTestFixture : public testing::Test { } ChargingProfile createChargingProfile(ChargingSchedule chargingSchedule) { + return createChargingProfile(chargingSchedule, ChargingProfilePurposeType::TxDefaultProfile); + } + + ChargingProfile createChargingProfile(ChargingSchedule chargingSchedule, ChargingProfilePurposeType chargingProfilePurpose) { auto chargingProfileId = 1; auto stackLevel = 1; - auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; auto chargingProfileKind = ChargingProfileKindType::Absolute; auto recurrencyKind = RecurrencyKindType::Daily; return ChargingProfile{ @@ -647,7 +650,6 @@ TEST_F(ChargepointTestFixture, AddChargePointMaxProfile) { } TEST_F(ChargepointTestFixture, AddTxDefaultProfile_ConnectorId_eq_0) { - // GTEST_SKIP(); std::cout << ">>> AddTxDefaultProfile_ConnectorId_gt_0\n"; auto handler = createSmartChargingHandler(1); auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); @@ -671,7 +673,6 @@ TEST_F(ChargepointTestFixture, AddTxDefaultProfile_ConnectorId_eq_0) { } TEST_F(ChargepointTestFixture, AddTxDefaultProfile_ConnectorId_eq_0_Retrieved_at_0__NoProfilesReturned) { - // GTEST_SKIP(); std::cout << ">>> AddTxDefaultProfile_ConnectorId_eq_0_Retrieved_at_0__ThrowsException\n"; auto handler = createSmartChargingHandler(1); auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); @@ -694,7 +695,6 @@ TEST_F(ChargepointTestFixture, AddTxDefaultProfile_ConnectorId_eq_0) { * SmartChargingHandler::add_tx_default_profile test */ TEST_F(ChargepointTestFixture, AddTxDefaultProfile__ConnectorId_gt_0) { - // GTEST_SKIP(); auto handler = createSmartChargingHandlerWithChargePointMaxProfile(); auto valid_profiles = handler->get_valid_profiles({}, {}, 0); ASSERT_EQ(1, valid_profiles.size()); @@ -719,7 +719,6 @@ TEST_F(ChargepointTestFixture, AddTxDefaultProfile__ConnectorId_gt_0) { * greater than the number of connectors in the SmartChargingHandler's connectors map. */ TEST_F(ChargepointTestFixture, AddTxDefaultProfile__ConnectorIdOverMax__ThrowsException) { - // GTEST_SKIP(); auto handler = createSmartChargingHandler(); auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); @@ -735,7 +734,31 @@ TEST_F(ChargepointTestFixture, AddTxDefaultProfile__ConnectorIdOverMax__ThrowsEx } ASSERT_FALSE(false); - } +} + +/** + * SmartChargingHandler::add_tx_profile + */ +TEST_F(ChargepointTestFixture, AddTxProfile) { + auto handler = createSmartChargingHandlerWithChargePointMaxProfile(); + auto valid_profiles = handler->get_valid_profiles({}, {}, 0); + ASSERT_EQ(1, valid_profiles.size()); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A), ChargingProfilePurposeType::TxProfile); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + + const int connector_id = 2; + handler->add_tx_profile(profile, connector_id); + + valid_profiles = handler->get_valid_profiles({}, {}, connector_id); + ASSERT_EQ(2, valid_profiles.size()); + auto chargepoint_max_profile = valid_profiles[0]; + ASSERT_EQ(ChargingProfilePurposeType::ChargePointMaxProfile, chargepoint_max_profile.chargingProfilePurpose); + ASSERT_EQ(ChargingProfileKindType::Absolute, chargepoint_max_profile.chargingProfileKind); + auto tx_default_profile = valid_profiles[1]; + ASSERT_EQ(ChargingProfilePurposeType::TxProfile, tx_default_profile.chargingProfilePurpose); + ASSERT_EQ(ChargingProfileKindType::Absolute, tx_default_profile.chargingProfileKind); +} + } // namespace v16 } // namespace ocpp \ No newline at end of file From 2fe6c05729ea8701edecf9cfe7da35dcf33860e1 Mon Sep 17 00:00:00 2001 From: Christoph <367712+folkengine@users.noreply.github.com> Date: Fri, 2 Feb 2024 08:46:04 -0800 Subject: [PATCH 09/13] Refactored as per PR comments Signed-off-by: Christoph <367712+folkengine@users.noreply.github.com> --- .../lib/ocpp/v16/chargepoint_test_fixture.cpp | 205 +++++++++++++++++ .../ocpp/v16/test_smart_charging_handler.cpp | 210 +----------------- 2 files changed, 208 insertions(+), 207 deletions(-) create mode 100644 tests/lib/ocpp/v16/chargepoint_test_fixture.cpp diff --git a/tests/lib/ocpp/v16/chargepoint_test_fixture.cpp b/tests/lib/ocpp/v16/chargepoint_test_fixture.cpp new file mode 100644 index 000000000..681d2cf33 --- /dev/null +++ b/tests/lib/ocpp/v16/chargepoint_test_fixture.cpp @@ -0,0 +1,205 @@ +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace ocpp { +namespace v16 { + +/** + * Chargepoint Test Fixture + * + * Validate Profile Test Matrix: + * + * Positive Boundary Conditions: + * - PB01 Valid Profile + * - PB02 Valid Profile No startSchedule & handler allows no startSchedule & profile.chargingProfileKind == Absolute + * - PB03 Valid Profile No startSchedule & handler allows no startSchedule & profile.chargingProfileKind == Relative + * - PB04 Absolute ChargePointMaxProfile Profile with connector id 0 + * - PB05 Absolute TxDefaultProfile + * - PB06 Absolute TxProfile ignore_no_transaction == true + * - PB07 Absolute TxProfile && connector transaction != nullptr && transaction_id matches SKIPPED: was not able to test + * + * Negative Boundary Conditions: + * - NB01 Valid Profile, ConnectorID gt this->connectors.size() + * - NB02 Valid Profile, ConnectorID lt 0 + * - NB03 profile.stackLevel lt 0 + * - NB04 profile.stackLevel gt profile_max_stack_level + * - NB05 profile.chargingProfileKind == Absolute && !profile.chargingSchedule.startSchedule + * - NB06 Number of installed Profiles is > max_charging_profiles_installed + * - NB07 Invalid ChargingSchedule + * - NB08 profile.chargingProfileKind == Recurring && !profile.recurrencyKind + * - NB09 profile.chargingProfileKind == Recurring && !startSchedule + * - NB10 profile.chargingProfileKind == Recurring && !startSchedule && !allow_charging_profile_without_start_schedule + * - NB11 Absolute ChargePointMaxProfile Profile with connector id not 0 + * - NB12 Absolute TxProfile connector_id == 0 + */ +class ChargepointTestFixture : public testing::Test { +protected: + void SetUp() override { + } + + // Adding transactiosn to the connectors because Profiles other than ChargePointMaxProfile need it in order to be valid. + void addConnector(int id) { + auto connector = Connector{id}; + + auto timer = std::unique_ptr(); + + connector.transaction = std::make_shared(id, "test", "test", 1, std::nullopt, ocpp::DateTime(), std::move(timer)); + connectors[id] = std::make_shared(connector); + } + + ChargingSchedule createChargeSchedule() { + return ChargingSchedule{{}}; + } + + ChargingSchedule createChargeSchedule(ChargingRateUnit chargingRateUnit) { + std::vector chargingSchedulePeriod; + std::optional duration; + std::optional startSchedule; + std::optional minChargingRate; + + return ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; + } + + ChargingProfile createChargingProfile(ChargingSchedule chargingSchedule) { + return createChargingProfile(chargingSchedule, ChargingProfilePurposeType::TxDefaultProfile); + } + + ChargingProfile createChargingProfile(ChargingSchedule chargingSchedule, ChargingProfilePurposeType chargingProfilePurpose) { + auto chargingProfileId = 1; + auto stackLevel = 1; + auto chargingProfileKind = ChargingProfileKindType::Absolute; + auto recurrencyKind = RecurrencyKindType::Daily; + return ChargingProfile{ + chargingProfileId, + stackLevel, + chargingProfilePurpose, + chargingProfileKind, + chargingSchedule, + {}, // transactionId + recurrencyKind, + {}, // validFrom + {} // validTo + }; + } + + /** + * TxDefaultProfile, stack #1: time-of-day limitation to 2 kW, recurring every day from 17:00h to 20:00h. + * + * This profile is Example #1 taken from the OCPP 2.0.1 Spec Part 2, page 241. + */ + ChargingProfile createChargingProfile_Example1() { + auto chargingRateUnit = ChargingRateUnit::W; + auto chargingSchedulePeriod = std::vector{ChargingSchedulePeriod{0, 2000, 1}}; + auto duration = 1080; + auto startSchedule = ocpp::DateTime("2024-01-17T17:00:00"); + float minChargingRate = 0; + auto chargingSchedule = + ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; + + auto chargingProfileId = 1; + auto stackLevel = 1; + auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; + auto chargingProfileKind = ChargingProfileKindType::Absolute; + auto recurrencyKind = RecurrencyKindType::Daily; + return ChargingProfile{ + chargingProfileId, + stackLevel, + chargingProfilePurpose, + chargingProfileKind, + chargingSchedule, + {}, // transactionId + recurrencyKind, + {}, // validFrom + {} // validTo + }; + } + + /** + * TxDefaultProfile, stack #2: overruling Sundays to no limit, recurring every week starting 2020-01-05. + * + * This profile is Example #2 taken from the OCPP 2.0.1 Spec Part 2, page 241. + */ + ChargingProfile createChargingProfile_Example2() { + auto chargingRateUnit = ChargingRateUnit::W; + auto chargingSchedulePeriod = std::vector{ChargingSchedulePeriod{0, 999999, 1}}; + auto duration = 0; + auto startSchedule = ocpp::DateTime("2020-01-19T00:00:00"); + float minChargingRate = 0; + auto chargingSchedule = + ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; + + auto chargingProfileId = 11; + auto stackLevel = 2; + auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; + auto chargingProfileKind = ChargingProfileKindType::Recurring; + auto recurrencyKind = RecurrencyKindType::Weekly; + return ChargingProfile{chargingProfileId, + stackLevel, + chargingProfilePurpose, + chargingProfileKind, + chargingSchedule, + {}, // transactionId + recurrencyKind, + {}, + {}}; + } + + SmartChargingHandler* createSmartChargingHandler() { + return createSmartChargingHandler(0); + } + + SmartChargingHandler* createSmartChargingHandler(const int number_of_connectors) { + for (int i = 0; i <= number_of_connectors; i++) { + addConnector(i); + } + + const std::string chargepoint_id = "1"; + const fs::path database_path = "na"; + const fs::path init_script_path = "na"; + + std::shared_ptr database_handler = + std::make_shared(chargepoint_id, database_path, init_script_path); + + auto handler = new SmartChargingHandler(connectors, database_handler, true); + + return handler; + } + + + SmartChargingHandler* createSmartChargingHandlerWithChargePointMaxProfile() { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(10); + + profile.chargingProfilePurpose = ChargingProfilePurposeType::ChargePointMaxProfile; + profile.chargingProfileKind = ChargingProfileKindType::Absolute; + const int connector_id = 0; + const int max_charging_profiles_installed = 10; + bool is_profile_valid = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, + max_charging_profiles_installed, charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + handler->add_charge_point_max_profile(profile); + + return handler; + } + + // Default values used within the tests + std::map> connectors; + std::shared_ptr database_handler; + + const int connector_id = 1; + bool ignore_no_transaction = true; + const int profile_max_stack_level = 10; + const int max_charging_profiles_installed = 20; + const int charging_schedule_max_periods = 10; +}; + +} // namespace v16 +} // namespace ocpp \ No newline at end of file diff --git a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp index bcc972ec1..6b8114b54 100644 --- a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp +++ b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp @@ -1,222 +1,17 @@ #include #include #include -namespace fs = std::filesystem; +#include "chargepoint_test_fixture.cpp" #include #include #include #include #include - namespace ocpp { namespace v16 { -/** - * Chargepoint Test Fixture - * - * Test Matrix: - * - * Positive Boundary Conditions: - * - PB01 Valid Profile - * - PB02 Valid Profile No startSchedule & handler allows no startSchedule & profile.chargingProfileKind == Absolute - * - PB03 Valid Profile No startSchedule & handler allows no startSchedule & profile.chargingProfileKind == Relative - * - PB04 Absolute ChargePointMaxProfile Profile with connector id 0 - * - PB05 Absolute TxDefaultProfile - * - PB06 Absolute TxProfile ignore_no_transaction == true - * - PB07 Absolute TxProfile && connector transaction != nullptr && transaction_id matches SKIPPED: was not able to test - * - * Negative Boundary Conditions: - * - NB01 Valid Profile, ConnectorID gt this->connectors.size() - * - NB02 Valid Profile, ConnectorID lt 0 - * - NB03 profile.stackLevel lt 0 - * - NB04 profile.stackLevel gt profile_max_stack_level - * - NB05 profile.chargingProfileKind == Absolute && !profile.chargingSchedule.startSchedule - * - NB06 Number of installed Profiles is > max_charging_profiles_installed - * - NB07 Invalid ChargingSchedule - * - NB08 profile.chargingProfileKind == Recurring && !profile.recurrencyKind - * - NB09 profile.chargingProfileKind == Recurring && !startSchedule - * - NB10 profile.chargingProfileKind == Recurring && !startSchedule && !allow_charging_profile_without_start_schedule - * - NB11 Absolute ChargePointMaxProfile Profile with connector id not 0 - * - NB12 Absolute TxProfile connector_id == 0 - */ -class ChargepointTestFixture : public testing::Test { -protected: - void SetUp() override { - } - - // Adding transactiosn to the connectors because Profiles other than ChargePointMaxProfile need it in order to be valid. - void addConnector(int id) { - auto connector = Connector{id}; - - auto timer = std::unique_ptr(); - - connector.transaction = std::make_shared(id, "test", "test", 1, std::nullopt, ocpp::DateTime(), std::move(timer)); - connectors[id] = std::make_shared(connector); - } - - ChargingSchedule createChargeSchedule() { - return ChargingSchedule{{}}; - } - - ChargingSchedule createChargeSchedule(ChargingRateUnit chargingRateUnit) { - std::vector chargingSchedulePeriod; - std::optional duration; - std::optional startSchedule; - std::optional minChargingRate; - - return ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; - } - - ChargingProfile createChargingProfile(ChargingSchedule chargingSchedule) { - return createChargingProfile(chargingSchedule, ChargingProfilePurposeType::TxDefaultProfile); - } - - ChargingProfile createChargingProfile(ChargingSchedule chargingSchedule, ChargingProfilePurposeType chargingProfilePurpose) { - auto chargingProfileId = 1; - auto stackLevel = 1; - auto chargingProfileKind = ChargingProfileKindType::Absolute; - auto recurrencyKind = RecurrencyKindType::Daily; - return ChargingProfile{ - chargingProfileId, - stackLevel, - chargingProfilePurpose, - chargingProfileKind, - chargingSchedule, - {}, // transactionId - recurrencyKind, - {}, // validFrom - {} // validTo - }; - } - - /** - * TxDefaultProfile, stack #1: time-of-day limitation to 2 kW, recurring every day from 17:00h to 20:00h. - * - * This profile is Example #1 taken from the OCPP 2.0.1 Spec Part 2, page 241. - */ - ChargingProfile createChargingProfile_Example1() { - auto chargingRateUnit = ChargingRateUnit::W; - auto chargingSchedulePeriod = std::vector{ChargingSchedulePeriod{0, 2000, 1}}; - auto duration = 1080; - auto startSchedule = ocpp::DateTime("2024-01-17T17:00:00"); - float minChargingRate = 0; - auto chargingSchedule = - ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; - - auto chargingProfileId = 1; - auto stackLevel = 1; - auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; - auto chargingProfileKind = ChargingProfileKindType::Absolute; - auto recurrencyKind = RecurrencyKindType::Daily; - return ChargingProfile{ - chargingProfileId, - stackLevel, - chargingProfilePurpose, - chargingProfileKind, - chargingSchedule, - {}, // transactionId - recurrencyKind, - {}, // validFrom - {} // validTo - }; - } - - /** - * TxDefaultProfile, stack #2: overruling Sundays to no limit, recurring every week starting 2020-01-05. - * - * This profile is Example #2 taken from the OCPP 2.0.1 Spec Part 2, page 241. - */ - ChargingProfile createChargingProfile_Example2() { - auto chargingRateUnit = ChargingRateUnit::W; - auto chargingSchedulePeriod = std::vector{ChargingSchedulePeriod{0, 999999, 1}}; - auto duration = 0; - auto startSchedule = ocpp::DateTime("2020-01-19T00:00:00"); - float minChargingRate = 0; - auto chargingSchedule = - ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; - - auto chargingProfileId = 11; - auto stackLevel = 2; - auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; - auto chargingProfileKind = ChargingProfileKindType::Recurring; - auto recurrencyKind = RecurrencyKindType::Weekly; - return ChargingProfile{chargingProfileId, - stackLevel, - chargingProfilePurpose, - chargingProfileKind, - chargingSchedule, - {}, // transactionId - recurrencyKind, - {}, - {}}; - } - - SmartChargingHandler* createSmartChargingHandler() { - // connectors[0] = std::make_shared(Connector{0}); - // connectors[1] = std::make_shared(Connector{1}); - - // const std::string chargepoint_id = "1"; - // const fs::path database_path = "na"; - // const fs::path init_script_path = "na"; - - // std::shared_ptr database_handler = - // std::make_shared(chargepoint_id, database_path, init_script_path); - - // auto handler = new SmartChargingHandler(connectors, database_handler, true); - - return createSmartChargingHandler(0); - } - - // - SmartChargingHandler* createSmartChargingHandler(const int number_of_connectors) { - for (int i = 0; i <= number_of_connectors; i++) { - // connectors[i] = std::make_shared(Connector{i}); - addConnector(i); - } - - const std::string chargepoint_id = "1"; - const fs::path database_path = "na"; - const fs::path init_script_path = "na"; - - std::shared_ptr database_handler = - std::make_shared(chargepoint_id, database_path, init_script_path); - - auto handler = new SmartChargingHandler(connectors, database_handler, true); - - return handler; - } - - - SmartChargingHandler* createSmartChargingHandlerWithChargePointMaxProfile() { - auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); - const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; - auto handler = createSmartChargingHandler(10); - - profile.chargingProfilePurpose = ChargingProfilePurposeType::ChargePointMaxProfile; - profile.chargingProfileKind = ChargingProfileKindType::Absolute; - const int connector_id = 0; - const int max_charging_profiles_installed = 10; - bool is_profile_valid = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); - handler->add_charge_point_max_profile(profile); - - return handler; - } - - // Default values used within the tests - std::map> connectors; - std::shared_ptr database_handler; - - const int connector_id = 1; - bool ignore_no_transaction = true; - const int profile_max_stack_level = 10; - const int max_charging_profiles_installed = 20; - const int charging_schedule_max_periods = 10; -}; - /** * PB01 Valid Profile * @@ -562,9 +357,10 @@ TEST_F(ChargepointTestFixture, ValidateProfile__AbsoluteTxProfileConnectorId0__R ASSERT_FALSE(sut); } + /** * - * 2. Testing the branches within clear_all_profiles_with_filter ClearAllProfilesWithFilter + * Testing the branches within clear_all_profiles_with_filter ClearAllProfilesWithFilter * */ From bf84c0c9808e44fee80466e769fcaefd9b21904b Mon Sep 17 00:00:00 2001 From: Christoph <367712+folkengine@users.noreply.github.com> Date: Fri, 2 Feb 2024 08:56:58 -0800 Subject: [PATCH 10/13] Fixed Codacity lints Signed-off-by: Christoph <367712+folkengine@users.noreply.github.com> --- tests/lib/ocpp/v16/chargepoint_test_fixture.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/lib/ocpp/v16/chargepoint_test_fixture.cpp b/tests/lib/ocpp/v16/chargepoint_test_fixture.cpp index 681d2cf33..76d36902f 100644 --- a/tests/lib/ocpp/v16/chargepoint_test_fixture.cpp +++ b/tests/lib/ocpp/v16/chargepoint_test_fixture.cpp @@ -180,11 +180,6 @@ class ChargepointTestFixture : public testing::Test { profile.chargingProfilePurpose = ChargingProfilePurposeType::ChargePointMaxProfile; profile.chargingProfileKind = ChargingProfileKindType::Absolute; - const int connector_id = 0; - const int max_charging_profiles_installed = 10; - bool is_profile_valid = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); handler->add_charge_point_max_profile(profile); return handler; From 422ab3cc9d9fceb92522e9abee7d523ef90b3345 Mon Sep 17 00:00:00 2001 From: Christoph <367712+folkengine@users.noreply.github.com> Date: Mon, 5 Feb 2024 06:06:41 -0800 Subject: [PATCH 11/13] Made fixture fields public Signed-off-by: Christoph <367712+folkengine@users.noreply.github.com> --- tests/lib/ocpp/v16/chargepoint_test_fixture.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/lib/ocpp/v16/chargepoint_test_fixture.cpp b/tests/lib/ocpp/v16/chargepoint_test_fixture.cpp index 76d36902f..9ce922820 100644 --- a/tests/lib/ocpp/v16/chargepoint_test_fixture.cpp +++ b/tests/lib/ocpp/v16/chargepoint_test_fixture.cpp @@ -40,7 +40,7 @@ namespace v16 { * - NB12 Absolute TxProfile connector_id == 0 */ class ChargepointTestFixture : public testing::Test { -protected: +public: void SetUp() override { } From 6cba072657d9ec9298401941aa9d59ede20df287 Mon Sep 17 00:00:00 2001 From: Christoph <367712+folkengine@users.noreply.github.com> Date: Mon, 5 Feb 2024 06:41:26 -0800 Subject: [PATCH 12/13] Being more specific with fixture fields to fix linting error Signed-off-by: Christoph <367712+folkengine@users.noreply.github.com> --- .../ocpp/v16/test_smart_charging_handler.cpp | 213 +++++++++++++----- 1 file changed, 154 insertions(+), 59 deletions(-) diff --git a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp index 6b8114b54..0f52ab595 100644 --- a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp +++ b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp @@ -22,9 +22,14 @@ TEST_F(ChargepointTestFixture, ValidateProfile) { const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + ChargepointTestFixture::connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_TRUE(sut); } @@ -39,9 +44,14 @@ TEST_F(ChargepointTestFixture, ValidateProfile__example1) { const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::W}; auto handler = createSmartChargingHandler(); - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + ChargepointTestFixture::connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_TRUE(sut); } @@ -55,9 +65,14 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ConnectorIdGreaterThanConnectors auto handler = createSmartChargingHandler(); const int connector_id = INT_MAX; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_FALSE(sut); } @@ -71,9 +86,14 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NegativeConnectorId auto handler = createSmartChargingHandler(); const int connector_id = -1; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_FALSE(sut); } @@ -87,9 +107,14 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ConnectorIdZero__Re auto handler = createSmartChargingHandler(); profile.stackLevel = -1; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + ChargepointTestFixture::connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_FALSE(sut); } @@ -102,10 +127,15 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_StackLevelGreaterTh const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); - profile.stackLevel = profile_max_stack_level + 1; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + profile.stackLevel = ChargepointTestFixture::profile_max_stack_level + 1; + bool sut = handler->validate_profile( + profile, + ChargepointTestFixture::connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_FALSE(sut); } @@ -125,9 +155,14 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ChargingProfileKind profile.chargingProfileKind = ChargingProfileKindType::Absolute; profile.chargingSchedule.startSchedule = std::nullopt; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + ChargepointTestFixture::connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_FALSE(sut); } @@ -144,9 +179,14 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_AllowsNoStartSchedu // Configure to have no start schedule profile.chargingProfileKind = ChargingProfileKindType::Absolute; profile.chargingSchedule.startSchedule = std::nullopt; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + ChargepointTestFixture::connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_TRUE(sut); } @@ -161,9 +201,14 @@ TEST_F(ChargepointTestFixture, auto handler = createSmartChargingHandler(); const int max_charging_profiles_installed = 0; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + ChargepointTestFixture::connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_FALSE(sut); } @@ -175,13 +220,17 @@ TEST_F(ChargepointTestFixture, */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_InvalidChargingSchedule__ReturnsFalse) { auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); - profile.chargingSchedule.chargingSchedulePeriod = std::vector{}; auto handler = createSmartChargingHandler(); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::W}; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + ChargepointTestFixture::connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_FALSE(sut); } @@ -197,9 +246,14 @@ TEST_F(ChargepointTestFixture, profile.chargingProfileKind = ChargingProfileKindType::Recurring; profile.recurrencyKind = std::nullopt; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + ChargepointTestFixture::connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_FALSE(sut); } @@ -220,9 +274,14 @@ TEST_F(ChargepointTestFixture, profile.chargingProfileKind = ChargingProfileKindType::Recurring; profile.chargingSchedule.startSchedule = std::nullopt; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + ChargepointTestFixture::connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_FALSE(sut); } @@ -237,9 +296,14 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NoStartScheduleAllo profile.chargingProfileKind = ChargingProfileKindType::Recurring; profile.chargingSchedule.startSchedule = std::nullopt; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + ChargepointTestFixture::connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_TRUE(sut); } @@ -258,9 +322,14 @@ TEST_F(ChargepointTestFixture, ValidateProfile__RecurringNoStartScheduleNotAllow profile.chargingProfileKind = ChargingProfileKindType::Recurring; profile.chargingSchedule.startSchedule = std::nullopt; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + ChargepointTestFixture::connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_FALSE(sut); } @@ -278,9 +347,14 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NotRecurrencyKindCo profile.chargingProfilePurpose = ChargingProfilePurposeType::ChargePointMaxProfile; profile.chargingProfileKind = ChargingProfileKindType::Absolute; const int connector_id = 0; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_TRUE(sut); } @@ -299,9 +373,15 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NotRecurrencyKindCo profile.chargingProfilePurpose = ChargingProfilePurposeType::ChargePointMaxProfile; profile.chargingProfileKind = ChargingProfileKindType::Absolute; const int connector_id = 1; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); + ASSERT_FALSE(sut); } @@ -316,9 +396,14 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfileTxDefaultProfile__Re profile.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; profile.chargingProfileKind = ChargingProfileKindType::Absolute; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + ChargepointTestFixture::connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_TRUE(sut); } @@ -333,9 +418,14 @@ TEST_F(ChargepointTestFixture, ValidateProfile__AbsoluteTxProfileIgnoreNoTransac profile.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; profile.chargingProfileKind = ChargingProfileKindType::Absolute; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + ChargepointTestFixture::connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_TRUE(sut); } @@ -351,9 +441,14 @@ TEST_F(ChargepointTestFixture, ValidateProfile__AbsoluteTxProfileConnectorId0__R profile.chargingProfileKind = ChargingProfileKindType::Absolute; profile.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; const int connector_id = 0; - bool sut = handler->validate_profile(profile, connector_id, ignore_no_transaction, profile_max_stack_level, - max_charging_profiles_installed, charging_schedule_max_periods, - charging_schedule_allowed_charging_rate_units); + bool sut = handler->validate_profile( + profile, + connector_id, + ChargepointTestFixture::ignore_no_transaction, + ChargepointTestFixture::profile_max_stack_level, + ChargepointTestFixture::max_charging_profiles_installed, + ChargepointTestFixture::charging_schedule_max_periods, + charging_schedule_allowed_charging_rate_units); ASSERT_FALSE(sut); } From 921899ec0620490a76188755d04d7df6d3168240 Mon Sep 17 00:00:00 2001 From: Christoph <367712+folkengine@users.noreply.github.com> Date: Mon, 5 Feb 2024 07:03:44 -0800 Subject: [PATCH 13/13] Rolled back test figure move to own file for linting issues Signed-off-by: Christoph <367712+folkengine@users.noreply.github.com> --- .../lib/ocpp/v16/chargepoint_test_fixture.cpp | 200 ------------------ .../ocpp/v16/test_smart_charging_handler.cpp | 187 +++++++++++++++- 2 files changed, 185 insertions(+), 202 deletions(-) delete mode 100644 tests/lib/ocpp/v16/chargepoint_test_fixture.cpp diff --git a/tests/lib/ocpp/v16/chargepoint_test_fixture.cpp b/tests/lib/ocpp/v16/chargepoint_test_fixture.cpp deleted file mode 100644 index 9ce922820..000000000 --- a/tests/lib/ocpp/v16/chargepoint_test_fixture.cpp +++ /dev/null @@ -1,200 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include -#include - -namespace ocpp { -namespace v16 { - -/** - * Chargepoint Test Fixture - * - * Validate Profile Test Matrix: - * - * Positive Boundary Conditions: - * - PB01 Valid Profile - * - PB02 Valid Profile No startSchedule & handler allows no startSchedule & profile.chargingProfileKind == Absolute - * - PB03 Valid Profile No startSchedule & handler allows no startSchedule & profile.chargingProfileKind == Relative - * - PB04 Absolute ChargePointMaxProfile Profile with connector id 0 - * - PB05 Absolute TxDefaultProfile - * - PB06 Absolute TxProfile ignore_no_transaction == true - * - PB07 Absolute TxProfile && connector transaction != nullptr && transaction_id matches SKIPPED: was not able to test - * - * Negative Boundary Conditions: - * - NB01 Valid Profile, ConnectorID gt this->connectors.size() - * - NB02 Valid Profile, ConnectorID lt 0 - * - NB03 profile.stackLevel lt 0 - * - NB04 profile.stackLevel gt profile_max_stack_level - * - NB05 profile.chargingProfileKind == Absolute && !profile.chargingSchedule.startSchedule - * - NB06 Number of installed Profiles is > max_charging_profiles_installed - * - NB07 Invalid ChargingSchedule - * - NB08 profile.chargingProfileKind == Recurring && !profile.recurrencyKind - * - NB09 profile.chargingProfileKind == Recurring && !startSchedule - * - NB10 profile.chargingProfileKind == Recurring && !startSchedule && !allow_charging_profile_without_start_schedule - * - NB11 Absolute ChargePointMaxProfile Profile with connector id not 0 - * - NB12 Absolute TxProfile connector_id == 0 - */ -class ChargepointTestFixture : public testing::Test { -public: - void SetUp() override { - } - - // Adding transactiosn to the connectors because Profiles other than ChargePointMaxProfile need it in order to be valid. - void addConnector(int id) { - auto connector = Connector{id}; - - auto timer = std::unique_ptr(); - - connector.transaction = std::make_shared(id, "test", "test", 1, std::nullopt, ocpp::DateTime(), std::move(timer)); - connectors[id] = std::make_shared(connector); - } - - ChargingSchedule createChargeSchedule() { - return ChargingSchedule{{}}; - } - - ChargingSchedule createChargeSchedule(ChargingRateUnit chargingRateUnit) { - std::vector chargingSchedulePeriod; - std::optional duration; - std::optional startSchedule; - std::optional minChargingRate; - - return ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; - } - - ChargingProfile createChargingProfile(ChargingSchedule chargingSchedule) { - return createChargingProfile(chargingSchedule, ChargingProfilePurposeType::TxDefaultProfile); - } - - ChargingProfile createChargingProfile(ChargingSchedule chargingSchedule, ChargingProfilePurposeType chargingProfilePurpose) { - auto chargingProfileId = 1; - auto stackLevel = 1; - auto chargingProfileKind = ChargingProfileKindType::Absolute; - auto recurrencyKind = RecurrencyKindType::Daily; - return ChargingProfile{ - chargingProfileId, - stackLevel, - chargingProfilePurpose, - chargingProfileKind, - chargingSchedule, - {}, // transactionId - recurrencyKind, - {}, // validFrom - {} // validTo - }; - } - - /** - * TxDefaultProfile, stack #1: time-of-day limitation to 2 kW, recurring every day from 17:00h to 20:00h. - * - * This profile is Example #1 taken from the OCPP 2.0.1 Spec Part 2, page 241. - */ - ChargingProfile createChargingProfile_Example1() { - auto chargingRateUnit = ChargingRateUnit::W; - auto chargingSchedulePeriod = std::vector{ChargingSchedulePeriod{0, 2000, 1}}; - auto duration = 1080; - auto startSchedule = ocpp::DateTime("2024-01-17T17:00:00"); - float minChargingRate = 0; - auto chargingSchedule = - ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; - - auto chargingProfileId = 1; - auto stackLevel = 1; - auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; - auto chargingProfileKind = ChargingProfileKindType::Absolute; - auto recurrencyKind = RecurrencyKindType::Daily; - return ChargingProfile{ - chargingProfileId, - stackLevel, - chargingProfilePurpose, - chargingProfileKind, - chargingSchedule, - {}, // transactionId - recurrencyKind, - {}, // validFrom - {} // validTo - }; - } - - /** - * TxDefaultProfile, stack #2: overruling Sundays to no limit, recurring every week starting 2020-01-05. - * - * This profile is Example #2 taken from the OCPP 2.0.1 Spec Part 2, page 241. - */ - ChargingProfile createChargingProfile_Example2() { - auto chargingRateUnit = ChargingRateUnit::W; - auto chargingSchedulePeriod = std::vector{ChargingSchedulePeriod{0, 999999, 1}}; - auto duration = 0; - auto startSchedule = ocpp::DateTime("2020-01-19T00:00:00"); - float minChargingRate = 0; - auto chargingSchedule = - ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; - - auto chargingProfileId = 11; - auto stackLevel = 2; - auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; - auto chargingProfileKind = ChargingProfileKindType::Recurring; - auto recurrencyKind = RecurrencyKindType::Weekly; - return ChargingProfile{chargingProfileId, - stackLevel, - chargingProfilePurpose, - chargingProfileKind, - chargingSchedule, - {}, // transactionId - recurrencyKind, - {}, - {}}; - } - - SmartChargingHandler* createSmartChargingHandler() { - return createSmartChargingHandler(0); - } - - SmartChargingHandler* createSmartChargingHandler(const int number_of_connectors) { - for (int i = 0; i <= number_of_connectors; i++) { - addConnector(i); - } - - const std::string chargepoint_id = "1"; - const fs::path database_path = "na"; - const fs::path init_script_path = "na"; - - std::shared_ptr database_handler = - std::make_shared(chargepoint_id, database_path, init_script_path); - - auto handler = new SmartChargingHandler(connectors, database_handler, true); - - return handler; - } - - - SmartChargingHandler* createSmartChargingHandlerWithChargePointMaxProfile() { - auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); - const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; - auto handler = createSmartChargingHandler(10); - - profile.chargingProfilePurpose = ChargingProfilePurposeType::ChargePointMaxProfile; - profile.chargingProfileKind = ChargingProfileKindType::Absolute; - handler->add_charge_point_max_profile(profile); - - return handler; - } - - // Default values used within the tests - std::map> connectors; - std::shared_ptr database_handler; - - const int connector_id = 1; - bool ignore_no_transaction = true; - const int profile_max_stack_level = 10; - const int max_charging_profiles_installed = 20; - const int charging_schedule_max_periods = 10; -}; - -} // namespace v16 -} // namespace ocpp \ No newline at end of file diff --git a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp index 0f52ab595..20285e8ee 100644 --- a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp +++ b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp @@ -2,7 +2,6 @@ #include #include -#include "chargepoint_test_fixture.cpp" #include #include #include @@ -12,6 +11,191 @@ namespace ocpp { namespace v16 { +/** + * Chargepoint Test Fixture + * + * Validate Profile Test Matrix: + * + * Positive Boundary Conditions: + * - PB01 Valid Profile + * - PB02 Valid Profile No startSchedule & handler allows no startSchedule & profile.chargingProfileKind == Absolute + * - PB03 Valid Profile No startSchedule & handler allows no startSchedule & profile.chargingProfileKind == Relative + * - PB04 Absolute ChargePointMaxProfile Profile with connector id 0 + * - PB05 Absolute TxDefaultProfile + * - PB06 Absolute TxProfile ignore_no_transaction == true + * - PB07 Absolute TxProfile && connector transaction != nullptr && transaction_id matches SKIPPED: was not able to test + * + * Negative Boundary Conditions: + * - NB01 Valid Profile, ConnectorID gt this->connectors.size() + * - NB02 Valid Profile, ConnectorID lt 0 + * - NB03 profile.stackLevel lt 0 + * - NB04 profile.stackLevel gt profile_max_stack_level + * - NB05 profile.chargingProfileKind == Absolute && !profile.chargingSchedule.startSchedule + * - NB06 Number of installed Profiles is > max_charging_profiles_installed + * - NB07 Invalid ChargingSchedule + * - NB08 profile.chargingProfileKind == Recurring && !profile.recurrencyKind + * - NB09 profile.chargingProfileKind == Recurring && !startSchedule + * - NB10 profile.chargingProfileKind == Recurring && !startSchedule && !allow_charging_profile_without_start_schedule + * - NB11 Absolute ChargePointMaxProfile Profile with connector id not 0 + * - NB12 Absolute TxProfile connector_id == 0 + */ +class ChargepointTestFixture : public testing::Test { +public: + void SetUp() override { + } + + // Adding transactiosn to the connectors because Profiles other than ChargePointMaxProfile need it in order to be valid. + void addConnector(int id) { + auto connector = Connector{id}; + + auto timer = std::unique_ptr(); + + connector.transaction = std::make_shared(id, "test", "test", 1, std::nullopt, ocpp::DateTime(), std::move(timer)); + connectors[id] = std::make_shared(connector); + } + + ChargingSchedule createChargeSchedule() { + return ChargingSchedule{{}}; + } + + ChargingSchedule createChargeSchedule(ChargingRateUnit chargingRateUnit) { + std::vector chargingSchedulePeriod; + std::optional duration; + std::optional startSchedule; + std::optional minChargingRate; + + return ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; + } + + ChargingProfile createChargingProfile(ChargingSchedule chargingSchedule) { + return createChargingProfile(chargingSchedule, ChargingProfilePurposeType::TxDefaultProfile); + } + + ChargingProfile createChargingProfile(ChargingSchedule chargingSchedule, ChargingProfilePurposeType chargingProfilePurpose) { + auto chargingProfileId = 1; + auto stackLevel = 1; + auto chargingProfileKind = ChargingProfileKindType::Absolute; + auto recurrencyKind = RecurrencyKindType::Daily; + return ChargingProfile{ + chargingProfileId, + stackLevel, + chargingProfilePurpose, + chargingProfileKind, + chargingSchedule, + {}, // transactionId + recurrencyKind, + {}, // validFrom + {} // validTo + }; + } + + /** + * TxDefaultProfile, stack #1: time-of-day limitation to 2 kW, recurring every day from 17:00h to 20:00h. + * + * This profile is Example #1 taken from the OCPP 2.0.1 Spec Part 2, page 241. + */ + ChargingProfile createChargingProfile_Example1() { + auto chargingRateUnit = ChargingRateUnit::W; + auto chargingSchedulePeriod = std::vector{ChargingSchedulePeriod{0, 2000, 1}}; + auto duration = 1080; + auto startSchedule = ocpp::DateTime("2024-01-17T17:00:00"); + float minChargingRate = 0; + auto chargingSchedule = + ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; + + auto chargingProfileId = 1; + auto stackLevel = 1; + auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; + auto chargingProfileKind = ChargingProfileKindType::Absolute; + auto recurrencyKind = RecurrencyKindType::Daily; + return ChargingProfile{ + chargingProfileId, + stackLevel, + chargingProfilePurpose, + chargingProfileKind, + chargingSchedule, + {}, // transactionId + recurrencyKind, + {}, // validFrom + {} // validTo + }; + } + + /** + * TxDefaultProfile, stack #2: overruling Sundays to no limit, recurring every week starting 2020-01-05. + * + * This profile is Example #2 taken from the OCPP 2.0.1 Spec Part 2, page 241. + */ + ChargingProfile createChargingProfile_Example2() { + auto chargingRateUnit = ChargingRateUnit::W; + auto chargingSchedulePeriod = std::vector{ChargingSchedulePeriod{0, 999999, 1}}; + auto duration = 0; + auto startSchedule = ocpp::DateTime("2020-01-19T00:00:00"); + float minChargingRate = 0; + auto chargingSchedule = + ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; + + auto chargingProfileId = 11; + auto stackLevel = 2; + auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; + auto chargingProfileKind = ChargingProfileKindType::Recurring; + auto recurrencyKind = RecurrencyKindType::Weekly; + return ChargingProfile{chargingProfileId, + stackLevel, + chargingProfilePurpose, + chargingProfileKind, + chargingSchedule, + {}, // transactionId + recurrencyKind, + {}, + {}}; + } + + SmartChargingHandler* createSmartChargingHandler() { + return createSmartChargingHandler(0); + } + + SmartChargingHandler* createSmartChargingHandler(const int number_of_connectors) { + for (int i = 0; i <= number_of_connectors; i++) { + addConnector(i); + } + + const std::string chargepoint_id = "1"; + const fs::path database_path = "na"; + const fs::path init_script_path = "na"; + + std::shared_ptr database_handler = + std::make_shared(chargepoint_id, database_path, init_script_path); + + auto handler = new SmartChargingHandler(connectors, database_handler, true); + + return handler; + } + + + SmartChargingHandler* createSmartChargingHandlerWithChargePointMaxProfile() { + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; + auto handler = createSmartChargingHandler(10); + + profile.chargingProfilePurpose = ChargingProfilePurposeType::ChargePointMaxProfile; + profile.chargingProfileKind = ChargingProfileKindType::Absolute; + handler->add_charge_point_max_profile(profile); + + return handler; + } + + // Default values used within the tests + std::map> connectors; + std::shared_ptr database_handler; + + const int connector_id = 1; + bool ignore_no_transaction = true; + const int profile_max_stack_level = 10; + const int max_charging_profiles_installed = 20; + const int charging_schedule_max_periods = 10; +}; + /** * PB01 Valid Profile * @@ -650,6 +834,5 @@ TEST_F(ChargepointTestFixture, AddTxProfile) { ASSERT_EQ(ChargingProfileKindType::Absolute, tx_default_profile.chargingProfileKind); } - } // namespace v16 } // namespace ocpp \ No newline at end of file