diff --git a/libs/utils/CMakeLists.txt b/libs/utils/CMakeLists.txt index e1b498359..064c9683f 100644 --- a/libs/utils/CMakeLists.txt +++ b/libs/utils/CMakeLists.txt @@ -29,6 +29,7 @@ if (UTILS) src/version.c src/version_range.c src/properties.c + src/properties_serialization.c src/utils.c src/filter.c src/celix_log_level.c diff --git a/libs/utils/gtest/CMakeLists.txt b/libs/utils/gtest/CMakeLists.txt index d04f9e30c..62c1ac0c1 100644 --- a/libs/utils/gtest/CMakeLists.txt +++ b/libs/utils/gtest/CMakeLists.txt @@ -29,6 +29,7 @@ add_executable(test_utils src/CelixUtilsTestSuite.cc src/ConvertUtilsTestSuite.cc src/PropertiesTestSuite.cc + src/PropertiesSerializationTestSuite.cc src/VersionTestSuite.cc src/ErrTestSuite.cc src/ThreadsTestSuite.cc diff --git a/libs/utils/gtest/src/PropertiesSerializationTestSuite.cc b/libs/utils/gtest/src/PropertiesSerializationTestSuite.cc new file mode 100644 index 000000000..d3bf6aa06 --- /dev/null +++ b/libs/utils/gtest/src/PropertiesSerializationTestSuite.cc @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include + +#include "celix_err.h" +#include "celix_properties.h" + +using ::testing::MatchesRegex; + +class PropertiesSerializationTestSuite : public ::testing::Test { + public: + PropertiesSerializationTestSuite() { celix_err_resetErrors(); } +}; + +TEST_F(PropertiesSerializationTestSuite, SaveEmptyPropertiesTest) { + //Given an empty properties object + celix_autoptr(celix_properties_t) props = celix_properties_create(); + + //And an in-memory stream + char* buf = nullptr; + size_t bufLen = 0; + FILE* stream = open_memstream(&buf, &bufLen); + + //When saving the properties to the stream + auto status = celix_properties_saveToStream(props, stream); + EXPECT_EQ(CELIX_SUCCESS, status); + + //Then the stream contains an empty JSON object + fclose(stream); + EXPECT_STREQ("{}", buf); +} + +TEST_F(PropertiesSerializationTestSuite, LoadEmptyPropertiesTest) { + //Given an empty JSON object + const char* json = "{}"; + FILE* stream = fmemopen((void*)json, strlen(json), "r"); + + //When loading the properties from the stream + celix_autoptr(celix_properties_t) props = nullptr; + auto status = celix_properties_loadFromStream(stream, &props); + EXPECT_EQ(CELIX_SUCCESS, status); + + //Then the properties object is empty + EXPECT_EQ(0, celix_properties_size(props)); + + fclose(stream); +} diff --git a/libs/utils/include/celix_properties.h b/libs/utils/include/celix_properties.h index 39e87c660..5903ecaa3 100644 --- a/libs/utils/include/celix_properties.h +++ b/libs/utils/include/celix_properties.h @@ -180,6 +180,12 @@ CELIX_UTILS_EXPORT celix_status_t celix_properties_store(celix_properties_t* pro const char* file, const char* header); +//TODO doc +CELIX_UTILS_EXPORT celix_status_t celix_properties_saveToStream(const celix_properties_t* properties, FILE* stream); + +//TODO doc +CELIX_UTILS_EXPORT celix_status_t celix_properties_loadFromStream(FILE* stream, celix_properties_t** out); + /** * @brief Get the entry for a given key in a property set. * diff --git a/libs/utils/src/properties_serialization.c b/libs/utils/src/properties_serialization.c new file mode 100644 index 000000000..4ebda01e9 --- /dev/null +++ b/libs/utils/src/properties_serialization.c @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "celix_properties.h" + +celix_status_t celix_properties_saveToStream(const celix_properties_t* properties, FILE* stream) { + celix_status_t status = CELIX_SUCCESS; + (void)properties; + fprintf(stream, "{}"); + return status; +} + +celix_status_t celix_properties_loadFromStream(FILE* stream, celix_properties_t** out) { + celix_status_t status = CELIX_SUCCESS; + (void)stream; + celix_autoptr(celix_properties_t) props = celix_properties_create(); + *out = celix_steal_ptr(props); + return status; +} \ No newline at end of file