@Test public void getUntaggingByTargetTypesTest() throws Exception { Map<String, String> filter = new HashMap<>(); filter.put("targetType", "ec2"); untaggedTargetTypeRequest.setAg("aws-all"); untaggedTargetTypeRequest.setFilter(filter); Map<String, Object> targetTypesMap = new HashMap<>(); targetTypesMap.put("targetType", "ec2"); List<Map<String, Object>> maintargetTypesList = new ArrayList<>(); maintargetTypesList.add(targetTypesMap); Map<String, Long> assetCountMap = new HashMap<>(); assetCountMap.put("ec2", 100l); assetCountMap.put("s3", 200l); String response = "{\"took\":806,\"timed_out\":false,\"_shards\":{\"total\":176,\"successful\":176,\"failed\":0},\"hits\":{\"total\":45266,\"max_score\":9.880274,\"hits\":[" + "{\"_index\":\"aws_ec2\",\"_type\":\"issue_ec2\",\"_id\":\"_id\",\"_score\":9.880274,\"_routing\":\"_routing\",\"_parent\":\"_parent\",\"_source\":{\"missingTags\":\"Role, Environment, Application, Stack\"," + "\"_docid\":\"_docid\",\"type\":\"issue\",\"accountid\":\"accountid\",\"issueStatus\":\"open\",\"accountname\":\"accountname\"," + "\"ruleId\":\"PacMan_TaggingRule_version-1_Ec2TaggingRule_ec2\",\"severity\":\"high\",\"_resourceid\":\"_resourceid\",\"ruleCategory\":\"tagging\"," + "\"targetType\":\"ec2\",\"issueDetails\":\"[{violationReason\\u003dMandatory tags missed for ec2 target type!, tags_associated\\u003d{\\\"Role\\\":\\\"Not Found\\\",\\\"Environment\\\":\\\"Not Found\\\",\\\"Application\\\":\\\"Not Found\\\",\\\"Stack\\\":\\\"Not Found\\\"}}]\",\"Stack\":\"Not Found\",\"Role\":\"Not Found\"," + "\"executionId\":\"executionId\",\"mandatoryTagsMissingFlg\":\"YES\",\"createdDate\":\"2018-06-22T21:01:01.603Z\",\"policyId\":\"PacMan_TaggingRule_version-1\",\"pac_ds\":\"aws\",\"modifiedDate\":\"2018-06-26T11:00:47.956Z\",\"Environment\":\"Not Found\",\"region\":\"region\",\"Application\":\"Not Found\",\"desc\":\"Missed tags for ec2 are Role, Environment, Application, Stack\"}}]}," + "\"aggregations\":{\"NAME\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[" + "{\"key\":\"key\",\"doc_count\":25032},{\"key\":\"key\",\"doc_count\":5589}," + "{\"key\":\"key\",\"doc_count\":1797},{\"key\":\"key\",\"doc_count\":9}]}}}"; when(repository.getRuleTargetTypesFromDbByPolicyId(anyString())) .thenReturn(maintargetTypesList); ReflectionTestUtils.setField(taggingServiceImpl, "mandatoryTags", "Application,Environment"); when(complainceRepository.getTotalAssetCount(anyString(), anyString(),anyString(),anyString())) .thenReturn(assetCountMap); when(repository.getUntaggedTargetTypeIssues(anyObject(), anyObject())) .thenReturn(response); assertThat( taggingServiceImpl .getUntaggingByTargetTypes(untaggedTargetTypeRequest), is(notNullValue())); when(repository.getRuleTargetTypesFromDbByPolicyId(anyString())) .thenThrow(new DataException()); assertThatThrownBy(() -> taggingServiceImpl.getUntaggingByTargetTypes(untaggedTargetTypeRequest)) .isInstanceOf(ServiceException.class); }
public List<Map<String, Object>> getUntaggingByTargetTypes(UntaggedTargetTypeRequest request) throws ServiceException { long assetCount ; long untaggedCount ; long taggedCount ; double compliancePercentage; String type; List<String> tagsList = new ArrayList<>(Arrays.asList(mandatoryTags.split(","))); List<Map<String, Object>> targetTypes; try { targetTypes = repository.getRuleTargetTypesFromDbByPolicyId(TAGGIG_POLICY); } catch (DataException e) { throw new ServiceException(e); } if (targetTypes.isEmpty()) { throw new ServiceException(NO_DATA_FOUND); } Map<String, String> filterTags = request.getFilter(); List<Map<String, Object>> unTagsList = new ArrayList<>(); Map<String, Long> assetCountByTypes = complainceRepository.getTotalAssetCount(request.getAg(), null,null,null); Map<String, Long> untaggedCountMap = getUntaggedTargetTypeIssues(request, tagsList); for (Map<String, Object> targetType : targetTypes) { type = targetType.get(TARGET_TYPE).toString(); Map<String, Object> data = new HashMap<>(); if (null != filterTags && !filterTags.isEmpty() && !filterTags.get(TARGET_TYPE).isEmpty() && (!type.equalsIgnoreCase(filterTags.get(TARGET_TYPE)))) { continue; } assetCount = (null != assetCountByTypes.get(type)) ? Long.parseLong(assetCountByTypes.get(type).toString()) : 0l; if (assetCount > 0) { untaggedCount = getUntaggedAsset(untaggedCountMap, type); if (untaggedCount > assetCount) { untaggedCount = assetCount; } taggedCount = assetCount - untaggedCount; compliancePercentage = (double) (taggedCount * INT_HUNDRED) / assetCount; compliancePercentage = Math.floor(compliancePercentage); data.put("name", type); data.put("untagged", untaggedCount); data.put("tagged", taggedCount); data.put("assetCount", assetCount); data.put(COMP_PERCENTAGE, compliancePercentage); unTagsList.add(data); } } if (unTagsList.isEmpty()) { throw new ServiceException(NO_DATA_FOUND); } return unTagsList; }
TaggingServiceImpl implements TaggingService, Constants { public List<Map<String, Object>> getUntaggingByTargetTypes(UntaggedTargetTypeRequest request) throws ServiceException { long assetCount ; long untaggedCount ; long taggedCount ; double compliancePercentage; String type; List<String> tagsList = new ArrayList<>(Arrays.asList(mandatoryTags.split(","))); List<Map<String, Object>> targetTypes; try { targetTypes = repository.getRuleTargetTypesFromDbByPolicyId(TAGGIG_POLICY); } catch (DataException e) { throw new ServiceException(e); } if (targetTypes.isEmpty()) { throw new ServiceException(NO_DATA_FOUND); } Map<String, String> filterTags = request.getFilter(); List<Map<String, Object>> unTagsList = new ArrayList<>(); Map<String, Long> assetCountByTypes = complainceRepository.getTotalAssetCount(request.getAg(), null,null,null); Map<String, Long> untaggedCountMap = getUntaggedTargetTypeIssues(request, tagsList); for (Map<String, Object> targetType : targetTypes) { type = targetType.get(TARGET_TYPE).toString(); Map<String, Object> data = new HashMap<>(); if (null != filterTags && !filterTags.isEmpty() && !filterTags.get(TARGET_TYPE).isEmpty() && (!type.equalsIgnoreCase(filterTags.get(TARGET_TYPE)))) { continue; } assetCount = (null != assetCountByTypes.get(type)) ? Long.parseLong(assetCountByTypes.get(type).toString()) : 0l; if (assetCount > 0) { untaggedCount = getUntaggedAsset(untaggedCountMap, type); if (untaggedCount > assetCount) { untaggedCount = assetCount; } taggedCount = assetCount - untaggedCount; compliancePercentage = (double) (taggedCount * INT_HUNDRED) / assetCount; compliancePercentage = Math.floor(compliancePercentage); data.put("name", type); data.put("untagged", untaggedCount); data.put("tagged", taggedCount); data.put("assetCount", assetCount); data.put(COMP_PERCENTAGE, compliancePercentage); unTagsList.add(data); } } if (unTagsList.isEmpty()) { throw new ServiceException(NO_DATA_FOUND); } return unTagsList; } }
TaggingServiceImpl implements TaggingService, Constants { public List<Map<String, Object>> getUntaggingByTargetTypes(UntaggedTargetTypeRequest request) throws ServiceException { long assetCount ; long untaggedCount ; long taggedCount ; double compliancePercentage; String type; List<String> tagsList = new ArrayList<>(Arrays.asList(mandatoryTags.split(","))); List<Map<String, Object>> targetTypes; try { targetTypes = repository.getRuleTargetTypesFromDbByPolicyId(TAGGIG_POLICY); } catch (DataException e) { throw new ServiceException(e); } if (targetTypes.isEmpty()) { throw new ServiceException(NO_DATA_FOUND); } Map<String, String> filterTags = request.getFilter(); List<Map<String, Object>> unTagsList = new ArrayList<>(); Map<String, Long> assetCountByTypes = complainceRepository.getTotalAssetCount(request.getAg(), null,null,null); Map<String, Long> untaggedCountMap = getUntaggedTargetTypeIssues(request, tagsList); for (Map<String, Object> targetType : targetTypes) { type = targetType.get(TARGET_TYPE).toString(); Map<String, Object> data = new HashMap<>(); if (null != filterTags && !filterTags.isEmpty() && !filterTags.get(TARGET_TYPE).isEmpty() && (!type.equalsIgnoreCase(filterTags.get(TARGET_TYPE)))) { continue; } assetCount = (null != assetCountByTypes.get(type)) ? Long.parseLong(assetCountByTypes.get(type).toString()) : 0l; if (assetCount > 0) { untaggedCount = getUntaggedAsset(untaggedCountMap, type); if (untaggedCount > assetCount) { untaggedCount = assetCount; } taggedCount = assetCount - untaggedCount; compliancePercentage = (double) (taggedCount * INT_HUNDRED) / assetCount; compliancePercentage = Math.floor(compliancePercentage); data.put("name", type); data.put("untagged", untaggedCount); data.put("tagged", taggedCount); data.put("assetCount", assetCount); data.put(COMP_PERCENTAGE, compliancePercentage); unTagsList.add(data); } } if (unTagsList.isEmpty()) { throw new ServiceException(NO_DATA_FOUND); } return unTagsList; }  }
TaggingServiceImpl implements TaggingService, Constants { public List<Map<String, Object>> getUntaggingByTargetTypes(UntaggedTargetTypeRequest request) throws ServiceException { long assetCount ; long untaggedCount ; long taggedCount ; double compliancePercentage; String type; List<String> tagsList = new ArrayList<>(Arrays.asList(mandatoryTags.split(","))); List<Map<String, Object>> targetTypes; try { targetTypes = repository.getRuleTargetTypesFromDbByPolicyId(TAGGIG_POLICY); } catch (DataException e) { throw new ServiceException(e); } if (targetTypes.isEmpty()) { throw new ServiceException(NO_DATA_FOUND); } Map<String, String> filterTags = request.getFilter(); List<Map<String, Object>> unTagsList = new ArrayList<>(); Map<String, Long> assetCountByTypes = complainceRepository.getTotalAssetCount(request.getAg(), null,null,null); Map<String, Long> untaggedCountMap = getUntaggedTargetTypeIssues(request, tagsList); for (Map<String, Object> targetType : targetTypes) { type = targetType.get(TARGET_TYPE).toString(); Map<String, Object> data = new HashMap<>(); if (null != filterTags && !filterTags.isEmpty() && !filterTags.get(TARGET_TYPE).isEmpty() && (!type.equalsIgnoreCase(filterTags.get(TARGET_TYPE)))) { continue; } assetCount = (null != assetCountByTypes.get(type)) ? Long.parseLong(assetCountByTypes.get(type).toString()) : 0l; if (assetCount > 0) { untaggedCount = getUntaggedAsset(untaggedCountMap, type); if (untaggedCount > assetCount) { untaggedCount = assetCount; } taggedCount = assetCount - untaggedCount; compliancePercentage = (double) (taggedCount * INT_HUNDRED) / assetCount; compliancePercentage = Math.floor(compliancePercentage); data.put("name", type); data.put("untagged", untaggedCount); data.put("tagged", taggedCount); data.put("assetCount", assetCount); data.put(COMP_PERCENTAGE, compliancePercentage); unTagsList.add(data); } } if (unTagsList.isEmpty()) { throw new ServiceException(NO_DATA_FOUND); } return unTagsList; }  @Override ResponseWithCount getUnTaggedAssetsByApplication(Request request); Map<String, Object> getTaggingSummary(String assetGroup); List<Map<String, Object>> getUntaggingByTargetTypes(UntaggedTargetTypeRequest request); @Override List<Map<String, Long>> taggingByApplication(String assetGroup, String targetType); }
TaggingServiceImpl implements TaggingService, Constants { public List<Map<String, Object>> getUntaggingByTargetTypes(UntaggedTargetTypeRequest request) throws ServiceException { long assetCount ; long untaggedCount ; long taggedCount ; double compliancePercentage; String type; List<String> tagsList = new ArrayList<>(Arrays.asList(mandatoryTags.split(","))); List<Map<String, Object>> targetTypes; try { targetTypes = repository.getRuleTargetTypesFromDbByPolicyId(TAGGIG_POLICY); } catch (DataException e) { throw new ServiceException(e); } if (targetTypes.isEmpty()) { throw new ServiceException(NO_DATA_FOUND); } Map<String, String> filterTags = request.getFilter(); List<Map<String, Object>> unTagsList = new ArrayList<>(); Map<String, Long> assetCountByTypes = complainceRepository.getTotalAssetCount(request.getAg(), null,null,null); Map<String, Long> untaggedCountMap = getUntaggedTargetTypeIssues(request, tagsList); for (Map<String, Object> targetType : targetTypes) { type = targetType.get(TARGET_TYPE).toString(); Map<String, Object> data = new HashMap<>(); if (null != filterTags && !filterTags.isEmpty() && !filterTags.get(TARGET_TYPE).isEmpty() && (!type.equalsIgnoreCase(filterTags.get(TARGET_TYPE)))) { continue; } assetCount = (null != assetCountByTypes.get(type)) ? Long.parseLong(assetCountByTypes.get(type).toString()) : 0l; if (assetCount > 0) { untaggedCount = getUntaggedAsset(untaggedCountMap, type); if (untaggedCount > assetCount) { untaggedCount = assetCount; } taggedCount = assetCount - untaggedCount; compliancePercentage = (double) (taggedCount * INT_HUNDRED) / assetCount; compliancePercentage = Math.floor(compliancePercentage); data.put("name", type); data.put("untagged", untaggedCount); data.put("tagged", taggedCount); data.put("assetCount", assetCount); data.put(COMP_PERCENTAGE, compliancePercentage); unTagsList.add(data); } } if (unTagsList.isEmpty()) { throw new ServiceException(NO_DATA_FOUND); } return unTagsList; }  @Override ResponseWithCount getUnTaggedAssetsByApplication(Request request); Map<String, Object> getTaggingSummary(String assetGroup); List<Map<String, Object>> getUntaggingByTargetTypes(UntaggedTargetTypeRequest request); @Override List<Map<String, Long>> taggingByApplication(String assetGroup, String targetType);  }