Skip to content

Commit

Permalink
Fix unit tests and paths. Update unit test device model database.
Browse files Browse the repository at this point in the history
Signed-off-by: Maaike Zijderveld, iolar <[email protected]>
  • Loading branch information
maaikez committed Jun 28, 2024
1 parent 29b33a0 commit 6b3e007
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 19 deletions.
27 changes: 26 additions & 1 deletion include/ocpp/v201/charge_point.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ class ChargePoint : public ChargePointInterface, private ocpp::ChargingStationBa
ocpp::CallResult<ResponseType> call_result = enhanced_response.message;
return call_result.msg;
};
};
}

/// \brief Checks if all connectors are effectively inoperative.
/// If this is the case, calls the all_connectors_unavailable_callback
Expand All @@ -763,6 +763,31 @@ class ChargePoint : public ChargePointInterface, private ocpp::ChargingStationBa
void execute_change_availability_request(ChangeAvailabilityRequest request, bool persist);

public:
/// \brief Construct a new ChargePoint object
/// \param evse_connector_structure Map that defines the structure of EVSE and connectors of the chargepoint. The
/// key represents the id of the EVSE and the value represents the number of connectors for this EVSE. The ids of
/// the EVSEs have to increment starting with 1.
/// \param device_model_storage_address address to device model storage (e.g. location of SQLite database)
/// \param initialize_device_model Set to true to initialize the device model database
/// \param device_model_migration_path Path to the device model database migration files
/// \param device_model_schemas_path Path to the device model schemas
/// \param config_path Path to the chargepoint configuration
/// \param ocpp_main_path Path where utility files for OCPP are read and written to
/// \param core_database_path Path to directory where core database is located
/// \param message_log_path Path to where logfiles are written to
/// \param evse_security Pointer to evse_security that manages security related operations; if nullptr
/// security_configuration must be set
/// \param callbacks Callbacks that will be registered for ChargePoint
/// \param security_configuration specifies the file paths that are required to set up the internal evse_security
/// implementation
ChargePoint(const std::map<int32_t, int32_t>& evse_connector_structure,
const std::string& device_model_storage_address, const bool initialize_device_model,
const std::string& device_model_migration_path, const std::string& device_model_schemas_path,
const std::string& config_path, const std::string& ocpp_main_path,
const std::string& core_database_path, const std::string& sql_init_path,
const std::string& message_log_path, const std::shared_ptr<EvseSecurity> evse_security,
const Callbacks& callbacks);

/// \brief Construct a new ChargePoint object
/// \param evse_connector_structure Map that defines the structure of EVSE and connectors of the chargepoint. The
/// key represents the id of the EVSE and the value represents the number of connectors for this EVSE. The ids of
Expand Down
21 changes: 20 additions & 1 deletion include/ocpp/v201/device_model_storage_sqlite.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,26 @@ class DeviceModelStorageSqlite : public DeviceModelStorage {

public:
/// \brief Opens SQLite connection at given \p db_path
/// \param db_path path to database
///
/// With this constructor, you can initialize the database as well.
///
/// \param db_path Path to database
/// \param migration_files_path Path to the migration files to initialize the database (only needs to be set if
/// `init_db` is true)
/// \param schemas_path Path to the device model schemas (only needs to be set if `init_db` is true)
/// \param config_path Path to the configuration file (only needs to be set if `init_db` is true)
/// \param init_db True to initialize the database
///
explicit DeviceModelStorageSqlite(const fs::path& db_path, const std::filesystem::path& migration_files_path,
const std::filesystem::path& schemas_path,
const std::filesystem::path& config_path, const bool init_db);

/// \brief Opens SQLite connection at given \p db_path
///
/// Will not initialize the database.
///
/// \param db_path Path to database
///
explicit DeviceModelStorageSqlite(const fs::path& db_path);
~DeviceModelStorageSqlite() = default;

Expand Down
15 changes: 14 additions & 1 deletion lib/ocpp/v201/charge_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <ocpp/v201/charge_point.hpp>
#include <ocpp/v201/ctrlr_component_variables.hpp>
#include <ocpp/v201/device_model_storage_sqlite.hpp>
#include <ocpp/v201/init_device_model_db.hpp>
#include <ocpp/v201/messages/FirmwareStatusNotification.hpp>
#include <ocpp/v201/messages/LogStatusNotification.hpp>
#include <ocpp/v201/notify_report_requests_splitter.hpp>
Expand Down Expand Up @@ -47,6 +46,20 @@ bool Callbacks::all_callbacks_valid() const {
(!this->ocpp_messages_callback.has_value() or this->ocpp_messages_callback.value() != nullptr);
}

ChargePoint::ChargePoint(const std::map<int32_t, int32_t>& evse_connector_structure,
const std::string& device_model_storage_address, const bool initialize_device_model,
const std::string& device_model_migration_path, const std::string& device_model_schemas_path,
const std::string& config_path, const std::string& ocpp_main_path,
const std::string& core_database_path, const std::string& sql_init_path,
const std::string& message_log_path, const std::shared_ptr<EvseSecurity> evse_security,
const Callbacks& callbacks) :
ChargePoint(evse_connector_structure,
std::make_unique<DeviceModelStorageSqlite>(device_model_storage_address, device_model_migration_path,
device_model_schemas_path, config_path,
initialize_device_model),
ocpp_main_path, core_database_path, sql_init_path, message_log_path, evse_security, callbacks) {
}

ChargePoint::ChargePoint(const std::map<int32_t, int32_t>& evse_connector_structure,
const std::string& device_model_storage_address, const std::string& ocpp_main_path,
const std::string& core_database_path, const std::string& sql_init_path,
Expand Down
25 changes: 23 additions & 2 deletions lib/ocpp/v201/device_model_storage_sqlite.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2020 - 2023 Pionix GmbH and Contributors to EVerest

#include "ocpp/v201/init_device_model_db.hpp"
#include <ocpp/v201/device_model_storage_sqlite.hpp>

#include <everest/logging.hpp>
#include <ocpp/common/database/sqlite_statement.hpp>
#include <ocpp/v201/device_model_storage_sqlite.hpp>
#include <ocpp/v201/charge_point.hpp>
#include <ocpp/v201/init_device_model_db.hpp>

namespace ocpp {

Expand All @@ -14,7 +18,20 @@ namespace v201 {
extern void filter_criteria_monitors(const std::vector<MonitoringCriterionEnum>& criteria,
std::vector<VariableMonitoringMeta>& monitors);

DeviceModelStorageSqlite::DeviceModelStorageSqlite(const fs::path& db_path) {
DeviceModelStorageSqlite::DeviceModelStorageSqlite(const fs::path& db_path, const fs::path& migration_files_path,
const fs::path& schemas_path, const fs::path& config_path,
const bool init_db) {
if (init_db) {
if (db_path.empty() || migration_files_path.empty() || schemas_path.empty() || config_path.empty()) {
EVLOG_error << "Can not initialize device model storage: one of the paths is empty.";
EVLOG_AND_THROW(
DeviceModelStorageError("Can not initialize device model storage: one of the paths is empty."));
}
InitDeviceModelDb init_device_model_db(db_path, migration_files_path);
init_device_model_db.initialize_database(schemas_path, false);
init_device_model_db.insert_config_and_default_values(schemas_path, config_path);
}

db = std::make_unique<ocpp::common::DatabaseConnection>(db_path);

if (!db->open_connection()) {
Expand All @@ -25,6 +42,10 @@ DeviceModelStorageSqlite::DeviceModelStorageSqlite(const fs::path& db_path) {
}
}

DeviceModelStorageSqlite::DeviceModelStorageSqlite(const fs::path& db_path) :
DeviceModelStorageSqlite(db_path, "", "", "", false) {
}

int DeviceModelStorageSqlite::get_component_id(const Component& component_id) {
std::string select_query =
"SELECT ID FROM COMPONENT WHERE NAME = ? AND INSTANCE IS ? AND EVSE_ID IS ? AND CONNECTOR_ID IS ?";
Expand Down
12 changes: 11 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ set(MIGRATION_FILES_LOCATION_V16 "${CMAKE_CURRENT_BINARY_DIR}/resources/v16/migr
set(MIGRATION_FILES_LOCATION_V201 "${CMAKE_CURRENT_BINARY_DIR}/resources/v201/migration_files")
set(MIGRATION_FILES_DEVICE_MODEL_LOCATION_V201 "${CMAKE_CURRENT_BINARY_DIR}/resources/v201/device_model_migration_files")
set(DEVICE_MODEL_DB_LOCATION_V201 "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATADIR}/everest/modules/OCPP201/device_model_storage.db")
set(DEVICE_MODEL_COMPONENT_SCHEMAS_LOCATION_V201 "${CMAKE_CURRENT_BINARY_DIR}/resources/v201/component_schemas")
set(DEVICE_MODEL_CONFIG_LOCATION_V201 "${CMAKE_CURRENT_BINARY_DIR}/resources/v201/config.json")
set(DEVICE_MODEL_COMPONENT_SCHEMAS_LOCATION_V201_CHANGED "${CMAKE_CURRENT_BINARY_DIR}/resources/v201/changed/component_schemas")
set(DEVICE_MODEL_CONFIG_LOCATION_V201_CHANGED "${CMAKE_CURRENT_BINARY_DIR}/resources/v201/changed/config.json")

add_executable(database_tests database_tests.cpp)

Expand Down Expand Up @@ -44,7 +48,13 @@ add_custom_command(TARGET libocpp_unit_tests POST_BUILD
COMMAND ${CMAKE_COMMAND} -E remove_directory ${MIGRATION_FILES_LOCATION_V201}
COMMAND ${CMAKE_COMMAND} -E copy_directory ${MIGRATION_FILES_SOURCE_DIR_V201} ${MIGRATION_FILES_LOCATION_V201}
COMMAND ${CMAKE_COMMAND} -E remove_directory ${MIGRATION_FILES_DEVICE_MODEL_LOCATION_V201}
COMMAND ${CMAKE_COMMAND} -E copy_directory ${MIGRATION_FILES_SOURCE_DIR_V201} ${MIGRATION_FILES_DEVICE_MODEL_LOCATION_V201}
COMMAND ${CMAKE_COMMAND} -E copy_directory ${MIGRATION_FILES_DEVICE_MODEL_SOURCE_DIR_V201} ${MIGRATION_FILES_DEVICE_MODEL_LOCATION_V201}
COMMAND ${CMAKE_COMMAND} -E remove_directory ${DEVICE_MODEL_COMPONENT_SCHEMAS_LOCATION_V201}
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/config/v201/resources/component_schemas ${DEVICE_MODEL_COMPONENT_SCHEMAS_LOCATION_V201}
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/config/v201/resources/config.json ${DEVICE_MODEL_CONFIG_LOCATION_V201}
COMMAND ${CMAKE_COMMAND} -E remove_directory ${DEVICE_MODEL_COMPONENT_SCHEMAS_LOCATION_V201_CHANGED}
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/config/v201/resources_changed/component_schemas ${DEVICE_MODEL_COMPONENT_SCHEMAS_LOCATION_V201_CHANGED}
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/config/v201/resources_changed/config.json ${DEVICE_MODEL_CONFIG_LOCATION_V201_CHANGED}
)

add_test(libocpp_unit_tests libocpp_unit_tests)
Expand Down
18 changes: 5 additions & 13 deletions tests/lib/ocpp/v201/test_init_device_model_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,12 @@ namespace ocpp::v201 {

class InitDeviceModelDbTest : public DatabaseTestingUtils {
protected:
const std::string DATABASE_PATH2 = "/tmp/pionix/test_db2.db";
const std::string DATABASE_PATH = "file::memory:?cache=shared";
const std::string MIGRATION_FILES_PATH = "/data/work/pionix/workspace/libocpp/config/v201/device_model_migrations";
// const std::string SCHEMAS_PATH = "/data/work/pionix/workspace/libocpp/config/v201/component_schemas";
const std::string SCHEMAS_PATH =
"/data/work/pionix/workspace/libocpp/tests/config/v201/resources/component_schemas";
const std::string SCHEMAS_PATH_CHANGED =
"/data/work/pionix/workspace/libocpp/tests/config/v201/resources_changed/component_schemas";
// const std::string CONFIG_PATH = "/data/work/pionix/workspace/libocpp/config/v201/config.json";
const std::string CONFIG_PATH = "/data/work/pionix/workspace/libocpp/tests/config/v201/resources/config.json";
const std::string CONFIG_PATH_CHANGED =
"/data/work/pionix/workspace/libocpp/tests/config/v201/resources_changed/config.json";

// std::unique_ptr<DatabaseConnectionInterface> database;
const std::string MIGRATION_FILES_PATH = "./resources/v201/device_model_migrations";
const std::string SCHEMAS_PATH = "./resources/v201/component_schemas";
const std::string SCHEMAS_PATH_CHANGED = "./resources/v201/changed/component_schemas";
const std::string CONFIG_PATH = "./resources/v201/config.json";
const std::string CONFIG_PATH_CHANGED = "./resources/v201/changed/config.json";

public:
InitDeviceModelDbTest() {
Expand Down
Binary file modified tests/resources/unittest_device_model.db
Binary file not shown.

0 comments on commit 6b3e007

Please sign in to comment.