Line data Source code
1 : /**
2 : * @file schema_conversion_test.cxx Test application that tests and demonstrates
3 : * the conversion from a generic protobuf message to an opmon entry.
4 : *
5 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
6 : * Licensing/copyright details are in the COPYING file that you should have
7 : * received with this code.
8 : */
9 :
10 : #include "opmonlib/Utils.hpp"
11 : #include "opmonlib/opmon/test.pb.h"
12 :
13 : #define BOOST_TEST_MODULE schema_conversion_test // NOLINT
14 :
15 : #include "boost/test/unit_test.hpp"
16 :
17 : #include <google/protobuf/util/json_util.h>
18 :
19 : using namespace dunedaq::opmonlib;
20 : using namespace dunedaq::opmon;
21 :
22 : BOOST_AUTO_TEST_SUITE(Schema_Conversion_Test)
23 :
24 2 : BOOST_AUTO_TEST_CASE(conversion)
25 : {
26 :
27 1 : const int64_t int_value = 394;
28 1 : const float float_value = 3.14;
29 1 : const double double_value = 6.28;
30 1 : const std::string string_value = "my_test";
31 1 : const bool bool_value = true;
32 1 : const auto tag_name = "channel";
33 1 : const auto tag_value = "365";
34 :
35 1 : dunedaq::opmon::TestInfo ti;
36 1 : ti.set_int_example(int_value);
37 1 : ti.set_float_example(double_value);
38 1 : ti.set_string_example(string_value);
39 1 : ti.set_bool_example(bool_value);
40 :
41 1 : dunedaq::opmon::ComplexInfo ci;
42 1 : ci.set_another_float(float_value);
43 1 : *ci.mutable_sub_message() = ti;
44 1 : ci.mutable_r_field()->Add(42);
45 :
46 1 : auto test_entry = to_entry(ti, {});
47 2 : auto complex_entry = to_entry(ci, { { tag_name, tag_value } });
48 :
49 : // std::string res;
50 : // google::protobuf::util::MessageToJsonString( test_entry, & res );
51 : // std::cout << res << std::endl;
52 :
53 : // res.clear();
54 : // google::protobuf::util::MessageToJsonString( complex_entry, & res );
55 : // std::cout << res << std::endl;
56 :
57 1 : BOOST_TEST(test_entry.data().size() == 4); // check that all the entry of the simple schema are in
58 1 : BOOST_TEST(complex_entry.data().size() == 5); // check that repeated entries are not added,
59 : // but nested structures are
60 :
61 : // checks that the values are preserved correctly
62 1 : auto value = test_entry.data().find("int_example");
63 1 : auto final_int = value->second.int8_value();
64 1 : BOOST_TEST(final_int == int_value);
65 :
66 1 : value = test_entry.data().find("float_example");
67 1 : auto final_double = value->second.double_value();
68 1 : BOOST_TEST(final_double == double_value);
69 :
70 1 : value = test_entry.data().find("string_example");
71 1 : auto final_string = value->second.string_value();
72 1 : BOOST_TEST(final_string == string_value);
73 :
74 1 : value = test_entry.data().find("bool_example");
75 1 : auto final_bool = value->second.boolean_value();
76 1 : BOOST_TEST(final_bool == bool_value);
77 :
78 1 : value = complex_entry.data().find("another_float");
79 1 : auto final_float = value->second.float_value();
80 1 : BOOST_TEST(final_float == float_value);
81 :
82 1 : value = complex_entry.data().find("sub_message.string_example");
83 1 : final_string = value->second.string_value();
84 1 : BOOST_TEST(final_string == string_value);
85 :
86 1 : BOOST_TEST(complex_entry.custom_origin().begin()->first == tag_name);
87 1 : BOOST_TEST(complex_entry.custom_origin().begin()->second == tag_value);
88 :
89 : // inverse
90 1 : auto reco_ti = from_entry<dunedaq::opmon::TestInfo>(test_entry);
91 1 : BOOST_TEST(reco_ti.int_example() == int_value);
92 1 : BOOST_CHECK_THROW(auto failed_reco_ti = from_entry<dunedaq::opmon::TestInfo>(complex_entry),
93 : dunedaq::opmonlib::NameMismatch);
94 1 : BOOST_CHECK_NO_THROW(auto succ_reco_ci = from_entry<dunedaq::opmon::ComplexInfo>(complex_entry));
95 2 : }
96 :
97 2 : BOOST_AUTO_TEST_CASE(name_setters)
98 : {
99 :
100 1 : const int64_t int_value = 394;
101 1 : const float float_value = 3.14;
102 1 : const double double_value = 6.28;
103 1 : const std::string string_value = "my_test";
104 1 : const bool bool_value = true;
105 :
106 1 : dunedaq::opmon::TestInfo ti;
107 :
108 2 : BOOST_CHECK_THROW(set_value(ti, "non_existing_field", double_value), dunedaq::opmonlib::MissingField);
109 :
110 1 : set_value(ti, "string_example", string_value);
111 1 : BOOST_TEST(string_value == ti.string_example());
112 :
113 1 : set_value(ti, "float_example", double_value);
114 1 : BOOST_TEST(double_value == ti.float_example());
115 :
116 1 : set_value(ti, "int_example", int_value);
117 1 : BOOST_TEST(int_value == ti.int_example());
118 1 : }
119 :
120 : BOOST_AUTO_TEST_SUITE_END()
|