SecurityGroupJoinDaoImpl extends GenericDaoBase<SecurityGroupJoinVO, Long> implements SecurityGroupJoinDao { @Override public SecurityGroupResponse newSecurityGroupResponse(SecurityGroupJoinVO vsg, Account caller) { SecurityGroupResponse sgResponse = new SecurityGroupResponse(); sgResponse.setId(vsg.getUuid()); sgResponse.setName(vsg.getName()); sgResponse.setDescription(vsg.getDescription()); ApiResponseHelper.populateOwner(sgResponse, vsg); Long rule_id = vsg.getRuleId(); if (rule_id != null && rule_id.longValue() > 0) { SecurityGroupRuleResponse ruleData = new SecurityGroupRuleResponse(); ruleData.setRuleId(vsg.getRuleUuid()); ruleData.setProtocol(vsg.getRuleProtocol()); if ("icmp".equalsIgnoreCase(vsg.getRuleProtocol())) { ruleData.setIcmpType(vsg.getRuleStartPort()); ruleData.setIcmpCode(vsg.getRuleEndPort()); } else { ruleData.setStartPort(vsg.getRuleStartPort()); ruleData.setEndPort(vsg.getRuleEndPort()); } if (vsg.getRuleAllowedNetworkId() != null) { List<SecurityGroupJoinVO> sgs = this.searchByIds(vsg.getRuleAllowedNetworkId()); if (sgs != null && sgs.size() > 0) { SecurityGroupJoinVO sg = sgs.get(0); ruleData.setSecurityGroupName(sg.getName()); ruleData.setAccountName(sg.getAccountName()); } } else { ruleData.setCidr(vsg.getRuleAllowedSourceIpCidr()); } List<ResourceTagJoinVO> tags = _resourceTagJoinDao.listBy(vsg.getRuleUuid(), ResourceTag.ResourceObjectType.SecurityGroupRule); Set<ResourceTagResponse> tagResponse = new HashSet<ResourceTagResponse>(); for (ResourceTagJoinVO tag: tags) { tagResponse.add(ApiDBUtils.newResourceTagResponse(tag, false)); } ruleData.setTags(tagResponse); if (vsg.getRuleType() == SecurityRuleType.IngressRule) { ruleData.setObjectName("ingressrule"); sgResponse.addSecurityGroupIngressRule(ruleData); } else { ruleData.setObjectName("egressrule"); sgResponse.addSecurityGroupEgressRule(ruleData); } } List<SecurityGroupVMMapVO> securityGroupVmMap = _securityGroupVMMapDao.listBySecurityGroup(vsg.getId()); s_logger.debug("newSecurityGroupResponse() -> virtualmachine count: " + securityGroupVmMap.size()); sgResponse.setVirtualMachineCount(securityGroupVmMap.size()); for(SecurityGroupVMMapVO securityGroupVMMapVO : securityGroupVmMap) { final UserVmVO userVmVO = _userVmDao.findById(securityGroupVMMapVO.getInstanceId()); if (userVmVO != null) { sgResponse.addVirtualMachineId(userVmVO.getUuid()); } } Long tag_id = vsg.getTagId(); if (tag_id != null && tag_id.longValue() > 0) { ResourceTagJoinVO vtag = ApiDBUtils.findResourceTagViewById(tag_id); if (vtag != null) { sgResponse.addTag(ApiDBUtils.newResourceTagResponse(vtag, false)); } } if (vsg.getJobId() != null) { sgResponse.setJobId(vsg.getJobUuid()); sgResponse.setJobStatus(vsg.getJobStatus()); } sgResponse.setObjectName("securitygroup"); return sgResponse; } protected  SecurityGroupJoinDaoImpl(); @Override SecurityGroupResponse newSecurityGroupResponse(SecurityGroupJoinVO vsg, Account caller); @Override SecurityGroupResponse setSecurityGroupResponse(SecurityGroupResponse vsgData, SecurityGroupJoinVO vsg); @Override List<SecurityGroupJoinVO> newSecurityGroupView(SecurityGroup sg); @Override List<SecurityGroupJoinVO> searchByIds(Long... sgIds); static final Logger s_logger; }
@Test public void virtualMachineCountEmptyTest() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { when(vsg.getId()).thenReturn(1L); SecurityGroupResponse securityGroupResponse = _securityGroupJoinDaoImpl.newSecurityGroupResponse(vsg, caller); Field virtualMachineCount = securityGroupResponse.getClass().getDeclaredField("virtualMachineCount"); virtualMachineCount.setAccessible(true); assertEquals(0, ((Integer)virtualMachineCount.get(securityGroupResponse)).intValue()); }
@Test public void virtualMachineCountOneTest() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { when(vsg.getId()).thenReturn(2L); SecurityGroupResponse securityGroupResponse = _securityGroupJoinDaoImpl.newSecurityGroupResponse(vsg, caller); Field virtualMachineCount = securityGroupResponse.getClass().getDeclaredField("virtualMachineCount"); virtualMachineCount.setAccessible(true); assertEquals(1, ((Integer)virtualMachineCount.get(securityGroupResponse)).intValue()); }
@Test public void virtualMachineCountTwoTest() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { when(vsg.getId()).thenReturn(3L); SecurityGroupResponse securityGroupResponse = _securityGroupJoinDaoImpl.newSecurityGroupResponse(vsg, caller); Field virtualMachineCount = securityGroupResponse.getClass().getDeclaredField("virtualMachineCount"); virtualMachineCount.setAccessible(true); assertEquals(2, ((Integer)virtualMachineCount.get(securityGroupResponse)).intValue()); }
@Test public void virtualMachineIDsEmptyTest() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { when(vsg.getId()).thenReturn(1L); SecurityGroupResponse securityGroupResponse = _securityGroupJoinDaoImpl.newSecurityGroupResponse(vsg, caller); Field fieldVirtualMachineIds = securityGroupResponse.getClass().getDeclaredField("virtualMachineIds"); fieldVirtualMachineIds.setAccessible(true); Set<String> virtualMachineIds = (Set<String>)fieldVirtualMachineIds.get(securityGroupResponse); assertEquals(0, virtualMachineIds.size()); }
@Test public void virtualMachineIDsOneTest() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { when(vsg.getId()).thenReturn(2L); SecurityGroupResponse securityGroupResponse = _securityGroupJoinDaoImpl.newSecurityGroupResponse(vsg, caller); Field fieldVirtualMachineIds = securityGroupResponse.getClass().getDeclaredField("virtualMachineIds"); fieldVirtualMachineIds.setAccessible(true); Set<String> virtualMachineIds = (Set<String>)fieldVirtualMachineIds.get(securityGroupResponse); assertEquals(1, virtualMachineIds.size()); assertTrue(virtualMachineIds.contains(uuidOne)); }
@Test public void virtualMachineIDsTwoTest() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { when(vsg.getId()).thenReturn(3L); SecurityGroupResponse securityGroupResponse = _securityGroupJoinDaoImpl.newSecurityGroupResponse(vsg, caller); Field fieldVirtualMachineIds = securityGroupResponse.getClass().getDeclaredField("virtualMachineIds"); fieldVirtualMachineIds.setAccessible(true); Set<String> virtualMachineIds = (Set<String>)fieldVirtualMachineIds.get(securityGroupResponse); assertEquals(2, virtualMachineIds.size()); assertTrue(virtualMachineIds.contains(uuidOne)); assertTrue(virtualMachineIds.contains(uuidTwo)); }