diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp index c31e7c7..2ffb52b 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp @@ -19,15 +19,21 @@ struct PGM_IO_VnfConverter : public PgmVnfConverter { }; // TODO(Laurynas-Jagutis) add call_with_catch for these functions. -PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(const PGM_IO_Handle* /*handle*/, char* file_buffer, +PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char* file_buffer, PGM_IO_Idx experimental_features) { - auto* converter = new PGM_IO_VnfConverter(file_buffer, nullptr, experimental_features); - parse_vnf_file_wrapper(converter); - return converter; + return call_with_catch( + handle, + [file_buffer, experimental_features] { + auto* converter = new PGM_IO_VnfConverter(file_buffer, nullptr, experimental_features); + parse_vnf_file_wrapper(converter); + return converter; + }, + PGM_IO_regular_error); } -char const* PGM_IO_get_vnf_input_data(const PGM_IO_Handle* /*handle*/, PGM_IO_VnfConverter* converter_ptr) { - return convert_input_wrapper(converter_ptr).c_str(); +char const* PGM_IO_get_vnf_input_data(PGM_IO_Handle* handle, PGM_IO_VnfConverter* converter_ptr) { + return call_with_catch( + handle, [converter_ptr] { return convert_input_wrapper(converter_ptr).c_str(); }, PGM_IO_regular_error); } void PGM_IO_destroy_vnf_converter(PGM_IO_VnfConverter* converter_ptr) { delete converter_ptr; } diff --git a/tests/c_api_tests/test_c_api_vnf_converter.cpp b/tests/c_api_tests/test_c_api_vnf_converter.cpp index c2e3817..4a16644 100644 --- a/tests/c_api_tests/test_c_api_vnf_converter.cpp +++ b/tests/c_api_tests/test_c_api_vnf_converter.cpp @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: MPL-2.0 +#include #include #include @@ -12,17 +13,40 @@ namespace power_grid_model_io_native { TEST_CASE("Test PGM_IO_create_vnf_converter") { - PGM_IO_Handle* handle = PGM_IO_create_handle(); PGM_IO_Idx experimental_feature_flag = 0; SUBCASE("Test PGM_IO_create_vnf_converter without experimental feature flag") { - CHECK_THROWS_AS(PGM_IO_create_vnf_converter(handle, nullptr, experimental_feature_flag), - power_grid_model::ExperimentalFeature); + PGM_IO_Handle* handle0 = PGM_IO_create_handle(); + auto converter = PGM_IO_create_vnf_converter(handle0, nullptr, experimental_feature_flag); + CHECK(PGM_IO_error_code(handle0) == PGM_IO_regular_error); + PGM_IO_destroy_vnf_converter(converter); + PGM_IO_destroy_handle(handle0); } SUBCASE("Test PGM_IO_create_vnf_converter with experimental feature flag") { - PGM_IO_Idx experimental_feature_flag = 1; + PGM_IO_Handle* handle1 = PGM_IO_create_handle(); + experimental_feature_flag = 1; + auto converter = PGM_IO_create_vnf_converter(handle1, nullptr, experimental_feature_flag); + CHECK(converter != nullptr); + PGM_IO_destroy_vnf_converter(converter); + PGM_IO_destroy_handle(handle1); } -}; +} + +TEST_CASE("Test PGM_IO_get_vnf_input_data") { + PGM_IO_Handle* handle = PGM_IO_create_handle(); + PGM_IO_Idx experimental_feature_flag = 0; + + experimental_feature_flag = 1; + auto converter = PGM_IO_create_vnf_converter(handle, nullptr, experimental_feature_flag); + CHECK(converter != nullptr); + + auto json_result = PGM_IO_get_vnf_input_data(handle, converter); + auto json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; + CHECK(strcmp(json_string, json_result) == 0); + + PGM_IO_destroy_vnf_converter(converter); + PGM_IO_destroy_handle(handle); +} -} // namespace power_grid_model_io_native \ No newline at end of file +} // namespace power_grid_model_io_native diff --git a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp index c7b2fbd..c0b5fe5 100644 --- a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp +++ b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp @@ -11,6 +11,7 @@ namespace power_grid_model_io_native { TEST_CASE("Test converter constructor") { SUBCASE("Without experimental features enabled") { + PgmVnfConverter(nullptr, nullptr, 0); CHECK_THROWS_AS(PgmVnfConverter(nullptr, nullptr, 0), power_grid_model::ExperimentalFeature); }