33import org .junit .jupiter .api .AfterEach ;
44import org .junit .jupiter .api .BeforeEach ;
55import org .junit .jupiter .api .Test ;
6+ import org .mockito .Mock ;
7+ import org .mockito .MockitoAnnotations ;
68import org .typesense .resources .Node ;
79
10+ import okhttp3 .Call ;
11+ import okhttp3 .OkHttpClient ;
12+ import okhttp3 .Request ;
13+
14+ import java .lang .reflect .Field ;
15+ import java .net .ConnectException ;
816import java .time .Duration ;
917import java .util .ArrayList ;
1018import java .util .List ;
1119
1220import static org .junit .jupiter .api .Assertions .assertEquals ;
21+ import static org .junit .jupiter .api .Assertions .assertThrows ;
22+ import static org .mockito .ArgumentMatchers .any ;
23+ import static org .mockito .Mockito .mock ;
24+ import static org .mockito .Mockito .times ;
25+ import static org .mockito .Mockito .verify ;
26+ import static org .mockito .Mockito .when ;
1327
1428class APICallTest {
1529
30+ @ Mock
31+ private OkHttpClient client ;
32+
33+ @ Mock
34+ private Call call ;
35+
1636 private ApiCall apiCall ;
1737 private Node nearestNode ;
38+ private List <Node > nodes ;
39+
40+ @ BeforeEach
41+ void setUp () {
42+ MockitoAnnotations .openMocks (this );
43+ nodes = new ArrayList <>();
44+ nodes .add (new Node ("http" , "localhost" , "8108" ));
45+ nodes .add (new Node ("http" , "localhost" , "7108" ));
46+ nodes .add (new Node ("http" , "localhost" , "6108" ));
47+ }
1848
19- void setUpNoNearestNode () throws Exception {
20- List <Node > nodes = new ArrayList <>();
21- nodes .add (new Node ("http" ,"localhost" ,"8108" ));
22- nodes .add (new Node ("http" ,"localhost" ,"7108" ));
23- nodes .add (new Node ("http" ,"localhost" ,"6108" ));
24- apiCall = new ApiCall (new Configuration (nodes , Duration .ofSeconds (3 ),"xyz" ));
49+ private void resetNodeIndex () throws Exception {
50+ Field nodeIndexField = ApiCall .class .getDeclaredField ("nodeIndex" );
51+ nodeIndexField .setAccessible (true );
52+ nodeIndexField .set (null , 0 );
2553 }
2654
27- void setUpNearestNode () throws Exception {
28- List < Node > nodes = new ArrayList <>( );
29- nodes . add ( new Node ( "http" , "localhost" , "8108" ));
30- nodes . add ( new Node ( "http" , "localhost" , "7108" ));
31- nodes . add ( new Node ( "http" , "localhost" , "6108" ));
32- nearestNode = new Node ("http" ,"localhost" ,"0000" );
33- apiCall = new ApiCall (new Configuration (nearestNode , nodes , Duration .ofSeconds (3 ),"xyz" ));
55+ void setUpNoNearestNode () {
56+ apiCall = new ApiCall ( new Configuration ( nodes , Duration . ofSeconds ( 3 ), "xyz" ), client );
57+ }
58+
59+ void setUpNearestNode () {
60+ nearestNode = new Node ("http" , "localhost" , "0000" );
61+ apiCall = new ApiCall (new Configuration (nearestNode , nodes , Duration .ofSeconds (3 ), "xyz" ), client );
3462 }
3563
3664 @ AfterEach
3765 void tearDown () throws Exception {
38-
66+ nodes = null ;
67+ apiCall = null ;
68+ resetNodeIndex ();
3969 }
4070
4171 @ Test
42- void testRoundRobin () throws Exception {
72+ void testRoundRobin () {
4373 setUpNoNearestNode ();
4474 assertEquals ("7108" , apiCall .getNode ().port );
4575 assertEquals ("6108" , apiCall .getNode ().port );
@@ -50,27 +80,96 @@ void testRoundRobin() throws Exception {
5080 assertEquals ("8108" , apiCall .getNode ().port );
5181 }
5282
83+ @ Test
84+ void testMakeRequestWithConnectException () throws Exception {
85+ setUpNoNearestNode ();
86+ String endpoint = "/collections" ;
87+ Request .Builder requestBuilder = new Request .Builder ().get ();
88+
89+ Call mockCall = mock (Call .class );
90+ when (client .newCall (any (Request .class ))).thenReturn (mockCall );
91+ when (mockCall .execute ()).thenThrow (new ConnectException ());
92+
93+ // Act
94+ assertThrows (ConnectException .class , () -> {
95+ apiCall .makeRequest (endpoint , null , requestBuilder , String .class );
96+ });
97+
98+ // Additional assertions
99+ nodes .forEach (node -> {
100+ assertEquals (false , node .isHealthy );
101+ });
102+
103+ verify (client , times (3 )).newCall (any (Request .class ));
104+ verify (mockCall , times (3 )).execute ();
105+ }
106+
107+ @ Test
108+ void testMakeRequestWithSocketTimeoutException () throws Exception {
109+ setUpNoNearestNode ();
110+ String endpoint = "/collections" ;
111+ Request .Builder requestBuilder = new Request .Builder ().get ();
112+
113+ Call mockCall = mock (Call .class );
114+ when (client .newCall (any (Request .class ))).thenReturn (mockCall );
115+ when (mockCall .execute ()).thenThrow (new java .net .SocketTimeoutException ());
116+
117+ // Act
118+ assertThrows (java .net .SocketTimeoutException .class , () -> {
119+ apiCall .makeRequest (endpoint , null , requestBuilder , String .class );
120+ });
121+
122+ // Additional assertions
123+ nodes .forEach (node -> {
124+ assertEquals (false , node .isHealthy );
125+ });
126+
127+ verify (client , times (3 )).newCall (any (Request .class ));
128+ verify (mockCall , times (3 )).execute ();
129+ }
130+
131+ @ Test
132+ void testMakeRequestWithUnknownHostException () throws Exception {
133+ setUpNoNearestNode ();
134+ String endpoint = "/collections" ;
135+ Request .Builder requestBuilder = new Request .Builder ().get ();
136+
137+ Call mockCall = mock (Call .class );
138+ when (client .newCall (any (Request .class ))).thenReturn (mockCall );
139+ when (mockCall .execute ()).thenThrow (new java .net .UnknownHostException ());
140+
141+ // Act
142+ assertThrows (java .net .UnknownHostException .class , () -> {
143+ apiCall .makeRequest (endpoint , null , requestBuilder , String .class );
144+ });
145+
146+ // Additional assertions
147+ nodes .forEach (node -> {
148+ assertEquals (false , node .isHealthy );
149+ });
150+
151+ verify (client , times (3 )).newCall (any (Request .class ));
152+ verify (mockCall , times (3 )).execute ();
153+ }
53154
54155 @ Test
55- void testUnhealthyNearestNode () throws Exception {
156+ void testUnhealthyNearestNode () {
56157 setUpNearestNode ();
57158 nearestNode .isHealthy = false ;
58159 assertEquals ("7108" , apiCall .getNode ().port );
59160 }
60161
61162 @ Test
62- void testHealthyNearestNode () throws Exception {
163+ void testHealthyNearestNode () {
63164 setUpNearestNode ();
64165 assertEquals ("0000" , apiCall .getNode ().port );
65166 }
66167
67168 @ Test
68- void testUnhealthyNearestNodeDueForHealthCheck () throws Exception {
169+ void testUnhealthyNearestNodeDueForHealthCheck () {
69170 setUpNearestNode ();
70171 nearestNode .isHealthy = false ;
71172 nearestNode .lastAccessTimestamp = nearestNode .lastAccessTimestamp .minusSeconds (63 );
72173 assertEquals ("0000" , apiCall .getNode ().port );
73174 }
74-
75-
76175}
0 commit comments