From 8a1e06c9ff4969872178b11c10ae09822a624f56 Mon Sep 17 00:00:00 2001 From: Coury Richards <146002925+couryrr-afs@users.noreply.github.com> Date: Fri, 2 Feb 2024 16:41:32 +0000 Subject: [PATCH] Created cases to cover highlevel filtering options Signed-off-by: Coury Richards <146002925+couryrr-afs@users.noreply.github.com> --- .ci/build-kit/install_and_test.sh | 3 +- include/ocpp/v16/database_handler.hpp | 2 +- .../ocpp/v16/test_smart_charging_handler.cpp | 617 +++++++++++++----- 3 files changed, 467 insertions(+), 155 deletions(-) diff --git a/.ci/build-kit/install_and_test.sh b/.ci/build-kit/install_and_test.sh index 140ee13b06..e15c1f68ec 100755 --- a/.ci/build-kit/install_and_test.sh +++ b/.ci/build-kit/install_and_test.sh @@ -8,7 +8,8 @@ cmake \ -G Ninja \ -DBUILD_TESTING=ON \ -DCMAKE_BUILD_TYPE=Debug \ - -DCMAKE_INSTALL_PREFIX="$WORKSPACE_PATH/dist" + -DCMAKE_INSTALL_PREFIX="$WORKSPACE_PATH/dist" \ + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ninja -j$(nproc) -C build install diff --git a/include/ocpp/v16/database_handler.hpp b/include/ocpp/v16/database_handler.hpp index a819a6c0bc..3ac15f967f 100644 --- a/include/ocpp/v16/database_handler.hpp +++ b/include/ocpp/v16/database_handler.hpp @@ -49,7 +49,7 @@ class DatabaseHandler : public ocpp::common::DatabaseHandlerBase { public: DatabaseHandler(const std::string& chargepoint_id, const fs::path& database_path, const fs::path& init_script_path); - ~DatabaseHandler(); + virtual ~DatabaseHandler(); /// \brief Opens the database connection, runs initialization script and initializes the CONNECTORS and /// AUTH_LIST_VERSION table. diff --git a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp index 4085cac4d9..74462ea71b 100644 --- a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp +++ b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp @@ -51,7 +51,8 @@ class ChargepointTestFixture : public testing::Test { auto timer = std::unique_ptr(); - connector.transaction = std::make_shared(id, "test", "test", 1, std::nullopt, ocpp::DateTime(), std::move(timer)); + connector.transaction = + std::make_shared(id, "test", "test", 1, std::nullopt, ocpp::DateTime(), std::move(timer)); connectors[id] = std::make_shared(connector); } @@ -68,12 +69,27 @@ class ChargepointTestFixture : public testing::Test { return ChargingSchedule{chargingRateUnit, chargingSchedulePeriod, duration, startSchedule, minChargingRate}; } - ChargingProfile createChargingProfile(std::optional id, ChargingSchedule chargingSchedule) { - // Is this working? + ChargingProfile createMaxChargingProfile(ChargingSchedule chargingSchedule) { + auto chargingProfileId = 1; + auto stackLevel = 1; + auto chargingProfilePurpose = ChargingProfilePurposeType::ChargePointMaxProfile; + auto chargingProfileKind = ChargingProfileKindType::Absolute; + auto recurrencyKind = RecurrencyKindType::Daily; + return ChargingProfile{ + chargingProfileId, + stackLevel, + chargingProfilePurpose, + chargingProfileKind, + chargingSchedule, + {}, // transactionId + recurrencyKind, + {}, // validFrom + {} // validTo + }; + } + + ChargingProfile createChargingProfile(ChargingSchedule chargingSchedule) { auto chargingProfileId = 1; - if (id) { - chargingProfileId = id.value(); - } auto stackLevel = 1; auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; auto chargingProfileKind = ChargingProfileKindType::Absolute; @@ -91,6 +107,41 @@ class ChargepointTestFixture : public testing::Test { }; } + ChargingProfile createTxChargingProfile(ChargingSchedule chargingSchedule) { + auto chargingProfileId = 1; + auto stackLevel = 1; + auto chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; + auto chargingProfileKind = ChargingProfileKindType::Absolute; + auto recurrencyKind = RecurrencyKindType::Daily; + return ChargingProfile{ + chargingProfileId, + stackLevel, + chargingProfilePurpose, + chargingProfileKind, + chargingSchedule, + {}, // transactionId + recurrencyKind, + {}, // validFrom + {} // validTo + }; + } + + ChargingProfile createChargingProfile(int id, int stackLevel, ChargingProfilePurposeType chargingProfilePurpose, + ChargingProfileKindType chargingProfileKind, + RecurrencyKindType recurrencyKind, ChargingSchedule chargingSchedule) { + return ChargingProfile{ + id, + 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. * @@ -183,7 +234,7 @@ class ChargepointTestFixture : public testing::Test { * Happy path simple test */ TEST_F(ChargepointTestFixture, ValidateProfile) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; addConnector(1); @@ -219,7 +270,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__example1) { * NB01 Valid Profile, ConnectorID gt this->connectors.size() */ TEST_F(ChargepointTestFixture, ValidateProfile__ConnectorIdGreaterThanConnectorsSize__ReturnsFalse) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); @@ -235,7 +286,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ConnectorIdGreaterThanConnectors * NB02 Valid Profile, ConnectorID lt 0 */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NegativeConnectorIdTest__ReturnsFalse) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); @@ -251,7 +302,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NegativeConnectorId * NB03 profile.stackLevel lt 0 */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ConnectorIdZero__ReturnsFalse) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); @@ -267,7 +318,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ConnectorIdZero__Re * NB04 profile.stackLevel gt this->profile_max_stack_level */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_StackLevelGreaterThanMaxStackLevel__ReturnsFalse) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); @@ -283,7 +334,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_StackLevelGreaterTh * NB05 profile.chargingProfileKind == Absolute && !profile.chargingSchedule.startSchedule */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ChargingProfileKindAbsoluteNoStartSchedule__ReturnsFalse) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + 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 addConnector(1); @@ -304,7 +355,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ChargingProfileKind * PB02 Valid Profile No startSchedule & handler allows no startSchedule */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_AllowsNoStartSchedule__ReturnsTrue) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + 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 addConnector(1); @@ -325,7 +376,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_AllowsNoStartSchedu */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_InstalledProfilesGreaterThanMaxInstalledProfiles__ReturnsFalse) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); @@ -343,7 +394,7 @@ TEST_F(ChargepointTestFixture, * Creating a ChargingProfile with a different ChargingRateUnit */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_InvalidChargingSchedule__ReturnsFalse) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); profile.chargingSchedule.chargingSchedulePeriod = std::vector{}; auto handler = createSmartChargingHandler(); @@ -360,7 +411,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_InvalidChargingSche */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ChargingProfileKindRecurringNoRecurrencyKind__ReturnsFalse) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); @@ -378,7 +429,7 @@ TEST_F(ChargepointTestFixture, */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_ChargingProfileKindRecurringNoStartSchedule__ReturnsFalse) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + 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 addConnector(1); @@ -399,7 +450,7 @@ TEST_F(ChargepointTestFixture, * 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)); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; addConnector(1); @@ -418,7 +469,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NoStartScheduleAllo * NB10 profile.chargingProfileKind == Recurring && !startSchedule && !allow_charging_profile_without_start_schedule */ TEST_F(ChargepointTestFixture, ValidateProfile__RecurringNoStartScheduleNotAllowed__ReturnsFalse) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; addConnector(1); auto allow_charging_profile_without_start_schedule = false; @@ -440,7 +491,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__RecurringNoStartScheduleNotAllow * Absolute ChargePointMaxProfile Profile need a connector id of 0 */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NotRecurrencyKindConnectorId0__ReturnsTrue) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); @@ -461,7 +512,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NotRecurrencyKindCo * ChargingProfileKindType of Relative */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NotRecurrencyKindConnectorIdNot0__ReturnsFalse) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); @@ -479,7 +530,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfile_NotRecurrencyKindCo * PB05 Absolute TxDefaultProfile */ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfileTxDefaultProfile__ReturnsTrue) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; addConnector(1); @@ -499,7 +550,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__ValidProfileTxDefaultProfile__Re * PB06 Absolute TxProfile ignore_no_transaction == true */ TEST_F(ChargepointTestFixture, ValidateProfile__AbsoluteTxProfileIgnoreNoTransaction__ReturnsTrue) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; addConnector(1); @@ -518,7 +569,7 @@ TEST_F(ChargepointTestFixture, ValidateProfile__AbsoluteTxProfileIgnoreNoTransac * NB12 Absolute TxProfile connector_id == 0 */ TEST_F(ChargepointTestFixture, ValidateProfile__AbsoluteTxProfileConnectorId0__ReturnsFalse) { - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); const std::vector& charging_schedule_allowed_charging_rate_units{ChargingRateUnit::A}; auto handler = createSmartChargingHandler(); @@ -531,105 +582,239 @@ TEST_F(ChargepointTestFixture, ValidateProfile__AbsoluteTxProfileConnectorId0__R ASSERT_FALSE(sut); } + /** * - * 2. Testing the branches within clear_all_profiles_with_filter ClearAllProfilesWithFilter + * 2. Testing the branches within ClearAllProfilesWithFilter * */ -/** - * NB - */ -TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__AllOptionalsEmpty_DoNotCheckIdOnly__ReturnsFalse) { - // No profiles set to remove +// FIXME: Should this be ignored. There are no filter parameters. +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__AllOptionalsEmpty__ReturnsFalse) { + GTEST_SKIP() << "No parameter filter is clearing all profiles"; + + const int connector_id_1 = 1; + addConnector(connector_id_1); + + const int connector_id_2 = 2; + addConnector(connector_id_2); + auto handler = createSmartChargingHandler(); - bool sut = handler->clear_all_profiles_with_filter({}, {}, {}, {}, false); + auto profile_1 = + createChargingProfile(1, 1, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); - ASSERT_FALSE(sut); + auto profile_2 = + createChargingProfile(2, 2, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); + + handler->add_tx_profile(profile_1, connector_id_1); + handler->add_tx_profile(profile_2, connector_id_2); + + auto profiles_1 = handler->get_valid_profiles({}, {}, connector_id_1); + auto profiles_2 = handler->get_valid_profiles({}, {}, connector_id_2); + ASSERT_EQ(1, profiles_1.size()); + ASSERT_EQ(1, profiles_2.size()); + + // All empty tokens + bool sut = handler->clear_all_profiles_with_filter(std::nullopt, std::nullopt, std::nullopt, std::nullopt, false); + + profiles_1 = handler->get_valid_profiles({}, {}, connector_id_1); + profiles_2 = handler->get_valid_profiles({}, {}, connector_id_2); + ASSERT_EQ(0, profiles_1.size()); + ASSERT_EQ(0, profiles_2.size()); + + ASSERT_TRUE(sut); } TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__AllOptionalsEmpty_CheckIdOnly__ReturnsFalse) { - // No profiles set to remove + const int connector_id = 1; + addConnector(connector_id); + + auto handler = createSmartChargingHandler(); + + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + + handler->add_tx_profile(profile, connector_id); + + // All empty tokens + bool sut = handler->clear_all_profiles_with_filter(std::nullopt, std::nullopt, std::nullopt, std::nullopt, true); + + ASSERT_FALSE(sut); +} + +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__NoProfiles_ProfileId_CheckIdOnly__ReturnsFalse) { auto handler = createSmartChargingHandler(); - bool sut = handler->clear_all_profiles_with_filter({}, {}, {}, {}, true); + bool sut = handler->clear_all_profiles_with_filter(1, std::nullopt, std::nullopt, std::nullopt, true); ASSERT_FALSE(sut); } -TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__OnlyOneMatchingProfileId_CheckIdOnly__ReturnsTrue) { +/** + * There is an issue open https://github.com/EVerest/libocpp/issues/432 for the below clear_all_profiles_with_filter + * The current method call will allow for all parameters to be passed in at a single time and then act on all of them. + * The issue is that a call should either have a profile id and delete that specific profile or any combination of the + * other three to delete n number of profiles (to a single one if given all three). + * The logic is exclusionary but the method does not guard against it which can put the system in an odd state. + */ + +// 0, 1 and many connectors + +// max, default, tx profiles + +// profile id only +// or +// connector id only + +// stacklevel only + +// charging profile purpose only + +// a mix of them all + +// Max + +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__Max_OnlyOneMatchingProfileId_CheckIdOnly__ReturnsTrue) { + const int connector_id = 0; auto handler = createSmartChargingHandler(); - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile = createMaxChargingProfile(createChargeSchedule(ChargingRateUnit::A)); handler->add_charge_point_max_profile(profile); - bool sut = handler->clear_all_profiles_with_filter(1, {}, {}, {}, true); + auto profiles = handler->get_valid_profiles({}, {}, connector_id); + + ASSERT_EQ(1, profiles.size()); + + bool sut = handler->clear_all_profiles_with_filter(1, std::nullopt, std::nullopt, std::nullopt, true); + profiles = handler->get_valid_profiles({}, {}, connector_id); + ASSERT_EQ(0, profiles.size()); ASSERT_TRUE(sut); } -TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__NoMatchingProfileId_CheckIdOnly__ReturnsFalse) { +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__Max_MatchingProfileId_CheckIdOnly__ReturnsTrue) { + const int connector_id = 0; + auto handler = createSmartChargingHandler(); - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile_1 = createChargingProfile(1, 1, ChargingProfilePurposeType::ChargePointMaxProfile, + ChargingProfileKindType::Absolute, RecurrencyKindType::Daily, + createChargeSchedule(ChargingRateUnit::A)); - handler->add_charge_point_max_profile(profile); + auto profile_2 = createChargingProfile(2, 2, ChargingProfilePurposeType::ChargePointMaxProfile, + ChargingProfileKindType::Absolute, RecurrencyKindType::Daily, + createChargeSchedule(ChargingRateUnit::A)); + + handler->add_charge_point_max_profile(profile_1); + handler->add_charge_point_max_profile(profile_2); + + auto profiles = handler->get_valid_profiles({}, {}, connector_id); + + ASSERT_EQ(2, profiles.size()); + + bool sut = handler->clear_all_profiles_with_filter(1, std::nullopt, std::nullopt, std::nullopt, true); + profiles = handler->get_valid_profiles({}, {}, connector_id); + + ASSERT_EQ(1, profiles.size()); + ASSERT_TRUE(sut); +} + +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__Max_MultipleNoMatchingProfileId_CheckIdOnly__ReturnsFalse) { + const int connector_id = 0; + + auto handler = createSmartChargingHandler(); + + auto profile_1 = createChargingProfile(1, 1, ChargingProfilePurposeType::ChargePointMaxProfile, + ChargingProfileKindType::Absolute, RecurrencyKindType::Daily, + createChargeSchedule(ChargingRateUnit::A)); - bool sut = handler->clear_all_profiles_with_filter(2, {}, {}, {}, true); + auto profile_2 = createChargingProfile(2, 2, ChargingProfilePurposeType::ChargePointMaxProfile, + ChargingProfileKindType::Absolute, RecurrencyKindType::Daily, + createChargeSchedule(ChargingRateUnit::A)); + handler->add_charge_point_max_profile(profile_1); + handler->add_charge_point_max_profile(profile_2); + + auto profiles = handler->get_valid_profiles({}, {}, connector_id); + + ASSERT_EQ(2, profiles.size()); + + bool sut = handler->clear_all_profiles_with_filter(3, std::nullopt, std::nullopt, std::nullopt, true); + profiles = handler->get_valid_profiles({}, {}, connector_id); + + ASSERT_EQ(2, profiles.size()); ASSERT_FALSE(sut); } -TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__MatchingProfileId_CheckIdOnly__ReturnsTrue) { +// Default + +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__Default_OnlyOneMatchingProfileId_CheckIdOnly__ReturnsTrue) { const int connector_id = 1; addConnector(connector_id); auto handler = createSmartChargingHandler(); - auto profile_1 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A)); - profile_1.stackLevel = 1; - profile_1.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; - profile_1.chargingProfileKind = ChargingProfileKindType::Absolute; + auto profile = createChargingProfile(createChargeSchedule(ChargingRateUnit::A)); - auto profile_2 = createChargingProfile(2, createChargeSchedule(ChargingRateUnit::A)); - profile_2.stackLevel = 2; - profile_2.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; - profile_2.chargingProfileKind = ChargingProfileKindType::Absolute; + handler->add_tx_default_profile(profile, connector_id); + + auto profiles = handler->get_valid_profiles({}, {}, connector_id); + + ASSERT_EQ(1, profiles.size()); + + bool sut = handler->clear_all_profiles_with_filter(1, std::nullopt, std::nullopt, std::nullopt, true); + profiles = handler->get_valid_profiles({}, {}, connector_id); + ASSERT_EQ(0, profiles.size()); + + ASSERT_TRUE(sut); +} + +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__Default_MatchingProfileId_CheckIdOnly__ReturnsTrue) { + const int connector_id = 1; + addConnector(connector_id); + + auto handler = createSmartChargingHandler(); + + auto profile_1 = + createChargingProfile(1, 1, ChargingProfilePurposeType::TxDefaultProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); + + auto profile_2 = + createChargingProfile(2, 2, ChargingProfilePurposeType::TxDefaultProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); handler->add_tx_default_profile(profile_1, connector_id); handler->add_tx_default_profile(profile_2, connector_id); - + auto profiles = handler->get_valid_profiles({}, {}, connector_id); ASSERT_EQ(2, profiles.size()); - - bool sut = handler->clear_all_profiles_with_filter(1, {}, {}, {}, true); + bool sut = handler->clear_all_profiles_with_filter(1, std::nullopt, std::nullopt, std::nullopt, true); profiles = handler->get_valid_profiles({}, {}, connector_id); ASSERT_EQ(1, profiles.size()); ASSERT_TRUE(sut); - } -TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__MultipleNoMatchingProfileId_CheckIdOnly__ReturnsFalse) { +TEST_F(ChargepointTestFixture, + ClearAllProfilesWithFilter__Default_MultipleNoMatchingProfileId_CheckIdOnly__ReturnsFalse) { const int connector_id = 1; addConnector(connector_id); auto handler = createSmartChargingHandler(); - auto profile_1 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A)); - profile_1.stackLevel = 1; - profile_1.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; - profile_1.chargingProfileKind = ChargingProfileKindType::Absolute; + auto profile_1 = + createChargingProfile(1, 1, ChargingProfilePurposeType::TxDefaultProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); - auto profile_2 = createChargingProfile(2, createChargeSchedule(ChargingRateUnit::A)); - profile_2.stackLevel = 2; - profile_2.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; - profile_2.chargingProfileKind = ChargingProfileKindType::Absolute; + auto profile_2 = + createChargingProfile(2, 2, ChargingProfilePurposeType::TxDefaultProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); handler->add_tx_default_profile(profile_1, connector_id); handler->add_tx_default_profile(profile_2, connector_id); @@ -638,38 +823,93 @@ TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__MultipleNoMatchingPro ASSERT_EQ(2, profiles.size()); - bool sut = handler->clear_all_profiles_with_filter(3, {}, {}, {}, true); + bool sut = handler->clear_all_profiles_with_filter(3, std::nullopt, std::nullopt, std::nullopt, true); profiles = handler->get_valid_profiles({}, {}, connector_id); ASSERT_EQ(2, profiles.size()); ASSERT_FALSE(sut); } -//FIXME -/* -Bad cases +// TX + +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__Tx_OnlyOneMatchingProfileId_CheckIdOnly__ReturnsTrue) { + const int connector_id = 1; + addConnector(connector_id); + + auto handler = createSmartChargingHandler(); + + auto profile = createTxChargingProfile(createChargeSchedule(ChargingRateUnit::A)); + + handler->add_tx_profile(profile, connector_id); + + auto profiles = handler->get_valid_profiles({}, {}, connector_id); + + ASSERT_EQ(1, profiles.size()); + + bool sut = handler->clear_all_profiles_with_filter(1, std::nullopt, std::nullopt, std::nullopt, true); + profiles = handler->get_valid_profiles({}, {}, connector_id); + ASSERT_EQ(0, profiles.size()); + + ASSERT_TRUE(sut); +} + +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__Tx_MatchingProfileId_CheckIdOnly__ReturnsTrue) { + const int connector_id = 1; + addConnector(connector_id); + + auto handler = createSmartChargingHandler(); + + auto profile_1 = + createChargingProfile(1, 1, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); + + auto profile_2 = + createChargingProfile(2, 2, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); + + handler->add_tx_profile(profile_1, connector_id); + handler->add_tx_profile(profile_2, connector_id); + + auto profiles = handler->get_valid_profiles({}, {}, connector_id); + + ASSERT_EQ(2, profiles.size()); + + bool sut = handler->clear_all_profiles_with_filter(1, std::nullopt, std::nullopt, std::nullopt, true); + profiles = handler->get_valid_profiles({}, {}, connector_id); + + ASSERT_EQ(1, profiles.size()); + ASSERT_TRUE(sut); +} + +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__Tx_MultipleNoMatchingProfileId_CheckIdOnly__ReturnsFalse) { + const int connector_id = 1; + addConnector(connector_id); + + auto handler = createSmartChargingHandler(); + + auto profile_1 = + createChargingProfile(1, 1, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); -Data is -connector 100 - pid 1 - pid 2 -connector 200 - pid 1 - pid 2 + auto profile_2 = + createChargingProfile(2, 2, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); -if called clear profiles with filter the following happens + handler->add_tx_profile(profile_1, connector_id); + handler->add_tx_profile(profile_2, connector_id); -pid 1 connector 100 -connector 100 should have 1 profile(s) remaining -connector 200 should have 2 profile(s) remaining + auto profiles = handler->get_valid_profiles({}, {}, connector_id); + + ASSERT_EQ(2, profiles.size()); -currently tests shows -connector 100 having 0 -connector 200 having 1 + bool sut = handler->clear_all_profiles_with_filter(3, std::nullopt, std::nullopt, std::nullopt, true); + profiles = handler->get_valid_profiles({}, {}, connector_id); -flipped is the same -*/ -TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__MatchingProfileIdAndConnector_CheckIdOnly__ReturnsTrue) { + ASSERT_EQ(2, profiles.size()); + ASSERT_FALSE(sut); +} + +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__ConnectorId__ReturnsTrue) { const int connector_id_1 = 100; addConnector(connector_id_1); @@ -678,32 +918,28 @@ TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__MatchingProfileIdAndC auto handler = createSmartChargingHandler(); - auto profile_c1_1 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A)); - profile_c1_1.stackLevel = 1; - profile_c1_1.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; - profile_c1_1.chargingProfileKind = ChargingProfileKindType::Absolute; + auto profile_c1_1 = + createChargingProfile(1, 1, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); - auto profile_c1_2 = createChargingProfile(2, createChargeSchedule(ChargingRateUnit::A)); - profile_c1_2.stackLevel = 2; - profile_c1_2.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; - profile_c1_2.chargingProfileKind = ChargingProfileKindType::Absolute; + auto profile_c1_2 = + createChargingProfile(2, 2, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); - auto profile_c2_1 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A)); - profile_c2_1.stackLevel = 1; - profile_c2_1.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; - profile_c2_1.chargingProfileKind = ChargingProfileKindType::Absolute; + auto profile_c2_3 = + createChargingProfile(3, 1, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); - auto profile_c2_2 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A)); - profile_c2_2.stackLevel = 2; - profile_c2_2.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; - profile_c2_2.chargingProfileKind = ChargingProfileKindType::Absolute; + auto profile_c2_4 = + createChargingProfile(4, 2, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); handler->add_tx_profile(profile_c1_1, connector_id_1); handler->add_tx_profile(profile_c1_2, connector_id_1); - handler->add_tx_profile(profile_c2_1, connector_id_2); - handler->add_tx_profile(profile_c2_2, connector_id_2); - + handler->add_tx_profile(profile_c2_3, connector_id_2); + handler->add_tx_profile(profile_c2_4, connector_id_2); + auto connector_id_1_profiles = handler->get_valid_profiles({}, {}, connector_id_1); auto connector_id_2_profiles = handler->get_valid_profiles({}, {}, connector_id_2); @@ -712,44 +948,71 @@ TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__MatchingProfileIdAndC auto check_id_only = false; - bool sut = handler->clear_all_profiles_with_filter(1, connector_id_1, std::nullopt, std::nullopt, check_id_only); - + bool sut = handler->clear_all_profiles_with_filter(std::nullopt, connector_id_1, std::nullopt, std::nullopt, + check_id_only); + connector_id_1_profiles = handler->get_valid_profiles({}, {}, connector_id_1); connector_id_2_profiles = handler->get_valid_profiles({}, {}, connector_id_2); - ASSERT_EQ(1, connector_id_1_profiles.size()); // Deleted everything on connector id 100 - ASSERT_EQ(2, connector_id_2_profiles.size()); // Deleted everything on connector id 200 + ASSERT_EQ(0, connector_id_1_profiles.size()); + ASSERT_EQ(2, connector_id_2_profiles.size()); ASSERT_TRUE(sut); - } -//FIXME -/* -Bad cases +// TODO: Needs negative -Broke each profile into a diff variable to confirm that I was not deleting shared ptrs... +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__StackLevel__ReturnsTrue) { + const int connector_id_1 = 100; + addConnector(connector_id_1); + + const int connector_id_2 = 200; + addConnector(connector_id_2); + + auto handler = createSmartChargingHandler(); + + auto profile_c1_1 = + createChargingProfile(1, 1, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); -Data is -connector 100 - pid 1 - pid 2 -connector 200 - pid 3 - pid 4 + auto profile_c1_2 = + createChargingProfile(2, 2, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); -if called clear profiles with filter the following happens + auto profile_c2_3 = + createChargingProfile(3, 1, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); -pid 999 connector 100 + auto profile_c2_4 = + createChargingProfile(4, 2, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); -connector 100 should have 2 profile(s) remaining -connector 200 should have 2 profile(s) remaining + handler->add_tx_profile(profile_c1_1, connector_id_1); + handler->add_tx_profile(profile_c1_2, connector_id_1); + + handler->add_tx_profile(profile_c2_3, connector_id_2); + handler->add_tx_profile(profile_c2_4, connector_id_2); + + auto connector_id_1_profiles = handler->get_valid_profiles({}, {}, connector_id_1); + auto connector_id_2_profiles = handler->get_valid_profiles({}, {}, connector_id_2); -currently tests shows -connector 100 having 0 -connector 200 having 2???????????? -*/ + ASSERT_EQ(2, connector_id_1_profiles.size()); + ASSERT_EQ(2, connector_id_2_profiles.size()); -TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__WithProfileIdMatchingAndConnector_CheckIdOnly__ReturnsTrue) { + auto check_id_only = false; + + bool sut = handler->clear_all_profiles_with_filter(std::nullopt, std::nullopt, 1, std::nullopt, check_id_only); + + connector_id_1_profiles = handler->get_valid_profiles({}, {}, connector_id_1); + connector_id_2_profiles = handler->get_valid_profiles({}, {}, connector_id_2); + + ASSERT_EQ(1, connector_id_1_profiles.size()); + ASSERT_EQ(1, connector_id_2_profiles.size()); + ASSERT_TRUE(sut); +} + +// TODO: Needs negative + +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__ChargingProfilePurposeType__ReturnsTrue) { const int connector_id_1 = 100; addConnector(connector_id_1); @@ -758,32 +1021,28 @@ TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__WithProfileIdMatching auto handler = createSmartChargingHandler(); - auto profile_c1_1 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A)); - profile_c1_1.stackLevel = 1; - profile_c1_1.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; - profile_c1_1.chargingProfileKind = ChargingProfileKindType::Absolute; + auto profile_c1_1 = + createChargingProfile(1, 1, ChargingProfilePurposeType::TxDefaultProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); - auto profile_c1_2 = createChargingProfile(2, createChargeSchedule(ChargingRateUnit::A)); - profile_c1_2.stackLevel = 2; - profile_c1_2.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; - profile_c1_2.chargingProfileKind = ChargingProfileKindType::Absolute; + auto profile_c1_2 = + createChargingProfile(2, 2, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); - auto profile_c2_1 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A)); - profile_c2_1.stackLevel = 1; - profile_c2_1.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; - profile_c2_1.chargingProfileKind = ChargingProfileKindType::Absolute; + auto profile_c2_3 = + createChargingProfile(3, 1, ChargingProfilePurposeType::TxDefaultProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); - auto profile_c2_2 = createChargingProfile(2, createChargeSchedule(ChargingRateUnit::A)); - profile_c2_2.stackLevel = 2; - profile_c2_2.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; - profile_c2_2.chargingProfileKind = ChargingProfileKindType::Absolute; + auto profile_c2_4 = + createChargingProfile(4, 2, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); - handler->add_tx_profile(profile_c1_1, connector_id_1); + handler->add_tx_default_profile(profile_c1_1, connector_id_1); handler->add_tx_profile(profile_c1_2, connector_id_1); - handler->add_tx_profile(profile_c2_1, connector_id_2); - handler->add_tx_profile(profile_c2_2, connector_id_2); - + handler->add_tx_default_profile(profile_c2_3, connector_id_2); + handler->add_tx_profile(profile_c2_4, connector_id_2); + auto connector_id_1_profiles = handler->get_valid_profiles({}, {}, connector_id_1); auto connector_id_2_profiles = handler->get_valid_profiles({}, {}, connector_id_2); @@ -792,17 +1051,69 @@ TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__WithProfileIdMatching auto check_id_only = false; - bool sut = handler->clear_all_profiles_with_filter(999, connector_id_1, std::nullopt, std::nullopt, check_id_only); - + bool sut = handler->clear_all_profiles_with_filter(std::nullopt, std::nullopt, std::nullopt, + ChargingProfilePurposeType::TxDefaultProfile, check_id_only); + connector_id_1_profiles = handler->get_valid_profiles({}, {}, connector_id_1); connector_id_2_profiles = handler->get_valid_profiles({}, {}, connector_id_2); - ASSERT_EQ(2, connector_id_1_profiles.size()); // Deleted everything on connector id 100 - ASSERT_EQ(2, connector_id_2_profiles.size()); // Deleted everything on connector id 100 + ASSERT_EQ(1, connector_id_1_profiles.size()); + ASSERT_EQ(1, connector_id_2_profiles.size()); ASSERT_TRUE(sut); - } +// TODO: Needs negative + +TEST_F(ChargepointTestFixture, + ClearAllProfilesWithFilter__ConnectorIdStackLevelChargingProfilePurposeType__ReturnsTrue) { + const int connector_id_1 = 100; + addConnector(connector_id_1); + + const int connector_id_2 = 200; + addConnector(connector_id_2); + + auto handler = createSmartChargingHandler(); + + auto profile_c1_1 = + createChargingProfile(1, 1, ChargingProfilePurposeType::TxDefaultProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); + + auto profile_c1_2 = + createChargingProfile(2, 2, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); + + auto profile_c2_3 = + createChargingProfile(3, 1, ChargingProfilePurposeType::TxDefaultProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); + + auto profile_c2_4 = + createChargingProfile(4, 2, ChargingProfilePurposeType::TxProfile, ChargingProfileKindType::Absolute, + RecurrencyKindType::Daily, createChargeSchedule(ChargingRateUnit::A)); + + handler->add_tx_default_profile(profile_c1_1, connector_id_1); + handler->add_tx_profile(profile_c1_2, connector_id_1); + + handler->add_tx_default_profile(profile_c2_3, connector_id_2); + handler->add_tx_profile(profile_c2_4, connector_id_2); + + auto connector_id_1_profiles = handler->get_valid_profiles({}, {}, connector_id_1); + auto connector_id_2_profiles = handler->get_valid_profiles({}, {}, connector_id_2); + + ASSERT_EQ(2, connector_id_1_profiles.size()); + ASSERT_EQ(2, connector_id_2_profiles.size()); + + auto check_id_only = false; + + bool sut = handler->clear_all_profiles_with_filter(std::nullopt, 100, 1, + ChargingProfilePurposeType::TxDefaultProfile, check_id_only); + + connector_id_1_profiles = handler->get_valid_profiles({}, {}, connector_id_1); + connector_id_2_profiles = handler->get_valid_profiles({}, {}, connector_id_2); + + ASSERT_EQ(1, connector_id_1_profiles.size()); + ASSERT_EQ(2, connector_id_2_profiles.size()); + ASSERT_TRUE(sut); +} } // namespace v16 } // namespace ocpp \ No newline at end of file