|
| 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.api.command.user.backup; |
| 18 | + |
| 19 | +import com.cloud.exception.InsufficientCapacityException; |
| 20 | +import com.cloud.exception.NetworkRuleConflictException; |
| 21 | +import com.cloud.exception.ResourceAllocationException; |
| 22 | +import com.cloud.exception.ResourceUnavailableException; |
| 23 | +import com.cloud.user.Account; |
| 24 | +import org.apache.cloudstack.api.ResponseGenerator; |
| 25 | +import org.apache.cloudstack.api.response.BackupScheduleResponse; |
| 26 | +import org.apache.cloudstack.api.response.ListResponse; |
| 27 | +import org.apache.cloudstack.backup.BackupManager; |
| 28 | +import org.apache.cloudstack.backup.BackupSchedule; |
| 29 | +import org.apache.cloudstack.context.CallContext; |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.runner.RunWith; |
| 34 | +import org.mockito.Mock; |
| 35 | +import org.mockito.Mockito; |
| 36 | +import org.mockito.junit.MockitoJUnitRunner; |
| 37 | + |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +@RunWith(MockitoJUnitRunner.class) |
| 42 | +public class ListBackupScheduleCmdTest { |
| 43 | + |
| 44 | + @Mock |
| 45 | + private BackupManager backupManager; |
| 46 | + |
| 47 | + @Mock |
| 48 | + private ResponseGenerator responseGenerator; |
| 49 | + |
| 50 | + private ListBackupScheduleCmd cmd; |
| 51 | + |
| 52 | + @Before |
| 53 | + public void setUp() { |
| 54 | + cmd = new ListBackupScheduleCmd(); |
| 55 | + cmd.backupManager = backupManager; |
| 56 | + cmd._responseGenerator = responseGenerator; |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testExecuteWithSchedules() throws ResourceUnavailableException, InsufficientCapacityException, ResourceAllocationException, NetworkRuleConflictException { |
| 61 | + BackupSchedule schedule = Mockito.mock(BackupSchedule.class); |
| 62 | + BackupScheduleResponse scheduleResponse = Mockito.mock(BackupScheduleResponse.class); |
| 63 | + List<BackupSchedule> schedules = new ArrayList<>(); |
| 64 | + schedules.add(schedule); |
| 65 | + |
| 66 | + Mockito.when(backupManager.listBackupSchedules(cmd)).thenReturn(schedules); |
| 67 | + Mockito.when(responseGenerator.createBackupScheduleResponse(schedule)).thenReturn(scheduleResponse); |
| 68 | + |
| 69 | + Account mockAccount = Mockito.mock(Account.class); |
| 70 | + CallContext callContext = Mockito.mock(CallContext.class); |
| 71 | + try (org.mockito.MockedStatic<CallContext> mocked = Mockito.mockStatic(CallContext.class)) { |
| 72 | + cmd.execute(); |
| 73 | + } |
| 74 | + |
| 75 | + ListResponse<?> response = (ListResponse<?>) cmd.getResponseObject(); |
| 76 | + Assert.assertNotNull(response); |
| 77 | + Assert.assertEquals(1, response.getResponses().size()); |
| 78 | + Assert.assertEquals(scheduleResponse, response.getResponses().get(0)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testExecuteWithNoSchedules() { |
| 83 | + Mockito.when(backupManager.listBackupSchedules(cmd)).thenReturn(new ArrayList<>()); |
| 84 | + CallContext callContext = Mockito.mock(CallContext.class); |
| 85 | + |
| 86 | + try (org.mockito.MockedStatic<CallContext> mocked = Mockito.mockStatic(CallContext.class)) { |
| 87 | + mocked.when(CallContext::current).thenReturn(callContext); |
| 88 | + cmd.execute(); |
| 89 | + } catch (ResourceUnavailableException | InsufficientCapacityException | ResourceAllocationException | |
| 90 | + NetworkRuleConflictException e) { |
| 91 | + throw new RuntimeException(e); |
| 92 | + } |
| 93 | + |
| 94 | + ListResponse<?> response = (ListResponse<?>) cmd.getResponseObject(); |
| 95 | + Assert.assertNotNull(response); |
| 96 | + Assert.assertEquals(0, response.getResponses().size()); |
| 97 | + } |
| 98 | +} |
0 commit comments