2525import static org .mockito .ArgumentMatchers .eq ;
2626import static org .mockito .Mockito .doNothing ;
2727import static org .mockito .Mockito .doReturn ;
28+ import static org .mockito .Mockito .doThrow ;
2829import static org .mockito .Mockito .mock ;
2930import static org .mockito .Mockito .times ;
3031import static org .mockito .Mockito .verify ;
7071
7172import com .fasterxml .jackson .databind .ObjectMapper ;
7273
74+
7375public class BackrollHttpClientProviderTest {
76+
7477 @ Spy
7578 @ InjectMocks
7679 BackrollHttpClientProvider backupHttpClientProvider ;
@@ -117,7 +120,6 @@ private void defaultTestHttpClient(String path)
117120 String virtualMachineResponseString = "{ \" state\" : \" SUCCESS\" , \" info\" : { \" archives\" : [ { \" archive\" : \" ROOT-00000\" , \" barchive\" : \" ROOT-00000\" , \" id\" : \" 25d55ad283aa400af464c76d713c07ad7d163abdd3b8fbcdbdc46b827e5e0457\" , \" name\" : \" ROOT-00000\" , \" start\" : \" 2024-11-08T18:24:48.000000\" , \" time\" : \" 2024-11-08T18:24:48.000000\" } ], \" encryption\" : { \" mode\" : \" none\" }, \" repository\" : { \" id\" : \" 36a11ebc0775a097c927735cc7015d19be7309be69fc15b896c5b1fd87fcbd79\" , \" last_modified\" : \" 2024-11-29T09:53:09.000000\" , \" location\" : \" /mnt/backup/backup1\" } } }" ;
118121
119122 CloseableHttpResponse response2 = mock (CloseableHttpResponse .class );
120-
121123 StatusLine statusLine = mock (StatusLine .class );
122124
123125 doReturn (httpClient ).when (backupHttpClientProvider ).createHttpClient ();
@@ -144,7 +146,7 @@ private void defaultTestHttpClient(String path)
144146 public void testCreateHttpClient_WithValidateCertificateTrue ()
145147 throws KeyManagementException , NoSuchAlgorithmException , URISyntaxException , BackrollApiException {
146148 backupHttpClientProvider = BackrollHttpClientProvider .createProvider (backupHttpClientProvider ,
147- "http://api.backup.demo.ccc:5050/api/v1" , "backroll-api" , "VviX8dALauSyYJMqVYJqf3UyZOpO3joS" , false ,
149+ "http://api.backup.demo.ccc:5050/api/v1" , "backroll-api" , "VviX8dALauSyYJMqVYJqf3UyZOpO3joS" , true ,
148150 300 , 600 );
149151
150152 // Mock HttpClientBuilder
@@ -155,25 +157,44 @@ public void testCreateHttpClient_WithValidateCertificateTrue()
155157 when (mockBuilder .build ()).thenReturn (httpClient );
156158 }
157159
160+ // Test the method
161+ CloseableHttpClient client = backupHttpClientProvider .createHttpClient ();
162+
163+ // Verify and assert
164+ assertNotNull (client );
165+ }
166+
167+ @ Test
168+ public void testCreateHttpClient_WithValidateCertificateFalse ()
169+ throws KeyManagementException , NoSuchAlgorithmException , URISyntaxException , BackrollApiException {
170+ backupHttpClientProvider = BackrollHttpClientProvider .createProvider (backupHttpClientProvider ,
171+ "http://api.backup.demo.ccc:5050/api/v1" , "backroll-api" , "VviX8dALauSyYJMqVYJqf3UyZOpO3joS" , false ,
172+ 300 , 600 );
158173
174+ // Mock HttpClientBuilder
175+ HttpClientBuilder mockBuilder = mock (HttpClientBuilder .class );
176+ try (MockedStatic <HttpClientBuilder > utilities = Mockito .mockStatic (HttpClientBuilder .class )) {
177+ utilities .when (HttpClientBuilder ::create ).thenReturn (mockBuilder );
178+ when (mockBuilder .setDefaultRequestConfig (config )).thenReturn (mockBuilder );
179+ when (mockBuilder .setSSLSocketFactory (any (SSLConnectionSocketFactory .class ))).thenReturn (mockBuilder );
180+ when (mockBuilder .build ()).thenReturn (httpClient );
181+ }
159182
160183 // Test the method
161184 CloseableHttpClient client = backupHttpClientProvider .createHttpClient ();
162185
163186 // Verify and assert
164- //verify(mockBuilder).setDefaultRequestConfig(config);
165187 assertNotNull (client );
166- //assertTrue(client.getClass() == CloseableHttpClient.class);
167- //assertEquals(mockHttpClient, client);
168188 }
189+
169190 @ Test
170191 public void NotOkBodyException_Test (){
171192 BackrollHttpClientProvider .NotOkBodyException exception = backupHttpClientProvider .new NotOkBodyException ();
172193 assertNotNull (exception );
173194 }
174195
175196 @ Test
176- public void get_Test_success () throws Exception , BackrollApiException , IOException {
197+ public void get_Test_success () throws Exception {
177198 // Arrange
178199 String path = "/test" ;
179200 defaultTestHttpClient (path );
@@ -191,7 +212,7 @@ public void get_Test_success() throws Exception, BackrollApiException, IOExcepti
191212 }
192213
193214 @ Test
194- public void delete_Test_success () throws Exception , BackrollApiException , IOException {
215+ public void delete_Test_success () throws Exception {
195216 // Arrange
196217 String path = "/test" ;
197218 defaultTestHttpClient (path );
@@ -209,25 +230,31 @@ public void delete_Test_success() throws Exception, BackrollApiException, IOExce
209230 }
210231
211232 @ Test
212- public void okBody_Test () throws BackrollApiException , IOException , NotOkBodyException {
213-
233+ public void okBody_Test_success () throws BackrollApiException , IOException , NotOkBodyException {
214234 StatusLine statusLine = mock (StatusLine .class );
215235 doReturn (statusLine ).when (response ).getStatusLine ();
216236 doReturn (HttpStatus .SC_OK ).when (statusLine ).getStatusCode ();
217237 doReturn (new StringEntity ("{\" mockKey\" : \" mockValue\" }" , ContentType .APPLICATION_JSON )).when (response )
218238 .getEntity ();
219239 doNothing ().when (response ).close ();
240+
220241 String result = backupHttpClientProvider .okBody (response );
221242 assertNotNull (result );
243+ }
222244
245+ @ Test (expected = BackrollHttpClientProvider .NotOkBodyException .class )
246+ public void okBody_Test_Error () throws BackrollApiException , IOException , NotOkBodyException {
247+ StatusLine statusLine = mock (StatusLine .class );
248+ doReturn (statusLine ).when (response ).getStatusLine ();
249+ doReturn (HttpStatus .SC_INTERNAL_SERVER_ERROR ).when (statusLine ).getStatusCode ();
250+
251+ backupHttpClientProvider .okBody (response );
223252 }
224253
225254 @ Test
226- public void waitGet_Test () throws Exception , BackrollApiException , IOException {
255+ public void waitGet_Test () throws Exception {
227256 String path = "/test" ;
228257 defaultTestHttpClient (path );
229- doReturn (response ).when (httpClient )
230- .execute (argThat (argument -> argument != null && argument .getURI ().toString ().contains ("/auth" )));
231258
232259 // Act
233260 VirtualMachineBackupsResponse result = backupHttpClientProvider .waitGet (path ,
@@ -236,36 +263,32 @@ public void waitGet_Test() throws Exception, BackrollApiException, IOException {
236263 // Assert
237264 assertNotNull (result );
238265 verify (backupHttpClientProvider , times (2 )).okBody (Mockito .any (CloseableHttpResponse .class ));
239- verify (httpClient , times (1 )).execute (Mockito .any (HttpPost .class ));
240266 verify (httpClient , times (1 )).execute (Mockito .any (HttpGet .class ));
241267 verify (response , times (1 )).close ();
242268 }
243269
244270 @ Test
245- public void waitGetWithoutParseResponse_Test () throws Exception , BackrollApiException , IOException {
271+ public void waitGetWithoutParseResponse_Test () throws Exception {
246272 String path = "/test" ;
247273 defaultTestHttpClient (path );
248- doReturn (response ).when (httpClient )
249- .execute (argThat (argument -> argument != null && argument .getURI ().toString ().contains ("/auth" )));
250274
251275 // Act
252276 String result = backupHttpClientProvider .waitGetWithoutParseResponse (path );
253277
254278 // Assert
255279 assertNotNull (result );
256280 verify (backupHttpClientProvider , times (2 )).okBody (Mockito .any (CloseableHttpResponse .class ));
257- verify (httpClient , times (1 )).execute (Mockito .any (HttpPost .class ));
258281 verify (httpClient , times (1 )).execute (Mockito .any (HttpGet .class ));
259282 verify (response , times (1 )).close ();
260283 }
261284
262285 @ Test
263- public void testPost_success () throws Exception , BackrollApiException , IOException {
286+ public void testPost_success () throws Exception {
264287 // Arrange
265288 String path = "/test" ;
266289 JSONObject json = new JSONObject ();
267-
268290 defaultTestHttpClient (path );
291+
269292 // Act
270293 VirtualMachineBackupsResponse result = backupHttpClientProvider .post (path , json ,
271294 VirtualMachineBackupsResponse .class );
@@ -277,4 +300,25 @@ public void testPost_success() throws Exception, BackrollApiException, IOExcepti
277300 verify (response , times (1 )).close ();
278301 }
279302
303+ @ Test
304+ public void testAuthenticationFailure () throws Exception {
305+ doReturn (false ).when (backupHttpClientProvider ).isAuthenticated ();
306+ doNothing ().when (backupHttpClientProvider ).login (Mockito .anyString (), Mockito .anyString ());
307+ backupHttpClientProvider .loginIfAuthenticationFailed ();
308+ verify (backupHttpClientProvider ).login (Mockito .anyString (), Mockito .anyString ());
309+ }
310+
311+ @ Test (expected = BackrollApiException .class )
312+ public void testLoginFailure () throws Exception {
313+ doReturn (false ).when (backupHttpClientProvider ).isAuthenticated ();
314+ doThrow (BackrollApiException .class ).when (backupHttpClientProvider ).login (Mockito .anyString (), Mockito .anyString ());
315+ backupHttpClientProvider .loginIfAuthenticationFailed ();
316+ }
317+
318+ @ Test
319+ public void testLoginSuccess () throws Exception {
320+ doReturn (true ).when (backupHttpClientProvider ).isAuthenticated ();
321+ backupHttpClientProvider .loginIfAuthenticationFailed ();
322+ verify (backupHttpClientProvider , times (0 )).login (Mockito .anyString (), Mockito .anyString ());
323+ }
280324}
0 commit comments