1+ package com .optimizely .ab .cmab ;
2+
3+ import java .util .Arrays ;
4+ import java .util .Collections ;
5+ import java .util .List ;
6+
7+ import static org .junit .Assert .assertEquals ;
8+ import static org .junit .Assert .assertNotEquals ;
9+ import static org .junit .Assert .assertNotNull ;
10+ import static org .junit .Assert .assertTrue ;
11+ import org .junit .Test ;
12+
13+ import com .optimizely .ab .config .Cmab ;
14+
15+ /**
16+ * Tests for {@link Cmab} configuration object.
17+ */
18+ public class CmabTest {
19+
20+ @ Test
21+ public void testCmabConstructorWithValidData () {
22+ List <String > attributeIds = Arrays .asList ("attr1" , "attr2" , "attr3" );
23+ int trafficAllocation = 4000 ;
24+
25+ Cmab cmab = new Cmab (attributeIds , trafficAllocation );
26+
27+ assertEquals ("AttributeIds should match" , attributeIds , cmab .getAttributeIds ());
28+ assertEquals ("TrafficAllocation should match" , trafficAllocation , cmab .getTrafficAllocation ());
29+ }
30+
31+ @ Test
32+ public void testCmabConstructorWithEmptyAttributeIds () {
33+ List <String > attributeIds = Collections .emptyList ();
34+ int trafficAllocation = 2000 ;
35+
36+ Cmab cmab = new Cmab (attributeIds , trafficAllocation );
37+
38+ assertEquals ("AttributeIds should be empty" , attributeIds , cmab .getAttributeIds ());
39+ assertTrue ("AttributeIds should be empty list" , cmab .getAttributeIds ().isEmpty ());
40+ assertEquals ("TrafficAllocation should match" , trafficAllocation , cmab .getTrafficAllocation ());
41+ }
42+
43+ @ Test
44+ public void testCmabConstructorWithSingleAttributeId () {
45+ List <String > attributeIds = Collections .singletonList ("single_attr" );
46+ int trafficAllocation = 3000 ;
47+
48+ Cmab cmab = new Cmab (attributeIds , trafficAllocation );
49+
50+ assertEquals ("AttributeIds should match" , attributeIds , cmab .getAttributeIds ());
51+ assertEquals ("Should have one attribute" , 1 , cmab .getAttributeIds ().size ());
52+ assertEquals ("Single attribute should match" , "single_attr" , cmab .getAttributeIds ().get (0 ));
53+ assertEquals ("TrafficAllocation should match" , trafficAllocation , cmab .getTrafficAllocation ());
54+ }
55+
56+ @ Test
57+ public void testCmabConstructorWithZeroTrafficAllocation () {
58+ List <String > attributeIds = Arrays .asList ("attr1" , "attr2" );
59+ int trafficAllocation = 0 ;
60+
61+ Cmab cmab = new Cmab (attributeIds , trafficAllocation );
62+
63+ assertEquals ("AttributeIds should match" , attributeIds , cmab .getAttributeIds ());
64+ assertEquals ("TrafficAllocation should be zero" , 0 , cmab .getTrafficAllocation ());
65+ }
66+
67+ @ Test
68+ public void testCmabConstructorWithMaxTrafficAllocation () {
69+ List <String > attributeIds = Arrays .asList ("attr1" );
70+ int trafficAllocation = 10000 ;
71+
72+ Cmab cmab = new Cmab (attributeIds , trafficAllocation );
73+
74+ assertEquals ("AttributeIds should match" , attributeIds , cmab .getAttributeIds ());
75+ assertEquals ("TrafficAllocation should be 10000" , 10000 , cmab .getTrafficAllocation ());
76+ }
77+
78+ @ Test
79+ public void testCmabEqualsAndHashCode () {
80+ List <String > attributeIds1 = Arrays .asList ("attr1" , "attr2" );
81+ List <String > attributeIds2 = Arrays .asList ("attr1" , "attr2" );
82+ List <String > attributeIds3 = Arrays .asList ("attr1" , "attr3" );
83+
84+ Cmab cmab1 = new Cmab (attributeIds1 , 4000 );
85+ Cmab cmab2 = new Cmab (attributeIds2 , 4000 );
86+ Cmab cmab3 = new Cmab (attributeIds3 , 4000 );
87+ Cmab cmab4 = new Cmab (attributeIds1 , 5000 );
88+
89+ // Test equals
90+ assertEquals ("CMAB with same data should be equal" , cmab1 , cmab2 );
91+ assertNotEquals ("CMAB with different attributeIds should not be equal" , cmab1 , cmab3 );
92+ assertNotEquals ("CMAB with different trafficAllocation should not be equal" , cmab1 , cmab4 );
93+
94+ // Test reflexivity
95+ assertEquals ("CMAB should equal itself" , cmab1 , cmab1 );
96+
97+ // Test null comparison
98+ assertNotEquals ("CMAB should not equal null" , cmab1 , null );
99+
100+ // Test hashCode consistency
101+ assertEquals ("Equal objects should have same hashCode" , cmab1 .hashCode (), cmab2 .hashCode ());
102+ }
103+
104+ @ Test
105+ public void testCmabToString () {
106+ List <String > attributeIds = Arrays .asList ("attr1" , "attr2" );
107+ int trafficAllocation = 4000 ;
108+
109+ Cmab cmab = new Cmab (attributeIds , trafficAllocation );
110+ String result = cmab .toString ();
111+
112+ assertNotNull ("toString should not return null" , result );
113+ assertTrue ("toString should contain attributeIds" , result .contains ("attributeIds" ));
114+ assertTrue ("toString should contain trafficAllocation" , result .contains ("trafficAllocation" ));
115+ assertTrue ("toString should contain attr1" , result .contains ("attr1" ));
116+ assertTrue ("toString should contain attr2" , result .contains ("attr2" ));
117+ assertTrue ("toString should contain 4000" , result .contains ("4000" ));
118+ }
119+
120+ @ Test
121+ public void testCmabToStringWithEmptyAttributeIds () {
122+ List <String > attributeIds = Collections .emptyList ();
123+ int trafficAllocation = 2000 ;
124+
125+ Cmab cmab = new Cmab (attributeIds , trafficAllocation );
126+ String result = cmab .toString ();
127+
128+ assertNotNull ("toString should not return null" , result );
129+ assertTrue ("toString should contain attributeIds" , result .contains ("attributeIds" ));
130+ assertTrue ("toString should contain trafficAllocation" , result .contains ("trafficAllocation" ));
131+ assertTrue ("toString should contain 2000" , result .contains ("2000" ));
132+ }
133+
134+ @ Test
135+ public void testCmabWithDuplicateAttributeIds () {
136+ List <String > attributeIds = Arrays .asList ("attr1" , "attr2" , "attr1" , "attr3" );
137+ int trafficAllocation = 4000 ;
138+
139+ Cmab cmab = new Cmab (attributeIds , trafficAllocation );
140+
141+ assertEquals ("AttributeIds should match exactly (including duplicates)" ,
142+ attributeIds , cmab .getAttributeIds ());
143+ assertEquals ("Should have 4 elements (including duplicate)" , 4 , cmab .getAttributeIds ().size ());
144+ }
145+
146+ @ Test
147+ public void testCmabWithRealWorldAttributeIds () {
148+ // Test with realistic attribute IDs from Optimizely
149+ List <String > attributeIds = Arrays .asList ("808797688" , "808797689" , "10401066117" );
150+ int trafficAllocation = 4000 ;
151+
152+ Cmab cmab = new Cmab (attributeIds , trafficAllocation );
153+
154+ assertEquals ("AttributeIds should match" , attributeIds , cmab .getAttributeIds ());
155+ assertEquals ("TrafficAllocation should match" , trafficAllocation , cmab .getTrafficAllocation ());
156+ assertTrue ("Should contain first attribute ID" , cmab .getAttributeIds ().contains ("808797688" ));
157+ assertTrue ("Should contain second attribute ID" , cmab .getAttributeIds ().contains ("808797689" ));
158+ assertTrue ("Should contain third attribute ID" , cmab .getAttributeIds ().contains ("10401066117" ));
159+ }
160+ }
0 commit comments