SecurityGroupProvider { public String getSecurityGroup(final List<String> securityGroupIds, final Region region, final String accountId) { DescribeSecurityGroupsResult result = null; final ObjectMapper objectMapper = new ObjectMapper(); String securityGroups = null; final AmazonEC2Client amazonEC2Client = clientProvider.getClient(AmazonEC2Client.class, accountId, region); if (amazonEC2Client == null) { throw new RuntimeException( String.format( "Somehow we could not create an Client with accountId: %s and region: %s", accountId, region.toString())); } else { try { final DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest(); request.setGroupIds(securityGroupIds); result = amazonEC2Client.describeSecurityGroups(request); } catch (final AmazonServiceException e) { if (e.getErrorCode().equals(INVALID_GROUP_NOT_FOUND)) { log.warn(e.getMessage()); } else { log.error(e.getMessage()); } return null; } try { securityGroups = objectMapper.writeValueAsString(result); } catch (final JsonProcessingException e) { log.error(e.getMessage()); return null; } return securityGroups; } } SecurityGroupProvider(final ClientProvider clientProvider); String getSecurityGroup(final List<String> securityGroupIds, final Region region, final String accountId); static final String INVALID_GROUP_NOT_FOUND; }
@Test(expected = RuntimeException.class) public void testNullAmazonClient() { when(clientProviderMock.getClient(any(), anyString(), any(Region.class))).thenReturn(null); securityGroupProvider = new SecurityGroupProvider(clientProviderMock); try { securityGroupProvider.getSecurityGroup(Lists.newArrayList("sg.1234"), REGION, "9876"); } finally { verify(clientProviderMock).getClient(any(), anyString(), any(Region.class)); } }
@Test public void testAmazonException(){ final AmazonServiceException amazonServiceException = new AmazonServiceException(""); amazonServiceException.setErrorCode("InvalidGroup.NotFound"); when(clientProviderMock.getClient(any(), anyString(), any(Region.class))).thenReturn(amazonEC2ClientMock); when(amazonEC2ClientMock.describeSecurityGroups(any(DescribeSecurityGroupsRequest.class))).thenThrow(amazonServiceException); securityGroupProvider = new SecurityGroupProvider(clientProviderMock); final String securityGroup = securityGroupProvider.getSecurityGroup(Lists.newArrayList("sg.1234"), REGION, "9876"); Assertions.assertThat(securityGroup).isEqualTo(null); verify(clientProviderMock).getClient(any(), anyString(), any(Region.class)); verify(amazonEC2ClientMock).describeSecurityGroups(any(DescribeSecurityGroupsRequest.class)); }
@Test public void testJsonException(){ final DescribeSecurityGroupsResult mockResult = spy(new DescribeSecurityGroupsResult()); when(clientProviderMock.getClient(any(), anyString(), any(Region.class))).thenReturn(amazonEC2ClientMock); when(mockResult.getSecurityGroups()).thenThrow(new IllegalStateException()); when(amazonEC2ClientMock.describeSecurityGroups(any(DescribeSecurityGroupsRequest.class))).thenReturn(mockResult); securityGroupProvider = new SecurityGroupProvider(clientProviderMock); final String securityGroup = securityGroupProvider.getSecurityGroup(Lists.newArrayList("sg.1234"), REGION, "9876"); Assertions.assertThat(securityGroup).isEqualTo(null); verify(clientProviderMock).getClient(any(), anyString(), any(Region.class)); verify(amazonEC2ClientMock).describeSecurityGroups(any(DescribeSecurityGroupsRequest.class)); }