|
| 1 | +#include "common/json.hpp" |
| 2 | +#include "common/producertable.h" |
| 3 | +#include "gtest/gtest.h" |
| 4 | + |
| 5 | +using namespace std; |
| 6 | +using namespace swss; |
| 7 | +using json = nlohmann::json; |
| 8 | + |
| 9 | +#define TEST_VIEW (7) |
| 10 | +#define TEST_DUMP_FILE "ut_dump_file.txt" |
| 11 | + |
| 12 | +TEST(JSON, test) |
| 13 | +{ |
| 14 | + /* Construct the file */ |
| 15 | + DBConnector db(TEST_VIEW, "localhost", 6379, 0); |
| 16 | + ProducerTable *p; |
| 17 | + p = new ProducerTable(&db, "UT_REDIS_CHANNEL", TEST_DUMP_FILE); |
| 18 | + |
| 19 | + vector<FieldValueTuple> fvTuples; |
| 20 | + FieldValueTuple fv1("test_field_1", "test_value_1"); |
| 21 | + fvTuples.push_back(fv1); |
| 22 | + |
| 23 | + p->set("test_key_1", fvTuples); |
| 24 | + |
| 25 | + FieldValueTuple fv2("test_field_2", "test_value_2"); |
| 26 | + fvTuples.push_back(fv2); |
| 27 | + |
| 28 | + p->set("test_key_2", fvTuples); |
| 29 | + |
| 30 | + p->del("test_key_1"); |
| 31 | + |
| 32 | + delete(p); |
| 33 | + |
| 34 | + /* Read the file and validate the content */ |
| 35 | + fstream file(TEST_DUMP_FILE, fstream::in); |
| 36 | + json j; |
| 37 | + file >> j; |
| 38 | + |
| 39 | + EXPECT_TRUE(j.is_array()); |
| 40 | + EXPECT_TRUE(j.size() == 3); |
| 41 | + |
| 42 | + for (size_t i = 0; i < j.size(); i++) |
| 43 | + { |
| 44 | + auto item = j[i]; |
| 45 | + EXPECT_TRUE(item.is_object()); |
| 46 | + EXPECT_TRUE(item.size() == 2); |
| 47 | + |
| 48 | + for (auto it = item.begin(); it != item.end(); it++) |
| 49 | + { |
| 50 | + if (it.key() == "OP") |
| 51 | + EXPECT_TRUE(it.value() == "SET" || it.value() == "DEL"); |
| 52 | + else |
| 53 | + { |
| 54 | + auto subitem = it.value(); |
| 55 | + EXPECT_TRUE(subitem.is_object()); |
| 56 | + if (subitem.size() > 0) |
| 57 | + { |
| 58 | + for (auto subit = subitem.begin(); subit != subitem.end(); subit++) |
| 59 | + { |
| 60 | + if (subit.key() == "test_field_1") |
| 61 | + EXPECT_EQ(subit.value(), "test_value_1"); |
| 62 | + if (subit.key() == "test_field_2") |
| 63 | + EXPECT_EQ(subit.value(), "test_value_2"); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + file.close(); |
| 70 | +} |
0 commit comments