|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package compute.snapshotschedule; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.ArgumentMatchers.anyLong; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.mockStatic; |
| 24 | +import static org.mockito.Mockito.times; |
| 25 | +import static org.mockito.Mockito.verify; |
| 26 | +import static org.mockito.Mockito.when; |
| 27 | + |
| 28 | +import com.google.api.gax.longrunning.OperationFuture; |
| 29 | +import com.google.cloud.compute.v1.AddResourcePoliciesDiskRequest; |
| 30 | +import com.google.cloud.compute.v1.DisksClient; |
| 31 | +import com.google.cloud.compute.v1.Operation; |
| 32 | +import com.google.cloud.compute.v1.Operation.Status; |
| 33 | +import com.google.cloud.compute.v1.RemoveResourcePoliciesDiskRequest; |
| 34 | +import java.io.IOException; |
| 35 | +import java.util.concurrent.ExecutionException; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | +import java.util.concurrent.TimeoutException; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | +import org.junit.jupiter.api.Timeout; |
| 40 | +import org.junit.runner.RunWith; |
| 41 | +import org.junit.runners.JUnit4; |
| 42 | +import org.mockito.MockedStatic; |
| 43 | + |
| 44 | +@RunWith(JUnit4.class) |
| 45 | +@Timeout(value = 6, unit = TimeUnit.MINUTES) |
| 46 | +public class SnapshotScheduleIT { |
| 47 | + private static final String PROJECT_ID = "GOOGLE_CLOUD_PROJECT"; |
| 48 | + private static final String ZONE = "asia-south1-a"; |
| 49 | + private static final String REGION = ZONE.substring(0, ZONE.lastIndexOf('-')); |
| 50 | + private static final String SCHEDULE_FOR_DISK = "test-schedule-for-disk"; |
| 51 | + private static final String DISK_NAME = "gcloud-test-disk"; |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testAttachSnapshotScheduleToDisk() |
| 55 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 56 | + try (MockedStatic<DisksClient> mockedDisksClient = mockStatic(DisksClient.class)) { |
| 57 | + DisksClient mockClient = mock(DisksClient.class); |
| 58 | + Operation operation = mock(Operation.class); |
| 59 | + OperationFuture mockFuture = mock(OperationFuture.class); |
| 60 | + |
| 61 | + mockedDisksClient.when(DisksClient::create).thenReturn(mockClient); |
| 62 | + when(mockClient.addResourcePoliciesAsync(any(AddResourcePoliciesDiskRequest.class))) |
| 63 | + .thenReturn(mockFuture); |
| 64 | + when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation); |
| 65 | + when(operation.getStatus()).thenReturn(Status.DONE); |
| 66 | + |
| 67 | + Status actualStatus = AttachSnapshotScheduleToDisk.attachSnapshotScheduleToDisk( |
| 68 | + PROJECT_ID, ZONE, DISK_NAME, SCHEDULE_FOR_DISK, REGION); |
| 69 | + |
| 70 | + verify(mockClient, times(1)).addResourcePoliciesAsync(any()); |
| 71 | + assertEquals(Status.DONE, actualStatus); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testRemoveSnapshotScheduleFromDisk() |
| 77 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 78 | + try (MockedStatic<DisksClient> mockedDisksClient = mockStatic(DisksClient.class)) { |
| 79 | + DisksClient mockClient = mock(DisksClient.class); |
| 80 | + Operation operation = mock(Operation.class); |
| 81 | + OperationFuture mockFuture = mock(OperationFuture.class); |
| 82 | + |
| 83 | + mockedDisksClient.when(DisksClient::create).thenReturn(mockClient); |
| 84 | + when(mockClient.removeResourcePoliciesAsync(any(RemoveResourcePoliciesDiskRequest.class))) |
| 85 | + .thenReturn(mockFuture); |
| 86 | + when(mockFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(operation); |
| 87 | + when(operation.getStatus()).thenReturn(Status.DONE); |
| 88 | + |
| 89 | + Status actualStatus = RemoveSnapshotScheduleFromDisk.removeSnapshotScheduleFromDisk( |
| 90 | + PROJECT_ID, ZONE, DISK_NAME, REGION, SCHEDULE_FOR_DISK); |
| 91 | + |
| 92 | + verify(mockClient, times(1)).removeResourcePoliciesAsync(any()); |
| 93 | + assertEquals(Status.DONE, actualStatus); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments