|
| 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 com.cloud.storage.snapshot; |
| 18 | + |
| 19 | +import com.cloud.storage.Snapshot; |
| 20 | +import com.cloud.storage.SnapshotPolicyVO; |
| 21 | +import com.cloud.storage.SnapshotScheduleVO; |
| 22 | +import com.cloud.storage.VolumeVO; |
| 23 | +import com.cloud.storage.dao.SnapshotPolicyDao; |
| 24 | +import com.cloud.storage.dao.SnapshotScheduleDao; |
| 25 | +import com.cloud.storage.dao.VolumeDao; |
| 26 | +import com.cloud.user.Account; |
| 27 | +import com.cloud.user.AccountVO; |
| 28 | +import com.cloud.user.dao.AccountDao; |
| 29 | +import org.junit.Assert; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.mockito.InjectMocks; |
| 33 | +import org.mockito.Mock; |
| 34 | +import org.mockito.Mockito; |
| 35 | +import org.mockito.Spy; |
| 36 | +import org.mockito.junit.MockitoJUnitRunner; |
| 37 | +import java.util.Date; |
| 38 | + |
| 39 | +@RunWith(MockitoJUnitRunner.class) |
| 40 | +public class SnapshotSchedulerImplTest { |
| 41 | + |
| 42 | + @Spy |
| 43 | + @InjectMocks |
| 44 | + SnapshotSchedulerImpl snapshotSchedulerImplSpy = new SnapshotSchedulerImpl(); |
| 45 | + |
| 46 | + @Mock |
| 47 | + SnapshotPolicyDao snapshotPolicyDaoMock; |
| 48 | + |
| 49 | + @Mock |
| 50 | + SnapshotPolicyVO snapshotPolicyVoMock; |
| 51 | + |
| 52 | + @Mock |
| 53 | + SnapshotScheduleDao snapshotScheduleDaoMock; |
| 54 | + |
| 55 | + @Mock |
| 56 | + AccountDao accountDaoMock; |
| 57 | + |
| 58 | + @Mock |
| 59 | + VolumeDao volumeDaoMock; |
| 60 | + |
| 61 | + @Mock |
| 62 | + VolumeVO volumeVoMock; |
| 63 | + |
| 64 | + @Mock |
| 65 | + AccountVO accountVoMock; |
| 66 | + |
| 67 | + @Test |
| 68 | + public void scheduleNextSnapshotJobTestParameterIsNullReturnNull() { |
| 69 | + SnapshotScheduleVO snapshotScheduleVO = null; |
| 70 | + |
| 71 | + Date result = snapshotSchedulerImplSpy.scheduleNextSnapshotJob(snapshotScheduleVO); |
| 72 | + |
| 73 | + Assert.assertNull(result); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void scheduleNextSnapshotJobTestIsManualPolicyIdReturnNull() { |
| 78 | + SnapshotScheduleVO snapshotScheduleVO = new SnapshotScheduleVO(); |
| 79 | + snapshotScheduleVO.setPolicyId(Snapshot.MANUAL_POLICY_ID); |
| 80 | + |
| 81 | + Date result = snapshotSchedulerImplSpy.scheduleNextSnapshotJob(snapshotScheduleVO); |
| 82 | + |
| 83 | + Assert.assertNull(result); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void scheduleNextSnapshotJobTestPolicyIsNotNullDoNotCallExpunge() { |
| 88 | + Date expected = new Date(); |
| 89 | + SnapshotScheduleVO snapshotScheduleVO = new SnapshotScheduleVO(); |
| 90 | + snapshotScheduleVO.setPolicyId(1l); |
| 91 | + |
| 92 | + Mockito.doReturn(snapshotPolicyVoMock).when(snapshotPolicyDaoMock).findById(Mockito.anyLong()); |
| 93 | + Mockito.doReturn(expected).when(snapshotSchedulerImplSpy).scheduleNextSnapshotJob(Mockito.any(SnapshotPolicyVO.class)); |
| 94 | + |
| 95 | + Date result = snapshotSchedulerImplSpy.scheduleNextSnapshotJob(snapshotScheduleVO); |
| 96 | + Assert.assertEquals(expected, result); |
| 97 | + |
| 98 | + Mockito.verify(snapshotScheduleDaoMock, Mockito.never()).expunge(Mockito.anyLong()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void scheduleNextSnapshotJobTestPolicyIsNullCallExpunge() { |
| 103 | + Date expected = new Date(); |
| 104 | + SnapshotPolicyVO snapshotPolicyVO = null; |
| 105 | + SnapshotScheduleVO snapshotScheduleVO = new SnapshotScheduleVO(); |
| 106 | + snapshotScheduleVO.setPolicyId(1l); |
| 107 | + |
| 108 | + Mockito.doReturn(snapshotPolicyVO).when(snapshotPolicyDaoMock).findById(Mockito.anyLong()); |
| 109 | + Mockito.doReturn(true).when(snapshotScheduleDaoMock).expunge(Mockito.anyLong()); |
| 110 | + Mockito.doReturn(expected).when(snapshotSchedulerImplSpy).scheduleNextSnapshotJob(snapshotPolicyVO); |
| 111 | + |
| 112 | + Date result = snapshotSchedulerImplSpy.scheduleNextSnapshotJob(snapshotScheduleVO); |
| 113 | + Assert.assertEquals(expected, result); |
| 114 | + |
| 115 | + Mockito.verify(snapshotScheduleDaoMock).expunge(Mockito.anyLong()); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void isAccountRemovedOrDisabledTestVolumeAccountIsNullReturnTrue() { |
| 120 | + SnapshotScheduleVO snapshotScheduleVO = new SnapshotScheduleVO(); |
| 121 | + |
| 122 | + Mockito.doReturn(null).when(accountDaoMock).findById(Mockito.anyLong()); |
| 123 | + |
| 124 | + boolean result = snapshotSchedulerImplSpy.isAccountRemovedOrDisabled(snapshotScheduleVO, volumeVoMock); |
| 125 | + |
| 126 | + Assert.assertTrue(result); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + public void isAccountRemovedOrDisabledTestVolumeAccountStateIsDisabledReturnTrue() { |
| 131 | + SnapshotScheduleVO snapshotScheduleVO = new SnapshotScheduleVO(); |
| 132 | + |
| 133 | + Mockito.doReturn(accountVoMock).when(accountDaoMock).findById(Mockito.anyLong()); |
| 134 | + Mockito.doReturn(Account.State.DISABLED).when(accountVoMock).getState(); |
| 135 | + |
| 136 | + boolean result = snapshotSchedulerImplSpy.isAccountRemovedOrDisabled(snapshotScheduleVO, volumeVoMock); |
| 137 | + |
| 138 | + Assert.assertTrue(result); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void isAccountRemovedOrDisabledTestVolumeAccountStateIsNotNullNorDisabledReturnFalse() { |
| 143 | + SnapshotScheduleVO snapshotScheduleVO = new SnapshotScheduleVO(); |
| 144 | + |
| 145 | + Mockito.doReturn(accountVoMock).when(accountDaoMock).findById(Mockito.anyLong()); |
| 146 | + Mockito.doReturn(Account.State.ENABLED).when(accountVoMock).getState(); |
| 147 | + |
| 148 | + boolean result = snapshotSchedulerImplSpy.isAccountRemovedOrDisabled(snapshotScheduleVO, volumeVoMock); |
| 149 | + |
| 150 | + Assert.assertFalse(result); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void canSnapshotBeScheduledTestVolumeIsRemovedReturnFalse() { |
| 155 | + SnapshotScheduleVO snapshotScheduleVO = new SnapshotScheduleVO(); |
| 156 | + |
| 157 | + Mockito.doReturn(new Date()).when(volumeVoMock).getRemoved(); |
| 158 | + |
| 159 | + boolean result = snapshotSchedulerImplSpy.canSnapshotBeScheduled(snapshotScheduleVO, volumeVoMock); |
| 160 | + |
| 161 | + Assert.assertFalse(result); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void canSnapshotBeScheduledTestVolumeIsNotAttachedToStoragePoolReturnFalse() { |
| 166 | + SnapshotScheduleVO snapshotScheduleVO = new SnapshotScheduleVO(); |
| 167 | + |
| 168 | + Mockito.doReturn(null).when(volumeVoMock).getPoolId(); |
| 169 | + |
| 170 | + boolean result = snapshotSchedulerImplSpy.canSnapshotBeScheduled(snapshotScheduleVO, volumeVoMock); |
| 171 | + |
| 172 | + Assert.assertFalse(result); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + public void canSnapshotBeScheduledTestAccountIsRemovedOrDisabledReturnFalse() { |
| 177 | + SnapshotScheduleVO snapshotScheduleVO = new SnapshotScheduleVO(); |
| 178 | + |
| 179 | + Mockito.doReturn(1l).when(volumeVoMock).getPoolId(); |
| 180 | + Mockito.doReturn(true).when(snapshotSchedulerImplSpy).isAccountRemovedOrDisabled(Mockito.any(), Mockito.any()); |
| 181 | + |
| 182 | + boolean result = snapshotSchedulerImplSpy.canSnapshotBeScheduled(snapshotScheduleVO, volumeVoMock); |
| 183 | + |
| 184 | + Assert.assertFalse(result); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + public void canSnapshotBeScheduledTestSnapshotPolicyIsRemovedCallRemove() { |
| 189 | + SnapshotScheduleVO snapshotScheduleVO = new SnapshotScheduleVO(); |
| 190 | + |
| 191 | + Mockito.doReturn(1l).when(volumeVoMock).getPoolId(); |
| 192 | + Mockito.doReturn(false).when(snapshotSchedulerImplSpy).isAccountRemovedOrDisabled(Mockito.any(), Mockito.any()); |
| 193 | + Mockito.doReturn(null).when(snapshotPolicyDaoMock).findById(Mockito.any()); |
| 194 | + |
| 195 | + boolean result = snapshotSchedulerImplSpy.canSnapshotBeScheduled(snapshotScheduleVO, volumeVoMock); |
| 196 | + |
| 197 | + Assert.assertTrue(result); |
| 198 | + |
| 199 | + Mockito.verify(snapshotScheduleDaoMock).remove(Mockito.anyLong()); |
| 200 | + } |
| 201 | + |
| 202 | + @Test |
| 203 | + public void canSnapshotBeScheduledTestSnapshotPolicyIsNotRemovedDoNotCallRemove() { |
| 204 | + SnapshotScheduleVO snapshotScheduleVO = new SnapshotScheduleVO(); |
| 205 | + SnapshotPolicyVO snapshotPolicyVO = new SnapshotPolicyVO(); |
| 206 | + |
| 207 | + Mockito.doReturn(1l).when(volumeVoMock).getPoolId(); |
| 208 | + Mockito.doReturn(false).when(snapshotSchedulerImplSpy).isAccountRemovedOrDisabled(Mockito.any(), Mockito.any()); |
| 209 | + Mockito.doReturn(snapshotPolicyVO).when(snapshotPolicyDaoMock).findById(Mockito.any()); |
| 210 | + |
| 211 | + boolean result = snapshotSchedulerImplSpy.canSnapshotBeScheduled(snapshotScheduleVO, volumeVoMock); |
| 212 | + |
| 213 | + Assert.assertTrue(result); |
| 214 | + |
| 215 | + Mockito.verify(snapshotScheduleDaoMock, Mockito.never()).remove(Mockito.anyLong()); |
| 216 | + } |
| 217 | +} |
0 commit comments