|
22 | 22 | import org.apache.cloudstack.api.BaseCmd.HTTPMethod; |
23 | 23 | import org.apache.cloudstack.api.command.user.vm.UpdateVMCmd; |
24 | 24 | import org.apache.cloudstack.context.CallContext; |
| 25 | +import org.junit.Assert; |
25 | 26 | import org.junit.Before; |
26 | 27 | import org.junit.Test; |
27 | 28 | import org.junit.runner.RunWith; |
|
34 | 35 | import org.powermock.core.classloader.annotations.PrepareForTest; |
35 | 36 | import org.powermock.modules.junit4.PowerMockRunner; |
36 | 37 |
|
| 38 | +import com.cloud.exception.InsufficientAddressCapacityException; |
37 | 39 | import com.cloud.exception.InsufficientCapacityException; |
38 | 40 | import com.cloud.exception.InvalidParameterValueException; |
39 | 41 | import com.cloud.exception.ResourceUnavailableException; |
| 42 | +import com.cloud.network.NetworkModel; |
40 | 43 | import com.cloud.storage.GuestOSVO; |
41 | 44 | import com.cloud.storage.dao.GuestOSDao; |
42 | 45 | import com.cloud.user.Account; |
@@ -70,6 +73,9 @@ public class UserVmManagerImplTest { |
70 | 73 | @Mock |
71 | 74 | private UserVmVO userVmVoMock; |
72 | 75 |
|
| 76 | + @Mock |
| 77 | + private NetworkModel networkModel; |
| 78 | + |
73 | 79 | private long vmId = 1l; |
74 | 80 |
|
75 | 81 | @Before |
@@ -221,4 +227,53 @@ private void configureDoNothingForMethodsThatWeDoNotWantToTest() throws Resource |
221 | 227 | Mockito.anyString(), Mockito.anyBoolean(), Mockito.any(HTTPMethod.class), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyListOf(Long.class), |
222 | 228 | Mockito.anyMap()); |
223 | 229 | } |
| 230 | + |
| 231 | + @Test |
| 232 | + public void validateOrReplaceMacAddressTestMacAddressValid() throws InsufficientAddressCapacityException { |
| 233 | + configureValidateOrReplaceMacAddressTest(0, "01:23:45:67:89:ab", "01:23:45:67:89:ab"); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + public void validateOrReplaceMacAddressTestMacAddressNull() throws InsufficientAddressCapacityException { |
| 238 | + configureValidateOrReplaceMacAddressTest(1, null, "01:23:45:67:89:ab"); |
| 239 | + } |
| 240 | + |
| 241 | + @Test |
| 242 | + public void validateOrReplaceMacAddressTestMacAddressBlank() throws InsufficientAddressCapacityException { |
| 243 | + configureValidateOrReplaceMacAddressTest(1, " ", "01:23:45:67:89:ab"); |
| 244 | + } |
| 245 | + |
| 246 | + @Test |
| 247 | + public void validateOrReplaceMacAddressTestMacAddressEmpty() throws InsufficientAddressCapacityException { |
| 248 | + configureValidateOrReplaceMacAddressTest(1, "", "01:23:45:67:89:ab"); |
| 249 | + } |
| 250 | + |
| 251 | + @Test |
| 252 | + public void validateOrReplaceMacAddressTestMacAddressNotValidOption1() throws InsufficientAddressCapacityException { |
| 253 | + configureValidateOrReplaceMacAddressTest(1, "abcdef:gh:ij:kl", "01:23:45:67:89:ab"); |
| 254 | + } |
| 255 | + |
| 256 | + @Test |
| 257 | + public void validateOrReplaceMacAddressTestMacAddressNotValidOption2() throws InsufficientAddressCapacityException { |
| 258 | + configureValidateOrReplaceMacAddressTest(1, "01:23:45:67:89:", "01:23:45:67:89:ab"); |
| 259 | + } |
| 260 | + |
| 261 | + @Test |
| 262 | + public void validateOrReplaceMacAddressTestMacAddressNotValidOption3() throws InsufficientAddressCapacityException { |
| 263 | + configureValidateOrReplaceMacAddressTest(1, "01:23:45:67:89:az", "01:23:45:67:89:ab"); |
| 264 | + } |
| 265 | + |
| 266 | + @Test |
| 267 | + public void validateOrReplaceMacAddressTestMacAddressNotValidOption4() throws InsufficientAddressCapacityException { |
| 268 | + configureValidateOrReplaceMacAddressTest(1, "@1:23:45:67:89:ab", "01:23:45:67:89:ab"); |
| 269 | + } |
| 270 | + |
| 271 | + private void configureValidateOrReplaceMacAddressTest(int times, String macAddress, String expectedMacAddress) throws InsufficientAddressCapacityException { |
| 272 | + Mockito.when(networkModel.getNextAvailableMacAddressInNetwork(Mockito.anyLong())).thenReturn(expectedMacAddress); |
| 273 | + |
| 274 | + String returnedMacAddress = userVmManagerImpl.validateOrReplaceMacAddress(macAddress, 1l); |
| 275 | + |
| 276 | + Mockito.verify(networkModel, Mockito.times(times)).getNextAvailableMacAddressInNetwork(Mockito.anyLong()); |
| 277 | + Assert.assertEquals(expectedMacAddress, returnedMacAddress); |
| 278 | + } |
224 | 279 | } |
0 commit comments