@Test public void test()throws Exception{ Rule rules = new Rule(); rules.setName("abc"); Collection<Rule> li = new ArrayList<>(); li.add(rules); ListRulesResult listRulesResult = new ListRulesResult(); listRulesResult.setRules(li); Collection<Rule> emptyList = new ArrayList<>(); ListRulesResult emptyRulesResult = new ListRulesResult(); emptyRulesResult.setRules(emptyList); mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( true); Map<String,Object>map=new HashMap<String, Object>(); map.put("client", cloudWatchEventsClient); CheckCloudWatchEventsForAllAccountsRule spy = Mockito.spy(new CheckCloudWatchEventsForAllAccountsRule()); Mockito.doReturn(map).when((BaseRule)spy).getClientFor(anyObject(), anyString(), anyObject()); when(cloudWatchEventsClient.listRules(anyObject())).thenReturn(listRulesResult); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(cloudWatchEventsClient.listRules(anyObject())).thenReturn(emptyRulesResult); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(cloudWatchEventsClient.listRules(anyObject())).thenThrow(new RuleExecutionFailedExeption()); assertThatThrownBy( () -> cloudWatchEventsForAllAccountsRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> cloudWatchEventsForAllAccountsRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); }
@Override public RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========CheckCloudWatchEventsForAllAccountsRule started========="); Map<String, String> temp = new HashMap<>(); temp.putAll(ruleParam); temp.put("region", "us-west-2"); Map<String, Object> map = null; Annotation annotation = null; AmazonCloudWatchEventsClient cloudWatchEventsClient = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String accountName = resourceAttributes.get("accountname"); String ruleName = ruleParam.get(PacmanRuleConstants.RULE_NAME); logger.info(resourceAttributes.get("accountid")); logger.info(resourceAttributes.get("accountname")); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); Gson gson = new Gson(); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); Map<String, Object> failedType = new HashMap<>(); if (!PacmanUtils.doesAllHaveValue(severity, category, ruleName)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } try { map = getClientFor(AWSService.CLOUDWATCH_EVENTS,roleIdentifyingString, temp); cloudWatchEventsClient = (AmazonCloudWatchEventsClient) map.get(PacmanSdkConstants.CLIENT); ListRulesRequest listRulesRequest = new ListRulesRequest(); listRulesRequest.setNamePrefix(ruleName); ListRulesResult listRulesResult = cloudWatchEventsClient.listRules(listRulesRequest); String state = null; boolean isRuleEmpty = false; boolean isDisabled = false; if (listRulesResult.getRules().isEmpty()) { isRuleEmpty = true; failedType.put("ruleList", "Empty"); } if (!listRulesResult.getRules().isEmpty()) { for (Rule result : listRulesResult.getRules()) { state = result.getState(); logger.info(state); if (!"ENABLED".equals(state)) { isDisabled = true; failedType.put("ruleState", "Disabled"); } } } if (isRuleEmpty || isDisabled) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Cloud watch events from "+ accountName+ " is not been sent to designated default event bus"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON,"Cloud watch events from "+ accountName+ " is not been sent to designated default event bus!!"); issue.put("failed_reason", gson.toJson(failedType)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckCloudWatchEventsForAllAccountsRule ended with annotation : {}=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } logger.debug("========CheckCloudWatchEventsForAllAccountsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }
CheckCloudWatchEventsForAllAccountsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========CheckCloudWatchEventsForAllAccountsRule started========="); Map<String, String> temp = new HashMap<>(); temp.putAll(ruleParam); temp.put("region", "us-west-2"); Map<String, Object> map = null; Annotation annotation = null; AmazonCloudWatchEventsClient cloudWatchEventsClient = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String accountName = resourceAttributes.get("accountname"); String ruleName = ruleParam.get(PacmanRuleConstants.RULE_NAME); logger.info(resourceAttributes.get("accountid")); logger.info(resourceAttributes.get("accountname")); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); Gson gson = new Gson(); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); Map<String, Object> failedType = new HashMap<>(); if (!PacmanUtils.doesAllHaveValue(severity, category, ruleName)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } try { map = getClientFor(AWSService.CLOUDWATCH_EVENTS,roleIdentifyingString, temp); cloudWatchEventsClient = (AmazonCloudWatchEventsClient) map.get(PacmanSdkConstants.CLIENT); ListRulesRequest listRulesRequest = new ListRulesRequest(); listRulesRequest.setNamePrefix(ruleName); ListRulesResult listRulesResult = cloudWatchEventsClient.listRules(listRulesRequest); String state = null; boolean isRuleEmpty = false; boolean isDisabled = false; if (listRulesResult.getRules().isEmpty()) { isRuleEmpty = true; failedType.put("ruleList", "Empty"); } if (!listRulesResult.getRules().isEmpty()) { for (Rule result : listRulesResult.getRules()) { state = result.getState(); logger.info(state); if (!"ENABLED".equals(state)) { isDisabled = true; failedType.put("ruleState", "Disabled"); } } } if (isRuleEmpty || isDisabled) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Cloud watch events from "+ accountName+ " is not been sent to designated default event bus"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON,"Cloud watch events from "+ accountName+ " is not been sent to designated default event bus!!"); issue.put("failed_reason", gson.toJson(failedType)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckCloudWatchEventsForAllAccountsRule ended with annotation : {}=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } logger.debug("========CheckCloudWatchEventsForAllAccountsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); } }
CheckCloudWatchEventsForAllAccountsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========CheckCloudWatchEventsForAllAccountsRule started========="); Map<String, String> temp = new HashMap<>(); temp.putAll(ruleParam); temp.put("region", "us-west-2"); Map<String, Object> map = null; Annotation annotation = null; AmazonCloudWatchEventsClient cloudWatchEventsClient = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String accountName = resourceAttributes.get("accountname"); String ruleName = ruleParam.get(PacmanRuleConstants.RULE_NAME); logger.info(resourceAttributes.get("accountid")); logger.info(resourceAttributes.get("accountname")); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); Gson gson = new Gson(); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); Map<String, Object> failedType = new HashMap<>(); if (!PacmanUtils.doesAllHaveValue(severity, category, ruleName)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } try { map = getClientFor(AWSService.CLOUDWATCH_EVENTS,roleIdentifyingString, temp); cloudWatchEventsClient = (AmazonCloudWatchEventsClient) map.get(PacmanSdkConstants.CLIENT); ListRulesRequest listRulesRequest = new ListRulesRequest(); listRulesRequest.setNamePrefix(ruleName); ListRulesResult listRulesResult = cloudWatchEventsClient.listRules(listRulesRequest); String state = null; boolean isRuleEmpty = false; boolean isDisabled = false; if (listRulesResult.getRules().isEmpty()) { isRuleEmpty = true; failedType.put("ruleList", "Empty"); } if (!listRulesResult.getRules().isEmpty()) { for (Rule result : listRulesResult.getRules()) { state = result.getState(); logger.info(state); if (!"ENABLED".equals(state)) { isDisabled = true; failedType.put("ruleState", "Disabled"); } } } if (isRuleEmpty || isDisabled) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Cloud watch events from "+ accountName+ " is not been sent to designated default event bus"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON,"Cloud watch events from "+ accountName+ " is not been sent to designated default event bus!!"); issue.put("failed_reason", gson.toJson(failedType)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckCloudWatchEventsForAllAccountsRule ended with annotation : {}=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } logger.debug("========CheckCloudWatchEventsForAllAccountsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  }
CheckCloudWatchEventsForAllAccountsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========CheckCloudWatchEventsForAllAccountsRule started========="); Map<String, String> temp = new HashMap<>(); temp.putAll(ruleParam); temp.put("region", "us-west-2"); Map<String, Object> map = null; Annotation annotation = null; AmazonCloudWatchEventsClient cloudWatchEventsClient = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String accountName = resourceAttributes.get("accountname"); String ruleName = ruleParam.get(PacmanRuleConstants.RULE_NAME); logger.info(resourceAttributes.get("accountid")); logger.info(resourceAttributes.get("accountname")); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); Gson gson = new Gson(); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); Map<String, Object> failedType = new HashMap<>(); if (!PacmanUtils.doesAllHaveValue(severity, category, ruleName)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } try { map = getClientFor(AWSService.CLOUDWATCH_EVENTS,roleIdentifyingString, temp); cloudWatchEventsClient = (AmazonCloudWatchEventsClient) map.get(PacmanSdkConstants.CLIENT); ListRulesRequest listRulesRequest = new ListRulesRequest(); listRulesRequest.setNamePrefix(ruleName); ListRulesResult listRulesResult = cloudWatchEventsClient.listRules(listRulesRequest); String state = null; boolean isRuleEmpty = false; boolean isDisabled = false; if (listRulesResult.getRules().isEmpty()) { isRuleEmpty = true; failedType.put("ruleList", "Empty"); } if (!listRulesResult.getRules().isEmpty()) { for (Rule result : listRulesResult.getRules()) { state = result.getState(); logger.info(state); if (!"ENABLED".equals(state)) { isDisabled = true; failedType.put("ruleState", "Disabled"); } } } if (isRuleEmpty || isDisabled) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Cloud watch events from "+ accountName+ " is not been sent to designated default event bus"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON,"Cloud watch events from "+ accountName+ " is not been sent to designated default event bus!!"); issue.put("failed_reason", gson.toJson(failedType)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckCloudWatchEventsForAllAccountsRule ended with annotation : {}=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } logger.debug("========CheckCloudWatchEventsForAllAccountsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes); @Override String getHelpText(); }
CheckCloudWatchEventsForAllAccountsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes) { logger.debug("========CheckCloudWatchEventsForAllAccountsRule started========="); Map<String, String> temp = new HashMap<>(); temp.putAll(ruleParam); temp.put("region", "us-west-2"); Map<String, Object> map = null; Annotation annotation = null; AmazonCloudWatchEventsClient cloudWatchEventsClient = null; String roleIdentifyingString = ruleParam.get(PacmanSdkConstants.Role_IDENTIFYING_STRING); String accountName = resourceAttributes.get("accountname"); String ruleName = ruleParam.get(PacmanRuleConstants.RULE_NAME); logger.info(resourceAttributes.get("accountid")); logger.info(resourceAttributes.get("accountname")); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); Gson gson = new Gson(); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); Map<String, Object> failedType = new HashMap<>(); if (!PacmanUtils.doesAllHaveValue(severity, category, ruleName)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } try { map = getClientFor(AWSService.CLOUDWATCH_EVENTS,roleIdentifyingString, temp); cloudWatchEventsClient = (AmazonCloudWatchEventsClient) map.get(PacmanSdkConstants.CLIENT); ListRulesRequest listRulesRequest = new ListRulesRequest(); listRulesRequest.setNamePrefix(ruleName); ListRulesResult listRulesResult = cloudWatchEventsClient.listRules(listRulesRequest); String state = null; boolean isRuleEmpty = false; boolean isDisabled = false; if (listRulesResult.getRules().isEmpty()) { isRuleEmpty = true; failedType.put("ruleList", "Empty"); } if (!listRulesResult.getRules().isEmpty()) { for (Rule result : listRulesResult.getRules()) { state = result.getState(); logger.info(state); if (!"ENABLED".equals(state)) { isDisabled = true; failedType.put("ruleState", "Disabled"); } } } if (isRuleEmpty || isDisabled) { annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,"Cloud watch events from "+ accountName+ " is not been sent to designated default event bus"); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON,"Cloud watch events from "+ accountName+ " is not been sent to designated default event bus!!"); issue.put("failed_reason", gson.toJson(failedType)); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========CheckCloudWatchEventsForAllAccountsRule ended with annotation : {}=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (UnableToCreateClientException e) { logger.error("unable to get client for following input", e); throw new InvalidInputException(e.toString()); } logger.debug("========CheckCloudWatchEventsForAllAccountsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS,PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam,Map<String, String> resourceAttributes); @Override String getHelpText();  }