mirror of https://github.com/apache/cloudstack.git
UT + unnessary login remove
This commit is contained in:
parent
a2dcaf251c
commit
81d9669603
|
|
@ -260,8 +260,6 @@ public class BackrollHttpClientProvider {
|
|||
}
|
||||
|
||||
public <T> T waitGet(String url, Class<T> classOfT) throws IOException, BackrollApiException {
|
||||
loginIfAuthenticationFailed();
|
||||
|
||||
// int threshold = 30; // 5 minutes
|
||||
int maxAttempts = 12; // 2 minutes
|
||||
|
||||
|
|
|
|||
|
|
@ -31,6 +31,8 @@ import org.apache.cloudstack.backup.backroll.model.BackrollTaskStatus;
|
|||
import org.apache.cloudstack.backup.backroll.model.BackrollVmBackup;
|
||||
import org.apache.cloudstack.backup.backroll.model.response.BackrollTaskRequestResponse;
|
||||
import org.apache.cloudstack.backup.backroll.model.response.TaskState;
|
||||
import org.apache.cloudstack.backup.backroll.model.response.archive.BackrollArchiveResponse;
|
||||
import org.apache.cloudstack.backup.backroll.model.response.archive.BackrollArchivesResponse;
|
||||
import org.apache.cloudstack.backup.backroll.model.response.archive.BackrollBackupsFromVMResponse;
|
||||
import org.apache.cloudstack.backup.backroll.model.response.metrics.backup.BackrollBackupMetricsResponse;
|
||||
import org.apache.cloudstack.backup.backroll.model.response.metrics.backup.BackupMetricsInfo;
|
||||
|
|
@ -134,6 +136,8 @@ public class BackrollClientTest {
|
|||
|
||||
BackrollBackupsFromVMResponse mockResponse = new BackrollBackupsFromVMResponse();
|
||||
mockResponse.state = TaskState.SUCCESS;
|
||||
mockResponse.archives = new BackrollArchivesResponse();
|
||||
mockResponse.archives.archives = Arrays.asList(new BackrollArchiveResponse());
|
||||
|
||||
doReturn(backrollTaskReqResponseMock).when(backrollHttpClientProviderMock).delete(Mockito.matches(".*/virtualmachines/.*"), Mockito.any());
|
||||
doReturn(mockResponse).when(backrollHttpClientProviderMock).waitGet(Mockito.anyString(), Mockito.any());
|
||||
|
|
|
|||
|
|
@ -16,23 +16,40 @@
|
|||
// under the License.
|
||||
package org.apache.cloudstack.backup.backroll.utils;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
|
||||
import org.apache.cloudstack.backup.backroll.model.response.api.LoginApiResponse;
|
||||
import org.apache.cloudstack.backup.backroll.model.response.metrics.virtualMachineBackups.VirtualMachineBackupsResponse;
|
||||
import org.apache.cloudstack.backup.backroll.utils.BackrollHttpClientProvider.NotOkBodyException;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.ParseException;
|
||||
import org.apache.http.StatusLine;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
|
|
@ -43,9 +60,13 @@ import org.junit.Before;
|
|||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.Spy;
|
||||
import org.apache.cloudstack.utils.security.SSLUtils;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
|
|
@ -60,15 +81,31 @@ public class BackrollHttpClientProviderTest {
|
|||
@Mock
|
||||
private CloseableHttpResponse response;
|
||||
|
||||
@Mock
|
||||
private RequestConfig config;
|
||||
|
||||
@Spy
|
||||
private SSLUtils sslUtils;
|
||||
|
||||
@Spy
|
||||
private HttpClientBuilder httpClientBuilder;
|
||||
|
||||
@Mock
|
||||
private SSLContext sslContext;
|
||||
|
||||
@Mock
|
||||
private SSLConnectionSocketFactory sslConnectionSocketFactory;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
backupHttpClientProvider = BackrollHttpClientProvider.createProvider(backupHttpClientProvider,
|
||||
"http://api.backup.demo.ccc:5050/api/v1", "backroll-api", "VviX8dALauSyYJMqVYJqf3UyZOpO3joS", false,
|
||||
"http://api.backup.demo.ccc:5050/api/v1", "backroll-api", "VviX8dALauSyYJMqVYJqf3UyZOpO3joS", true,
|
||||
300, 600);
|
||||
}
|
||||
|
||||
private void defaultTestHttpClient(String path) throws BackrollApiException, ClientProtocolException, IOException, NotOkBodyException {
|
||||
private void defaultTestHttpClient(String path)
|
||||
throws BackrollApiException, ClientProtocolException, IOException, NotOkBodyException {
|
||||
|
||||
LoginApiResponse responseLogin = new LoginApiResponse();
|
||||
responseLogin.accessToken = "dummyToken";
|
||||
|
|
@ -99,11 +136,44 @@ public class BackrollHttpClientProviderTest {
|
|||
|
||||
doNothing().when(response).close();
|
||||
|
||||
doReturn(new StringEntity("{\"mockKey\": \"mockValue\"}", ContentType.APPLICATION_JSON)).when(response).getEntity();
|
||||
doReturn(new StringEntity("{\"mockKey\": \"mockValue\"}", ContentType.APPLICATION_JSON)).when(response)
|
||||
.getEntity();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet_success() throws Exception, BackrollApiException, IOException {
|
||||
public void testCreateHttpClient_WithValidateCertificateTrue()
|
||||
throws KeyManagementException, NoSuchAlgorithmException, URISyntaxException, BackrollApiException {
|
||||
backupHttpClientProvider = BackrollHttpClientProvider.createProvider(backupHttpClientProvider,
|
||||
"http://api.backup.demo.ccc:5050/api/v1", "backroll-api", "VviX8dALauSyYJMqVYJqf3UyZOpO3joS", false,
|
||||
300, 600);
|
||||
|
||||
// Mock HttpClientBuilder
|
||||
HttpClientBuilder mockBuilder = mock(HttpClientBuilder.class);
|
||||
try (MockedStatic<HttpClientBuilder> utilities = Mockito.mockStatic(HttpClientBuilder.class)) {
|
||||
utilities.when(HttpClientBuilder::create).thenReturn(mockBuilder);
|
||||
when(mockBuilder.setDefaultRequestConfig(config)).thenReturn(mockBuilder);
|
||||
when(mockBuilder.build()).thenReturn(httpClient);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Test the method
|
||||
CloseableHttpClient client = backupHttpClientProvider.createHttpClient();
|
||||
|
||||
// Verify and assert
|
||||
//verify(mockBuilder).setDefaultRequestConfig(config);
|
||||
assertNotNull(client);
|
||||
//assertTrue(client.getClass() == CloseableHttpClient.class);
|
||||
//assertEquals(mockHttpClient, client);
|
||||
}
|
||||
@Test
|
||||
public void NotOkBodyException_Test(){
|
||||
BackrollHttpClientProvider.NotOkBodyException exception = backupHttpClientProvider.new NotOkBodyException();
|
||||
assertNotNull(exception);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void get_Test_success() throws Exception, BackrollApiException, IOException {
|
||||
// Arrange
|
||||
String path = "/test";
|
||||
defaultTestHttpClient(path);
|
||||
|
|
@ -120,6 +190,75 @@ public class BackrollHttpClientProviderTest {
|
|||
verify(response, times(1)).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void delete_Test_success() throws Exception, BackrollApiException, IOException {
|
||||
// Arrange
|
||||
String path = "/test";
|
||||
defaultTestHttpClient(path);
|
||||
|
||||
// Act
|
||||
VirtualMachineBackupsResponse result = backupHttpClientProvider.delete(path,
|
||||
VirtualMachineBackupsResponse.class);
|
||||
|
||||
// Assert
|
||||
assertNotNull(result);
|
||||
verify(backupHttpClientProvider, times(2)).okBody(Mockito.any(CloseableHttpResponse.class));
|
||||
verify(httpClient, times(1)).execute(Mockito.any(HttpPost.class));
|
||||
verify(httpClient, times(1)).execute(Mockito.any(HttpDelete.class));
|
||||
verify(response, times(1)).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void okBody_Test() throws BackrollApiException, IOException, NotOkBodyException {
|
||||
|
||||
StatusLine statusLine = mock(StatusLine.class);
|
||||
doReturn(statusLine).when(response).getStatusLine();
|
||||
doReturn(HttpStatus.SC_OK).when(statusLine).getStatusCode();
|
||||
doReturn(new StringEntity("{\"mockKey\": \"mockValue\"}", ContentType.APPLICATION_JSON)).when(response)
|
||||
.getEntity();
|
||||
doNothing().when(response).close();
|
||||
String result = backupHttpClientProvider.okBody(response);
|
||||
assertNotNull(result);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void waitGet_Test() throws Exception, BackrollApiException, IOException {
|
||||
String path = "/test";
|
||||
defaultTestHttpClient(path);
|
||||
doReturn(response).when(httpClient)
|
||||
.execute(argThat(argument -> argument != null && argument.getURI().toString().contains("/auth")));
|
||||
|
||||
// Act
|
||||
VirtualMachineBackupsResponse result = backupHttpClientProvider.waitGet(path,
|
||||
VirtualMachineBackupsResponse.class);
|
||||
|
||||
// Assert
|
||||
assertNotNull(result);
|
||||
verify(backupHttpClientProvider, times(2)).okBody(Mockito.any(CloseableHttpResponse.class));
|
||||
verify(httpClient, times(1)).execute(Mockito.any(HttpPost.class));
|
||||
verify(httpClient, times(1)).execute(Mockito.any(HttpGet.class));
|
||||
verify(response, times(1)).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void waitGetWithoutParseResponse_Test() throws Exception, BackrollApiException, IOException {
|
||||
String path = "/test";
|
||||
defaultTestHttpClient(path);
|
||||
doReturn(response).when(httpClient)
|
||||
.execute(argThat(argument -> argument != null && argument.getURI().toString().contains("/auth")));
|
||||
|
||||
// Act
|
||||
String result = backupHttpClientProvider.waitGetWithoutParseResponse(path);
|
||||
|
||||
// Assert
|
||||
assertNotNull(result);
|
||||
verify(backupHttpClientProvider, times(2)).okBody(Mockito.any(CloseableHttpResponse.class));
|
||||
verify(httpClient, times(1)).execute(Mockito.any(HttpPost.class));
|
||||
verify(httpClient, times(1)).execute(Mockito.any(HttpGet.class));
|
||||
verify(response, times(1)).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPost_success() throws Exception, BackrollApiException, IOException {
|
||||
// Arrange
|
||||
|
|
|
|||
Loading…
Reference in New Issue