@Test public void executeTest() throws Exception { mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(),anyString())).thenReturn( true); when(PacmanUtils.splitStringToAList(anyString(),anyString())).thenReturn(CommonTestUtils.getListString()); assertThat(nonStandardRegionsRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.isNonStandardRegion(anyObject(),anyObject())).thenReturn(true); assertThat(nonStandardRegionsRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); assertThat(nonStandardRegionsRule.execute(CommonTestUtils.getOneMoreMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")), is(notNullValue())); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> nonStandardRegionsRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); }
public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========NonStandardRegionsRule started========="); boolean isNonStandardRegion = false; String standardRegions = ruleParam.get(PacmanRuleConstants.STANDARD_REGIONS); String tagsSplitter = ruleParam.get(PacmanSdkConstants.SPLITTER_CHAR); String region = ruleParam.get(PacmanSdkConstants.REGION); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String targetType = ruleParam.get(PacmanRuleConstants.TARGET_TYPE); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); if (!PacmanUtils.doesAllHaveValue(standardRegions, tagsSplitter, severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } List<String> standardRegionsList = PacmanUtils.splitStringToAList(standardRegions, tagsSplitter); if (resourceAttributes != null) { if (targetType.equalsIgnoreCase(PacmanRuleConstants.TARGET_TYPE_EC2)) { if (resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.RUNNING_STATE) || resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.STOPPED_STATE)) { isNonStandardRegion = PacmanUtils.isNonStandardRegion(standardRegionsList, region); } } else { isNonStandardRegion = PacmanUtils.isNonStandardRegion(standardRegionsList, region); } } if (!isNonStandardRegion) { String description = "Non standard Region for " + targetType + " is " + region; List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); Annotation annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,description); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON,description); issueList.add(issue); annotation.put(PacmanRuleConstants.ISSUE_DETAILS,issueList.toString()); logger.debug("========NonStandardRegionsRule ended with an annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE,annotation); } else { logger.info(targetType, " has standard region"); } logger.debug("========NonStandardRegionsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }
NonStandardRegionsRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========NonStandardRegionsRule started========="); boolean isNonStandardRegion = false; String standardRegions = ruleParam.get(PacmanRuleConstants.STANDARD_REGIONS); String tagsSplitter = ruleParam.get(PacmanSdkConstants.SPLITTER_CHAR); String region = ruleParam.get(PacmanSdkConstants.REGION); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String targetType = ruleParam.get(PacmanRuleConstants.TARGET_TYPE); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); if (!PacmanUtils.doesAllHaveValue(standardRegions, tagsSplitter, severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } List<String> standardRegionsList = PacmanUtils.splitStringToAList(standardRegions, tagsSplitter); if (resourceAttributes != null) { if (targetType.equalsIgnoreCase(PacmanRuleConstants.TARGET_TYPE_EC2)) { if (resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.RUNNING_STATE) || resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.STOPPED_STATE)) { isNonStandardRegion = PacmanUtils.isNonStandardRegion(standardRegionsList, region); } } else { isNonStandardRegion = PacmanUtils.isNonStandardRegion(standardRegionsList, region); } } if (!isNonStandardRegion) { String description = "Non standard Region for " + targetType + " is " + region; List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); Annotation annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,description); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON,description); issueList.add(issue); annotation.put(PacmanRuleConstants.ISSUE_DETAILS,issueList.toString()); logger.debug("========NonStandardRegionsRule ended with an annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE,annotation); } else { logger.info(targetType, " has standard region"); } logger.debug("========NonStandardRegionsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); } }
NonStandardRegionsRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========NonStandardRegionsRule started========="); boolean isNonStandardRegion = false; String standardRegions = ruleParam.get(PacmanRuleConstants.STANDARD_REGIONS); String tagsSplitter = ruleParam.get(PacmanSdkConstants.SPLITTER_CHAR); String region = ruleParam.get(PacmanSdkConstants.REGION); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String targetType = ruleParam.get(PacmanRuleConstants.TARGET_TYPE); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); if (!PacmanUtils.doesAllHaveValue(standardRegions, tagsSplitter, severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } List<String> standardRegionsList = PacmanUtils.splitStringToAList(standardRegions, tagsSplitter); if (resourceAttributes != null) { if (targetType.equalsIgnoreCase(PacmanRuleConstants.TARGET_TYPE_EC2)) { if (resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.RUNNING_STATE) || resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.STOPPED_STATE)) { isNonStandardRegion = PacmanUtils.isNonStandardRegion(standardRegionsList, region); } } else { isNonStandardRegion = PacmanUtils.isNonStandardRegion(standardRegionsList, region); } } if (!isNonStandardRegion) { String description = "Non standard Region for " + targetType + " is " + region; List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); Annotation annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,description); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON,description); issueList.add(issue); annotation.put(PacmanRuleConstants.ISSUE_DETAILS,issueList.toString()); logger.debug("========NonStandardRegionsRule ended with an annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE,annotation); } else { logger.info(targetType, " has standard region"); } logger.debug("========NonStandardRegionsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  }
NonStandardRegionsRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========NonStandardRegionsRule started========="); boolean isNonStandardRegion = false; String standardRegions = ruleParam.get(PacmanRuleConstants.STANDARD_REGIONS); String tagsSplitter = ruleParam.get(PacmanSdkConstants.SPLITTER_CHAR); String region = ruleParam.get(PacmanSdkConstants.REGION); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String targetType = ruleParam.get(PacmanRuleConstants.TARGET_TYPE); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); if (!PacmanUtils.doesAllHaveValue(standardRegions, tagsSplitter, severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } List<String> standardRegionsList = PacmanUtils.splitStringToAList(standardRegions, tagsSplitter); if (resourceAttributes != null) { if (targetType.equalsIgnoreCase(PacmanRuleConstants.TARGET_TYPE_EC2)) { if (resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.RUNNING_STATE) || resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.STOPPED_STATE)) { isNonStandardRegion = PacmanUtils.isNonStandardRegion(standardRegionsList, region); } } else { isNonStandardRegion = PacmanUtils.isNonStandardRegion(standardRegionsList, region); } } if (!isNonStandardRegion) { String description = "Non standard Region for " + targetType + " is " + region; List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); Annotation annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,description); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON,description); issueList.add(issue); annotation.put(PacmanRuleConstants.ISSUE_DETAILS,issueList.toString()); logger.debug("========NonStandardRegionsRule ended with an annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE,annotation); } else { logger.info(targetType, " has standard region"); } logger.debug("========NonStandardRegionsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes); String getHelpText(); }
NonStandardRegionsRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========NonStandardRegionsRule started========="); boolean isNonStandardRegion = false; String standardRegions = ruleParam.get(PacmanRuleConstants.STANDARD_REGIONS); String tagsSplitter = ruleParam.get(PacmanSdkConstants.SPLITTER_CHAR); String region = ruleParam.get(PacmanSdkConstants.REGION); String severity = ruleParam.get(PacmanRuleConstants.SEVERITY); String category = ruleParam.get(PacmanRuleConstants.CATEGORY); String targetType = ruleParam.get(PacmanRuleConstants.TARGET_TYPE); MDC.put("executionId", ruleParam.get("executionId")); MDC.put("ruleId", ruleParam.get(PacmanSdkConstants.RULE_ID)); if (!PacmanUtils.doesAllHaveValue(standardRegions, tagsSplitter, severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } List<String> standardRegionsList = PacmanUtils.splitStringToAList(standardRegions, tagsSplitter); if (resourceAttributes != null) { if (targetType.equalsIgnoreCase(PacmanRuleConstants.TARGET_TYPE_EC2)) { if (resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.RUNNING_STATE) || resourceAttributes.get(PacmanRuleConstants.STATE_NAME).equalsIgnoreCase(PacmanRuleConstants.STOPPED_STATE)) { isNonStandardRegion = PacmanUtils.isNonStandardRegion(standardRegionsList, region); } } else { isNonStandardRegion = PacmanUtils.isNonStandardRegion(standardRegionsList, region); } } if (!isNonStandardRegion) { String description = "Non standard Region for " + targetType + " is " + region; List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); Annotation annotation = Annotation.buildAnnotation(ruleParam,Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION,description); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON,description); issueList.add(issue); annotation.put(PacmanRuleConstants.ISSUE_DETAILS,issueList.toString()); logger.debug("========NonStandardRegionsRule ended with an annotation {} : =========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE,PacmanRuleConstants.FAILURE_MESSAGE,annotation); } else { logger.info(targetType, " has standard region"); } logger.debug("========NonStandardRegionsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes); String getHelpText();  }