|
15 | 15 | package com.cloud.network.vpn; |
16 | 16 |
|
17 | 17 | import com.cloud.exception.InvalidParameterValueException; |
| 18 | +import com.cloud.network.VpnUser.State; |
| 19 | +import com.cloud.network.VpnUserVO; |
| 20 | +import com.cloud.network.dao.RemoteAccessVpnDao; |
| 21 | +import com.cloud.network.dao.RemoteAccessVpnVO; |
| 22 | +import com.cloud.network.dao.VpnUserDao; |
| 23 | +import com.cloud.network.element.RemoteAccessVPNServiceProvider; |
| 24 | +import com.cloud.user.Account; |
| 25 | +import com.cloud.user.AccountManager; |
| 26 | +import com.cloud.user.AccountVO; |
| 27 | +import com.cloud.user.dao.AccountDao; |
18 | 28 | import com.cloud.utils.exception.CloudRuntimeException; |
19 | 29 | import com.cloud.utils.net.NetUtils; |
20 | 30 | import junit.framework.TestCase; |
| 31 | +import org.apache.cloudstack.context.CallContext; |
| 32 | +import org.springframework.test.util.ReflectionTestUtils; |
| 33 | + |
| 34 | +import java.util.Collections; |
21 | 35 | import org.junit.Assert; |
22 | 36 | import org.junit.Test; |
23 | 37 | import org.junit.runner.RunWith; |
@@ -222,4 +236,40 @@ public void validateHandleExceptionOnValidateIpRangeErrorWhenInvalidParameterVal |
222 | 236 |
|
223 | 237 | assertEquals(expectedMessage, assertThrows.getMessage()); |
224 | 238 | } |
| 239 | + |
| 240 | + @Test |
| 241 | + public void applyVpnUsersHandlesNullProviderResultWithoutNpe() throws Exception { |
| 242 | + RemoteAccessVpnManagerImpl mgr = new RemoteAccessVpnManagerImpl(); |
| 243 | + |
| 244 | + AccountDao accountDao = Mockito.mock(AccountDao.class); |
| 245 | + AccountManager accountMgr = Mockito.mock(AccountManager.class); |
| 246 | + VpnUserDao vpnUsersDao = Mockito.mock(VpnUserDao.class); |
| 247 | + RemoteAccessVpnDao remoteAccessVpnDao = Mockito.mock(RemoteAccessVpnDao.class); |
| 248 | + RemoteAccessVPNServiceProvider provider = Mockito.mock(RemoteAccessVPNServiceProvider.class); |
| 249 | + ReflectionTestUtils.setField(mgr, "_accountDao", accountDao); |
| 250 | + ReflectionTestUtils.setField(mgr, "_accountMgr", accountMgr); |
| 251 | + ReflectionTestUtils.setField(mgr, "_vpnUsersDao", vpnUsersDao); |
| 252 | + ReflectionTestUtils.setField(mgr, "_remoteAccessVpnDao", remoteAccessVpnDao); |
| 253 | + ReflectionTestUtils.setField(mgr, "_vpnServiceProviders", Collections.singletonList(provider)); |
| 254 | + |
| 255 | + Mockito.when(accountDao.findById(1L)).thenReturn(Mockito.mock(AccountVO.class)); |
| 256 | + |
| 257 | + RemoteAccessVpnVO vpn = Mockito.mock(RemoteAccessVpnVO.class); |
| 258 | + Mockito.when(vpn.getNetworkId()).thenReturn(null); |
| 259 | + Mockito.when(remoteAccessVpnDao.findByAccount(1L)).thenReturn(Collections.singletonList(vpn)); |
| 260 | + |
| 261 | + VpnUserVO user = Mockito.mock(VpnUserVO.class); |
| 262 | + Mockito.when(user.getState()).thenReturn(State.Revoke); |
| 263 | + Mockito.when(vpnUsersDao.listByAccount(1L)).thenReturn(Collections.singletonList(user)); |
| 264 | + |
| 265 | + Mockito.when(provider.applyVpnUsers(Mockito.eq(vpn), Mockito.anyList())).thenReturn(null); |
| 266 | + |
| 267 | + try (MockedStatic<CallContext> callContextMock = Mockito.mockStatic(CallContext.class)) { |
| 268 | + CallContext callContext = Mockito.mock(CallContext.class); |
| 269 | + callContextMock.when(CallContext::current).thenReturn(callContext); |
| 270 | + Mockito.when(callContext.getCallingAccount()).thenReturn(Mockito.mock(Account.class)); |
| 271 | + |
| 272 | + Assert.assertTrue(mgr.applyVpnUsers(1L, "someuser", false)); |
| 273 | + } |
| 274 | + } |
225 | 275 | } |
0 commit comments