### Question:
GatewaySecurityGroupDecorator { public void extendGatewaySecurityGroupWithDefaultGatewayCidrs(Stack stack, Tunnel tunnel) { Set<InstanceGroup> gateways = filterForGateways(stack); Set<String> defaultGatewayCidrs = filterEmptyCidrs(); LOGGER.debug("Extending security group for gateways: [{}] DefaultGatewayCidrs: [{}] Tunnel: [{}]", gateways.stream().map(InstanceGroup::getGroupName).collect(Collectors.joining(", ")), defaultGatewayCidrs, tunnel); if (!defaultGatewayCidrs.isEmpty() && !tunnel.useCcm()) { extendGatewaysSecurityGroupsWithRules(stack, gateways, defaultGatewayCidrs); } }  void extendGatewaySecurityGroupWithDefaultGatewayCidrs(Stack stack, Tunnel tunnel);  }### Answer:
@Test public void testConvertExtendsGatewaySecurityGroupsWithDefaultGatewayCidrsWithoutCCM() { ReflectionTestUtils.setField(underTest, "defaultGatewayCidr", Set.of("0.0.0.0/0", "1.1.1.1/1")); ReflectionTestUtils.setField(underTest, "nginxPort", 9443); underTest.extendGatewaySecurityGroupWithDefaultGatewayCidrs(stack, Tunnel.DIRECT); Set<InstanceGroup> gateways = stack.getInstanceGroups().stream().filter(ig -> InstanceGroupType.isGateway(ig.getInstanceGroupType())).collect(Collectors.toSet()); for (InstanceGroup ig : gateways) { assertEquals(2, ig.getSecurityGroup().getSecurityRules().stream() .filter(rule -> rule.getCidr().equals("0.0.0.0/0") || rule.getCidr().equals("1.1.1.1/1") && Arrays.stream(rule.getPorts()).anyMatch(port -> port.equals("9443"))).count()); } }
@Test public void testConvertDoesntExtendGatewaySecurityGroupsWithDefaultGatewayCidrsIfItsEmptyWithoutCCM() { ReflectionTestUtils.setField(underTest, "defaultGatewayCidr", Set.of()); underTest.extendGatewaySecurityGroupWithDefaultGatewayCidrs(stack, Tunnel.DIRECT); Set<InstanceGroup> gateways = stack.getInstanceGroups().stream().filter(ig -> InstanceGroupType.isGateway(ig.getInstanceGroupType())).collect(Collectors.toSet()); for (InstanceGroup ig : gateways) { assertNull(ig.getSecurityGroup()); } }
@Test public void testConvertDoesntExtendsGatewaySecurityGroupsWithDefaultGatewayCidrsWithCCM() { ReflectionTestUtils.setField(underTest, "defaultGatewayCidr", Set.of("0.0.0.0/0", "1.1.1.1/1")); ReflectionTestUtils.setField(underTest, "nginxPort", 9443); underTest.extendGatewaySecurityGroupWithDefaultGatewayCidrs(stack, Tunnel.CCM); Set<InstanceGroup> gateways = stack.getInstanceGroups().stream().filter(ig -> InstanceGroupType.isGateway(ig.getInstanceGroupType())).collect(Collectors.toSet()); for (InstanceGroup ig : gateways) { assertNull(ig.getSecurityGroup()); } }
@Test public void testConvertDoesntExtendGatewaySecurityGroupsWithDefaultGatewayCidrsIfItsEmptyWithCCM() { ReflectionTestUtils.setField(underTest, "defaultGatewayCidr", Set.of()); underTest.extendGatewaySecurityGroupWithDefaultGatewayCidrs(stack, Tunnel.CCM); Set<InstanceGroup> gateways = stack.getInstanceGroups().stream().filter(ig -> InstanceGroupType.isGateway(ig.getInstanceGroupType())).collect(Collectors.toSet()); for (InstanceGroup ig : gateways) { assertNull(ig.getSecurityGroup()); } }