|
| 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 | + |
| 18 | +package org.apache.cloudstack.api.command.admin.backup; |
| 19 | + |
| 20 | +import com.cloud.exception.InvalidParameterValueException; |
| 21 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 22 | +import org.apache.cloudstack.api.ApiErrorCode; |
| 23 | +import org.apache.cloudstack.api.ResponseGenerator; |
| 24 | +import org.apache.cloudstack.api.ServerApiException; |
| 25 | +import org.apache.cloudstack.api.response.BackupOfferingResponse; |
| 26 | +import org.apache.cloudstack.backup.BackupManager; |
| 27 | +import org.apache.cloudstack.backup.BackupOffering; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.mockito.Mock; |
| 32 | +import org.mockito.junit.MockitoJUnitRunner; |
| 33 | +import org.springframework.test.util.ReflectionTestUtils; |
| 34 | + |
| 35 | +import static org.junit.Assert.assertEquals; |
| 36 | +import static org.junit.Assert.assertNotNull; |
| 37 | +import static org.junit.Assert.assertNull; |
| 38 | +import static org.junit.Assert.assertTrue; |
| 39 | +import static org.junit.Assert.fail; |
| 40 | +import static org.mockito.ArgumentMatchers.any; |
| 41 | +import static org.mockito.Mockito.when; |
| 42 | + |
| 43 | +@RunWith(MockitoJUnitRunner.class) |
| 44 | +public class CloneBackupOfferingCmdTest { |
| 45 | + |
| 46 | + private CloneBackupOfferingCmd cloneBackupOfferingCmd; |
| 47 | + |
| 48 | + @Mock |
| 49 | + private BackupManager backupManager; |
| 50 | + |
| 51 | + @Mock |
| 52 | + private ResponseGenerator responseGenerator; |
| 53 | + |
| 54 | + @Mock |
| 55 | + private BackupOffering mockBackupOffering; |
| 56 | + |
| 57 | + @Mock |
| 58 | + private BackupOfferingResponse mockBackupOfferingResponse; |
| 59 | + |
| 60 | + @Before |
| 61 | + public void setUp() { |
| 62 | + cloneBackupOfferingCmd = new CloneBackupOfferingCmd(); |
| 63 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "backupManager", backupManager); |
| 64 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "_responseGenerator", responseGenerator); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testGetSourceOfferingId() { |
| 69 | + Long sourceOfferingId = 999L; |
| 70 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "sourceOfferingId", sourceOfferingId); |
| 71 | + assertEquals(sourceOfferingId, cloneBackupOfferingCmd.getSourceOfferingId()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testGetName() { |
| 76 | + String name = "ClonedBackupOffering"; |
| 77 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "name", name); |
| 78 | + assertEquals(name, cloneBackupOfferingCmd.getName()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testGetDescription() { |
| 83 | + String description = "Cloned Backup Offering Description"; |
| 84 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "description", description); |
| 85 | + assertEquals(description, cloneBackupOfferingCmd.getDescription()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void testGetZoneId() { |
| 90 | + Long zoneId = 123L; |
| 91 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "zoneId", zoneId); |
| 92 | + assertEquals(zoneId, cloneBackupOfferingCmd.getZoneId()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testGetExternalId() { |
| 97 | + String externalId = "external-backup-123"; |
| 98 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "externalId", externalId); |
| 99 | + assertEquals(externalId, cloneBackupOfferingCmd.getExternalId()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testGetAllowUserDrivenBackups() { |
| 104 | + Boolean allowUserDrivenBackups = true; |
| 105 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "userDrivenBackups", allowUserDrivenBackups); |
| 106 | + assertEquals(allowUserDrivenBackups, cloneBackupOfferingCmd.getUserDrivenBackups()); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testAllowUserDrivenBackupsDefaultTrue() { |
| 111 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "userDrivenBackups", null); |
| 112 | + Boolean result = cloneBackupOfferingCmd.getUserDrivenBackups(); |
| 113 | + assertTrue(result == null || result); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testAllowUserDrivenBackupsFalse() { |
| 118 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "userDrivenBackups", false); |
| 119 | + assertEquals(Boolean.FALSE, cloneBackupOfferingCmd.getUserDrivenBackups()); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testExecuteSuccess() throws Exception { |
| 124 | + Long sourceOfferingId = 999L; |
| 125 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "sourceOfferingId", sourceOfferingId); |
| 126 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "name", "ClonedBackupOffering"); |
| 127 | + |
| 128 | + when(backupManager.cloneBackupOffering(any(CloneBackupOfferingCmd.class))).thenReturn(mockBackupOffering); |
| 129 | + when(responseGenerator.createBackupOfferingResponse(mockBackupOffering)).thenReturn(mockBackupOfferingResponse); |
| 130 | + |
| 131 | + cloneBackupOfferingCmd.execute(); |
| 132 | + |
| 133 | + assertNotNull(cloneBackupOfferingCmd.getResponseObject()); |
| 134 | + assertEquals(mockBackupOfferingResponse, cloneBackupOfferingCmd.getResponseObject()); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testExecuteFailure() throws Exception { |
| 139 | + Long sourceOfferingId = 999L; |
| 140 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "sourceOfferingId", sourceOfferingId); |
| 141 | + |
| 142 | + when(backupManager.cloneBackupOffering(any(CloneBackupOfferingCmd.class))).thenReturn(null); |
| 143 | + |
| 144 | + try { |
| 145 | + cloneBackupOfferingCmd.execute(); |
| 146 | + fail("Expected ServerApiException to be thrown"); |
| 147 | + } catch (ServerApiException e) { |
| 148 | + assertEquals(ApiErrorCode.INTERNAL_ERROR, e.getErrorCode()); |
| 149 | + assertEquals("Failed to clone backup offering", e.getMessage()); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testExecuteWithInvalidParameterException() throws Exception { |
| 155 | + Long sourceOfferingId = 999L; |
| 156 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "sourceOfferingId", sourceOfferingId); |
| 157 | + |
| 158 | + when(backupManager.cloneBackupOffering(any(CloneBackupOfferingCmd.class))) |
| 159 | + .thenThrow(new InvalidParameterValueException("Invalid source offering ID")); |
| 160 | + |
| 161 | + try { |
| 162 | + cloneBackupOfferingCmd.execute(); |
| 163 | + fail("Expected ServerApiException to be thrown"); |
| 164 | + } catch (ServerApiException e) { |
| 165 | + assertEquals(ApiErrorCode.PARAM_ERROR, e.getErrorCode()); |
| 166 | + assertEquals("Invalid source offering ID", e.getMessage()); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void testExecuteWithCloudRuntimeException() throws Exception { |
| 172 | + Long sourceOfferingId = 999L; |
| 173 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "sourceOfferingId", sourceOfferingId); |
| 174 | + |
| 175 | + when(backupManager.cloneBackupOffering(any(CloneBackupOfferingCmd.class))) |
| 176 | + .thenThrow(new CloudRuntimeException("Runtime error during clone")); |
| 177 | + |
| 178 | + try { |
| 179 | + cloneBackupOfferingCmd.execute(); |
| 180 | + fail("Expected ServerApiException to be thrown"); |
| 181 | + } catch (ServerApiException e) { |
| 182 | + assertEquals(ApiErrorCode.INTERNAL_ERROR, e.getErrorCode()); |
| 183 | + assertEquals("Runtime error during clone", e.getMessage()); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + public void testExecuteSuccessWithAllParameters() throws Exception { |
| 189 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "sourceOfferingId", 999L); |
| 190 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "name", "ClonedBackupOffering"); |
| 191 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "description", "Test Description"); |
| 192 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "zoneId", 123L); |
| 193 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "externalId", "ext-123"); |
| 194 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "userDrivenBackups", true); |
| 195 | + |
| 196 | + when(backupManager.cloneBackupOffering(any(CloneBackupOfferingCmd.class))).thenReturn(mockBackupOffering); |
| 197 | + when(responseGenerator.createBackupOfferingResponse(mockBackupOffering)).thenReturn(mockBackupOfferingResponse); |
| 198 | + |
| 199 | + cloneBackupOfferingCmd.execute(); |
| 200 | + |
| 201 | + assertNotNull(cloneBackupOfferingCmd.getResponseObject()); |
| 202 | + assertEquals(mockBackupOfferingResponse, cloneBackupOfferingCmd.getResponseObject()); |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + public void testCloneWithAllParameters() { |
| 207 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "sourceOfferingId", 999L); |
| 208 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "name", "ClonedBackupOffering"); |
| 209 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "description", "Cloned backup offering for testing"); |
| 210 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "zoneId", 123L); |
| 211 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "externalId", "external-backup-123"); |
| 212 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "userDrivenBackups", true); |
| 213 | + |
| 214 | + assertEquals(Long.valueOf(999L), cloneBackupOfferingCmd.getSourceOfferingId()); |
| 215 | + assertEquals("ClonedBackupOffering", cloneBackupOfferingCmd.getName()); |
| 216 | + assertEquals("Cloned backup offering for testing", cloneBackupOfferingCmd.getDescription()); |
| 217 | + assertEquals(Long.valueOf(123L), cloneBackupOfferingCmd.getZoneId()); |
| 218 | + assertEquals("external-backup-123", cloneBackupOfferingCmd.getExternalId()); |
| 219 | + assertEquals(Boolean.TRUE, cloneBackupOfferingCmd.getUserDrivenBackups()); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + public void testCloneWithMinimalParameters() { |
| 224 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "sourceOfferingId", 999L); |
| 225 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "name", "ClonedBackupOffering"); |
| 226 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "description", "Description"); |
| 227 | + |
| 228 | + assertEquals(Long.valueOf(999L), cloneBackupOfferingCmd.getSourceOfferingId()); |
| 229 | + assertEquals("ClonedBackupOffering", cloneBackupOfferingCmd.getName()); |
| 230 | + assertEquals("Description", cloneBackupOfferingCmd.getDescription()); |
| 231 | + |
| 232 | + assertNull(cloneBackupOfferingCmd.getZoneId()); |
| 233 | + assertNull(cloneBackupOfferingCmd.getExternalId()); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + public void testSourceOfferingIdNullByDefault() { |
| 238 | + assertNull(cloneBackupOfferingCmd.getSourceOfferingId()); |
| 239 | + } |
| 240 | + |
| 241 | + @Test |
| 242 | + public void testNameNullByDefault() { |
| 243 | + assertNull(cloneBackupOfferingCmd.getName()); |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + public void testDescriptionNullByDefault() { |
| 248 | + assertNull(cloneBackupOfferingCmd.getDescription()); |
| 249 | + } |
| 250 | + |
| 251 | + @Test |
| 252 | + public void testZoneIdNullByDefault() { |
| 253 | + assertNull(cloneBackupOfferingCmd.getZoneId()); |
| 254 | + } |
| 255 | + |
| 256 | + @Test |
| 257 | + public void testExternalIdNullByDefault() { |
| 258 | + assertNull(cloneBackupOfferingCmd.getExternalId()); |
| 259 | + } |
| 260 | + |
| 261 | + @Test |
| 262 | + public void testCloneBackupOfferingInheritingZone() { |
| 263 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "sourceOfferingId", 999L); |
| 264 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "name", "ClonedBackupOffering"); |
| 265 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "description", "Clone with inherited zone"); |
| 266 | + |
| 267 | + assertEquals(Long.valueOf(999L), cloneBackupOfferingCmd.getSourceOfferingId()); |
| 268 | + assertNull(cloneBackupOfferingCmd.getZoneId()); |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + public void testCloneBackupOfferingInheritingExternalId() { |
| 273 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "sourceOfferingId", 999L); |
| 274 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "name", "ClonedBackupOffering"); |
| 275 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "description", "Clone with inherited external ID"); |
| 276 | + |
| 277 | + assertEquals(Long.valueOf(999L), cloneBackupOfferingCmd.getSourceOfferingId()); |
| 278 | + assertNull(cloneBackupOfferingCmd.getExternalId()); |
| 279 | + } |
| 280 | + |
| 281 | + @Test |
| 282 | + public void testCloneBackupOfferingOverridingZone() { |
| 283 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "sourceOfferingId", 999L); |
| 284 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "name", "ClonedBackupOffering"); |
| 285 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "description", "Clone with new zone"); |
| 286 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "zoneId", 456L); |
| 287 | + |
| 288 | + assertEquals(Long.valueOf(999L), cloneBackupOfferingCmd.getSourceOfferingId()); |
| 289 | + assertEquals(Long.valueOf(456L), cloneBackupOfferingCmd.getZoneId()); |
| 290 | + } |
| 291 | + |
| 292 | + @Test |
| 293 | + public void testCloneBackupOfferingDisallowUserDrivenBackups() { |
| 294 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "sourceOfferingId", 999L); |
| 295 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "name", "ClonedBackupOffering"); |
| 296 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "description", "Clone without user-driven backups"); |
| 297 | + ReflectionTestUtils.setField(cloneBackupOfferingCmd, "userDrivenBackups", false); |
| 298 | + |
| 299 | + assertEquals(Boolean.FALSE, cloneBackupOfferingCmd.getUserDrivenBackups()); |
| 300 | + } |
| 301 | +} |
| 302 | + |
| 303 | + |
0 commit comments