Skip to content

Commit 81d9669

Browse files
committed
UT + unnessary login remove
1 parent a2dcaf2 commit 81d9669

File tree

3 files changed

+147
-6
lines changed

3 files changed

+147
-6
lines changed

plugins/backup/backroll/src/main/java/org/apache/cloudstack/backup/backroll/utils/BackrollHttpClientProvider.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,6 @@ public String okBody(final CloseableHttpResponse response) throws NotOkBodyExcep
260260
}
261261

262262
public <T> T waitGet(String url, Class<T> classOfT) throws IOException, BackrollApiException {
263-
loginIfAuthenticationFailed();
264-
265263
// int threshold = 30; // 5 minutes
266264
int maxAttempts = 12; // 2 minutes
267265

plugins/backup/backroll/src/test/java/org/apache/cloudstack/backup/backroll/BackrollClientTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.apache.cloudstack.backup.backroll.model.BackrollVmBackup;
3232
import org.apache.cloudstack.backup.backroll.model.response.BackrollTaskRequestResponse;
3333
import org.apache.cloudstack.backup.backroll.model.response.TaskState;
34+
import org.apache.cloudstack.backup.backroll.model.response.archive.BackrollArchiveResponse;
35+
import org.apache.cloudstack.backup.backroll.model.response.archive.BackrollArchivesResponse;
3436
import org.apache.cloudstack.backup.backroll.model.response.archive.BackrollBackupsFromVMResponse;
3537
import org.apache.cloudstack.backup.backroll.model.response.metrics.backup.BackrollBackupMetricsResponse;
3638
import org.apache.cloudstack.backup.backroll.model.response.metrics.backup.BackupMetricsInfo;
@@ -134,6 +136,8 @@ public void deleteBackup_Test() throws IOException, BackrollApiException{
134136

135137
BackrollBackupsFromVMResponse mockResponse = new BackrollBackupsFromVMResponse();
136138
mockResponse.state = TaskState.SUCCESS;
139+
mockResponse.archives = new BackrollArchivesResponse();
140+
mockResponse.archives.archives = Arrays.asList(new BackrollArchiveResponse());
137141

138142
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock).delete(Mockito.matches(".*/virtualmachines/.*"), Mockito.any());
139143
doReturn(mockResponse).when(backrollHttpClientProviderMock).waitGet(Mockito.anyString(), Mockito.any());

plugins/backup/backroll/src/test/java/org/apache/cloudstack/backup/backroll/utils/BackrollHttpClientProviderTest.java

Lines changed: 143 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,40 @@
1616
// under the License.
1717
package org.apache.cloudstack.backup.backroll.utils;
1818

19+
import static org.junit.Assert.assertEquals;
1920
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertThat;
22+
import static org.junit.Assert.assertTrue;
23+
import static org.mockito.ArgumentMatchers.any;
2024
import static org.mockito.ArgumentMatchers.argThat;
25+
import static org.mockito.ArgumentMatchers.eq;
2126
import static org.mockito.Mockito.doNothing;
2227
import static org.mockito.Mockito.doReturn;
2328
import static org.mockito.Mockito.mock;
2429
import static org.mockito.Mockito.times;
2530
import static org.mockito.Mockito.verify;
31+
import static org.mockito.Mockito.when;
2632

2733
import java.io.IOException;
34+
import java.net.URISyntaxException;
35+
import java.security.KeyManagementException;
36+
import java.security.NoSuchAlgorithmException;
37+
import java.security.SecureRandom;
38+
39+
import javax.net.ssl.SSLContext;
40+
import javax.net.ssl.TrustManager;
2841

2942
import org.apache.cloudstack.backup.backroll.model.response.api.LoginApiResponse;
3043
import org.apache.cloudstack.backup.backroll.model.response.metrics.virtualMachineBackups.VirtualMachineBackupsResponse;
3144
import org.apache.cloudstack.backup.backroll.utils.BackrollHttpClientProvider.NotOkBodyException;
45+
import org.apache.http.HttpEntity;
3246
import org.apache.http.HttpStatus;
47+
import org.apache.http.ParseException;
3348
import org.apache.http.StatusLine;
3449
import org.apache.http.client.ClientProtocolException;
50+
import org.apache.http.client.config.RequestConfig;
3551
import org.apache.http.client.methods.CloseableHttpResponse;
52+
import org.apache.http.client.methods.HttpDelete;
3653
import org.apache.http.client.methods.HttpGet;
3754
import org.apache.http.client.methods.HttpPost;
3855
import org.apache.http.entity.ContentType;
@@ -43,9 +60,13 @@
4360
import org.junit.Test;
4461
import org.mockito.InjectMocks;
4562
import org.mockito.Mock;
63+
import org.mockito.MockedStatic;
4664
import org.mockito.Mockito;
4765
import org.mockito.MockitoAnnotations;
4866
import org.mockito.Spy;
67+
import org.apache.cloudstack.utils.security.SSLUtils;
68+
import org.apache.http.impl.client.HttpClientBuilder;
69+
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
4970

5071
import com.fasterxml.jackson.databind.ObjectMapper;
5172

@@ -60,15 +81,31 @@ public class BackrollHttpClientProviderTest {
6081
@Mock
6182
private CloseableHttpResponse response;
6283

84+
@Mock
85+
private RequestConfig config;
86+
87+
@Spy
88+
private SSLUtils sslUtils;
89+
90+
@Spy
91+
private HttpClientBuilder httpClientBuilder;
92+
93+
@Mock
94+
private SSLContext sslContext;
95+
96+
@Mock
97+
private SSLConnectionSocketFactory sslConnectionSocketFactory;
98+
6399
@Before
64100
public void setUp() throws Exception {
65101
MockitoAnnotations.openMocks(this);
66102
backupHttpClientProvider = BackrollHttpClientProvider.createProvider(backupHttpClientProvider,
67-
"http://api.backup.demo.ccc:5050/api/v1", "backroll-api", "VviX8dALauSyYJMqVYJqf3UyZOpO3joS", false,
103+
"http://api.backup.demo.ccc:5050/api/v1", "backroll-api", "VviX8dALauSyYJMqVYJqf3UyZOpO3joS", true,
68104
300, 600);
69105
}
70106

71-
private void defaultTestHttpClient(String path) throws BackrollApiException, ClientProtocolException, IOException, NotOkBodyException {
107+
private void defaultTestHttpClient(String path)
108+
throws BackrollApiException, ClientProtocolException, IOException, NotOkBodyException {
72109

73110
LoginApiResponse responseLogin = new LoginApiResponse();
74111
responseLogin.accessToken = "dummyToken";
@@ -99,11 +136,44 @@ private void defaultTestHttpClient(String path) throws BackrollApiException, Cli
99136

100137
doNothing().when(response).close();
101138

102-
doReturn(new StringEntity("{\"mockKey\": \"mockValue\"}", ContentType.APPLICATION_JSON)).when(response).getEntity();
139+
doReturn(new StringEntity("{\"mockKey\": \"mockValue\"}", ContentType.APPLICATION_JSON)).when(response)
140+
.getEntity();
103141
}
104142

105143
@Test
106-
public void testGet_success() throws Exception, BackrollApiException, IOException {
144+
public void testCreateHttpClient_WithValidateCertificateTrue()
145+
throws KeyManagementException, NoSuchAlgorithmException, URISyntaxException, BackrollApiException {
146+
backupHttpClientProvider = BackrollHttpClientProvider.createProvider(backupHttpClientProvider,
147+
"http://api.backup.demo.ccc:5050/api/v1", "backroll-api", "VviX8dALauSyYJMqVYJqf3UyZOpO3joS", false,
148+
300, 600);
149+
150+
// Mock HttpClientBuilder
151+
HttpClientBuilder mockBuilder = mock(HttpClientBuilder.class);
152+
try (MockedStatic<HttpClientBuilder> utilities = Mockito.mockStatic(HttpClientBuilder.class)) {
153+
utilities.when(HttpClientBuilder::create).thenReturn(mockBuilder);
154+
when(mockBuilder.setDefaultRequestConfig(config)).thenReturn(mockBuilder);
155+
when(mockBuilder.build()).thenReturn(httpClient);
156+
}
157+
158+
159+
160+
// Test the method
161+
CloseableHttpClient client = backupHttpClientProvider.createHttpClient();
162+
163+
// Verify and assert
164+
//verify(mockBuilder).setDefaultRequestConfig(config);
165+
assertNotNull(client);
166+
//assertTrue(client.getClass() == CloseableHttpClient.class);
167+
//assertEquals(mockHttpClient, client);
168+
}
169+
@Test
170+
public void NotOkBodyException_Test(){
171+
BackrollHttpClientProvider.NotOkBodyException exception = backupHttpClientProvider.new NotOkBodyException();
172+
assertNotNull(exception);
173+
}
174+
175+
@Test
176+
public void get_Test_success() throws Exception, BackrollApiException, IOException {
107177
// Arrange
108178
String path = "/test";
109179
defaultTestHttpClient(path);
@@ -120,6 +190,75 @@ public void testGet_success() throws Exception, BackrollApiException, IOExceptio
120190
verify(response, times(1)).close();
121191
}
122192

193+
@Test
194+
public void delete_Test_success() throws Exception, BackrollApiException, IOException {
195+
// Arrange
196+
String path = "/test";
197+
defaultTestHttpClient(path);
198+
199+
// Act
200+
VirtualMachineBackupsResponse result = backupHttpClientProvider.delete(path,
201+
VirtualMachineBackupsResponse.class);
202+
203+
// Assert
204+
assertNotNull(result);
205+
verify(backupHttpClientProvider, times(2)).okBody(Mockito.any(CloseableHttpResponse.class));
206+
verify(httpClient, times(1)).execute(Mockito.any(HttpPost.class));
207+
verify(httpClient, times(1)).execute(Mockito.any(HttpDelete.class));
208+
verify(response, times(1)).close();
209+
}
210+
211+
@Test
212+
public void okBody_Test() throws BackrollApiException, IOException, NotOkBodyException {
213+
214+
StatusLine statusLine = mock(StatusLine.class);
215+
doReturn(statusLine).when(response).getStatusLine();
216+
doReturn(HttpStatus.SC_OK).when(statusLine).getStatusCode();
217+
doReturn(new StringEntity("{\"mockKey\": \"mockValue\"}", ContentType.APPLICATION_JSON)).when(response)
218+
.getEntity();
219+
doNothing().when(response).close();
220+
String result = backupHttpClientProvider.okBody(response);
221+
assertNotNull(result);
222+
223+
}
224+
225+
@Test
226+
public void waitGet_Test() throws Exception, BackrollApiException, IOException {
227+
String path = "/test";
228+
defaultTestHttpClient(path);
229+
doReturn(response).when(httpClient)
230+
.execute(argThat(argument -> argument != null && argument.getURI().toString().contains("/auth")));
231+
232+
// Act
233+
VirtualMachineBackupsResponse result = backupHttpClientProvider.waitGet(path,
234+
VirtualMachineBackupsResponse.class);
235+
236+
// Assert
237+
assertNotNull(result);
238+
verify(backupHttpClientProvider, times(2)).okBody(Mockito.any(CloseableHttpResponse.class));
239+
verify(httpClient, times(1)).execute(Mockito.any(HttpPost.class));
240+
verify(httpClient, times(1)).execute(Mockito.any(HttpGet.class));
241+
verify(response, times(1)).close();
242+
}
243+
244+
@Test
245+
public void waitGetWithoutParseResponse_Test() throws Exception, BackrollApiException, IOException {
246+
String path = "/test";
247+
defaultTestHttpClient(path);
248+
doReturn(response).when(httpClient)
249+
.execute(argThat(argument -> argument != null && argument.getURI().toString().contains("/auth")));
250+
251+
// Act
252+
String result = backupHttpClientProvider.waitGetWithoutParseResponse(path);
253+
254+
// Assert
255+
assertNotNull(result);
256+
verify(backupHttpClientProvider, times(2)).okBody(Mockito.any(CloseableHttpResponse.class));
257+
verify(httpClient, times(1)).execute(Mockito.any(HttpPost.class));
258+
verify(httpClient, times(1)).execute(Mockito.any(HttpGet.class));
259+
verify(response, times(1)).close();
260+
}
261+
123262
@Test
124263
public void testPost_success() throws Exception, BackrollApiException, IOException {
125264
// Arrange

0 commit comments

Comments
 (0)