@SuppressWarnings("static-access") @Test public void fetchSNSTopicsTest() throws Exception { mockStatic(AmazonSNSClientBuilder.class); AmazonSNSClient snsClient = PowerMockito.mock(AmazonSNSClient.class); AmazonSNSClientBuilder amazonSNSClientBuilder = PowerMockito.mock(AmazonSNSClientBuilder.class); AWSStaticCredentialsProvider awsStaticCredentialsProvider = PowerMockito.mock(AWSStaticCredentialsProvider.class); PowerMockito.whenNew(AWSStaticCredentialsProvider.class).withAnyArguments().thenReturn(awsStaticCredentialsProvider); when(amazonSNSClientBuilder.standard()).thenReturn(amazonSNSClientBuilder); when(amazonSNSClientBuilder.withCredentials(anyObject())).thenReturn(amazonSNSClientBuilder); when(amazonSNSClientBuilder.withRegion(anyString())).thenReturn(amazonSNSClientBuilder); when(amazonSNSClientBuilder.build()).thenReturn(snsClient); ListSubscriptionsResult listSubscriptionDefinitionsResult = new ListSubscriptionsResult(); List<Subscription> subscriptionList = new ArrayList<>(); subscriptionList.add(new Subscription()); listSubscriptionDefinitionsResult.setSubscriptions(subscriptionList); when(snsClient.listSubscriptions( new ListSubscriptionsRequest())).thenReturn(listSubscriptionDefinitionsResult); assertThat(snsInventoryUtil.fetchSNSTopics(new BasicSessionCredentials("awsAccessKey", "awsSecretKey", "sessionToken"), "skipRegions", "account","accountName").size(), is(1)); }
public static Map<String,List<Topic>> fetchSNSTopics(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) { Map<String,List<Topic>> topicMap = new LinkedHashMap<>(); AmazonSNSClient snsClient ; String expPrefix = "{\"errcode\": \"NO_RES_REG\" ,\"accountId\": \""+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"snstopic\" , \"region\":\"" ; for(Region region : RegionUtils.getRegions()) { try{ if(!skipRegions.contains(region.getName())){ List<Topic> topiList = new ArrayList<>(); snsClient = (AmazonSNSClient) AmazonSNSClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); String nextToken = null; ListTopicsResult listTopicResult; do { listTopicResult = snsClient.listTopics(new ListTopicsRequest().withNextToken(nextToken)); topiList.addAll(listTopicResult.getTopics()); nextToken = listTopicResult.getNextToken(); }while(nextToken!=null); if(!topiList.isEmpty() ) { log.debug("Account : " + accountId + " Type : SNS Topics "+ region.getName()+" >> " + topiList.size()); topicMap.put(accountId+delimiter+accountName+delimiter+region.getName(), topiList); } } }catch(Exception e){ log.warn(expPrefix+ region.getName()+"\", \"cause\":\"" +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"snstopic",e.getMessage()); } } return topicMap; }
SNSInventoryUtil { public static Map<String,List<Topic>> fetchSNSTopics(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) { Map<String,List<Topic>> topicMap = new LinkedHashMap<>(); AmazonSNSClient snsClient ; String expPrefix = "{\"errcode\": \"NO_RES_REG\" ,\"accountId\": \""+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"snstopic\" , \"region\":\"" ; for(Region region : RegionUtils.getRegions()) { try{ if(!skipRegions.contains(region.getName())){ List<Topic> topiList = new ArrayList<>(); snsClient = (AmazonSNSClient) AmazonSNSClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); String nextToken = null; ListTopicsResult listTopicResult; do { listTopicResult = snsClient.listTopics(new ListTopicsRequest().withNextToken(nextToken)); topiList.addAll(listTopicResult.getTopics()); nextToken = listTopicResult.getNextToken(); }while(nextToken!=null); if(!topiList.isEmpty() ) { log.debug("Account : " + accountId + " Type : SNS Topics "+ region.getName()+" >> " + topiList.size()); topicMap.put(accountId+delimiter+accountName+delimiter+region.getName(), topiList); } } }catch(Exception e){ log.warn(expPrefix+ region.getName()+"\", \"cause\":\"" +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"snstopic",e.getMessage()); } } return topicMap; } }
SNSInventoryUtil { public static Map<String,List<Topic>> fetchSNSTopics(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) { Map<String,List<Topic>> topicMap = new LinkedHashMap<>(); AmazonSNSClient snsClient ; String expPrefix = "{\"errcode\": \"NO_RES_REG\" ,\"accountId\": \""+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"snstopic\" , \"region\":\"" ; for(Region region : RegionUtils.getRegions()) { try{ if(!skipRegions.contains(region.getName())){ List<Topic> topiList = new ArrayList<>(); snsClient = (AmazonSNSClient) AmazonSNSClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); String nextToken = null; ListTopicsResult listTopicResult; do { listTopicResult = snsClient.listTopics(new ListTopicsRequest().withNextToken(nextToken)); topiList.addAll(listTopicResult.getTopics()); nextToken = listTopicResult.getNextToken(); }while(nextToken!=null); if(!topiList.isEmpty() ) { log.debug("Account : " + accountId + " Type : SNS Topics "+ region.getName()+" >> " + topiList.size()); topicMap.put(accountId+delimiter+accountName+delimiter+region.getName(), topiList); } } }catch(Exception e){ log.warn(expPrefix+ region.getName()+"\", \"cause\":\"" +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"snstopic",e.getMessage()); } } return topicMap; } private  SNSInventoryUtil(); }
SNSInventoryUtil { public static Map<String,List<Topic>> fetchSNSTopics(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) { Map<String,List<Topic>> topicMap = new LinkedHashMap<>(); AmazonSNSClient snsClient ; String expPrefix = "{\"errcode\": \"NO_RES_REG\" ,\"accountId\": \""+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"snstopic\" , \"region\":\"" ; for(Region region : RegionUtils.getRegions()) { try{ if(!skipRegions.contains(region.getName())){ List<Topic> topiList = new ArrayList<>(); snsClient = (AmazonSNSClient) AmazonSNSClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); String nextToken = null; ListTopicsResult listTopicResult; do { listTopicResult = snsClient.listTopics(new ListTopicsRequest().withNextToken(nextToken)); topiList.addAll(listTopicResult.getTopics()); nextToken = listTopicResult.getNextToken(); }while(nextToken!=null); if(!topiList.isEmpty() ) { log.debug("Account : " + accountId + " Type : SNS Topics "+ region.getName()+" >> " + topiList.size()); topicMap.put(accountId+delimiter+accountName+delimiter+region.getName(), topiList); } } }catch(Exception e){ log.warn(expPrefix+ region.getName()+"\", \"cause\":\"" +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"snstopic",e.getMessage()); } } return topicMap; } private  SNSInventoryUtil(); static Map<String,List<Topic>> fetchSNSTopics(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName); }
SNSInventoryUtil { public static Map<String,List<Topic>> fetchSNSTopics(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName) { Map<String,List<Topic>> topicMap = new LinkedHashMap<>(); AmazonSNSClient snsClient ; String expPrefix = "{\"errcode\": \"NO_RES_REG\" ,\"accountId\": \""+accountId + "\",\"Message\": \"Exception in fetching info for resource in specific region\" ,\"type\": \"snstopic\" , \"region\":\"" ; for(Region region : RegionUtils.getRegions()) { try{ if(!skipRegions.contains(region.getName())){ List<Topic> topiList = new ArrayList<>(); snsClient = (AmazonSNSClient) AmazonSNSClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(temporaryCredentials)).withRegion(region.getName()).build(); String nextToken = null; ListTopicsResult listTopicResult; do { listTopicResult = snsClient.listTopics(new ListTopicsRequest().withNextToken(nextToken)); topiList.addAll(listTopicResult.getTopics()); nextToken = listTopicResult.getNextToken(); }while(nextToken!=null); if(!topiList.isEmpty() ) { log.debug("Account : " + accountId + " Type : SNS Topics "+ region.getName()+" >> " + topiList.size()); topicMap.put(accountId+delimiter+accountName+delimiter+region.getName(), topiList); } } }catch(Exception e){ log.warn(expPrefix+ region.getName()+"\", \"cause\":\"" +e.getMessage()+"\"}"); ErrorManageUtil.uploadError(accountId,region.getName(),"snstopic",e.getMessage()); } } return topicMap; } private  SNSInventoryUtil(); static Map<String,List<Topic>> fetchSNSTopics(BasicSessionCredentials temporaryCredentials, String skipRegions,String accountId,String accountName);  }