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 .disks ;
18+
19+ import static org .junit .Assert .assertEquals ;
20+ import static org .mockito .ArgumentMatchers .any ;
21+ import static org .mockito .Mockito .mock ;
22+ import static org .mockito .Mockito .mockStatic ;
23+ import static org .mockito .Mockito .times ;
24+ import static org .mockito .Mockito .verify ;
25+ import static org .mockito .Mockito .when ;
26+
27+ import com .google .api .gax .longrunning .OperationFuture ;
28+ import com .google .cloud .compute .v1 .Disk ;
29+ import com .google .cloud .compute .v1 .DisksClient ;
30+ import com .google .cloud .compute .v1 .InsertDiskRequest ;
31+ import com .google .cloud .compute .v1 .InsertStoragePoolRequest ;
32+ import com .google .cloud .compute .v1 .Operation ;
33+ import com .google .cloud .compute .v1 .StoragePool ;
34+ import com .google .cloud .compute .v1 .StoragePoolsClient ;
35+ import compute .disks .storagepool .CreateDiskInStoragePool ;
36+ import compute .disks .storagepool .CreateHyperdiskStoragePool ;
37+ import java .util .concurrent .TimeUnit ;
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+ import org .mockito .Mockito ;
44+
45+ @ RunWith (JUnit4 .class )
46+ @ Timeout (value = 5 , unit = TimeUnit .MINUTES )
47+ public class HyperdiskIT {
48+ private static final String PROJECT_ID = "project-id" ;
49+ private static final String ZONE = "asia-east1-a" ;
50+ private static final String HYPERDISK_IN_POOL_NAME = "hyperdisk" ;
51+ private static final String STORAGE_POOL_NAME = "storage-pool" ;
52+ private static final String PERFORMANCE_PROVISIONING_TYPE = "advanced" ;
53+ private static final String CAPACITY_PROVISIONING_TYPE = "advanced" ;
54+
55+ @ Test
56+ public void testCreateHyperdiskStoragePool () throws Exception {
57+ String poolType = String .format (
58+ "projects/%s/zones/%s/storagePoolTypes/%s" , PROJECT_ID , ZONE , "hyperdisk-balanced" );
59+ StoragePool storagePool = StoragePool .newBuilder ()
60+ .setZone (ZONE )
61+ .setName (STORAGE_POOL_NAME )
62+ .setStoragePoolType (poolType )
63+ .setCapacityProvisioningType (CAPACITY_PROVISIONING_TYPE )
64+ .setPoolProvisionedCapacityGb (10240 )
65+ .setPoolProvisionedIops (10000 )
66+ .setPoolProvisionedThroughput (1024 )
67+ .setPerformanceProvisioningType (PERFORMANCE_PROVISIONING_TYPE )
68+ .build ();
69+ try (MockedStatic <StoragePoolsClient > mockedStoragePoolsClient =
70+ mockStatic (StoragePoolsClient .class )) {
71+ StoragePoolsClient mockClient = mock (StoragePoolsClient .class );
72+ OperationFuture <Operation , Operation > mockFuture =
73+ mock (OperationFuture .class , Mockito .RETURNS_DEEP_STUBS );
74+ Operation operation = mock (Operation .class , Mockito .RETURNS_DEEP_STUBS );
75+
76+ mockedStoragePoolsClient .when (StoragePoolsClient ::create ).thenReturn (mockClient );
77+ when (mockClient .insertAsync (any (InsertStoragePoolRequest .class )))
78+ .thenReturn (mockFuture );
79+ when (mockFuture .get ()).thenReturn (operation );
80+ when (operation .getStatus ()).thenReturn (Operation .Status .DONE );
81+ when (mockClient .get (PROJECT_ID , ZONE , STORAGE_POOL_NAME )).thenReturn (storagePool );
82+
83+
84+ StoragePool expectedStoragePool = CreateHyperdiskStoragePool
85+ .createHyperdiskStoragePool (PROJECT_ID , ZONE , STORAGE_POOL_NAME , poolType ,
86+ CAPACITY_PROVISIONING_TYPE , 10240 , 10000 , 1024 ,
87+ PERFORMANCE_PROVISIONING_TYPE );
88+
89+ verify (mockClient , times (1 )).insertAsync (any (InsertStoragePoolRequest .class ));
90+ verify (mockFuture , times (1 )).get ();
91+ assertEquals (storagePool , expectedStoragePool );
92+ }
93+ }
94+
95+ @ Test
96+ public void testCreateDiskInStoragePool () throws Exception {
97+ String diskType = String .format ("zones/%s/diskTypes/hyperdisk-balanced" , ZONE );
98+ Disk expectedHyperdisk = Disk .newBuilder ()
99+ .setZone (ZONE )
100+ .setName (HYPERDISK_IN_POOL_NAME )
101+ .setType (diskType )
102+ .setSizeGb (10L )
103+ .setProvisionedIops (3000L )
104+ .setProvisionedThroughput (140L )
105+ .build ();
106+ String storagePoolLink = String .format ("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/storagePools/%s" ,
107+ PROJECT_ID , ZONE , STORAGE_POOL_NAME );
108+
109+ try (MockedStatic <DisksClient > mockedDisksClient = mockStatic (DisksClient .class )) {
110+ DisksClient mockClient = mock (DisksClient .class );
111+ OperationFuture <Operation , Operation > mockFuture =
112+ mock (OperationFuture .class , Mockito .RETURNS_DEEP_STUBS );
113+ Operation operation = mock (Operation .class , Mockito .RETURNS_DEEP_STUBS );
114+
115+ mockedDisksClient .when (DisksClient ::create ).thenReturn (mockClient );
116+ when (mockClient .insertAsync (any (InsertDiskRequest .class ))).thenReturn (mockFuture );
117+ when (mockFuture .get ()).thenReturn (operation );
118+ when (operation .getStatus ()).thenReturn (Operation .Status .DONE );
119+ when (mockClient .get (PROJECT_ID , ZONE , HYPERDISK_IN_POOL_NAME )).thenReturn (expectedHyperdisk );
120+
121+
122+ Disk returnedDisk = CreateDiskInStoragePool
123+ .createDiskInStoragePool (PROJECT_ID , ZONE , HYPERDISK_IN_POOL_NAME , storagePoolLink ,
124+ diskType , 10 , 3000 , 140 );
125+
126+ verify (mockClient , times (1 )).insertAsync (any (InsertDiskRequest .class ));
127+ verify (mockFuture , times (1 )).get ();
128+ assertEquals (expectedHyperdisk , returnedDisk );
129+ }
130+ }
131+ }
0 commit comments