|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.cloudstack.storage.snapshot; |
| 20 | + |
| 21 | +import com.cloud.storage.DataStoreRole; |
| 22 | +import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore; |
| 23 | +import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreDriver; |
| 24 | +import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotDataFactory; |
| 25 | +import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo; |
| 26 | +import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult; |
| 27 | +import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory; |
| 28 | +import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo; |
| 29 | +import org.apache.cloudstack.framework.async.AsyncCallFuture; |
| 30 | +import org.apache.cloudstack.framework.async.AsyncCallbackDispatcher; |
| 31 | +import org.apache.cloudstack.storage.command.CommandResult; |
| 32 | +import org.junit.Assert; |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.Test; |
| 35 | +import org.junit.runner.RunWith; |
| 36 | +import org.mockito.InjectMocks; |
| 37 | +import org.mockito.Mock; |
| 38 | +import org.mockito.Mockito; |
| 39 | +import org.mockito.MockitoAnnotations; |
| 40 | +import org.mockito.Spy; |
| 41 | +import org.powermock.api.mockito.PowerMockito; |
| 42 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 43 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 44 | +import org.springframework.test.context.ContextConfiguration; |
| 45 | +import org.springframework.test.context.support.AnnotationConfigContextLoader; |
| 46 | + |
| 47 | +@RunWith(PowerMockRunner.class) |
| 48 | +@PrepareForTest({SnapshotServiceImpl.class}) |
| 49 | +@ContextConfiguration(loader = AnnotationConfigContextLoader.class) |
| 50 | +public class SnapshotServiceImplTest { |
| 51 | + |
| 52 | + @Spy |
| 53 | + @InjectMocks |
| 54 | + private SnapshotServiceImpl snapshotService = new SnapshotServiceImpl(); |
| 55 | + |
| 56 | + @Mock |
| 57 | + VolumeDataFactory volFactory; |
| 58 | + |
| 59 | + @Mock |
| 60 | + SnapshotDataFactory _snapshotFactory; |
| 61 | + |
| 62 | + @Mock |
| 63 | + AsyncCallFuture<SnapshotResult> futureMock; |
| 64 | + |
| 65 | + @Mock |
| 66 | + AsyncCallbackDispatcher<SnapshotServiceImpl, CommandResult> caller; |
| 67 | + |
| 68 | + @Before |
| 69 | + public void testSetUp() throws Exception { |
| 70 | + MockitoAnnotations.initMocks(this); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testRevertSnapshotWithNoPrimaryStorageEntry() throws Exception { |
| 75 | + SnapshotInfo snapshot = Mockito.mock(SnapshotInfo.class); |
| 76 | + VolumeInfo volumeInfo = Mockito.mock(VolumeInfo.class); |
| 77 | + |
| 78 | + Mockito.when(snapshot.getId()).thenReturn(1L); |
| 79 | + Mockito.when(snapshot.getVolumeId()).thenReturn(1L); |
| 80 | + Mockito.when(_snapshotFactory.getSnapshot(1L, DataStoreRole.Primary)).thenReturn(null); |
| 81 | + Mockito.when(volFactory.getVolume(1L, DataStoreRole.Primary)).thenReturn(volumeInfo); |
| 82 | + |
| 83 | + PrimaryDataStore store = Mockito.mock(PrimaryDataStore.class); |
| 84 | + Mockito.when(volumeInfo.getDataStore()).thenReturn(store); |
| 85 | + |
| 86 | + PrimaryDataStoreDriver driver = Mockito.mock(PrimaryDataStoreDriver.class); |
| 87 | + Mockito.when(store.getDriver()).thenReturn(driver); |
| 88 | + Mockito.doNothing().when(driver).revertSnapshot(snapshot, null, caller); |
| 89 | + |
| 90 | + SnapshotResult result = Mockito.mock(SnapshotResult.class); |
| 91 | + PowerMockito.whenNew(AsyncCallFuture.class).withNoArguments().thenReturn(futureMock); |
| 92 | + Mockito.when(futureMock.get()).thenReturn(result); |
| 93 | + Mockito.when(result.isFailed()).thenReturn(false); |
| 94 | + |
| 95 | + Assert.assertEquals(true, snapshotService.revertSnapshot(snapshot)); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments