forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationManagerCloneIntegrationTest.java
More file actions
1054 lines (861 loc) · 54.8 KB
/
ConfigurationManagerCloneIntegrationTest.java
File metadata and controls
1054 lines (861 loc) · 54.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.configuration;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.domain.Domain;
import com.cloud.domain.DomainVO;
import com.cloud.domain.dao.DomainDao;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.network.Network;
import com.cloud.network.Networks;
import com.cloud.offering.DiskOffering;
import com.cloud.offering.NetworkOffering;
import com.cloud.offering.ServiceOffering;
import com.cloud.offerings.NetworkOfferingVO;
import com.cloud.offerings.dao.NetworkOfferingDao;
import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
import com.cloud.service.ServiceOfferingVO;
import com.cloud.service.dao.ServiceOfferingDao;
import com.cloud.service.dao.ServiceOfferingDetailsDao;
import com.cloud.storage.DiskOfferingVO;
import com.cloud.storage.Storage;
import com.cloud.storage.dao.DiskOfferingDao;
import com.cloud.user.Account;
import com.cloud.user.AccountVO;
import com.cloud.user.User;
import com.cloud.user.UserVO;
import com.cloud.user.dao.AccountDao;
import com.cloud.user.dao.UserDao;
import com.cloud.utils.DomainHelper;
import com.cloud.utils.db.EntityManager;
import com.cloud.vm.VirtualMachine;
import com.cloud.gpu.dao.VgpuProfileDao;
import org.apache.cloudstack.api.ApiConstants;
import org.apache.cloudstack.api.command.admin.network.CloneNetworkOfferingCmd;
import org.apache.cloudstack.api.command.admin.offering.CloneDiskOfferingCmd;
import org.apache.cloudstack.api.command.admin.offering.CloneServiceOfferingCmd;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.resourcedetail.dao.DiskOfferingDetailsDao;
import org.apache.cloudstack.vm.lease.VMLeaseManager;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.Silent.class)
public class ConfigurationManagerCloneIntegrationTest {
@InjectMocks
@Spy
private ConfigurationManagerImpl configurationManager;
@Mock
private ServiceOfferingDao serviceOfferingDao;
@Mock
private ServiceOfferingDetailsDao serviceOfferingDetailsDao;
@Mock
private DiskOfferingDao diskOfferingDao;
@Mock
private DiskOfferingDetailsDao diskOfferingDetailsDao;
@Mock
private NetworkOfferingDao networkOfferingDao;
@Mock
private NetworkOfferingServiceMapDao networkOfferingServiceMapDao;
@Mock
private DomainDao domainDao;
@Mock
private DataCenterDao dataCenterDao;
@Mock
private EntityManager entityManager;
@Mock
private com.cloud.network.NetworkModel _networkModel;
@Mock
private com.cloud.offerings.dao.NetworkOfferingDetailsDao networkOfferingDetailsDao;
@Mock
private VgpuProfileDao vgpuProfileDao;
@Mock
private AccountDao accountDao;
@Mock
private UserDao userDao;
@Mock
private DomainHelper domainHelper;
private MockedStatic<CallContext> callContextMock;
@Before
public void setUp() {
callContextMock = Mockito.mockStatic(CallContext.class);
CallContext callContext = mock(CallContext.class);
callContextMock.when(CallContext::current).thenReturn(callContext);
AccountVO account = mock(AccountVO.class);
User user = mock(User.class);
Domain domain = mock(DomainVO.class);
UserVO userVO = mock(UserVO.class);
Mockito.lenient().when(callContext.getCallingAccount()).thenReturn(account);
Mockito.lenient().when(callContext.getCallingUser()).thenReturn(user);
Mockito.lenient().when(callContext.getCallingUserId()).thenReturn(1L);
Mockito.lenient().when(account.getDomainId()).thenReturn(1L);
Mockito.lenient().when(account.getId()).thenReturn(1L);
Mockito.lenient().when(user.getId()).thenReturn(1L);
Mockito.lenient().when(entityManager.findById(eq(Domain.class), anyLong())).thenReturn(domain);
Mockito.doAnswer(invocation -> {
DiskOfferingVO d = mock(DiskOfferingVO.class);
when(d.getId()).thenReturn(999L);
return d;
}).when(configurationManager).createDiskOffering(
anyLong(), anyList(), anyList(), anyString(), anyString(), anyString(),
anyLong(), anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any(),
anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyInt(), anyString(), any(), anyLong(), anyBoolean(), anyBoolean());
// User/Account DAO stubs used by createDiskOffering
Mockito.lenient().when(userDao.findById(anyLong())).thenReturn(userVO);
Mockito.lenient().when(userVO.getAccountId()).thenReturn(1L);
Mockito.lenient().when(userVO.getRemoved()).thenReturn(null);
Mockito.lenient().when(accountDao.findById(anyLong())).thenReturn(account);
Mockito.lenient().when(account.getType()).thenReturn(Account.Type.ADMIN);
}
@After
public void tearDown() {
if (callContextMock != null) {
callContextMock.close();
}
}
@Test(expected = InvalidParameterValueException.class)
public void testCloneServiceOfferingFailsWhenSourceNotFound() {
CloneServiceOfferingCmd cmd = mock(CloneServiceOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(999L);
when(cmd.getServiceOfferingName()).thenReturn("cloned-offering");
when(serviceOfferingDao.findById(999L)).thenReturn(null);
configurationManager.cloneServiceOffering(cmd);
}
@Test
public void testCloneServiceOfferingInheritsAllPropertiesFromSource() {
Long sourceId = 1L;
ServiceOfferingVO sourceOffering = mock(ServiceOfferingVO.class);
when(sourceOffering.getId()).thenReturn(sourceId);
when(sourceOffering.getName()).thenReturn("source-offering");
when(sourceOffering.getDisplayText()).thenReturn("Source Display Text");
when(sourceOffering.getCpu()).thenReturn(2);
when(sourceOffering.getSpeed()).thenReturn(1000);
when(sourceOffering.getRamSize()).thenReturn(2048);
when(sourceOffering.isOfferHA()).thenReturn(true);
when(sourceOffering.getLimitCpuUse()).thenReturn(false);
when(sourceOffering.isVolatileVm()).thenReturn(false);
when(sourceOffering.isCustomized()).thenReturn(false);
when(sourceOffering.isDynamicScalingEnabled()).thenReturn(true);
when(sourceOffering.getDiskOfferingStrictness()).thenReturn(false);
when(sourceOffering.getHostTag()).thenReturn("host-tag");
when(sourceOffering.getRateMbps()).thenReturn(100);
when(sourceOffering.getDeploymentPlanner()).thenReturn("FirstFitPlanner");
when(sourceOffering.isSystemUse()).thenReturn(false);
when(sourceOffering.getVmType()).thenReturn(VirtualMachine.Type.User.toString());
when(sourceOffering.getDiskOfferingId()).thenReturn(null);
when(sourceOffering.getVgpuProfileId()).thenReturn(null);
when(sourceOffering.getGpuCount()).thenReturn(null);
when(sourceOffering.getGpuDisplay()).thenReturn(false);
CloneServiceOfferingCmd cmd = mock(CloneServiceOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(sourceId);
when(cmd.getServiceOfferingName()).thenReturn("cloned-offering");
when(cmd.getFullUrlParams()).thenReturn(new HashMap<>());
// Ensure no vGPU is specified in the command (explicitly stub to null)
when(cmd.getVgpuProfileId()).thenReturn(null);
when(cmd.getGpuCount()).thenReturn(null);
when(serviceOfferingDao.findById(sourceId)).thenReturn(sourceOffering);
ServiceOfferingVO clonedOffering = mock(ServiceOfferingVO.class);
when(clonedOffering.getId()).thenReturn(2L);
when(clonedOffering.getName()).thenReturn("cloned-offering");
when(clonedOffering.getCpu()).thenReturn(2);
when(clonedOffering.getSpeed()).thenReturn(1000);
when(clonedOffering.getRamSize()).thenReturn(2048);
when(serviceOfferingDao.persist(any(ServiceOfferingVO.class))).thenReturn(clonedOffering);
DiskOfferingVO persistedDisk = mock(DiskOfferingVO.class);
when(persistedDisk.getId()).thenReturn(999L);
when(diskOfferingDao.findById(anyLong())).thenReturn(persistedDisk);
when(persistedDisk.getProvisioningType()).thenReturn(Storage.ProvisioningType.THIN);
when(diskOfferingDao.persist(any(DiskOfferingVO.class))).thenReturn(persistedDisk);
Mockito.doReturn(clonedOffering).when(configurationManager).createServiceOffering(
anyLong(), anyBoolean(), any(VirtualMachine.Type.class), anyString(),
any(Integer.class), any(Integer.class), any(Integer.class), anyString(), anyString(), anyBoolean(),
anyBoolean(), anyBoolean(), anyBoolean(), anyString(), anyList(), anyList(), anyString(), any(Integer.class),
anyString(), anyMap(), anyLong(), any(Boolean.class),
anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
any(Integer.class), anyString(), anyLong(), anyBoolean(), anyLong(), anyBoolean(), anyBoolean(), anyBoolean(),
anyLong(), any(Integer.class), any(Boolean.class), anyBoolean(), any(Integer.class), any(VMLeaseManager.ExpiryAction.class)
);
ServiceOffering result = configurationManager.cloneServiceOffering(cmd);
Assert.assertNotNull("Cloned offering should not be null", result);
verify(serviceOfferingDao).findById(sourceId);
Assert.assertEquals("Cloned offering should have correct name", "cloned-offering", result.getName());
Assert.assertEquals("Cloned offering should inherit CPU count", Integer.valueOf(2), result.getCpu());
Assert.assertEquals("Cloned offering should inherit CPU speed", Integer.valueOf(1000), result.getSpeed());
Assert.assertEquals("Cloned offering should inherit RAM", Integer.valueOf(2048), result.getRamSize());
}
@Test
public void testCloneServiceOfferingOverridesProvidedParameters() {
Long sourceId = 1L;
ServiceOfferingVO sourceOffering = mock(ServiceOfferingVO.class);
when(sourceOffering.getId()).thenReturn(sourceId);
when(sourceOffering.getName()).thenReturn("source-offering");
when(sourceOffering.getDisplayText()).thenReturn("Source Display Text");
when(sourceOffering.getCpu()).thenReturn(2);
when(sourceOffering.getSpeed()).thenReturn(1000);
when(sourceOffering.getRamSize()).thenReturn(2048);
when(sourceOffering.isOfferHA()).thenReturn(true);
when(sourceOffering.getLimitCpuUse()).thenReturn(false);
when(sourceOffering.isVolatileVm()).thenReturn(false);
when(sourceOffering.isCustomized()).thenReturn(false);
when(sourceOffering.isDynamicScalingEnabled()).thenReturn(true);
when(sourceOffering.getDiskOfferingStrictness()).thenReturn(false);
when(sourceOffering.isSystemUse()).thenReturn(false);
when(sourceOffering.getVmType()).thenReturn(VirtualMachine.Type.User.toString());
when(sourceOffering.getDiskOfferingId()).thenReturn(1L);
when(sourceOffering.getVgpuProfileId()).thenReturn(null);
when(sourceOffering.getGpuCount()).thenReturn(null);
CloneServiceOfferingCmd cmd = mock(CloneServiceOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(sourceId);
when(cmd.getServiceOfferingName()).thenReturn("cloned-offering");
when(cmd.getDisplayText()).thenReturn("New Display Text");
when(cmd.getCpuNumber()).thenReturn(4);
when(cmd.getCpuSpeed()).thenReturn(2000);
when(cmd.getMemory()).thenReturn(4096);
when(cmd.getVgpuProfileId()).thenReturn(null);
when(cmd.getGpuCount()).thenReturn(null);
when(cmd.getDiskOfferingId()).thenReturn(null);
DiskOfferingVO diskOffering = mock(DiskOfferingVO.class);
when(diskOfferingDao.findById(1L)).thenReturn(diskOffering);
when(diskOffering.getProvisioningType()).thenReturn(Storage.ProvisioningType.THIN);
Map<String, String> params = new HashMap<>();
params.put(ApiConstants.OFFER_HA, "false");
when(cmd.getFullUrlParams()).thenReturn(params);
when(cmd.isOfferHa()).thenReturn(false);
when(serviceOfferingDao.findById(sourceId)).thenReturn(sourceOffering);
ServiceOfferingVO clonedOffering = mock(ServiceOfferingVO.class);
when(clonedOffering.getId()).thenReturn(2L);
when(clonedOffering.getName()).thenReturn("cloned-offering");
when(clonedOffering.getDisplayText()).thenReturn("New Display Text");
when(clonedOffering.getCpu()).thenReturn(4);
when(clonedOffering.getSpeed()).thenReturn(2000);
when(clonedOffering.getRamSize()).thenReturn(4096);
when(clonedOffering.isOfferHA()).thenReturn(false);
when(serviceOfferingDao.persist(any(ServiceOfferingVO.class))).thenReturn(clonedOffering);
Mockito.doReturn(clonedOffering).when(configurationManager).createServiceOffering(
anyLong(), anyBoolean(), any(), anyString(), eq(4), eq(4096), eq(2000),
anyString(), anyString(), anyBoolean(), eq(false), anyBoolean(), anyBoolean(),
anyString(), anyList(), anyList(), anyString(), anyInt(), anyString(), any(),
anyLong(), anyBoolean(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyLong(), anyLong(), anyInt(), anyString(), anyLong(), anyBoolean(), anyLong(),
anyBoolean(), anyBoolean(), anyBoolean(), anyLong(), anyInt(), anyBoolean(),
anyBoolean(), anyInt(), any());
ServiceOffering result = configurationManager.cloneServiceOffering(cmd);
Assert.assertNotNull("Cloned offering should not be null", result);
verify(serviceOfferingDao).findById(sourceId);
Assert.assertEquals("Cloned offering should override display text", "New Display Text", result.getDisplayText());
Assert.assertEquals("Cloned offering should override CPU count", Integer.valueOf(4), result.getCpu());
Assert.assertEquals("Cloned offering should override CPU speed", Integer.valueOf(2000), result.getSpeed());
Assert.assertEquals("Cloned offering should override RAM", Integer.valueOf(4096), result.getRamSize());
Assert.assertEquals("Cloned offering should override HA", Boolean.FALSE, result.isOfferHA());
}
@Test(expected = InvalidParameterValueException.class)
public void testCloneDiskOfferingFailsWhenSourceNotFound() {
CloneDiskOfferingCmd cmd = mock(CloneDiskOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(999L);
when(cmd.getOfferingName()).thenReturn("cloned-disk-offering");
when(diskOfferingDao.findById(999L)).thenReturn(null);
configurationManager.cloneDiskOffering(cmd);
}
@Test
public void testCloneDiskOfferingInheritsAllPropertiesFromSource() {
Long sourceId = 1L;
DiskOfferingVO sourceOffering = mock(DiskOfferingVO.class);
when(sourceOffering.getId()).thenReturn(sourceId);
when(sourceOffering.getName()).thenReturn("source-disk");
when(sourceOffering.getDisplayText()).thenReturn("Source Disk Display");
when(sourceOffering.getDiskSize()).thenReturn(10L);
when(sourceOffering.getTags()).thenReturn("tag1");
when(sourceOffering.isCustomized()).thenReturn(false);
when(sourceOffering.getDisplayOffering()).thenReturn(true);
when(sourceOffering.isCustomizedIops()).thenReturn(false);
when(sourceOffering.getDiskSizeStrictness()).thenReturn(false);
when(sourceOffering.getEncrypt()).thenReturn(false);
when(sourceOffering.isUseLocalStorage()).thenReturn(false);
when(sourceOffering.getProvisioningType()).thenReturn(Storage.ProvisioningType.THIN);
when(sourceOffering.getMinIops()).thenReturn(1000L);
when(sourceOffering.getMaxIops()).thenReturn(2000L);
CloneDiskOfferingCmd cmd = mock(CloneDiskOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(sourceId);
when(cmd.getOfferingName()).thenReturn("cloned-disk-offering");
when(cmd.getDiskSize()).thenReturn(null);
when(diskOfferingDao.findById(sourceId)).thenReturn(sourceOffering);
when(diskOfferingDetailsDao.findDomainIds(sourceId)).thenReturn(Collections.emptyList());
when(diskOfferingDetailsDao.findZoneIds(sourceId)).thenReturn(Collections.emptyList());
when(diskOfferingDetailsDao.getDetail(eq(sourceId), anyString())).thenReturn(null);
when(cmd.getMinIops()).thenReturn(null);
when(cmd.getMaxIops()).thenReturn(null);
DiskOfferingVO clonedOffering = mock(DiskOfferingVO.class);
when(clonedOffering.getId()).thenReturn(2L);
when(clonedOffering.getName()).thenReturn("cloned-disk-offering");
when(clonedOffering.getDisplayText()).thenReturn("Source Disk Display");
when(clonedOffering.getDiskSize()).thenReturn(10L);
when(clonedOffering.getTags()).thenReturn("tag1");
when(diskOfferingDao.persist(any(DiskOfferingVO.class))).thenReturn(clonedOffering);
Mockito.doReturn(clonedOffering).when(configurationManager).createDiskOffering(
anyLong(), anyList(), anyList(), anyString(), anyString(), anyString(),
anyLong(), anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any(),
anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyInt(), anyString(), any(), anyLong(), anyBoolean(), anyBoolean());
DiskOffering result = configurationManager.cloneDiskOffering(cmd);
Assert.assertNotNull("Cloned disk offering should not be null", result);
verify(diskOfferingDao).findById(sourceId);
Assert.assertEquals("Cloned offering should have correct name", "cloned-disk-offering", result.getName());
Assert.assertEquals("Cloned offering should inherit display text", "Source Disk Display", result.getDisplayText());
Assert.assertEquals("Cloned offering should inherit disk size", 10L, result.getDiskSize());
Assert.assertEquals("Cloned offering should inherit tags", "tag1", result.getTags());
}
@Test
public void testCloneDiskOfferingOverridesProvidedParameters() {
Long sourceId = 1L;
DiskOfferingVO sourceOffering = mock(DiskOfferingVO.class);
when(sourceOffering.getId()).thenReturn(sourceId);
when(sourceOffering.getName()).thenReturn("source-disk");
when(sourceOffering.getDisplayText()).thenReturn("Source Disk Display");
when(sourceOffering.getDiskSize()).thenReturn(100L);
when(sourceOffering.getTags()).thenReturn("tag1");
when(sourceOffering.isCustomized()).thenReturn(false);
when(sourceOffering.getProvisioningType()).thenReturn(Storage.ProvisioningType.THIN);
when(sourceOffering.isUseLocalStorage()).thenReturn(false);
CloneDiskOfferingCmd cmd = mock(CloneDiskOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(sourceId);
when(cmd.getOfferingName()).thenReturn("cloned-disk-offering");
when(cmd.getDisplayText()).thenReturn("New Disk Display");
when(cmd.getDiskSize()).thenReturn(20L);
when(cmd.getTags()).thenReturn("tag1,tag2");
when(cmd.getFullUrlParams()).thenReturn(new HashMap<>());
when(cmd.getMinIops()).thenReturn(100L);
when(cmd.getMaxIops()).thenReturn(200L);
when(diskOfferingDao.findById(sourceId)).thenReturn(sourceOffering);
when(diskOfferingDetailsDao.findDomainIds(sourceId)).thenReturn(Collections.emptyList());
when(diskOfferingDetailsDao.findZoneIds(sourceId)).thenReturn(Collections.emptyList());
when(diskOfferingDetailsDao.getDetail(eq(sourceId), anyString())).thenReturn(null);
DiskOfferingVO clonedOffering = mock(DiskOfferingVO.class);
when(clonedOffering.getId()).thenReturn(2L);
when(clonedOffering.getName()).thenReturn("cloned-disk-offering");
when(clonedOffering.getDisplayText()).thenReturn("New Disk Display");
when(clonedOffering.getDiskSize()).thenReturn(21L);
when(clonedOffering.getTags()).thenReturn("tag1,tag2");
// Ensure the real createDiskOffering path will return our mocked offering when it calls persist
when(diskOfferingDao.persist(any(DiskOfferingVO.class))).thenReturn(clonedOffering);
Mockito.doReturn(clonedOffering).when(configurationManager).createDiskOffering(
anyLong(), anyList(), anyList(), eq("cloned-disk-offering"), eq("New Disk Display"), anyString(),
anyLong(), eq("tag1,tag2"), anyBoolean(), anyBoolean(), anyBoolean(), any(),
eq(100L), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyInt(), anyString(), any(), anyLong(), anyBoolean(), anyBoolean());
DiskOffering result = configurationManager.cloneDiskOffering(cmd);
Assert.assertNotNull("Cloned disk offering should not be null", result);
verify(diskOfferingDao).findById(sourceId);
}
@Test
public void testCloneDiskOfferingInheritsDomainAndZoneRestrictions() {
Long sourceId = 1L;
List<Long> domainIds = new ArrayList<>();
domainIds.add(1L);
domainIds.add(2L);
List<Long> zoneIds = new ArrayList<>();
zoneIds.add(1L);
DiskOfferingVO sourceOffering = mock(DiskOfferingVO.class);
when(sourceOffering.getId()).thenReturn(sourceId);
when(sourceOffering.getName()).thenReturn("source-disk");
when(sourceOffering.getProvisioningType()).thenReturn(Storage.ProvisioningType.THIN);
when(sourceOffering.isUseLocalStorage()).thenReturn(false);
when(sourceOffering.getDiskSize()).thenReturn(10L);
when(sourceOffering.getMinIops()).thenReturn(1000L);
when(sourceOffering.getMaxIops()).thenReturn(2000L);
CloneDiskOfferingCmd cmd = mock(CloneDiskOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(sourceId);
when(cmd.getOfferingName()).thenReturn("cloned-disk-offering");
when(cmd.getFullUrlParams()).thenReturn(new HashMap<>());
when(cmd.getDomainIds()).thenReturn(null);
when(cmd.getZoneIds()).thenReturn(null);
when(cmd.getDiskSize()).thenReturn(null);
when(cmd.getMinIops()).thenReturn(null);
when(cmd.getMaxIops()).thenReturn(null);
when(diskOfferingDao.findById(sourceId)).thenReturn(sourceOffering);
when(diskOfferingDetailsDao.findDomainIds(sourceId)).thenReturn(domainIds);
when(diskOfferingDetailsDao.findZoneIds(sourceId)).thenReturn(zoneIds);
when(diskOfferingDetailsDao.getDetail(eq(sourceId), anyString())).thenReturn(null);
DiskOfferingVO clonedOffering = mock(DiskOfferingVO.class);
when(clonedOffering.getId()).thenReturn(2L);
when(diskOfferingDao.persist(any(DiskOfferingVO.class))).thenReturn(clonedOffering);
Mockito.doReturn(clonedOffering).when(configurationManager).createDiskOffering(
anyLong(), eq(domainIds), eq(zoneIds), anyString(), anyString(), anyString(),
anyLong(), anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any(),
anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyInt(), anyString(), any(), anyLong(), anyBoolean(), anyBoolean());
DiskOffering result = configurationManager.cloneDiskOffering(cmd);
Assert.assertNotNull("Cloned disk offering should not be null", result);
verify(diskOfferingDao).findById(sourceId);
verify(diskOfferingDetailsDao).findDomainIds(sourceId);
verify(diskOfferingDetailsDao).findZoneIds(sourceId);
}
@Test
public void testCloneServiceOfferingCanInheritDetailsFromSource() {
Long sourceId = 1L;
ServiceOfferingVO sourceOffering = mock(ServiceOfferingVO.class);
when(sourceOffering.getId()).thenReturn(sourceId);
when(sourceOffering.getCpu()).thenReturn(2);
when(sourceOffering.getSpeed()).thenReturn(1000);
when(sourceOffering.getRamSize()).thenReturn(2048);
when(sourceOffering.isSystemUse()).thenReturn(false);
when(sourceOffering.getVmType()).thenReturn(VirtualMachine.Type.User.toString());
when(sourceOffering.getVgpuProfileId()).thenReturn(null);
when(sourceOffering.getGpuCount()).thenReturn(null);
CloneServiceOfferingCmd cmd = mock(CloneServiceOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(sourceId);
when(cmd.getServiceOfferingName()).thenReturn("cloned-offering");
when(cmd.getFullUrlParams()).thenReturn(new HashMap<>());
when(cmd.getDetails()).thenReturn(null);
when(cmd.getVgpuProfileId()).thenReturn(null);
when(cmd.getGpuCount()).thenReturn(null);
when(serviceOfferingDao.findById(sourceId)).thenReturn(sourceOffering);
DiskOfferingVO diskOfferingVO = mock(DiskOfferingVO.class);
when(diskOfferingDao.findById(anyLong())).thenReturn(diskOfferingVO);
when(diskOfferingVO.getProvisioningType()).thenReturn(Storage.ProvisioningType.THIN);
ServiceOfferingVO clonedOffering = mock(ServiceOfferingVO.class);
when(clonedOffering.getId()).thenReturn(2L);
when(serviceOfferingDao.persist(any(ServiceOfferingVO.class))).thenReturn(clonedOffering);
Mockito.doReturn(clonedOffering).when(configurationManager).createServiceOffering(
anyLong(), anyBoolean(), any(), anyString(), anyInt(), anyInt(), anyInt(),
anyString(), anyString(), anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean(),
anyString(), anyList(), anyList(), anyString(), anyInt(), anyString(), any(),
anyLong(), anyBoolean(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyLong(), anyLong(), anyInt(), anyString(), anyLong(), anyBoolean(), anyLong(),
anyBoolean(), anyBoolean(), anyBoolean(), anyLong(), anyInt(), anyBoolean(),
anyBoolean(), anyInt(), any());
ServiceOffering result = configurationManager.cloneServiceOffering(cmd);
Assert.assertNotNull("Cloned offering should not be null", result);
verify(serviceOfferingDao).findById(sourceId);
}
@Test
public void testCloneDiskOfferingVerifiesInheritedValues() {
Long sourceId = 1L;
DiskOfferingVO sourceOffering = new DiskOfferingVO("source-disk", "Source Disk Offering",
Storage.ProvisioningType.THIN, 50L, "production,ssd", false, false, 1000L, 5000L);
sourceOffering.setDisplayOffering(true);
sourceOffering.setDiskSizeStrictness(false);
sourceOffering.setEncrypt(true);
sourceOffering.setUseLocalStorage(false);
sourceOffering.setHypervisorSnapshotReserve(20);
CloneDiskOfferingCmd cmd = mock(CloneDiskOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(sourceId);
when(cmd.getOfferingName()).thenReturn("cloned-disk-offering");
when(cmd.getFullUrlParams()).thenReturn(new HashMap<>());
when(cmd.getDiskSize()).thenReturn(null);
when(cmd.getMinIops()).thenReturn(null);
when(cmd.getMaxIops()).thenReturn(null);
when(diskOfferingDao.findById(sourceId)).thenReturn(sourceOffering);
when(diskOfferingDetailsDao.findDomainIds(sourceId)).thenReturn(Collections.emptyList());
when(diskOfferingDetailsDao.findZoneIds(sourceId)).thenReturn(Collections.emptyList());
when(diskOfferingDetailsDao.getDetail(eq(sourceId), anyString())).thenReturn(null);
DiskOfferingVO clonedOffering = new DiskOfferingVO("cloned-disk-offering", "Source Disk Offering",
Storage.ProvisioningType.THIN, 50L, "production,ssd", false, false, 1000L, 5000L);
clonedOffering.setEncrypt(true);
clonedOffering.setHypervisorSnapshotReserve(20);
when(diskOfferingDao.persist(any(DiskOfferingVO.class))).thenReturn(clonedOffering);
Mockito.doReturn(clonedOffering).when(configurationManager).createDiskOffering(
anyLong(), anyList(), anyList(), eq("cloned-disk-offering"), eq("Source Disk Offering"),
anyString(), eq(50L), eq("production,ssd"), anyBoolean(), anyBoolean(),
anyBoolean(), any(), eq(1000L), eq(5000L), anyLong(), anyLong(), anyLong(),
anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyLong(), anyLong(), eq(20), anyString(), any(), anyLong(), anyBoolean(),
eq(true));
DiskOffering result = configurationManager.cloneDiskOffering(cmd);
Assert.assertNotNull("Cloned disk offering should not be null", result);
Assert.assertEquals("Should inherit display text", "Source Disk Offering", result.getDisplayText());
Assert.assertEquals("Should inherit disk size", 50L, result.getDiskSize());
Assert.assertEquals("Should inherit tags", "production,ssd", result.getTags());
Assert.assertEquals("Should inherit min IOPS", Long.valueOf(1000L), result.getMinIops());
Assert.assertEquals("Should inherit max IOPS", Long.valueOf(5000L), result.getMaxIops());
Assert.assertEquals("Should inherit hypervisor snapshot reserve", Integer.valueOf(20), result.getHypervisorSnapshotReserve());
verify(diskOfferingDao).findById(sourceId);
}
@Test
public void testCloneDiskOfferingVerifiesOverriddenValues() {
Long sourceId = 1L;
DiskOfferingVO sourceOffering = new DiskOfferingVO("source-disk", "Source Disk Offering",
Storage.ProvisioningType.THIN, 5L, "production", false, false, 1000L, 5000L);
sourceOffering.setEncrypt(false);
CloneDiskOfferingCmd cmd = mock(CloneDiskOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(sourceId);
when(cmd.getOfferingName()).thenReturn("cloned-disk-offering");
when(cmd.getDisplayText()).thenReturn("Cloned Disk Offering - Updated");
when(cmd.getDiskSize()).thenReturn(10L);
when(cmd.getTags()).thenReturn("production,high-performance");
when(cmd.getMinIops()).thenReturn(2000L);
when(cmd.getMaxIops()).thenReturn(10000L);
Map<String, String> params = new HashMap<>();
params.put(ApiConstants.ENCRYPT, "true");
when(cmd.getFullUrlParams()).thenReturn(params);
when(cmd.getEncrypt()).thenReturn(true);
when(diskOfferingDao.findById(sourceId)).thenReturn(sourceOffering);
when(diskOfferingDetailsDao.findDomainIds(sourceId)).thenReturn(Collections.emptyList());
when(diskOfferingDetailsDao.findZoneIds(sourceId)).thenReturn(Collections.emptyList());
when(diskOfferingDetailsDao.getDetail(eq(sourceId), anyString())).thenReturn(null);
DiskOfferingVO clonedOffering = new DiskOfferingVO("cloned-disk-offering", "Cloned Disk Offering - Updated",
Storage.ProvisioningType.THIN, 10L, "production,high-performance", false, false, 2000L, 10000L);
clonedOffering.setEncrypt(true);
when(diskOfferingDao.persist(any(DiskOfferingVO.class))).thenReturn(clonedOffering);
Mockito.doReturn(clonedOffering).when(configurationManager).createDiskOffering(
anyLong(), anyList(), anyList(), eq("cloned-disk-offering"),
eq("Cloned Disk Offering - Updated"), anyString(), eq(10L),
eq("production,high-performance"), anyBoolean(), anyBoolean(), anyBoolean(),
any(), eq(2000L), eq(10000L), anyLong(), anyLong(), anyLong(), anyLong(),
anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyLong(), anyInt(), anyString(), any(), anyLong(), anyBoolean(), eq(true));
DiskOffering result = configurationManager.cloneDiskOffering(cmd);
Assert.assertNotNull("Cloned disk offering should not be null", result);
Assert.assertEquals("Should override display text", "Cloned Disk Offering - Updated", result.getDisplayText());
Assert.assertEquals("Should override disk size", 10L, result.getDiskSize());
Assert.assertEquals("Should override tags", "production,high-performance", result.getTags());
Assert.assertEquals("Should override min IOPS", Long.valueOf(2000L), result.getMinIops());
Assert.assertEquals("Should override max IOPS", Long.valueOf(10000L), result.getMaxIops());
Assert.assertTrue("Should override encrypt flag", result.getEncrypt());
verify(diskOfferingDao).findById(sourceId);
}
@Test
public void testCloneDiskOfferingInheritsBytesReadWriteRates() {
Long sourceId = 1L;
DiskOfferingVO sourceOffering = new DiskOfferingVO("source-disk", "Source Disk",
Storage.ProvisioningType.THIN, 53L, "tag1", false, false, null, null);
sourceOffering.setBytesReadRate(10485760L);
sourceOffering.setBytesReadRateMax(20971520L);
sourceOffering.setBytesReadRateMaxLength(60L);
sourceOffering.setBytesWriteRate(10485760L);
sourceOffering.setBytesWriteRateMax(20971520L);
sourceOffering.setBytesWriteRateMaxLength(60L);
CloneDiskOfferingCmd cmd = mock(CloneDiskOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(sourceId);
when(cmd.getOfferingName()).thenReturn("cloned-disk");
when(cmd.getFullUrlParams()).thenReturn(new HashMap<>());
when(cmd.getDiskSize()).thenReturn(null);
when(cmd.getMinIops()).thenReturn(null);
when(cmd.getMaxIops()).thenReturn(null);
when(diskOfferingDao.findById(sourceId)).thenReturn(sourceOffering);
when(diskOfferingDetailsDao.findDomainIds(sourceId)).thenReturn(Collections.emptyList());
when(diskOfferingDetailsDao.findZoneIds(sourceId)).thenReturn(Collections.emptyList());
when(diskOfferingDetailsDao.getDetail(eq(sourceId), anyString())).thenReturn(null);
DiskOfferingVO clonedOffering = new DiskOfferingVO("cloned-disk", "Source Disk",
Storage.ProvisioningType.THIN, 53L, "tag1", false, false, null, null);
clonedOffering.setBytesReadRate(10485760L);
clonedOffering.setBytesReadRateMax(20971520L);
clonedOffering.setBytesReadRateMaxLength(60L);
clonedOffering.setBytesWriteRate(10485760L);
clonedOffering.setBytesWriteRateMax(20971520L);
clonedOffering.setBytesWriteRateMaxLength(60L);
when(diskOfferingDao.persist(any(DiskOfferingVO.class))).thenReturn(clonedOffering);
Mockito.doReturn(clonedOffering).when(configurationManager).createDiskOffering(
anyLong(), anyList(), anyList(), anyString(), anyString(), anyString(),
anyLong(), anyString(), anyBoolean(), anyBoolean(), anyBoolean(), any(),
anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(), anyLong(),
anyInt(), anyString(), any(), anyLong(), anyBoolean(), anyBoolean());
DiskOffering result = configurationManager.cloneDiskOffering(cmd);
Assert.assertNotNull("Cloned disk offering should not be null", result);
Assert.assertEquals("Should inherit bytes read rate", Long.valueOf(10485760L), result.getBytesReadRate());
Assert.assertEquals("Should inherit bytes read rate max", Long.valueOf(20971520L), result.getBytesReadRateMax());
Assert.assertEquals("Should inherit bytes read rate max length", Long.valueOf(60L), result.getBytesReadRateMaxLength());
Assert.assertEquals("Should inherit bytes write rate", Long.valueOf(10485760L), result.getBytesWriteRate());
Assert.assertEquals("Should inherit bytes write rate max", Long.valueOf(20971520L), result.getBytesWriteRateMax());
Assert.assertEquals("Should inherit bytes write rate max length", Long.valueOf(60L), result.getBytesWriteRateMaxLength());
verify(diskOfferingDao).findById(sourceId);
}
@Test(expected = InvalidParameterValueException.class)
public void testCloneNetworkOfferingFailsWhenSourceNotFound() {
CloneNetworkOfferingCmd cmd = mock(CloneNetworkOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(999L);
when(cmd.getNetworkOfferingName()).thenReturn("cloned-network-offering");
when(networkOfferingDao.findById(999L)).thenReturn(null);
configurationManager.cloneNetworkOffering(cmd);
}
@Test(expected = InvalidParameterValueException.class)
public void testCloneNetworkOfferingFailsWhenNameIsNull() {
CloneNetworkOfferingCmd cmd = mock(CloneNetworkOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(1L);
when(cmd.getNetworkOfferingName()).thenReturn(null);
NetworkOfferingVO sourceOffering = mock(NetworkOfferingVO.class);
when(sourceOffering.getId()).thenReturn(1L);
when(networkOfferingDao.findById(1L)).thenReturn(sourceOffering);
configurationManager.cloneNetworkOffering(cmd);
}
@Test(expected = InvalidParameterValueException.class)
public void testCloneNetworkOfferingFailsWhenNameAlreadyExists() {
CloneNetworkOfferingCmd cmd = mock(CloneNetworkOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(1L);
when(cmd.getNetworkOfferingName()).thenReturn("existing-offering");
NetworkOfferingVO sourceOffering = mock(NetworkOfferingVO.class);
when(sourceOffering.getId()).thenReturn(1L);
when(sourceOffering.getName()).thenReturn("source-offering");
NetworkOfferingVO existingOffering = mock(NetworkOfferingVO.class);
when(existingOffering.getId()).thenReturn(2L);
when(networkOfferingDao.findById(1L)).thenReturn(sourceOffering);
when(networkOfferingDao.findByUniqueName("existing-offering")).thenReturn(existingOffering);
configurationManager.cloneNetworkOffering(cmd);
}
@Test
public void testCloneNetworkOfferingInheritsAllPropertiesFromSource() {
Long sourceId = 1L;
NetworkOfferingVO sourceOffering = mock(NetworkOfferingVO.class);
when(sourceOffering.getId()).thenReturn(sourceId);
when(sourceOffering.getName()).thenReturn("source-network-offering");
when(sourceOffering.getDisplayText()).thenReturn("Source Network Offering");
when(sourceOffering.getGuestType()).thenReturn(Network.GuestType.Isolated);
when(sourceOffering.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
when(sourceOffering.getAvailability()).thenReturn(NetworkOffering.Availability.Optional);
when(sourceOffering.getState()).thenReturn(NetworkOffering.State.Enabled);
when(sourceOffering.isDefault()).thenReturn(false);
when(sourceOffering.isConserveMode()).thenReturn(true);
when(sourceOffering.isEgressDefaultPolicy()).thenReturn(false);
when(sourceOffering.isPersistent()).thenReturn(false);
CloneNetworkOfferingCmd cmd = mock(CloneNetworkOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(sourceId);
when(cmd.getNetworkOfferingName()).thenReturn("cloned-network-offering");
when(networkOfferingDao.findById(sourceId)).thenReturn(sourceOffering);
when(networkOfferingDao.findByUniqueName("cloned-network-offering")).thenReturn(null);
// Mock the network model to return service provider map
Map<Network.Service, java.util.Set<Network.Provider>> serviceProviderMap = new HashMap<>();
when(configurationManager._networkModel.getNetworkOfferingServiceProvidersMap(sourceId))
.thenReturn(serviceProviderMap);
NetworkOfferingVO clonedOffering = mock(NetworkOfferingVO.class);
when(clonedOffering.getId()).thenReturn(2L);
when(clonedOffering.getName()).thenReturn("cloned-network-offering");
when(clonedOffering.getDisplayText()).thenReturn("Source Network Offering");
when(clonedOffering.getGuestType()).thenReturn(Network.GuestType.Isolated);
Mockito.doReturn(clonedOffering).when(configurationManager).createNetworkOffering(any());
NetworkOffering result = configurationManager.cloneNetworkOffering(cmd);
Assert.assertNotNull("Cloned network offering should not be null", result);
verify(networkOfferingDao).findById(sourceId);
Assert.assertEquals("Should have correct name", "cloned-network-offering", result.getName());
}
@Test
public void testCloneNetworkOfferingOverridesDisplayText() {
Long sourceId = 1L;
NetworkOfferingVO sourceOffering = mock(NetworkOfferingVO.class);
when(sourceOffering.getId()).thenReturn(sourceId);
when(sourceOffering.getName()).thenReturn("source-network-offering");
when(sourceOffering.getDisplayText()).thenReturn("Source Network Offering");
when(sourceOffering.getGuestType()).thenReturn(Network.GuestType.Isolated);
when(sourceOffering.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
CloneNetworkOfferingCmd cmd = mock(CloneNetworkOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(sourceId);
when(cmd.getNetworkOfferingName()).thenReturn("cloned-network-offering");
when(cmd.getDisplayText()).thenReturn("New Display Text for Network");
when(networkOfferingDao.findById(sourceId)).thenReturn(sourceOffering);
when(networkOfferingDao.findByUniqueName("cloned-network-offering")).thenReturn(null);
Map<Network.Service, java.util.Set<Network.Provider>> serviceProviderMap = new HashMap<>();
when(configurationManager._networkModel.getNetworkOfferingServiceProvidersMap(sourceId))
.thenReturn(serviceProviderMap);
NetworkOfferingVO clonedOffering = mock(NetworkOfferingVO.class);
when(clonedOffering.getId()).thenReturn(2L);
when(clonedOffering.getName()).thenReturn("cloned-network-offering");
when(clonedOffering.getDisplayText()).thenReturn("New Display Text for Network");
Mockito.doReturn(clonedOffering).when(configurationManager).createNetworkOffering(any());
NetworkOffering result = configurationManager.cloneNetworkOffering(cmd);
Assert.assertNotNull("Cloned network offering should not be null", result);
Assert.assertEquals("Should override display text", "New Display Text for Network", result.getDisplayText());
verify(networkOfferingDao).findById(sourceId);
}
@Test
public void testCloneNetworkOfferingHandlesAddServices() {
Long sourceId = 1L;
NetworkOfferingVO sourceOffering = mock(NetworkOfferingVO.class);
when(sourceOffering.getId()).thenReturn(sourceId);
when(sourceOffering.getName()).thenReturn("source-network-offering");
when(sourceOffering.getDisplayText()).thenReturn("Source Network Offering");
when(sourceOffering.getGuestType()).thenReturn(Network.GuestType.Isolated);
when(sourceOffering.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
CloneNetworkOfferingCmd cmd = mock(CloneNetworkOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(sourceId);
when(cmd.getNetworkOfferingName()).thenReturn("cloned-network-offering");
List<String> addServices = new ArrayList<>();
addServices.add("Vpn");
addServices.add("StaticNat");
when(cmd.getAddServices()).thenReturn(addServices);
when(cmd.getSupportedServices()).thenReturn(null);
when(networkOfferingDao.findById(sourceId)).thenReturn(sourceOffering);
when(networkOfferingDao.findByUniqueName("cloned-network-offering")).thenReturn(null);
Map<Network.Service, java.util.Set<Network.Provider>> serviceProviderMap = new HashMap<>();
java.util.Set<Network.Provider> dhcpProviders = new java.util.HashSet<>();
dhcpProviders.add(Network.Provider.VirtualRouter);
serviceProviderMap.put(Network.Service.Dhcp, dhcpProviders);
when(configurationManager._networkModel.getNetworkOfferingServiceProvidersMap(sourceId))
.thenReturn(serviceProviderMap);
NetworkOfferingVO clonedOffering = mock(NetworkOfferingVO.class);
when(clonedOffering.getId()).thenReturn(2L);
when(clonedOffering.getName()).thenReturn("cloned-network-offering");
Mockito.doReturn(clonedOffering).when(configurationManager).createNetworkOffering(any());
NetworkOffering result = configurationManager.cloneNetworkOffering(cmd);
Assert.assertNotNull("Cloned network offering should not be null", result);
verify(networkOfferingDao).findById(sourceId);
}
@Test
public void testCloneNetworkOfferingHandlesDropServices() {
Long sourceId = 1L;
NetworkOfferingVO sourceOffering = mock(NetworkOfferingVO.class);
when(sourceOffering.getId()).thenReturn(sourceId);
when(sourceOffering.getName()).thenReturn("source-network-offering");
when(sourceOffering.getDisplayText()).thenReturn("Source Network Offering");
when(sourceOffering.getGuestType()).thenReturn(Network.GuestType.Isolated);
when(sourceOffering.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
CloneNetworkOfferingCmd cmd = mock(CloneNetworkOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(sourceId);
when(cmd.getNetworkOfferingName()).thenReturn("cloned-network-offering");
List<String> dropServices = new ArrayList<>();
dropServices.add("Firewall");
when(cmd.getDropServices()).thenReturn(dropServices);
when(cmd.getSupportedServices()).thenReturn(null);
when(networkOfferingDao.findById(sourceId)).thenReturn(sourceOffering);
when(networkOfferingDao.findByUniqueName("cloned-network-offering")).thenReturn(null);
Map<Network.Service, java.util.Set<Network.Provider>> serviceProviderMap = new HashMap<>();
java.util.Set<Network.Provider> dhcpProviders = new java.util.HashSet<>();
dhcpProviders.add(Network.Provider.VirtualRouter);
serviceProviderMap.put(Network.Service.Dhcp, dhcpProviders);
java.util.Set<Network.Provider> firewallProviders = new java.util.HashSet<>();
firewallProviders.add(Network.Provider.VirtualRouter);
serviceProviderMap.put(Network.Service.Firewall, firewallProviders);
when(configurationManager._networkModel.getNetworkOfferingServiceProvidersMap(sourceId))
.thenReturn(serviceProviderMap);
NetworkOfferingVO clonedOffering = mock(NetworkOfferingVO.class);
when(clonedOffering.getId()).thenReturn(2L);
when(clonedOffering.getName()).thenReturn("cloned-network-offering");
Mockito.doReturn(clonedOffering).when(configurationManager).createNetworkOffering(any());
NetworkOffering result = configurationManager.cloneNetworkOffering(cmd);
Assert.assertNotNull("Cloned network offering should not be null", result);
verify(networkOfferingDao).findById(sourceId);
}
@Test
public void testCloneNetworkOfferingOverridesSupportedServices() {
Long sourceId = 1L;
NetworkOfferingVO sourceOffering = mock(NetworkOfferingVO.class);
when(sourceOffering.getId()).thenReturn(sourceId);
when(sourceOffering.getName()).thenReturn("source-network-offering");
when(sourceOffering.getDisplayText()).thenReturn("Source Network Offering");
when(sourceOffering.getGuestType()).thenReturn(Network.GuestType.Isolated);
when(sourceOffering.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
CloneNetworkOfferingCmd cmd = mock(CloneNetworkOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(sourceId);
when(cmd.getNetworkOfferingName()).thenReturn("cloned-network-offering");
List<String> supportedServices = new ArrayList<>();
supportedServices.add("Dhcp");
supportedServices.add("Dns");
supportedServices.add("SourceNat");
when(cmd.getSupportedServices()).thenReturn(supportedServices);
when(networkOfferingDao.findById(sourceId)).thenReturn(sourceOffering);
when(networkOfferingDao.findByUniqueName("cloned-network-offering")).thenReturn(null);
Map<Network.Service, java.util.Set<Network.Provider>> serviceProviderMap = new HashMap<>();
when(configurationManager._networkModel.getNetworkOfferingServiceProvidersMap(sourceId))
.thenReturn(serviceProviderMap);
NetworkOfferingVO clonedOffering = mock(NetworkOfferingVO.class);
when(clonedOffering.getId()).thenReturn(2L);
when(clonedOffering.getName()).thenReturn("cloned-network-offering");
Mockito.doReturn(clonedOffering).when(configurationManager).createNetworkOffering(any());
NetworkOffering result = configurationManager.cloneNetworkOffering(cmd);
Assert.assertNotNull("Cloned network offering should not be null", result);
verify(networkOfferingDao).findById(sourceId);
}
@Test
public void testCloneNetworkOfferingInheritsGuestTypeAndTrafficType() {
Long sourceId = 1L;
NetworkOfferingVO sourceOffering = mock(NetworkOfferingVO.class);
when(sourceOffering.getId()).thenReturn(sourceId);
when(sourceOffering.getName()).thenReturn("source-network-offering");
when(sourceOffering.getDisplayText()).thenReturn("Source Network Offering");
when(sourceOffering.getGuestType()).thenReturn(Network.GuestType.Shared);
when(sourceOffering.getTrafficType()).thenReturn(Networks.TrafficType.Guest);
when(sourceOffering.getAvailability()).thenReturn(NetworkOffering.Availability.Required);
CloneNetworkOfferingCmd cmd = mock(CloneNetworkOfferingCmd.class);
when(cmd.getSourceOfferingId()).thenReturn(sourceId);
when(cmd.getNetworkOfferingName()).thenReturn("cloned-network-offering");
when(cmd.getGuestIpType()).thenReturn(null); // Should inherit
when(networkOfferingDao.findById(sourceId)).thenReturn(sourceOffering);
when(networkOfferingDao.findByUniqueName("cloned-network-offering")).thenReturn(null);
Map<Network.Service, java.util.Set<Network.Provider>> serviceProviderMap = new HashMap<>();
when(configurationManager._networkModel.getNetworkOfferingServiceProvidersMap(sourceId))
.thenReturn(serviceProviderMap);