From 86dabcebe033fe6bc742f80f758fb4f89a38c52d Mon Sep 17 00:00:00 2001 From: Coury Richards <146002925+couryrr-afs@users.noreply.github.com> Date: Fri, 26 Jan 2024 17:10:06 +0000 Subject: [PATCH] Added transaction initalization to tests Signed-off-by: Coury Richards <146002925+couryrr-afs@users.noreply.github.com> --- .../ocpp/v16/test_smart_charging_handler.cpp | 215 +++++++++++++++++- 1 file changed, 203 insertions(+), 12 deletions(-) diff --git a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp index 43f23f760..4085cac4d 100644 --- a/tests/lib/ocpp/v16/test_smart_charging_handler.cpp +++ b/tests/lib/ocpp/v16/test_smart_charging_handler.cpp @@ -1,6 +1,7 @@ #include #include #include +#include namespace fs = std::filesystem; #include @@ -45,8 +46,13 @@ class ChargepointTestFixture : public testing::Test { void SetUp() override { } - void addConnector(int id){ - connectors[id] = std::make_shared(Connector{id}); + 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() { @@ -63,9 +69,10 @@ class ChargepointTestFixture : public testing::Test { } ChargingProfile createChargingProfile(std::optional id, ChargingSchedule chargingSchedule) { + // Is this working? auto chargingProfileId = 1; if (id) { - auto chargingProfileId = id.value(); + chargingProfileId = id.value(); } auto stackLevel = 1; auto chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; @@ -581,20 +588,31 @@ TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__MatchingProfileId_Che addConnector(connector_id); auto handler = createSmartChargingHandler(); - - auto profile_max = createChargingProfile(9, createChargeSchedule(ChargingRateUnit::A)); auto profile_1 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A)); + profile_1.stackLevel = 1; + profile_1.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; + profile_1.chargingProfileKind = ChargingProfileKindType::Absolute; + 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_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); + profiles = handler->get_valid_profiles({}, {}, connector_id); + ASSERT_EQ(1, profiles.size()); ASSERT_TRUE(sut); + } TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__MultipleNoMatchingProfileId_CheckIdOnly__ReturnsFalse) { @@ -603,15 +621,188 @@ TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__MultipleNoMatchingPro auto handler = createSmartChargingHandler(); - auto profile = createChargingProfile({}, createChargeSchedule(ChargingRateUnit::A)); + auto profile_1 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A)); + profile_1.stackLevel = 1; + profile_1.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; + profile_1.chargingProfileKind = ChargingProfileKindType::Absolute; - handler->add_tx_default_profile(profile, connector_id); - handler->add_tx_default_profile(profile, connector_id); + auto profile_2 = createChargingProfile(2, createChargeSchedule(ChargingRateUnit::A)); + profile_2.stackLevel = 2; + profile_2.chargingProfilePurpose = ChargingProfilePurposeType::TxDefaultProfile; + profile_2.chargingProfileKind = ChargingProfileKindType::Absolute; - bool sut = handler->clear_all_profiles_with_filter(2, {}, {}, {}, true); + 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(3, {}, {}, {}, true); + profiles = handler->get_valid_profiles({}, {}, connector_id); + + ASSERT_EQ(2, profiles.size()); ASSERT_FALSE(sut); } +//FIXME +/* +Bad cases + +Data is +connector 100 + pid 1 + pid 2 +connector 200 + pid 1 + pid 2 + +if called clear profiles with filter the following happens + +pid 1 connector 100 +connector 100 should have 1 profile(s) remaining +connector 200 should have 2 profile(s) remaining + +currently tests shows +connector 100 having 0 +connector 200 having 1 + +flipped is the same +*/ +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__MatchingProfileIdAndConnector_CheckIdOnly__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, createChargeSchedule(ChargingRateUnit::A)); + profile_c1_1.stackLevel = 1; + profile_c1_1.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; + profile_c1_1.chargingProfileKind = ChargingProfileKindType::Absolute; + + 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_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_2 = createChargingProfile(1, createChargeSchedule(ChargingRateUnit::A)); + profile_c2_2.stackLevel = 2; + profile_c2_2.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; + profile_c2_2.chargingProfileKind = ChargingProfileKindType::Absolute; + + 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); + + 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(1, 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_TRUE(sut); + +} + +//FIXME +/* +Bad cases + +Broke each profile into a diff variable to confirm that I was not deleting shared ptrs... + +Data is +connector 100 + pid 1 + pid 2 +connector 200 + pid 3 + pid 4 + +if called clear profiles with filter the following happens + +pid 999 connector 100 + +connector 100 should have 2 profile(s) remaining +connector 200 should have 2 profile(s) remaining + +currently tests shows +connector 100 having 0 +connector 200 having 2???????????? +*/ + +TEST_F(ChargepointTestFixture, ClearAllProfilesWithFilter__WithProfileIdMatchingAndConnector_CheckIdOnly__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, createChargeSchedule(ChargingRateUnit::A)); + profile_c1_1.stackLevel = 1; + profile_c1_1.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; + profile_c1_1.chargingProfileKind = ChargingProfileKindType::Absolute; + + 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_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_2 = createChargingProfile(2, createChargeSchedule(ChargingRateUnit::A)); + profile_c2_2.stackLevel = 2; + profile_c2_2.chargingProfilePurpose = ChargingProfilePurposeType::TxProfile; + profile_c2_2.chargingProfileKind = ChargingProfileKindType::Absolute; + + 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); + + 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(999, 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(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_TRUE(sut); + +} + + } // namespace v16 } // namespace ocpp \ No newline at end of file