|
| 1 | +#include "flagd/sync/sync.h" |
| 2 | + |
| 3 | +#include <gtest/gtest.h> |
| 4 | + |
| 5 | +#include <nlohmann/json.hpp> |
| 6 | + |
| 7 | +namespace flagd { |
| 8 | + |
| 9 | +class TestableSync : public FlagSync { |
| 10 | + public: |
| 11 | + using FlagSync::FlagSync; |
| 12 | + |
| 13 | + absl::Status Init(const openfeature::EvaluationContext& ctx) override { |
| 14 | + return absl::OkStatus(); |
| 15 | + } |
| 16 | + absl::Status Shutdown() override { return absl::OkStatus(); } |
| 17 | + |
| 18 | + void TriggerUpdate(const nlohmann::json& new_json) { |
| 19 | + this->UpdateFlags(new_json); |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | +class SyncTest : public ::testing::Test { |
| 24 | + protected: |
| 25 | + TestableSync sync_; |
| 26 | +}; |
| 27 | + |
| 28 | +TEST_F(SyncTest, ValidatorAcceptsValidJson) { |
| 29 | + nlohmann::json valid_json = R"({ |
| 30 | + "flags": { |
| 31 | + "my-flag": { |
| 32 | + "state": "ENABLED", |
| 33 | + "variants": { |
| 34 | + "on": true, |
| 35 | + "off": false |
| 36 | + }, |
| 37 | + "defaultVariant": "on" |
| 38 | + } |
| 39 | + } |
| 40 | + })"_json; |
| 41 | + |
| 42 | + sync_.TriggerUpdate(valid_json); |
| 43 | + |
| 44 | + std::shared_ptr<const nlohmann::json> flags = sync_.GetFlags(); |
| 45 | + EXPECT_TRUE(flags->contains("my-flag")); |
| 46 | +} |
| 47 | + |
| 48 | +TEST_F(SyncTest, ValidatorRejectsInvalidJson) { |
| 49 | + // Missing 'flags' field |
| 50 | + nlohmann::json invalid_json = R"({ |
| 51 | + "something": "else" |
| 52 | + })"_json; |
| 53 | + |
| 54 | + sync_.TriggerUpdate(invalid_json); |
| 55 | + |
| 56 | + std::shared_ptr<const nlohmann::json> flags = sync_.GetFlags(); |
| 57 | + EXPECT_TRUE(flags->empty()); |
| 58 | +} |
| 59 | + |
| 60 | +TEST_F(SyncTest, ValidatorRejectsInvalidType) { |
| 61 | + // 'state' should be a string, not an integer |
| 62 | + nlohmann::json invalid_json = R"({ |
| 63 | + "flags": { |
| 64 | + "my-flag": { |
| 65 | + "state": 123, |
| 66 | + "variants": { |
| 67 | + "on": true, |
| 68 | + "off": false |
| 69 | + }, |
| 70 | + "defaultVariant": "on" |
| 71 | + } |
| 72 | + } |
| 73 | + })"_json; |
| 74 | + |
| 75 | + sync_.TriggerUpdate(invalid_json); |
| 76 | + |
| 77 | + std::shared_ptr<const nlohmann::json> flags = sync_.GetFlags(); |
| 78 | + EXPECT_TRUE(flags->empty()); |
| 79 | +} |
| 80 | + |
| 81 | +TEST_F(SyncTest, ValidatorRejectsMissingVariants) { |
| 82 | + // Missing 'variants' field |
| 83 | + nlohmann::json invalid_json = R"({ |
| 84 | + "flags": { |
| 85 | + "my-flag": { |
| 86 | + "state": "ENABLED", |
| 87 | + "defaultVariant": "on" |
| 88 | + } |
| 89 | + } |
| 90 | + })"_json; |
| 91 | + |
| 92 | + sync_.TriggerUpdate(invalid_json); |
| 93 | + |
| 94 | + std::shared_ptr<const nlohmann::json> flags = sync_.GetFlags(); |
| 95 | + EXPECT_TRUE(flags->empty()); |
| 96 | +} |
| 97 | + |
| 98 | +TEST_F(SyncTest, ValidatorRejectsMalformedFlag) { |
| 99 | + // 'state' has invalid value |
| 100 | + nlohmann::json invalid_json = R"({ |
| 101 | + "flags": { |
| 102 | + "my-flag": { |
| 103 | + "state": "INVALID_STATE", |
| 104 | + "variants": { |
| 105 | + "on": true |
| 106 | + }, |
| 107 | + "defaultVariant": "on" |
| 108 | + } |
| 109 | + } |
| 110 | + })"_json; |
| 111 | + |
| 112 | + sync_.TriggerUpdate(invalid_json); |
| 113 | + |
| 114 | + std::shared_ptr<const nlohmann::json> flags = sync_.GetFlags(); |
| 115 | + EXPECT_TRUE(flags->empty()); |
| 116 | +} |
| 117 | + |
| 118 | +TEST_F(SyncTest, MetadataIsExtracted) { |
| 119 | + nlohmann::json json_with_metadata = R"({ |
| 120 | + "flags": {}, |
| 121 | + "metadata": { |
| 122 | + "foo": "bar" |
| 123 | + } |
| 124 | + })"_json; |
| 125 | + |
| 126 | + sync_.TriggerUpdate(json_with_metadata); |
| 127 | + |
| 128 | + std::shared_ptr<const nlohmann::json> metadata = sync_.GetMetadata(); |
| 129 | + EXPECT_EQ((*metadata)["foo"], "bar"); |
| 130 | +} |
| 131 | + |
| 132 | +} // namespace flagd |
0 commit comments