|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package org.apache.cloudstack.backup.backroll.utils; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertNotNull; |
| 21 | +import static org.junit.Assert.assertThrows; |
| 22 | +import static org.mockito.ArgumentMatchers.any; |
| 23 | +import static org.mockito.ArgumentMatchers.argThat; |
| 24 | +import static org.mockito.Mockito.doNothing; |
| 25 | +import static org.mockito.Mockito.doReturn; |
| 26 | +import static org.mockito.Mockito.mock; |
| 27 | +import static org.mockito.Mockito.times; |
| 28 | +import static org.mockito.Mockito.verify; |
| 29 | +import static org.mockito.Mockito.when; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.lang.reflect.Method; |
| 33 | +import java.net.http.HttpResponse; |
| 34 | + |
| 35 | +import org.apache.cloudstack.backup.BackrollBackupProvider; |
| 36 | +import org.apache.cloudstack.backup.backroll.BackrollClient; |
| 37 | +import org.apache.cloudstack.backup.backroll.model.response.api.LoginApiResponse; |
| 38 | +import org.apache.cloudstack.backup.backroll.model.response.metrics.virtualMachineBackups.VirtualMachineBackupsResponse; |
| 39 | +import org.apache.cloudstack.backup.backroll.utils.BackrollHttpClientProvider.NotOkBodyException; |
| 40 | +import org.apache.http.HttpHeaders; |
| 41 | +import org.apache.http.HttpStatus; |
| 42 | +import org.apache.http.ParseException; |
| 43 | +import org.apache.http.StatusLine; |
| 44 | +import org.apache.http.client.ClientProtocolException; |
| 45 | +import org.apache.http.client.methods.CloseableHttpResponse; |
| 46 | +import org.apache.http.client.methods.HttpGet; |
| 47 | +import org.apache.http.client.methods.HttpPost; |
| 48 | +import org.apache.http.entity.ContentType; |
| 49 | +import org.apache.http.entity.StringEntity; |
| 50 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 51 | +import org.apache.logging.log4j.Logger; |
| 52 | +import org.json.JSONObject; |
| 53 | +import org.junit.Before; |
| 54 | +import org.junit.Test; |
| 55 | +import org.mockito.InjectMocks; |
| 56 | +import org.mockito.Mock; |
| 57 | +import org.mockito.Mockito; |
| 58 | +import org.mockito.MockitoAnnotations; |
| 59 | +import org.mockito.Spy; |
| 60 | + |
| 61 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 62 | + |
| 63 | +public class BackrollHttpClientProviderTest { |
| 64 | + @Spy |
| 65 | + @InjectMocks |
| 66 | + BackrollHttpClientProvider backupHttpClientProvider; |
| 67 | + |
| 68 | + @Mock |
| 69 | + private CloseableHttpClient httpClient; |
| 70 | + |
| 71 | + @Mock |
| 72 | + private CloseableHttpResponse response; |
| 73 | + |
| 74 | + @Before |
| 75 | + public void setUp() throws Exception { |
| 76 | + Long zoneId = 2L; |
| 77 | + MockitoAnnotations.openMocks(this); |
| 78 | + backupHttpClientProvider = BackrollHttpClientProvider.createProvider(backupHttpClientProvider, |
| 79 | + "http://api.backup.demo.ccc:5050/api/v1", "backroll-api", "VviX8dALauSyYJMqVYJqf3UyZOpO3joS", false, |
| 80 | + 300, 600); |
| 81 | + } |
| 82 | + |
| 83 | + private void defaultTestHttpClient(String path) throws BackrollApiException, ClientProtocolException, IOException, NotOkBodyException { |
| 84 | + |
| 85 | + LoginApiResponse responseLogin = new LoginApiResponse(); |
| 86 | + responseLogin.accessToken = "dummyToken"; |
| 87 | + responseLogin.expiresIn = 3600; |
| 88 | + responseLogin.notBeforePolicy = "dummyNotBeforePolicy"; |
| 89 | + responseLogin.refreshExpiresIn = "7200"; |
| 90 | + responseLogin.scope = "dummyScope"; |
| 91 | + |
| 92 | + 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\" } } }"; |
| 93 | + |
| 94 | + CloseableHttpResponse response2 = mock(CloseableHttpResponse.class); |
| 95 | + |
| 96 | + StatusLine statusLine = mock(StatusLine.class); |
| 97 | + |
| 98 | + doReturn(httpClient).when(backupHttpClientProvider).createHttpClient(); |
| 99 | + |
| 100 | + doReturn(response).when(httpClient) |
| 101 | + .execute(argThat(argument -> argument != null && argument.getURI().toString().contains("login"))); |
| 102 | + |
| 103 | + doReturn(response2).when(httpClient) |
| 104 | + .execute(argThat(argument -> argument != null && argument.getURI().toString().contains(path))); |
| 105 | + |
| 106 | + doReturn(new ObjectMapper().writeValueAsString(responseLogin)).when(backupHttpClientProvider).okBody(response); |
| 107 | + doReturn(virtualMachineResponseString).when(backupHttpClientProvider).okBody(response2); |
| 108 | + |
| 109 | + doReturn(statusLine).when(response).getStatusLine(); |
| 110 | + doReturn(HttpStatus.SC_OK).when(statusLine).getStatusCode(); |
| 111 | + |
| 112 | + doNothing().when(response).close(); |
| 113 | + |
| 114 | + doReturn(new StringEntity("{\"mockKey\": \"mockValue\"}", ContentType.APPLICATION_JSON)).when(response).getEntity(); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testGet_success() throws Exception, BackrollApiException, IOException { |
| 119 | + // Arrange |
| 120 | + String path = "/test"; |
| 121 | + defaultTestHttpClient(path); |
| 122 | + |
| 123 | + // Act |
| 124 | + VirtualMachineBackupsResponse result = backupHttpClientProvider.get(path, |
| 125 | + VirtualMachineBackupsResponse.class); |
| 126 | + |
| 127 | + // Assert |
| 128 | + assertNotNull(result); |
| 129 | + verify(backupHttpClientProvider, times(2)).okBody(Mockito.any(CloseableHttpResponse.class)); |
| 130 | + verify(httpClient, times(1)).execute(Mockito.any(HttpPost.class)); |
| 131 | + verify(httpClient, times(1)).execute(Mockito.any(HttpGet.class)); |
| 132 | + verify(response, times(1)).close(); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testPost_success() throws Exception, BackrollApiException, IOException { |
| 137 | + // Arrange |
| 138 | + String path = "/test"; |
| 139 | + JSONObject json = new JSONObject(); |
| 140 | + |
| 141 | + defaultTestHttpClient(path); |
| 142 | + // Act |
| 143 | + VirtualMachineBackupsResponse result = backupHttpClientProvider.post(path, json, |
| 144 | + VirtualMachineBackupsResponse.class); |
| 145 | + |
| 146 | + // Assert |
| 147 | + assertNotNull(result); |
| 148 | + verify(backupHttpClientProvider, times(2)).okBody(Mockito.any(CloseableHttpResponse.class)); |
| 149 | + verify(httpClient, times(2)).execute(Mockito.any(HttpPost.class)); |
| 150 | + verify(response, times(1)).close(); |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments