@@ -99,4 +99,267 @@ public class OntapPrimaryDatastoreLifecycleTest {
9999 private List <HostVO > mockHosts ;
100100 private Map <String , String > poolDetails ;
101101
102+ @ BeforeEach
103+ void setUp () {
104+ // Create a mock that implements both DataStore and PrimaryDataStoreInfo interfaces
105+ dataStore = Mockito .mock (DataStore .class , withSettings ()
106+ .extraInterfaces (PrimaryDataStoreInfo .class ));
107+
108+ ClusterVO clusterVO = new ClusterVO (1L , 1L , "clusterName" );
109+ clusterVO .setHypervisorType ("KVM" );
110+ when (_clusterDao .findById (1L )).thenReturn (clusterVO );
111+
112+ when (storageStrategy .connect ()).thenReturn (true );
113+ when (storageStrategy .getNetworkInterface ()).thenReturn ("testNetworkInterface" );
114+
115+ Volume volume = new Volume ();
116+ volume .setUuid ("test-volume-uuid" );
117+ volume .setName ("testVolume" );
118+ when (storageStrategy .createStorageVolume (any (), any ())).thenReturn (volume );
119+
120+ // Setup for attachCluster tests
121+ // Configure dataStore mock with necessary methods (works for both DataStore and PrimaryDataStoreInfo)
122+ when (dataStore .getId ()).thenReturn (1L );
123+ when (((PrimaryDataStoreInfo ) dataStore ).getClusterId ()).thenReturn (1L );
124+ mockHosts = new ArrayList <>();
125+ HostVO host1 = new HostVO ("host1-guid" );
126+ host1 .setPrivateIpAddress ("192.168.1.10" );
127+ host1 .setClusterId (1L );
128+ HostVO host2 = new HostVO ("host2-guid" );
129+ host2 .setPrivateIpAddress ("192.168.1.11" );
130+ host2 .setClusterId (1L );
131+ mockHosts .add (host1 );
132+ mockHosts .add (host2 );
133+ poolDetails = new HashMap <>();
134+ poolDetails .put ("username" , "admin" );
135+ poolDetails .put ("password" , "password" );
136+ poolDetails .put ("svmName" , "svm1" );
137+ poolDetails .put ("protocol" , "NFS3" );
138+ poolDetails .put ("managementLIF" , "192.168.1.100" );
139+ poolDetails .put ("isDisaggregated" , "false" );
140+ }
141+
142+ @ Test
143+ public void testInitialize_positive () {
144+
145+ Map <String , Object > dsInfos = new HashMap <>();
146+ dsInfos .put ("username" , "testUser" );
147+ dsInfos .put ("password" , "testPassword" );
148+ dsInfos .put ("url" , "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false" );
149+ dsInfos .put ("zoneId" ,1L );
150+ dsInfos .put ("podId" ,1L );
151+ dsInfos .put ("clusterId" , 1L );
152+ dsInfos .put ("name" , "testStoragePool" );
153+ dsInfos .put ("providerName" , "testProvider" );
154+ dsInfos .put ("capacityBytes" ,200000L );
155+ dsInfos .put ("managed" ,true );
156+ dsInfos .put ("tags" , "testTag" );
157+ dsInfos .put ("isTagARule" , false );
158+ dsInfos .put ("details" , new HashMap <String , String >());
159+
160+ try (MockedStatic <StorageProviderFactory > storageProviderFactory = Mockito .mockStatic (StorageProviderFactory .class )) {
161+ storageProviderFactory .when (() -> StorageProviderFactory .getStrategy (any ())).thenReturn (storageStrategy );
162+ ontapPrimaryDatastoreLifecycle .initialize (dsInfos );
163+ }
164+ }
165+
166+ @ Test
167+ public void testInitialize_null_Arg () {
168+ Exception ex = assertThrows (CloudRuntimeException .class ,() ->
169+ ontapPrimaryDatastoreLifecycle .initialize (null ));
170+ assertTrue (ex .getMessage ().contains ("Datastore info map is null, cannot create primary storage" ));
171+ }
172+
173+ @ Test
174+ public void testInitialize_missingRequiredDetailKey () {
175+ Map <String , Object > dsInfos = new HashMap <>();
176+ dsInfos .put ("url" , "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1" );
177+ dsInfos .put ("zoneId" ,1L );
178+ dsInfos .put ("podId" ,1L );
179+ dsInfos .put ("clusterId" , 1L );
180+ dsInfos .put ("name" , "testStoragePool" );
181+ dsInfos .put ("providerName" , "testProvider" );
182+ dsInfos .put ("capacityBytes" ,200000L );
183+ dsInfos .put ("managed" ,true );
184+ dsInfos .put ("tags" , "testTag" );
185+ dsInfos .put ("isTagARule" , false );
186+ dsInfos .put ("details" , new HashMap <String , String >());
187+
188+ try (MockedStatic <StorageProviderFactory > storageProviderFactory = Mockito .mockStatic (StorageProviderFactory .class )) {
189+ storageProviderFactory .when (() -> StorageProviderFactory .getStrategy (any ())).thenReturn (storageStrategy );
190+ Exception ex = assertThrows (CloudRuntimeException .class , () -> ontapPrimaryDatastoreLifecycle .initialize (dsInfos ));
191+ assertTrue (ex .getMessage ().contains ("missing detail" ));
192+ }
193+ }
194+
195+ @ Test
196+ public void testInitialize_invalidCapacityBytes () {
197+ Map <String , Object > dsInfos = new HashMap <>();
198+ dsInfos .put ("url" , "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false" );
199+ dsInfos .put ("zoneId" ,1L );
200+ dsInfos .put ("podId" ,1L );
201+ dsInfos .put ("clusterId" , 1L );
202+ dsInfos .put ("name" , "testStoragePool" );
203+ dsInfos .put ("providerName" , "testProvider" );
204+ dsInfos .put ("capacityBytes" ,-1L );
205+ dsInfos .put ("managed" ,true );
206+ dsInfos .put ("tags" , "testTag" );
207+ dsInfos .put ("isTagARule" , false );
208+ dsInfos .put ("details" , new HashMap <String , String >());
209+
210+ try (MockedStatic <StorageProviderFactory > storageProviderFactory = Mockito .mockStatic (StorageProviderFactory .class )) {
211+ storageProviderFactory .when (() -> StorageProviderFactory .getStrategy (any ())).thenReturn (storageStrategy );
212+ ontapPrimaryDatastoreLifecycle .initialize (dsInfos );
213+ }
214+ }
215+
216+ @ Test
217+ public void testInitialize_unmanagedStorage () {
218+ Map <String , Object > dsInfos = new HashMap <>();
219+ dsInfos .put ("url" , "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false" );
220+ dsInfos .put ("zoneId" ,1L );
221+ dsInfos .put ("podId" ,1L );
222+ dsInfos .put ("clusterId" , 1L );
223+ dsInfos .put ("name" , "testStoragePool" );
224+ dsInfos .put ("providerName" , "testProvider" );
225+ dsInfos .put ("capacityBytes" ,200000L );
226+ dsInfos .put ("managed" ,false );
227+ dsInfos .put ("tags" , "testTag" );
228+ dsInfos .put ("isTagARule" , false );
229+ dsInfos .put ("details" , new HashMap <String , String >());
230+
231+ Exception ex = assertThrows (CloudRuntimeException .class , () -> {
232+ try (MockedStatic <StorageProviderFactory > storageProviderFactory = Mockito .mockStatic (StorageProviderFactory .class )) {
233+ storageProviderFactory .when (() -> StorageProviderFactory .getStrategy (any ())).thenReturn (storageStrategy );
234+ ontapPrimaryDatastoreLifecycle .initialize (dsInfos );
235+ }
236+ });
237+ assertTrue (ex .getMessage ().contains ("must be managed" ));
238+ }
239+
240+ @ Test
241+ public void testInitialize_nullStoragePoolName () {
242+ Map <String , Object > dsInfos = new HashMap <>();
243+ dsInfos .put ("url" , "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false" );
244+ dsInfos .put ("zoneId" ,1L );
245+ dsInfos .put ("podId" ,1L );
246+ dsInfos .put ("clusterId" , 1L );
247+ dsInfos .put ("name" , null );
248+ dsInfos .put ("providerName" , "testProvider" );
249+ dsInfos .put ("capacityBytes" ,200000L );
250+ dsInfos .put ("managed" ,true );
251+ dsInfos .put ("tags" , "testTag" );
252+ dsInfos .put ("isTagARule" , false );
253+ dsInfos .put ("details" , new HashMap <String , String >());
254+
255+ Exception ex = assertThrows (CloudRuntimeException .class , () -> {
256+ try (MockedStatic <StorageProviderFactory > storageProviderFactory = Mockito .mockStatic (StorageProviderFactory .class )) {
257+ storageProviderFactory .when (() -> StorageProviderFactory .getStrategy (any ())).thenReturn (storageStrategy );
258+ ontapPrimaryDatastoreLifecycle .initialize (dsInfos );
259+ }
260+ });
261+ assertTrue (ex .getMessage ().contains ("Storage pool name is null or empty" ));
262+ }
263+
264+ @ Test
265+ public void testInitialize_nullProviderName () {
266+ Map <String , Object > dsInfos = new HashMap <>();
267+ dsInfos .put ("url" , "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false" );
268+ dsInfos .put ("zoneId" ,1L );
269+ dsInfos .put ("podId" ,1L );
270+ dsInfos .put ("clusterId" , 1L );
271+ dsInfos .put ("name" , "testStoragePool" );
272+ dsInfos .put ("providerName" , null );
273+ dsInfos .put ("capacityBytes" ,200000L );
274+ dsInfos .put ("managed" ,true );
275+ dsInfos .put ("tags" , "testTag" );
276+ dsInfos .put ("isTagARule" , false );
277+ dsInfos .put ("details" , new HashMap <String , String >());
278+
279+ Exception ex = assertThrows (CloudRuntimeException .class , () -> {
280+ try (MockedStatic <StorageProviderFactory > storageProviderFactory = Mockito .mockStatic (StorageProviderFactory .class )) {
281+ storageProviderFactory .when (() -> StorageProviderFactory .getStrategy (any ())).thenReturn (storageStrategy );
282+ ontapPrimaryDatastoreLifecycle .initialize (dsInfos );
283+ }
284+ });
285+ assertTrue (ex .getMessage ().contains ("Provider name is null or empty" ));
286+ }
287+
288+ @ Test
289+ public void testInitialize_nullPodAndClusterAndZone () {
290+ Map <String , Object > dsInfos = new HashMap <>();
291+ dsInfos .put ("url" , "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false" );
292+ dsInfos .put ("zoneId" ,null );
293+ dsInfos .put ("podId" ,null );
294+ dsInfos .put ("clusterId" , null );
295+ dsInfos .put ("name" , "testStoragePool" );
296+ dsInfos .put ("providerName" , "testProvider" );
297+ dsInfos .put ("capacityBytes" ,200000L );
298+ dsInfos .put ("managed" ,true );
299+ dsInfos .put ("tags" , "testTag" );
300+ dsInfos .put ("isTagARule" , false );
301+ dsInfos .put ("details" , new HashMap <String , String >());
302+
303+ Exception ex = assertThrows (CloudRuntimeException .class , () -> {
304+ try (MockedStatic <StorageProviderFactory > storageProviderFactory = Mockito .mockStatic (StorageProviderFactory .class )) {
305+ storageProviderFactory .when (() -> StorageProviderFactory .getStrategy (any ())).thenReturn (storageStrategy );
306+ ontapPrimaryDatastoreLifecycle .initialize (dsInfos );
307+ }
308+ });
309+ assertTrue (ex .getMessage ().contains ("Pod Id, Cluster Id and Zone Id are all null" ));
310+ }
311+
312+ @ Test
313+ public void testInitialize_clusterNotKVM () {
314+ ClusterVO clusterVO = new ClusterVO (2L , 1L , "clusterName" );
315+ clusterVO .setHypervisorType ("XenServer" );
316+ when (_clusterDao .findById (2L )).thenReturn (clusterVO );
317+
318+ Map <String , Object > dsInfos = new HashMap <>();
319+ dsInfos .put ("url" , "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false" );
320+ dsInfos .put ("zoneId" ,1L );
321+ dsInfos .put ("podId" ,1L );
322+ dsInfos .put ("clusterId" , 2L );
323+ dsInfos .put ("name" , "testStoragePool" );
324+ dsInfos .put ("providerName" , "testProvider" );
325+ dsInfos .put ("capacityBytes" ,200000L );
326+ dsInfos .put ("managed" ,true );
327+ dsInfos .put ("tags" , "testTag" );
328+ dsInfos .put ("isTagARule" , false );
329+ dsInfos .put ("details" , new HashMap <String , String >());
330+
331+ Exception ex = assertThrows (CloudRuntimeException .class , () -> {
332+ try (MockedStatic <StorageProviderFactory > storageProviderFactory = Mockito .mockStatic (StorageProviderFactory .class )) {
333+ storageProviderFactory .when (() -> StorageProviderFactory .getStrategy (any ())).thenReturn (storageStrategy );
334+ ontapPrimaryDatastoreLifecycle .initialize (dsInfos );
335+ }
336+ });
337+ assertTrue (ex .getMessage ().contains ("ONTAP primary storage is supported only for KVM hypervisor" ));
338+ }
339+
340+ @ Test
341+ @ Ig
342+ public void testInitialize_unexpectedDetailKey () {
343+ Map <String , Object > dsInfos = new HashMap <>();
344+ dsInfos .put ("url" , "username=testUser;password=testPassword;svmName=testSVM;protocol=NFS3;managementLIF=192.168.1.1;isDisaggregated=false;unexpectedKey=unexpectedValue" );
345+ dsInfos .put ("zoneId" ,1L );
346+ dsInfos .put ("podId" ,1L );
347+ dsInfos .put ("clusterId" , 1L );
348+ dsInfos .put ("name" , "testStoragePool" );
349+ dsInfos .put ("providerName" , "testProvider" );
350+ dsInfos .put ("capacityBytes" ,200000L );
351+ dsInfos .put ("managed" ,true );
352+ dsInfos .put ("tags" , "testTag" );
353+ dsInfos .put ("isTagARule" , false );
354+ dsInfos .put ("details" , new HashMap <String , String >());
355+
356+ Exception ex = assertThrows (CloudRuntimeException .class , () -> {
357+ try (MockedStatic <StorageProviderFactory > storageProviderFactory = Mockito .mockStatic (StorageProviderFactory .class )) {
358+ storageProviderFactory .when (() -> StorageProviderFactory .getStrategy (any ())).thenReturn (storageStrategy );
359+ ontapPrimaryDatastoreLifecycle .initialize (dsInfos );
360+ }
361+ });
362+ assertTrue (ex .getMessage ().contains ("Unexpected ONTAP detail key in URL" ));
363+ }
364+
102365}
0 commit comments