1+ # frozen_string_literal: true
2+
3+ require 'spec_helper'
4+
5+ describe SplitIoClient ::RuleBasedSegmentMatcher do
6+ let ( :config ) { SplitIoClient ::SplitConfig . new ( debug_enabled : true ) }
7+ let ( :segments_repository ) { SplitIoClient ::Cache ::Repositories ::SegmentsRepository . new ( config ) }
8+ let ( :flag_sets_repository ) { SplitIoClient ::Cache ::Repositories ::MemoryFlagSetsRepository . new ( [ ] ) }
9+ let ( :flag_set_filter ) { SplitIoClient ::Cache ::Filter ::FlagSetsFilter . new ( [ ] ) }
10+ let ( :splits_repository ) { SplitIoClient ::Cache ::Repositories ::SplitsRepository . new ( config , flag_sets_repository , flag_set_filter ) }
11+
12+ context '#string_type' do
13+ it 'is not string type matcher' do
14+ expect ( described_class . new ( nil , nil , nil , config ) . string_type? ) . to be false
15+ end
16+ end
17+
18+ context 'test_matcher' do
19+ it 'return false if excluded key is passed' do
20+ rbs_repositoy = SplitIoClient ::Cache ::Repositories ::RuleBasedSegmentsRepository . new ( config )
21+ rbs_repositoy . update ( [ { name : 'foo' , trafficTypeName : 'tt_name_1' , conditions : [ ] , excluded : { keys : [ 'key1' ] , segments : [ ] } } ] , [ ] , -1 )
22+ matcher = described_class . new ( segments_repository , rbs_repositoy , 'foo' , config )
23+ expect ( matcher . match? ( value : 'key1' ) ) . to be false
24+ end
25+
26+ it 'return false if excluded segment is passed' do
27+ rbs_repositoy = SplitIoClient ::Cache ::Repositories ::RuleBasedSegmentsRepository . new ( config )
28+ evaluator = SplitIoClient ::Engine ::Parser ::Evaluator . new ( segments_repository , splits_repository , rbs_repositoy , true )
29+ segments_repository . add_to_segment ( { :name => 'segment1' , :added => [ ] , :removed => [ ] } )
30+ rbs_repositoy . update ( [ { :name => 'foo' , :trafficTypeName => 'tt_name_1' , :conditions => [ ] , :excluded => { :keys => [ 'key1' ] , :segments => [ { :name => 'segment1' , :type => 'standard' } ] } } ] , [ ] , -1 )
31+ matcher = described_class . new ( segments_repository , rbs_repositoy , 'foo' , config )
32+ expect ( matcher . match? ( value : 'key2' ) ) . to be false
33+ end
34+
35+ it 'return false if excluded rb segment is matched' do
36+ rbs_repositoy = SplitIoClient ::Cache ::Repositories ::RuleBasedSegmentsRepository . new ( config )
37+ rbs = { :name => 'sample_rule_based_segment' , :trafficTypeName => 'tt_name_1' , :conditions => [ {
38+ :matcherGroup => {
39+ :combiner => "AND" ,
40+ :matchers => [
41+ {
42+ :matcherType => "WHITELIST" ,
43+ :negate => false ,
44+ :userDefinedSegmentMatcherData => nil ,
45+ :whitelistMatcherData => {
46+ :whitelist => [
47+ 48+ "bilal"
49+ ]
50+ } ,
51+ :unaryNumericMatcherData => nil ,
52+ :betweenMatcherData => nil
53+ }
54+ ]
55+ }
56+ } ] , :excluded => { :keys => [ ] , :segments => [ { :name => 'no_excludes' , :type => 'rule-based' } ] } }
57+ rbs2 = { :name => 'no_excludes' , :trafficTypeName => 'tt_name_1' ,
58+ :conditions => [ {
59+ :matcherGroup => {
60+ :combiner => "AND" ,
61+ :matchers => [
62+ {
63+ :keySelector => {
64+ :trafficType => "user" ,
65+ :attribute => "email"
66+ } ,
67+ :matcherType => "ENDS_WITH" ,
68+ :negate => false ,
69+ :whitelistMatcherData => {
70+ :whitelist => [
71+ "@split.io"
72+ ]
73+ }
74+ }
75+ ]
76+ }
77+ }
78+ ] , :excluded => { :keys => [ ] , :segments => [ ] } }
79+
80+ rbs_repositoy . update ( [ rbs , rbs2 ] , [ ] , -1 )
81+ matcher = described_class . new ( segments_repository , rbs_repositoy , 'sample_rule_based_segment' , config )
82+ expect ( matcher . match? ( value :
'[email protected] ' , attributes :
{ 'email' :
'[email protected] ' } ) ) . to be false 83+ expect ( matcher . match? ( value : 'bilal' , attributes : { 'email' : 'bilal' } ) ) . to be true
84+ end
85+
86+ it 'return true if condition matches' do
87+ rule_based_segment = { :name => 'corge' , :trafficTypeName => 'tt_name_5' ,
88+ :excluded => { :keys => [ ] , :segments => [ ] } ,
89+ :conditions => [
90+ {
91+ :contitionType => 'WHITELIST' ,
92+ :label => 'some_label' ,
93+ :matcherGroup => {
94+ :matchers => [
95+ {
96+ :keySelector => nil ,
97+ :matcherType => 'WHITELIST' ,
98+ :whitelistMatcherData => {
99+ :whitelist => [ 'k1' , 'k2' , 'k3' ]
100+ } ,
101+ :negate => false ,
102+ }
103+ ] ,
104+ :combiner => 'AND'
105+ }
106+ } ]
107+ }
108+
109+ rbs_repositoy = SplitIoClient ::Cache ::Repositories ::RuleBasedSegmentsRepository . new ( config )
110+ rbs_repositoy . update ( [ rule_based_segment ] , [ ] , -1 )
111+ matcher = described_class . new ( segments_repository , rbs_repositoy , 'corge' , config )
112+ expect ( matcher . match? ( { :matching_key => 'user' , :attributes => { } } ) ) . to be false
113+ expect ( matcher . match? ( { :matching_key => 'k1' , :attributes => { } } ) ) . to be true
114+ end
115+
116+ it 'return true if dependent rb segment matches' do
117+ rbs_repositoy = SplitIoClient ::Cache ::Repositories ::RuleBasedSegmentsRepository . new ( config )
118+ rbs = {
119+ :changeNumber => 5 ,
120+ :name => "dependent_rbs" ,
121+ :status => "ACTIVE" ,
122+ :trafficTypeName => "user" ,
123+ :excluded => { :keys => [ "[email protected] " , "[email protected] " ] , :segments => [ ] } , 124+ :conditions => [
125+ {
126+ :matcherGroup => {
127+ :combiner => "AND" ,
128+ :matchers => [
129+ {
130+ :keySelector => {
131+ :trafficType => "user" ,
132+ :attribute => "email"
133+ } ,
134+ :matcherType => "ENDS_WITH" ,
135+ :negate => false ,
136+ :whitelistMatcherData => {
137+ :whitelist => [
138+ "@split.io"
139+ ]
140+ }
141+ }
142+ ]
143+ }
144+ }
145+ ] }
146+ rbs2 = {
147+ :changeNumber => 5 ,
148+ :name => "sample_rule_based_segment" ,
149+ :status => "ACTIVE" ,
150+ :trafficTypeName => "user" ,
151+ :excluded => {
152+ :keys => [ ] ,
153+ :segments => [ ]
154+ } ,
155+ :conditions => [
156+ {
157+ :conditionType => "ROLLOUT" ,
158+ :matcherGroup => {
159+ :combiner => "AND" ,
160+ :matchers => [
161+ {
162+ :keySelector => {
163+ :trafficType => "user"
164+ } ,
165+ :matcherType => "IN_RULE_BASED_SEGMENT" ,
166+ :negate => false ,
167+ :userDefinedSegmentMatcherData => {
168+ :segmentName => "dependent_rbs"
169+ }
170+ }
171+ ]
172+ }
173+ }
174+ ]
175+ }
176+ rbs_repositoy . update ( [ rbs , rbs2 ] , [ ] , -1 )
177+ matcher = described_class . new ( segments_repository , rbs_repositoy , 'sample_rule_based_segment' , config )
178+ expect ( matcher . match? ( value :
'[email protected] ' , attributes :
{ 'email' :
'[email protected] ' } ) ) . to be true 179+ expect ( matcher . match? ( value : 'bilal' , attributes : { 'email' : 'bilal' } ) ) . to be false
180+ end
181+ end
182+ end
0 commit comments