3
3
import org .junit .jupiter .api .AfterEach ;
4
4
import org .junit .jupiter .api .BeforeEach ;
5
5
import org .junit .jupiter .api .Test ;
6
+ import org .mockito .Mock ;
7
+ import org .mockito .MockitoAnnotations ;
6
8
import org .typesense .resources .Node ;
7
9
10
+ import okhttp3 .Call ;
11
+ import okhttp3 .OkHttpClient ;
12
+ import okhttp3 .Request ;
13
+
14
+ import java .lang .reflect .Field ;
15
+ import java .net .ConnectException ;
8
16
import java .time .Duration ;
9
17
import java .util .ArrayList ;
10
18
import java .util .List ;
11
19
12
20
import 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 ;
13
27
14
28
class APICallTest {
15
29
30
+ @ Mock
31
+ private OkHttpClient client ;
32
+
33
+ @ Mock
34
+ private Call call ;
35
+
16
36
private ApiCall apiCall ;
17
37
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
+ }
18
48
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 );
25
53
}
26
54
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 );
34
62
}
35
63
36
64
@ AfterEach
37
65
void tearDown () throws Exception {
38
-
66
+ nodes = null ;
67
+ apiCall = null ;
68
+ resetNodeIndex ();
39
69
}
40
70
41
71
@ Test
42
- void testRoundRobin () throws Exception {
72
+ void testRoundRobin () {
43
73
setUpNoNearestNode ();
44
74
assertEquals ("7108" , apiCall .getNode ().port );
45
75
assertEquals ("6108" , apiCall .getNode ().port );
@@ -50,27 +80,96 @@ void testRoundRobin() throws Exception {
50
80
assertEquals ("8108" , apiCall .getNode ().port );
51
81
}
52
82
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
+ }
53
154
54
155
@ Test
55
- void testUnhealthyNearestNode () throws Exception {
156
+ void testUnhealthyNearestNode () {
56
157
setUpNearestNode ();
57
158
nearestNode .isHealthy = false ;
58
159
assertEquals ("7108" , apiCall .getNode ().port );
59
160
}
60
161
61
162
@ Test
62
- void testHealthyNearestNode () throws Exception {
163
+ void testHealthyNearestNode () {
63
164
setUpNearestNode ();
64
165
assertEquals ("0000" , apiCall .getNode ().port );
65
166
}
66
167
67
168
@ Test
68
- void testUnhealthyNearestNodeDueForHealthCheck () throws Exception {
169
+ void testUnhealthyNearestNodeDueForHealthCheck () {
69
170
setUpNearestNode ();
70
171
nearestNode .isHealthy = false ;
71
172
nearestNode .lastAccessTimestamp = nearestNode .lastAccessTimestamp .minusSeconds (63 );
72
173
assertEquals ("0000" , apiCall .getNode ().port );
73
174
}
74
-
75
-
76
175
}
0 commit comments