#include #include "envoy/config/core/v3/extension.pb.h" #include "common/matcher/exact_map_matcher.h" #include "common/matcher/matcher.h" #include "test/common/matcher/test_utility.h" #include "gtest/gtest.h" namespace Envoy { namespace Matcher { class ExactMapMatcherTest : public ::testing::Test { public: void verifyNoMatch(const MatchTree::MatchResult& result) { EXPECT_EQ(MatchState::MatchComplete, result.match_state_); EXPECT_FALSE(result.on_match_.has_value()); } void verifyImmediateMatch(const MatchTree::MatchResult& result, absl::string_view expected_value) { EXPECT_EQ(MatchState::MatchComplete, result.match_state_); EXPECT_TRUE(result.on_match_.has_value()); EXPECT_EQ(nullptr, result.on_match_->matcher_); EXPECT_NE(result.on_match_->action_cb_, nullptr); EXPECT_EQ(*static_cast(result.on_match_->action_cb_().get()), *stringValue(expected_value)); } void verifyNotEnoughDataForMatch(const MatchTree::MatchResult& result) { EXPECT_EQ(MatchState::UnableToMatch, result.match_state_); EXPECT_FALSE(result.on_match_.has_value()); } }; TEST_F(ExactMapMatcherTest, NoMatch) { ExactMapMatcher matcher( std::make_unique( DataInputGetResult{DataInputGetResult::DataAvailability::AllDataAvailable, "blah"}), absl::nullopt); TestData data; const auto result = matcher.match(data); verifyNoMatch(result); } TEST_F(ExactMapMatcherTest, NoMatchDueToNoData) { ExactMapMatcher matcher( std::make_unique(DataInputGetResult{ DataInputGetResult::DataAvailability::AllDataAvailable, absl::nullopt}), absl::nullopt); TestData data; const auto result = matcher.match(data); verifyNoMatch(result); } TEST_F(ExactMapMatcherTest, NoMatchWithFallback) { ExactMapMatcher matcher( std::make_unique( DataInputGetResult{DataInputGetResult::DataAvailability::AllDataAvailable, "blah"}), stringOnMatch("no_match")); TestData data; const auto result = matcher.match(data); verifyImmediateMatch(result, "no_match"); } TEST_F(ExactMapMatcherTest, Match) { ExactMapMatcher matcher( std::make_unique( DataInputGetResult{DataInputGetResult::DataAvailability::AllDataAvailable, "match"}), stringOnMatch("no_match")); matcher.addChild("match", stringOnMatch("match")); TestData data; const auto result = matcher.match(data); verifyImmediateMatch(result, "match"); } TEST_F(ExactMapMatcherTest, DataNotAvailable) { ExactMapMatcher matcher(std::make_unique(DataInputGetResult{ DataInputGetResult::DataAvailability::NotAvailable, {}}), stringOnMatch("no_match")); matcher.addChild("match", stringOnMatch("match")); TestData data; const auto result = matcher.match(data); verifyNotEnoughDataForMatch(result); } TEST_F(ExactMapMatcherTest, MoreDataMightBeAvailableNoMatch) { ExactMapMatcher matcher( std::make_unique(DataInputGetResult{ DataInputGetResult::DataAvailability::MoreDataMightBeAvailable, "no match"}), stringOnMatch("no_match")); matcher.addChild("match", stringOnMatch("match")); TestData data; const auto result = matcher.match(data); verifyNotEnoughDataForMatch(result); } TEST_F(ExactMapMatcherTest, MoreDataMightBeAvailableMatch) { ExactMapMatcher matcher( std::make_unique(DataInputGetResult{ DataInputGetResult::DataAvailability::MoreDataMightBeAvailable, "match"}), stringOnMatch("no_match")); matcher.addChild("match", stringOnMatch("match")); TestData data; const auto result = matcher.match(data); verifyImmediateMatch(result, "match"); } } // namespace Matcher } // namespace Envoy