2121import static org .mockito .Mockito .lenient ;
2222import static org .mockito .Mockito .when ;
2323
24+ import org .apache .cloudstack .api .command .user .address .ListPublicIpAddressesCmd ;
2425import org .apache .cloudstack .api .command .user .ssh .RegisterSSHKeyPairCmd ;
26+ import org .apache .cloudstack .framework .config .ConfigKey ;
27+ import org .junit .Before ;
2528import org .junit .Test ;
2629import org .junit .runner .RunWith ;
2730import org .mockito .Mock ;
2831import org .mockito .Mockito ;
2932import org .mockito .Spy ;
3033import org .mockito .runners .MockitoJUnitRunner ;
34+ import org .powermock .reflect .Whitebox ;
3135
36+ import com .cloud .dc .Vlan .VlanType ;
3237import com .cloud .exception .InvalidParameterValueException ;
38+ import com .cloud .network .IpAddress ;
39+ import com .cloud .network .IpAddressManagerImpl ;
40+ import com .cloud .network .dao .IPAddressVO ;
3341import com .cloud .user .Account ;
3442import com .cloud .user .SSHKeyPair ;
3543import com .cloud .user .SSHKeyPairVO ;
3644import com .cloud .user .dao .SSHKeyPairDao ;
45+ import com .cloud .utils .db .SearchCriteria ;
3746
3847@ RunWith (MockitoJUnitRunner .class )
3948public class ManagementServerImplTest {
4049
50+ @ Mock
51+ SearchCriteria <IPAddressVO > sc ;
52+
4153 @ Mock
4254 RegisterSSHKeyPairCmd regCmd ;
4355
@@ -54,9 +66,20 @@ public class ManagementServerImplTest {
5466 @ Mock
5567 SSHKeyPair sshKeyPair ;
5668
69+ @ Mock
70+ IpAddressManagerImpl ipAddressManagerImpl ;
71+
5772 @ Spy
5873 ManagementServerImpl spy ;
5974
75+ ConfigKey mockConfig ;
76+
77+ @ Before
78+ public void setup () {
79+ mockConfig = Mockito .mock (ConfigKey .class );
80+ Whitebox .setInternalState (ipAddressManagerImpl .getClass (), "SystemVmPublicIpReservationModeStrictness" , mockConfig );
81+ }
82+
6083 @ Test (expected = InvalidParameterValueException .class )
6184 public void testDuplicateRegistraitons (){
6285 String accountName = "account" ;
@@ -107,4 +130,95 @@ public void testSuccess(){
107130 spy .registerSSHKeyPair (regCmd );
108131 Mockito .verify (spy , Mockito .times (3 )).getPublicKeyFromKeyKeyMaterial (anyString ());
109132 }
133+
134+ @ Test
135+ public void setParametersTestWhenStateIsFreeAndSystemVmPublicIsTrue () throws IllegalArgumentException , IllegalAccessException , NoSuchFieldException , SecurityException {
136+ Mockito .when (mockConfig .value ()).thenReturn (Boolean .TRUE );
137+
138+ ListPublicIpAddressesCmd cmd = Mockito .mock (ListPublicIpAddressesCmd .class );
139+ Mockito .when (cmd .getNetworkId ()).thenReturn (10L );
140+ Mockito .when (cmd .getZoneId ()).thenReturn (null );
141+ Mockito .when (cmd .getIpAddress ()).thenReturn (null );
142+ Mockito .when (cmd .getPhysicalNetworkId ()).thenReturn (null );
143+ Mockito .when (cmd .getVlanId ()).thenReturn (null );
144+ Mockito .when (cmd .getId ()).thenReturn (null );
145+ Mockito .when (cmd .isSourceNat ()).thenReturn (null );
146+ Mockito .when (cmd .isStaticNat ()).thenReturn (null );
147+ Mockito .when (cmd .getState ()).thenReturn (IpAddress .State .Free .name ());
148+ Mockito .when (cmd .getTags ()).thenReturn (null );
149+ spy .setParameters (sc , cmd , VlanType .VirtualNetwork , Boolean .FALSE );
150+
151+ Mockito .verify (sc , Mockito .times (1 )).setJoinParameters ("vlanSearch" , "vlanType" , VlanType .VirtualNetwork );
152+ Mockito .verify (sc , Mockito .times (1 )).setParameters ("display" , false );
153+ Mockito .verify (sc , Mockito .times (1 )).setParameters ("sourceNetworkId" , 10L );
154+ Mockito .verify (sc , Mockito .times (1 )).setParameters ("state" , "Free" );
155+ Mockito .verify (sc , Mockito .times (1 )).setParameters ("forsystemvms" , false );
156+ }
157+
158+ @ Test
159+ public void setParametersTestWhenStateIsFreeAndSystemVmPublicIsFalse () {
160+ Mockito .when (mockConfig .value ()).thenReturn (Boolean .FALSE );
161+ ListPublicIpAddressesCmd cmd = Mockito .mock (ListPublicIpAddressesCmd .class );
162+ Mockito .when (cmd .getNetworkId ()).thenReturn (10L );
163+ Mockito .when (cmd .getZoneId ()).thenReturn (null );
164+ Mockito .when (cmd .getIpAddress ()).thenReturn (null );
165+ Mockito .when (cmd .getPhysicalNetworkId ()).thenReturn (null );
166+ Mockito .when (cmd .getVlanId ()).thenReturn (null );
167+ Mockito .when (cmd .getId ()).thenReturn (null );
168+ Mockito .when (cmd .isSourceNat ()).thenReturn (null );
169+ Mockito .when (cmd .isStaticNat ()).thenReturn (null );
170+ Mockito .when (cmd .getState ()).thenReturn (IpAddress .State .Free .name ());
171+ Mockito .when (cmd .getTags ()).thenReturn (null );
172+ spy .setParameters (sc , cmd , VlanType .VirtualNetwork , Boolean .FALSE );
173+
174+ Mockito .verify (sc , Mockito .times (1 )).setJoinParameters ("vlanSearch" , "vlanType" , VlanType .VirtualNetwork );
175+ Mockito .verify (sc , Mockito .times (1 )).setParameters ("display" , false );
176+ Mockito .verify (sc , Mockito .times (1 )).setParameters ("sourceNetworkId" , 10L );
177+ Mockito .verify (sc , Mockito .times (1 )).setParameters ("state" , "Free" );
178+ Mockito .verify (sc , Mockito .never ()).setParameters ("forsystemvms" , false );
179+ }
180+
181+ @ Test
182+ public void setParametersTestWhenStateIsNullAndSystemVmPublicIsFalse () {
183+ Mockito .when (mockConfig .value ()).thenReturn (Boolean .FALSE );
184+ ListPublicIpAddressesCmd cmd = Mockito .mock (ListPublicIpAddressesCmd .class );
185+ Mockito .when (cmd .getNetworkId ()).thenReturn (10L );
186+ Mockito .when (cmd .getZoneId ()).thenReturn (null );
187+ Mockito .when (cmd .getIpAddress ()).thenReturn (null );
188+ Mockito .when (cmd .getPhysicalNetworkId ()).thenReturn (null );
189+ Mockito .when (cmd .getVlanId ()).thenReturn (null );
190+ Mockito .when (cmd .getId ()).thenReturn (null );
191+ Mockito .when (cmd .isSourceNat ()).thenReturn (null );
192+ Mockito .when (cmd .isStaticNat ()).thenReturn (null );
193+ Mockito .when (cmd .getState ()).thenReturn (null );
194+ Mockito .when (cmd .getTags ()).thenReturn (null );
195+ spy .setParameters (sc , cmd , VlanType .VirtualNetwork , Boolean .TRUE );
196+
197+ Mockito .verify (sc , Mockito .times (1 )).setJoinParameters ("vlanSearch" , "vlanType" , VlanType .VirtualNetwork );
198+ Mockito .verify (sc , Mockito .times (1 )).setParameters ("display" , false );
199+ Mockito .verify (sc , Mockito .times (1 )).setParameters ("sourceNetworkId" , 10L );
200+ Mockito .verify (sc , Mockito .never ()).setParameters ("forsystemvms" , false );
201+ }
202+
203+ @ Test
204+ public void setParametersTestWhenStateIsNullAndSystemVmPublicIsTrue () {
205+ Mockito .when (mockConfig .value ()).thenReturn (Boolean .TRUE );
206+ ListPublicIpAddressesCmd cmd = Mockito .mock (ListPublicIpAddressesCmd .class );
207+ Mockito .when (cmd .getNetworkId ()).thenReturn (10L );
208+ Mockito .when (cmd .getZoneId ()).thenReturn (null );
209+ Mockito .when (cmd .getIpAddress ()).thenReturn (null );
210+ Mockito .when (cmd .getPhysicalNetworkId ()).thenReturn (null );
211+ Mockito .when (cmd .getVlanId ()).thenReturn (null );
212+ Mockito .when (cmd .getId ()).thenReturn (null );
213+ Mockito .when (cmd .isSourceNat ()).thenReturn (null );
214+ Mockito .when (cmd .isStaticNat ()).thenReturn (null );
215+ Mockito .when (cmd .getState ()).thenReturn (null );
216+ Mockito .when (cmd .getTags ()).thenReturn (null );
217+ spy .setParameters (sc , cmd , VlanType .VirtualNetwork , Boolean .TRUE );
218+
219+ Mockito .verify (sc , Mockito .times (1 )).setJoinParameters ("vlanSearch" , "vlanType" , VlanType .VirtualNetwork );
220+ Mockito .verify (sc , Mockito .times (1 )).setParameters ("display" , false );
221+ Mockito .verify (sc , Mockito .times (1 )).setParameters ("sourceNetworkId" , 10L );
222+ Mockito .verify (sc , Mockito .never ()).setParameters ("forsystemvms" , false );
223+ }
110224}
0 commit comments