1515package com .optimizely .ab .android .sdk .cmab ;
1616
1717import com .optimizely .ab .android .shared .Client ;
18- import com .optimizely .ab .cmab .client .CmabClientHelper ;
1918
2019import org .junit .Before ;
2120import org .junit .Test ;
2221import org .junit .runner .RunWith ;
2322import org .mockito .ArgumentCaptor ;
24- import org .powermock .api .mockito .PowerMockito ;
25- import org .powermock .core .classloader .annotations .PowerMockIgnore ;
26- import org .powermock .core .classloader .annotations .PrepareForTest ;
27- import org .powermock .modules .junit4 .PowerMockRunner ;
23+ import org .mockito .junit .MockitoJUnitRunner ;
2824import org .slf4j .Logger ;
2925
3026import java .io .ByteArrayOutputStream ;
3733import static org .junit .Assert .*;
3834import static org .mockito .ArgumentMatchers .*;
3935import static org .mockito .Mockito .*;
40- import static org .powermock .api .mockito .PowerMockito .mockStatic ;
41- import static org .powermock .api .mockito .PowerMockito .when ;
4236
4337/**
4438 * Tests for {@link DefaultCmabClient}
4539 */
46- @ RunWith (PowerMockRunner .class )
47- @ PrepareForTest ({CmabClientHelper .class })
48- @ PowerMockIgnore ({"javax.net.ssl.*" , "javax.security.*" })
40+ @ RunWith (MockitoJUnitRunner .class )
4941public class DefaultCmabClientTest {
5042
5143 private Client mockClient ;
52- private Logger mockLogger ;
53- private HttpURLConnection mockUrlConnection ;
54- private ByteArrayOutputStream mockOutputStream ;
55-
56- private DefaultCmabClient cmabClient ;
44+ private CmabClientHelperAndroid mockCmabClientHelper ;
45+ private DefaultCmabClient mockCmabClient ;
5746 private String testRuleId = "test-rule-123" ;
5847 private String testUserId = "test-user-456" ;
5948 private String testCmabUuid = "test-uuid-789" ;
@@ -62,101 +51,72 @@ public class DefaultCmabClientTest {
6251 @ Before
6352 public void setup () {
6453 mockClient = mock (Client .class );
65- mockLogger = mock (Logger .class );
66- mockUrlConnection = mock (HttpURLConnection .class );
67- mockOutputStream = mock (ByteArrayOutputStream .class );
68-
69- cmabClient = new DefaultCmabClient (mockClient );
54+ mockCmabClientHelper = spy (new CmabClientHelperAndroid ());
7055
7156 testAttributes = new HashMap <>();
7257 testAttributes .put ("age" , 25 );
7358 testAttributes .put ("country" , "US" );
74-
75- // Mock static methods
76- mockStatic (CmabClientHelper .class );
7759 }
7860
7961 @ Test
8062 public void testFetchDecisionSuccess () throws Exception {
81- // Mock successful HTTP response
63+ HttpURLConnection mockUrlConnection = mock (HttpURLConnection .class );
64+ ByteArrayOutputStream mockOutputStream = mock (ByteArrayOutputStream .class );
65+
8266 String mockResponseJson = "{\" variation_id\" :\" variation_1\" ,\" status\" :\" success\" }" ;
8367 when (mockClient .openConnection (any (URL .class ))).thenReturn (mockUrlConnection );
8468 when (mockUrlConnection .getResponseCode ()).thenReturn (200 );
8569 when (mockClient .readStream (mockUrlConnection )).thenReturn (mockResponseJson );
8670 when (mockUrlConnection .getOutputStream ()).thenReturn (mockOutputStream );
87- when (mockClient .execute (any (Client .Request .class ), eq ( 2 ), eq ( 2 ))).thenAnswer (invocation -> {
71+ when (mockClient .execute (any (Client .Request .class ), anyInt ( ), anyInt ( ))).thenAnswer (invocation -> {
8872 Client .Request <String > request = invocation .getArgument (0 );
8973 return request .execute ();
9074 });
9175
92- // Mock the helper methods
93- when (CmabClientHelper .buildRequestJson (testUserId , testRuleId , testAttributes , testCmabUuid ))
94- .thenReturn ("{\" user_id\" :\" test-user-456\" }" );
95- when (CmabClientHelper .validateResponse (mockResponseJson )).thenReturn (true );
96- when (CmabClientHelper .parseVariationId (mockResponseJson )).thenReturn ("variation_1" );
76+ doReturn ("{\" user_id\" :\" test-user-456\" }" )
77+ .when (mockCmabClientHelper )
78+ .buildRequestJson (any (), any (), any (), any ());
79+ doReturn (true )
80+ .when (mockCmabClientHelper )
81+ .validateResponse (any ());
82+ doReturn ("variation_1" )
83+ .when (mockCmabClientHelper )
84+ .parseVariationId (any ());
85+
86+ mockCmabClient = new DefaultCmabClient (mockClient , mockCmabClientHelper );
9787
98- String result = cmabClient .fetchDecision (testRuleId , testUserId , testAttributes , testCmabUuid );
88+ String result = mockCmabClient .fetchDecision (testRuleId , testUserId , testAttributes , testCmabUuid );
9989
10090 assertEquals ("variation_1" , result );
101- verify (mockUrlConnection ).setConnectTimeout (10000 );
102- verify (mockUrlConnection ).setReadTimeout (60000 );
91+
92+ verify (mockUrlConnection ).setConnectTimeout (10 *1000 );
93+ verify (mockUrlConnection ).setReadTimeout (60 *1000 );
10394 verify (mockUrlConnection ).setRequestMethod ("POST" );
10495 verify (mockUrlConnection ).setRequestProperty ("content-type" , "application/json" );
10596 verify (mockUrlConnection ).setDoOutput (true );
106- verify (mockLogger ).debug ("Successfully fetched CMAB decision: {}" , mockResponseJson );
10797 }
10898
10999 @ Test
110100 public void testFetchDecisionConnectionFailure () throws Exception {
111- // Mock connection failure
112101 when (mockClient .openConnection (any (URL .class ))).thenReturn (null );
113- when (mockClient .execute (any (Client .Request .class ), eq (2 ), eq (2 ))).thenAnswer (invocation -> {
114- Client .Request <String > request = invocation .getArgument (0 );
115- return request .execute ();
116- });
117-
118- // Mock the helper methods
119- when (CmabClientHelper .buildRequestJson (testUserId , testRuleId , testAttributes , testCmabUuid ))
120- .thenReturn ("{\" user_id\" :\" test-user-456\" }" );
121-
122- String result = cmabClient .fetchDecision (testRuleId , testUserId , testAttributes , testCmabUuid );
123-
124- assertNull (result );
125- }
126-
127- @ Test
128- public void testConnectionTimeouts () throws Exception {
129- when (mockClient .openConnection (any (URL .class ))).thenReturn (mockUrlConnection );
130- when (mockUrlConnection .getResponseCode ()).thenReturn (200 );
131- when (mockClient .readStream (mockUrlConnection )).thenReturn ("{\" variation_id\" :\" test\" }" );
132- when (mockUrlConnection .getOutputStream ()).thenReturn (mockOutputStream );
133102 when (mockClient .execute (any (Client .Request .class ), anyInt (), anyInt ())).thenAnswer (invocation -> {
134103 Client .Request <String > request = invocation .getArgument (0 );
135104 return request .execute ();
136105 });
137106
138- when (CmabClientHelper .buildRequestJson (any (), any (), any (), any ())).thenReturn ("{}" );
139- when (CmabClientHelper .validateResponse (any ())).thenReturn (true );
140- when (CmabClientHelper .parseVariationId (any ())).thenReturn ("test" );
141-
142- cmabClient .fetchDecision (testRuleId , testUserId , testAttributes , testCmabUuid );
107+ mockCmabClient = new DefaultCmabClient (mockClient , mockCmabClientHelper );
143108
144- verify ( mockUrlConnection ). setConnectTimeout ( 10 * 1000 ); // 10 seconds
145- verify ( mockUrlConnection ). setReadTimeout ( 60 * 1000 ); // 60 seconds
109+ String result = mockCmabClient . fetchDecision ( testRuleId , testUserId , testAttributes , testCmabUuid );
110+ assertNull ( result );
146111 }
147112
148113 @ Test
149114 public void testRetryOnFailureWithRetryBackoff () throws Exception {
150- when (mockClient .openConnection (any (URL .class ))).thenReturn (mockUrlConnection );
151- when (mockUrlConnection .getResponseCode ()).thenReturn (500 );
152- when (mockUrlConnection .getResponseMessage ()).thenReturn ("Server Error" );
153- when (mockUrlConnection .getOutputStream ()).thenReturn (mockOutputStream );
154-
155- when (mockClient .execute (any (Client .Request .class ), eq (1 ), eq (2 ))).thenReturn (null );
115+ when (mockClient .execute (any (Client .Request .class ), anyInt (), anyInt ())).thenReturn (null );
156116
157- when ( CmabClientHelper . buildRequestJson ( any (), any (), any (), any ())). thenReturn ( "{}" );
117+ mockCmabClient = new DefaultCmabClient ( mockClient , mockCmabClientHelper );
158118
159- String result = cmabClient .fetchDecision (testRuleId , testUserId , testAttributes , testCmabUuid );
119+ String result = mockCmabClient .fetchDecision (testRuleId , testUserId , testAttributes , testCmabUuid );
160120 assertNull (result );
161121
162122 // Verify the retry configuration matches our constants
0 commit comments