@Test public void test()throws Exception{ Datapoint dt = new Datapoint(); dt.setTimestamp(new Date()); dt.setSum(50.00); Collection<Datapoint> li = new ArrayList<>(); li.add(dt); GetMetricStatisticsResult result = new GetMetricStatisticsResult(); result.setDatapoints(li); Collection<Datapoint> emptyList = new ArrayList<>(); GetMetricStatisticsResult emptyDetectorsResult = new GetMetricStatisticsResult(); emptyDetectorsResult.setDatapoints(emptyList); mockStatic(PacmanUtils.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( true); Map<String,Object>map=new HashMap<String, Object>(); map.put("client", cloudWatchClient); LambdaFunctionThrottleInvocationsRule spy = Mockito.spy(new LambdaFunctionThrottleInvocationsRule()); Mockito.doReturn(map).when((BaseRule)spy).getClientFor(anyObject(), anyString(), anyObject()); when(cloudWatchClient.getMetricStatistics(anyObject())).thenReturn(result); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(cloudWatchClient.getMetricStatistics(anyObject())).thenReturn(emptyDetectorsResult); spy.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 ")); when(cloudWatchClient.getMetricStatistics(anyObject())).thenThrow(new InvalidInputException()); assertThatThrownBy( () -> functionThrottleInvocationsRule.execute(CommonTestUtils.getMapString("r_123 "),CommonTestUtils.getMapString("r_123 "))).isInstanceOf(InvalidInputException.class); when(PacmanUtils.doesAllHaveValue(anyString(),anyString(),anyString())).thenReturn( false); assertThatThrownBy( () -> functionThrottleInvocationsRule.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("========LambdaFunctionThrottleInvocationsRule started========="); if (resourceAttributes != null && !StringUtils.isNullOrEmpty(resourceAttributes .get(PacmanRuleConstants.FUNCTION_NAME_RES_ATTR))) { String resourceId = resourceAttributes .get(PacmanRuleConstants.FUNCTION_NAME_RES_ATTR); String roleIdentifyingString = ruleParam .get(PacmanSdkConstants.Role_IDENTIFYING_STRING); AmazonCloudWatchClient cloudWatchClient = null; Annotation annotation = null; int invocationSum = 0; int targetThreshold = Integer.parseInt(ruleParam .get(PacmanRuleConstants.THRESHOLD)); 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(String.valueOf(targetThreshold), severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } try { cloudWatchClient = getClient(roleIdentifyingString, ruleParam); final GetMetricStatisticsRequest request = request(resourceId); final GetMetricStatisticsResult result = cloudWatchClient .getMetricStatistics(request); for (final Datapoint dataPoint : result.getDatapoints()) { invocationSum = invocationSum + dataPoint.getSum().intValue(); } if (invocationSum > targetThreshold) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation .put(PacmanSdkConstants.DESCRIPTION, "Lambda function throttle invocation count exceeded target threshold"); annotation.put(PacmanRuleConstants.LAMBDA_INVOCATION_COUNT, ":" + invocationSum); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Lambda function throttle invocation count exceeded target threshold"); issue.put(PacmanRuleConstants.THRESHOLD, targetThreshold); issue.put("invocation_sum", invocationSum); issue.put(PacmanRuleConstants.TIME_PERIOD_HRS, TWENTYFOURHOURS); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========LambdaFunctionThrottleInvocationsRule ended with an annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (InvalidInputException e) { logger.error(e.toString()); throw new InvalidInputException(e.getMessage()); } } logger.debug("========LambdaFunctionThrottleInvocationsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }
LambdaFunctionThrottleInvocationsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========LambdaFunctionThrottleInvocationsRule started========="); if (resourceAttributes != null && !StringUtils.isNullOrEmpty(resourceAttributes .get(PacmanRuleConstants.FUNCTION_NAME_RES_ATTR))) { String resourceId = resourceAttributes .get(PacmanRuleConstants.FUNCTION_NAME_RES_ATTR); String roleIdentifyingString = ruleParam .get(PacmanSdkConstants.Role_IDENTIFYING_STRING); AmazonCloudWatchClient cloudWatchClient = null; Annotation annotation = null; int invocationSum = 0; int targetThreshold = Integer.parseInt(ruleParam .get(PacmanRuleConstants.THRESHOLD)); 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(String.valueOf(targetThreshold), severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } try { cloudWatchClient = getClient(roleIdentifyingString, ruleParam); final GetMetricStatisticsRequest request = request(resourceId); final GetMetricStatisticsResult result = cloudWatchClient .getMetricStatistics(request); for (final Datapoint dataPoint : result.getDatapoints()) { invocationSum = invocationSum + dataPoint.getSum().intValue(); } if (invocationSum > targetThreshold) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation .put(PacmanSdkConstants.DESCRIPTION, "Lambda function throttle invocation count exceeded target threshold"); annotation.put(PacmanRuleConstants.LAMBDA_INVOCATION_COUNT, ":" + invocationSum); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Lambda function throttle invocation count exceeded target threshold"); issue.put(PacmanRuleConstants.THRESHOLD, targetThreshold); issue.put("invocation_sum", invocationSum); issue.put(PacmanRuleConstants.TIME_PERIOD_HRS, TWENTYFOURHOURS); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========LambdaFunctionThrottleInvocationsRule ended with an annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (InvalidInputException e) { logger.error(e.toString()); throw new InvalidInputException(e.getMessage()); } } logger.debug("========LambdaFunctionThrottleInvocationsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); } }
LambdaFunctionThrottleInvocationsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========LambdaFunctionThrottleInvocationsRule started========="); if (resourceAttributes != null && !StringUtils.isNullOrEmpty(resourceAttributes .get(PacmanRuleConstants.FUNCTION_NAME_RES_ATTR))) { String resourceId = resourceAttributes .get(PacmanRuleConstants.FUNCTION_NAME_RES_ATTR); String roleIdentifyingString = ruleParam .get(PacmanSdkConstants.Role_IDENTIFYING_STRING); AmazonCloudWatchClient cloudWatchClient = null; Annotation annotation = null; int invocationSum = 0; int targetThreshold = Integer.parseInt(ruleParam .get(PacmanRuleConstants.THRESHOLD)); 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(String.valueOf(targetThreshold), severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } try { cloudWatchClient = getClient(roleIdentifyingString, ruleParam); final GetMetricStatisticsRequest request = request(resourceId); final GetMetricStatisticsResult result = cloudWatchClient .getMetricStatistics(request); for (final Datapoint dataPoint : result.getDatapoints()) { invocationSum = invocationSum + dataPoint.getSum().intValue(); } if (invocationSum > targetThreshold) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation .put(PacmanSdkConstants.DESCRIPTION, "Lambda function throttle invocation count exceeded target threshold"); annotation.put(PacmanRuleConstants.LAMBDA_INVOCATION_COUNT, ":" + invocationSum); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Lambda function throttle invocation count exceeded target threshold"); issue.put(PacmanRuleConstants.THRESHOLD, targetThreshold); issue.put("invocation_sum", invocationSum); issue.put(PacmanRuleConstants.TIME_PERIOD_HRS, TWENTYFOURHOURS); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========LambdaFunctionThrottleInvocationsRule ended with an annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (InvalidInputException e) { logger.error(e.toString()); throw new InvalidInputException(e.getMessage()); } } logger.debug("========LambdaFunctionThrottleInvocationsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  }
LambdaFunctionThrottleInvocationsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========LambdaFunctionThrottleInvocationsRule started========="); if (resourceAttributes != null && !StringUtils.isNullOrEmpty(resourceAttributes .get(PacmanRuleConstants.FUNCTION_NAME_RES_ATTR))) { String resourceId = resourceAttributes .get(PacmanRuleConstants.FUNCTION_NAME_RES_ATTR); String roleIdentifyingString = ruleParam .get(PacmanSdkConstants.Role_IDENTIFYING_STRING); AmazonCloudWatchClient cloudWatchClient = null; Annotation annotation = null; int invocationSum = 0; int targetThreshold = Integer.parseInt(ruleParam .get(PacmanRuleConstants.THRESHOLD)); 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(String.valueOf(targetThreshold), severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } try { cloudWatchClient = getClient(roleIdentifyingString, ruleParam); final GetMetricStatisticsRequest request = request(resourceId); final GetMetricStatisticsResult result = cloudWatchClient .getMetricStatistics(request); for (final Datapoint dataPoint : result.getDatapoints()) { invocationSum = invocationSum + dataPoint.getSum().intValue(); } if (invocationSum > targetThreshold) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation .put(PacmanSdkConstants.DESCRIPTION, "Lambda function throttle invocation count exceeded target threshold"); annotation.put(PacmanRuleConstants.LAMBDA_INVOCATION_COUNT, ":" + invocationSum); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Lambda function throttle invocation count exceeded target threshold"); issue.put(PacmanRuleConstants.THRESHOLD, targetThreshold); issue.put("invocation_sum", invocationSum); issue.put(PacmanRuleConstants.TIME_PERIOD_HRS, TWENTYFOURHOURS); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========LambdaFunctionThrottleInvocationsRule ended with an annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (InvalidInputException e) { logger.error(e.toString()); throw new InvalidInputException(e.getMessage()); } } logger.debug("========LambdaFunctionThrottleInvocationsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); @Override String getHelpText(); }
LambdaFunctionThrottleInvocationsRule extends BaseRule { @Override public RuleResult execute(Map<String, String> ruleParam, Map<String, String> resourceAttributes) { logger.debug("========LambdaFunctionThrottleInvocationsRule started========="); if (resourceAttributes != null && !StringUtils.isNullOrEmpty(resourceAttributes .get(PacmanRuleConstants.FUNCTION_NAME_RES_ATTR))) { String resourceId = resourceAttributes .get(PacmanRuleConstants.FUNCTION_NAME_RES_ATTR); String roleIdentifyingString = ruleParam .get(PacmanSdkConstants.Role_IDENTIFYING_STRING); AmazonCloudWatchClient cloudWatchClient = null; Annotation annotation = null; int invocationSum = 0; int targetThreshold = Integer.parseInt(ruleParam .get(PacmanRuleConstants.THRESHOLD)); 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(String.valueOf(targetThreshold), severity, category)) { logger.info(PacmanRuleConstants.MISSING_CONFIGURATION); throw new InvalidInputException(PacmanRuleConstants.MISSING_CONFIGURATION); } try { cloudWatchClient = getClient(roleIdentifyingString, ruleParam); final GetMetricStatisticsRequest request = request(resourceId); final GetMetricStatisticsResult result = cloudWatchClient .getMetricStatistics(request); for (final Datapoint dataPoint : result.getDatapoints()) { invocationSum = invocationSum + dataPoint.getSum().intValue(); } if (invocationSum > targetThreshold) { annotation = Annotation.buildAnnotation(ruleParam, Annotation.Type.ISSUE); annotation .put(PacmanSdkConstants.DESCRIPTION, "Lambda function throttle invocation count exceeded target threshold"); annotation.put(PacmanRuleConstants.LAMBDA_INVOCATION_COUNT, ":" + invocationSum); annotation.put(PacmanRuleConstants.SEVERITY, severity); annotation.put(PacmanRuleConstants.CATEGORY, category); issue.put(PacmanRuleConstants.VIOLATION_REASON, "Lambda function throttle invocation count exceeded target threshold"); issue.put(PacmanRuleConstants.THRESHOLD, targetThreshold); issue.put("invocation_sum", invocationSum); issue.put(PacmanRuleConstants.TIME_PERIOD_HRS, TWENTYFOURHOURS); issueList.add(issue); annotation.put("issueDetails", issueList.toString()); logger.debug("========LambdaFunctionThrottleInvocationsRule ended with an annotation {} :=========",annotation); return new RuleResult(PacmanSdkConstants.STATUS_FAILURE, PacmanRuleConstants.FAILURE_MESSAGE, annotation); } } catch (InvalidInputException e) { logger.error(e.toString()); throw new InvalidInputException(e.getMessage()); } } logger.debug("========LambdaFunctionThrottleInvocationsRule ended========="); return new RuleResult(PacmanSdkConstants.STATUS_SUCCESS, PacmanRuleConstants.SUCCESS_MESSAGE); }  @Override RuleResult execute(Map<String, String> ruleParam,
            Map<String, String> resourceAttributes); @Override String getHelpText();  }