VpcVirtualRouterElement extends VirtualRouterElement implements VpcProvider, Site2SiteVpnServiceProvider, NetworkACLServiceProvider { @Override public String[] applyVpnUsers(final RemoteAccessVpn vpn, final List<? extends VpnUser> users) throws ResourceUnavailableException { final Long vpcId = vpn.getVpcId(); if (vpcId == null) { return null; } final List<DomainRouterVO> routers = _vpcRouterMgr.getVpcRouters(vpcId); if (routers == null) { s_logger.debug("Cannot apply vpn users on the backend; virtual router doesn't exist in the network " + vpcId); return null; } final Vpc vpc = _entityMgr.findById(Vpc.class, vpcId); final DataCenterVO dcVO = _dcDao.findById(vpc.getZoneId()); final NetworkTopology networkTopology = networkTopologyContext.retrieveNetworkTopology(dcVO); String[] result = null; final List<String> combinedResults = new ArrayList<String>(); for (final DomainRouterVO domainRouterVO : routers) { result = networkTopology.applyVpnUsers(vpn, users, domainRouterVO); combinedResults.addAll(Arrays.asList(result)); } result = new String[combinedResults.size()]; final Object [] resultCast = combinedResults.toArray(); System.arraycopy(resultCast, 0, result, 0, resultCast.length); return result; }  @Override boolean implementVpc(final Vpc vpc, final DeployDestination dest, final ReservationContext context); @Override boolean shutdownVpc(final Vpc vpc, final ReservationContext context); @Override boolean implement(final Network network, final NetworkOffering offering, final DeployDestination dest, final ReservationContext context); @Override boolean prepare(final Network network, final NicProfile nic, final VirtualMachineProfile vm, final DeployDestination dest, final ReservationContext context); @Override boolean shutdown(final Network network, final ReservationContext context, final boolean cleanup); @Override boolean destroy(final Network config, final ReservationContext context); @Override Provider getProvider(); @Override Map<Service, Map<Capability, String>> getCapabilities(); @Override boolean createPrivateGateway(final PrivateGateway gateway); @Override boolean deletePrivateGateway(final PrivateGateway gateway); @Override boolean applyIps(final Network network, final List<? extends PublicIpAddress> ipAddress, final Set<Service> services); @Override boolean applyNetworkACLs(final Network network, final List<? extends NetworkACLItem> rules); @Override boolean applyStaticRoutes(final Vpc vpc, final List<StaticRouteProfile> routes); @Override boolean applyACLItemsToPrivateGw(final PrivateGateway gateway, final List<? extends NetworkACLItem> rules); @Override boolean startSite2SiteVpn(final Site2SiteVpnConnection conn); @Override boolean stopSite2SiteVpn(final Site2SiteVpnConnection conn); @Override String[] applyVpnUsers(final RemoteAccessVpn vpn, final List<? extends VpnUser> users); @Override boolean startVpn(final RemoteAccessVpn vpn); @Override boolean stopVpn(final RemoteAccessVpn vpn);  }
@Test public void testApplyVpnUsers() { vpcVirtualRouterElement._vpcRouterMgr = _vpcRouterMgr; final AdvancedNetworkTopology advancedNetworkTopology = Mockito.mock(AdvancedNetworkTopology.class); final BasicNetworkTopology basicNetworkTopology = Mockito.mock(BasicNetworkTopology.class); networkTopologyContext.setAdvancedNetworkTopology(advancedNetworkTopology); networkTopologyContext.setBasicNetworkTopology(basicNetworkTopology); networkTopologyContext.init(); final Vpc vpc = Mockito.mock(Vpc.class); final DataCenterVO dataCenterVO = Mockito.mock(DataCenterVO.class); final RemoteAccessVpn remoteAccessVpn = Mockito.mock(RemoteAccessVpn.class); final DomainRouterVO domainRouterVO1 = Mockito.mock(DomainRouterVO.class); final DomainRouterVO domainRouterVO2 = Mockito.mock(DomainRouterVO.class); final VpnUser vpnUser1 = Mockito.mock(VpnUser.class); final VpnUser vpnUser2 = Mockito.mock(VpnUser.class); final List<VpnUser> users = new ArrayList<VpnUser>(); users.add(vpnUser1); users.add(vpnUser2); final List<DomainRouterVO> routers = new ArrayList<DomainRouterVO>(); routers.add(domainRouterVO1); routers.add(domainRouterVO2); final Long vpcId = new Long(1l); final Long zoneId = new Long(1l); when(remoteAccessVpn.getVpcId()).thenReturn(vpcId); when(_vpcRouterMgr.getVpcRouters(vpcId)).thenReturn(routers); when(_entityMgr.findById(Vpc.class, vpcId)).thenReturn(vpc); when(vpc.getZoneId()).thenReturn(zoneId); when(_dcDao.findById(zoneId)).thenReturn(dataCenterVO); when(networkTopologyContext.retrieveNetworkTopology(dataCenterVO)).thenReturn(advancedNetworkTopology); try { when(advancedNetworkTopology.applyVpnUsers(remoteAccessVpn, users, domainRouterVO1)).thenReturn(new String[]{"user1", "user2"}); when(advancedNetworkTopology.applyVpnUsers(remoteAccessVpn, users, domainRouterVO2)).thenReturn(new String[]{"user3", "user4"}); } catch (final ResourceUnavailableException e) { fail(e.getMessage()); } try { final String [] results = vpcVirtualRouterElement.applyVpnUsers(remoteAccessVpn, users); assertNotNull(results); assertEquals(results[0], "user1"); assertEquals(results[1], "user2"); assertEquals(results[2], "user3"); assertEquals(results[3], "user4"); } catch (final ResourceUnavailableException e) { fail(e.getMessage()); } verify(remoteAccessVpn, times(1)).getVpcId(); verify(vpc, times(1)).getZoneId(); verify(_dcDao, times(1)).findById(zoneId); verify(networkTopologyContext, times(1)).retrieveNetworkTopology(dataCenterVO); }
@Test public void testApplyVpnUsersException1() { vpcVirtualRouterElement._vpcRouterMgr = _vpcRouterMgr; final AdvancedNetworkTopology advancedNetworkTopology = Mockito.mock(AdvancedNetworkTopology.class); final BasicNetworkTopology basicNetworkTopology = Mockito.mock(BasicNetworkTopology.class); networkTopologyContext.setAdvancedNetworkTopology(advancedNetworkTopology); networkTopologyContext.setBasicNetworkTopology(basicNetworkTopology); networkTopologyContext.init(); final RemoteAccessVpn remoteAccessVpn = Mockito.mock(RemoteAccessVpn.class); final List<VpnUser> users = new ArrayList<VpnUser>(); when(remoteAccessVpn.getVpcId()).thenReturn(null); try { final String [] results = vpcVirtualRouterElement.applyVpnUsers(remoteAccessVpn, users); assertNull(results); } catch (final ResourceUnavailableException e) { fail(e.getMessage()); } verify(remoteAccessVpn, times(1)).getVpcId(); }
@Test public void testApplyVpnUsersException2() { vpcVirtualRouterElement._vpcRouterMgr = _vpcRouterMgr; final AdvancedNetworkTopology advancedNetworkTopology = Mockito.mock(AdvancedNetworkTopology.class); final BasicNetworkTopology basicNetworkTopology = Mockito.mock(BasicNetworkTopology.class); networkTopologyContext.setAdvancedNetworkTopology(advancedNetworkTopology); networkTopologyContext.setBasicNetworkTopology(basicNetworkTopology); networkTopologyContext.init(); final RemoteAccessVpn remoteAccessVpn = Mockito.mock(RemoteAccessVpn.class); final List<VpnUser> users = new ArrayList<VpnUser>(); final Long vpcId = new Long(1l); when(remoteAccessVpn.getVpcId()).thenReturn(vpcId); when(_vpcRouterMgr.getVpcRouters(vpcId)).thenReturn(null); try { final String [] results = vpcVirtualRouterElement.applyVpnUsers(remoteAccessVpn, users); assertNull(results); } catch (final ResourceUnavailableException e) { fail(e.getMessage()); } verify(remoteAccessVpn, times(1)).getVpcId(); }