@Test public void executeTest() throws Exception { mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( true); assertThat(ec2StoppedInstanceForLongRule.execute(CommonTestUtils.getAnotherMapString("r_123 "),CommonTestUtils.getAnotherMapString("r_123 ")), is(notNullValue())); assertThat(ec2StoppedInstanceForLongRule.execute(CommonTestUtils.getAnotherMapString("r_123 "),CommonTestUtils.getCommonMapString("r_123 ")), is(notNullValue())); assertThat(ec2StoppedInstanceForLongRule.execute(CommonTestUtils.getAnotherMapString("r_123 "),null), is(notNullValue())); assertThatThrownBy( () -> ec2StoppedInstanceForLongRule.execute(CommonTestUtils.getAnotherMapString("r_123 "),CommonTestUtils.getOneMoreMapString("r_123 "))).isInstanceOf(RuleExecutionFailedExeption.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> ec2StoppedInstanceForLongRule.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("========EC2StoppedInstanceForLongRule started========="); String targetstoppedDurationInString = ruleParam .get(PacmanRuleConstants.STOPPED_DURATION); String state = null; String reasonLogged = null; long stoppedDuration = 0L; long targetStoppedDuration = 0L; Annotation annotation = null; 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)); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(targetstoppedDurationInString, severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException( PacmanRuleConstants.MISSING_CONFIGURATION); } if (null != resourceAttributes && resourceAttributes.containsKey(PacmanRuleConstants.STATE_NAME) && resourceAttributes.get(PacmanRuleConstants.STATE_NAME) .equalsIgnoreCase(PacmanRuleConstants.STOPPED_INSTANCE)) { String instanceId = resourceAttributes .get(PacmanRuleConstants.INSTANCEID); if (!StringUtils.isNullOrEmpty(resourceAttributes .get(PacmanRuleConstants.STATE_TRANSITION_REASON))) { reasonLogged = resourceAttributes .get(PacmanRuleConstants.STATE_TRANSITION_REASON); state = resourceAttributes.get(PacmanRuleConstants.STATE_NAME); if (!StringUtils.isNullOrEmpty(state) && state.equalsIgnoreCase(PacmanRuleConstants.STOPPED_INSTANCE) && !StringUtils.isNullOrEmpty(reasonLogged)) { String extractedDateString = getDate(reasonLogged); stoppedDuration = calculateStoppedDuration(getFomatedDate(extractedDateString)); targetStoppedDuration = Long .parseLong((targetstoppedDurationInString)); logger.debug(" Instance been stopped for {} days ", stoppedDuration); } } else { logger.error("Unable to determine as statetransitionreason is empty "); throw new RuleExecutionFailedExeption("Unable to determine"); } if (stoppedDuration > targetStoppedDuration) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Instance been stopped for " + stoppedDuration + " days"); issue.put(PacmanRuleConstants.VIOLATION_REASON, "ResourceId " + instanceId + "been stopped for " + stoppedDuration + " days"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.SUBTYPE, Annotation.Type.RECOMMENDATION.toString()); annotation.put(PacmanRuleConstants.CATEGORY, category); logger.debug( "========EC2StoppedInstanceForLongRule ended with an annotation {}=========", annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } else { logger.debug("Instance is running state : {} ", ruleParam.get(PacmanSdkConstants.RESOURCE_ID)); } logger.debug("========EC2StoppedInstanceForLongRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }
EC2StoppedInstanceForLongRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========EC2StoppedInstanceForLongRule started========="); String targetstoppedDurationInString = ruleParam .get(PacmanRuleConstants.STOPPED_DURATION); String state = null; String reasonLogged = null; long stoppedDuration = 0L; long targetStoppedDuration = 0L; Annotation annotation = null; 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)); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(targetstoppedDurationInString, severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException( PacmanRuleConstants.MISSING_CONFIGURATION); } if (null != resourceAttributes && resourceAttributes.containsKey(PacmanRuleConstants.STATE_NAME) && resourceAttributes.get(PacmanRuleConstants.STATE_NAME) .equalsIgnoreCase(PacmanRuleConstants.STOPPED_INSTANCE)) { String instanceId = resourceAttributes .get(PacmanRuleConstants.INSTANCEID); if (!StringUtils.isNullOrEmpty(resourceAttributes .get(PacmanRuleConstants.STATE_TRANSITION_REASON))) { reasonLogged = resourceAttributes .get(PacmanRuleConstants.STATE_TRANSITION_REASON); state = resourceAttributes.get(PacmanRuleConstants.STATE_NAME); if (!StringUtils.isNullOrEmpty(state) && state.equalsIgnoreCase(PacmanRuleConstants.STOPPED_INSTANCE) && !StringUtils.isNullOrEmpty(reasonLogged)) { String extractedDateString = getDate(reasonLogged); stoppedDuration = calculateStoppedDuration(getFomatedDate(extractedDateString)); targetStoppedDuration = Long .parseLong((targetstoppedDurationInString)); logger.debug(" Instance been stopped for {} days ", stoppedDuration); } } else { logger.error("Unable to determine as statetransitionreason is empty "); throw new RuleExecutionFailedExeption("Unable to determine"); } if (stoppedDuration > targetStoppedDuration) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Instance been stopped for " + stoppedDuration + " days"); issue.put(PacmanRuleConstants.VIOLATION_REASON, "ResourceId " + instanceId + "been stopped for " + stoppedDuration + " days"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.SUBTYPE, Annotation.Type.RECOMMENDATION.toString()); annotation.put(PacmanRuleConstants.CATEGORY, category); logger.debug( "========EC2StoppedInstanceForLongRule ended with an annotation {}=========", annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } else { logger.debug("Instance is running state : {} ", ruleParam.get(PacmanSdkConstants.RESOURCE_ID)); } logger.debug("========EC2StoppedInstanceForLongRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); } }
EC2StoppedInstanceForLongRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========EC2StoppedInstanceForLongRule started========="); String targetstoppedDurationInString = ruleParam .get(PacmanRuleConstants.STOPPED_DURATION); String state = null; String reasonLogged = null; long stoppedDuration = 0L; long targetStoppedDuration = 0L; Annotation annotation = null; 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)); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(targetstoppedDurationInString, severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException( PacmanRuleConstants.MISSING_CONFIGURATION); } if (null != resourceAttributes && resourceAttributes.containsKey(PacmanRuleConstants.STATE_NAME) && resourceAttributes.get(PacmanRuleConstants.STATE_NAME) .equalsIgnoreCase(PacmanRuleConstants.STOPPED_INSTANCE)) { String instanceId = resourceAttributes .get(PacmanRuleConstants.INSTANCEID); if (!StringUtils.isNullOrEmpty(resourceAttributes .get(PacmanRuleConstants.STATE_TRANSITION_REASON))) { reasonLogged = resourceAttributes .get(PacmanRuleConstants.STATE_TRANSITION_REASON); state = resourceAttributes.get(PacmanRuleConstants.STATE_NAME); if (!StringUtils.isNullOrEmpty(state) && state.equalsIgnoreCase(PacmanRuleConstants.STOPPED_INSTANCE) && !StringUtils.isNullOrEmpty(reasonLogged)) { String extractedDateString = getDate(reasonLogged); stoppedDuration = calculateStoppedDuration(getFomatedDate(extractedDateString)); targetStoppedDuration = Long .parseLong((targetstoppedDurationInString)); logger.debug(" Instance been stopped for {} days ", stoppedDuration); } } else { logger.error("Unable to determine as statetransitionreason is empty "); throw new RuleExecutionFailedExeption("Unable to determine"); } if (stoppedDuration > targetStoppedDuration) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Instance been stopped for " + stoppedDuration + " days"); issue.put(PacmanRuleConstants.VIOLATION_REASON, "ResourceId " + instanceId + "been stopped for " + stoppedDuration + " days"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.SUBTYPE, Annotation.Type.RECOMMENDATION.toString()); annotation.put(PacmanRuleConstants.CATEGORY, category); logger.debug( "========EC2StoppedInstanceForLongRule ended with an annotation {}=========", annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } else { logger.debug("Instance is running state : {} ", ruleParam.get(PacmanSdkConstants.RESOURCE_ID)); } logger.debug("========EC2StoppedInstanceForLongRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  }
EC2StoppedInstanceForLongRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========EC2StoppedInstanceForLongRule started========="); String targetstoppedDurationInString = ruleParam .get(PacmanRuleConstants.STOPPED_DURATION); String state = null; String reasonLogged = null; long stoppedDuration = 0L; long targetStoppedDuration = 0L; Annotation annotation = null; 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)); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(targetstoppedDurationInString, severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException( PacmanRuleConstants.MISSING_CONFIGURATION); } if (null != resourceAttributes && resourceAttributes.containsKey(PacmanRuleConstants.STATE_NAME) && resourceAttributes.get(PacmanRuleConstants.STATE_NAME) .equalsIgnoreCase(PacmanRuleConstants.STOPPED_INSTANCE)) { String instanceId = resourceAttributes .get(PacmanRuleConstants.INSTANCEID); if (!StringUtils.isNullOrEmpty(resourceAttributes .get(PacmanRuleConstants.STATE_TRANSITION_REASON))) { reasonLogged = resourceAttributes .get(PacmanRuleConstants.STATE_TRANSITION_REASON); state = resourceAttributes.get(PacmanRuleConstants.STATE_NAME); if (!StringUtils.isNullOrEmpty(state) && state.equalsIgnoreCase(PacmanRuleConstants.STOPPED_INSTANCE) && !StringUtils.isNullOrEmpty(reasonLogged)) { String extractedDateString = getDate(reasonLogged); stoppedDuration = calculateStoppedDuration(getFomatedDate(extractedDateString)); targetStoppedDuration = Long .parseLong((targetstoppedDurationInString)); logger.debug(" Instance been stopped for {} days ", stoppedDuration); } } else { logger.error("Unable to determine as statetransitionreason is empty "); throw new RuleExecutionFailedExeption("Unable to determine"); } if (stoppedDuration > targetStoppedDuration) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Instance been stopped for " + stoppedDuration + " days"); issue.put(PacmanRuleConstants.VIOLATION_REASON, "ResourceId " + instanceId + "been stopped for " + stoppedDuration + " days"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.SUBTYPE, Annotation.Type.RECOMMENDATION.toString()); annotation.put(PacmanRuleConstants.CATEGORY, category); logger.debug( "========EC2StoppedInstanceForLongRule ended with an annotation {}=========", annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } else { logger.debug("Instance is running state : {} ", ruleParam.get(PacmanSdkConstants.RESOURCE_ID)); } logger.debug("========EC2StoppedInstanceForLongRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); String getHelpText(); }
EC2StoppedInstanceForLongRule extends BaseRule { public RuleResult execute(final Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========EC2StoppedInstanceForLongRule started========="); String targetstoppedDurationInString = ruleParam .get(PacmanRuleConstants.STOPPED_DURATION); String state = null; String reasonLogged = null; long stoppedDuration = 0L; long targetStoppedDuration = 0L; Annotation annotation = null; 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)); List<LinkedHashMap<String, Object>> issueList = new ArrayList<>(); LinkedHashMap<String, Object> issue = new LinkedHashMap<>(); if (!PacmanUtils.doesAllHaveValue(targetstoppedDurationInString, severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException( PacmanRuleConstants.MISSING_CONFIGURATION); } if (null != resourceAttributes && resourceAttributes.containsKey(PacmanRuleConstants.STATE_NAME) && resourceAttributes.get(PacmanRuleConstants.STATE_NAME) .equalsIgnoreCase(PacmanRuleConstants.STOPPED_INSTANCE)) { String instanceId = resourceAttributes .get(PacmanRuleConstants.INSTANCEID); if (!StringUtils.isNullOrEmpty(resourceAttributes .get(PacmanRuleConstants.STATE_TRANSITION_REASON))) { reasonLogged = resourceAttributes .get(PacmanRuleConstants.STATE_TRANSITION_REASON); state = resourceAttributes.get(PacmanRuleConstants.STATE_NAME); if (!StringUtils.isNullOrEmpty(state) && state.equalsIgnoreCase(PacmanRuleConstants.STOPPED_INSTANCE) && !StringUtils.isNullOrEmpty(reasonLogged)) { String extractedDateString = getDate(reasonLogged); stoppedDuration = calculateStoppedDuration(getFomatedDate(extractedDateString)); targetStoppedDuration = Long .parseLong((targetstoppedDurationInString)); logger.debug(" Instance been stopped for {} days ", stoppedDuration); } } else { logger.error("Unable to determine as statetransitionreason is empty "); throw new RuleExecutionFailedExeption("Unable to determine"); } if (stoppedDuration > targetStoppedDuration) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation.put(PacmanSdkConstants.DESCRIPTION, "Instance been stopped for " + stoppedDuration + " days"); issue.put(PacmanRuleConstants.VIOLATION_REASON, "ResourceId " + instanceId + "been stopped for " + stoppedDuration + " days"); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.SUBTYPE, Annotation.Type.RECOMMENDATION.toString()); annotation.put(PacmanRuleConstants.CATEGORY, category); logger.debug( "========EC2StoppedInstanceForLongRule ended with an annotation {}=========", annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } else { logger.debug("Instance is running state : {} ", ruleParam.get(PacmanSdkConstants.RESOURCE_ID)); } logger.debug("========EC2StoppedInstanceForLongRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  RuleResult execute(final Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); String getHelpText();  }