@Test public void taggingByApplicationTest() throws Exception { String reponse = "{\"count\":1000,\"took\":9157,\"timed_out\":false,\"_shards\":{\"total\":176,\"successful\":176,\"failed\":0},\"hits\":{\"total\":280654,\"max_score\":0,\"hits\":[]},\"aggregations\":{\"NAME\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[{\"key\":\"snapshot\",\"doc_count\":22930},{\"key\":\"eni\",\"doc_count\":11927},{\"key\":\"sg\",\"doc_count\":5337},{\"key\":\"rdssnapshot\",\"doc_count\":3658},{\"key\":\"volume\",\"doc_count\":3581},{\"key\":\"stack\",\"doc_count\":3539},{\"key\":\"lambda\",\"doc_count\":2843},{\"key\":\"iamrole\",\"doc_count\":2768},{\"key\":\"s3\",\"doc_count\":1537},{\"key\":\"subnet\",\"doc_count\":1447},{\"key\":\"phd\",\"doc_count\":1221},{\"key\":\"launchconfig\",\"doc_count\":1192},{\"key\":\"iamuser\",\"doc_count\":1187},{\"key\":\"snstopic\",\"doc_count\":932},{\"key\":\"routetable\",\"doc_count\":727},{\"key\":\"asgpolicy\",\"doc_count\":691},{\"key\":\"networkacl\",\"doc_count\":617},{\"key\":\"corpdomain\",\"doc_count\":572},{\"key\":\"checks\",\"doc_count\":521},{\"key\":\"dhcpoption\",\"doc_count\":451},{\"key\":\"internetgateway\",\"doc_count\":435},{\"key\":\"vpc\",\"doc_count\":432},{\"key\":\"kms\",\"doc_count\":402},{\"key\":\"dynamodb\",\"doc_count\":385},{\"key\":\"ec2\",\"doc_count\":338},{\"key\":\"elasticip\",\"doc_count\":328},{\"key\":\"targetgroup\",\"doc_count\":300},{\"key\":\"virtualinterface\",\"doc_count\":286},{\"key\":\"peeringconnection\",\"doc_count\":269},{\"key\":\"api\",\"doc_count\":258},{\"key\":\"asg\",\"doc_count\":209},{\"key\":\"cloudfront\",\"doc_count\":175},{\"key\":\"classicelb\",\"doc_count\":159},{\"key\":\"rdsdb\",\"doc_count\":139},{\"key\":\"nat\",\"doc_count\":122},{\"key\":\"vpngateway\",\"doc_count\":88},{\"key\":\"rdscluster\",\"doc_count\":69},{\"key\":\"customergateway\",\"doc_count\":63},{\"key\":\"appelb\",\"doc_count\":51},{\"key\":\"vpnconnection\",\"doc_count\":51},{\"key\":\"efs\",\"doc_count\":38},{\"key\":\"elasticsearch\",\"doc_count\":38},{\"key\":\"wafdomain\",\"doc_count\":29},{\"key\":\"account\",\"doc_count\":25},{\"key\":\"emr\",\"doc_count\":20},{\"key\":\"directconnect\",\"doc_count\":4},{\"key\":\"redshift\",\"doc_count\":4}]}}}"; String targetTypes = "'ec2','s3','volume'"; Map<String, Long> tagMap = new HashMap<>(); tagMap.put("ec2", 100l); tagMap.put("volume", 200l); tagMap.put("assets", 200l); when(complainceRepository.getTargetTypeForAG(anyString(), anyString())) .thenReturn(targetTypes); when( elasticSearchRepository .getTotalDocumentCountForIndexAndTypeWithMustNotTermsFilter( anyString(), anyString(), anyObject(), anyObject(), anyObject(), anyObject(), anyObject(), anyObject(), anyObject())) .thenReturn(5000l); when( elasticSearchRepository.getTotalDistributionForIndexAndType(anyString(), anyString(), anyObject(), anyObject(), anyObject(), anyString(), anyInt(), anyObject())) .thenReturn(tagMap); when(repository.getTaggingByApplication(anyString(), anyString())) .thenReturn(reponse); assertThat(taggingServiceImpl.taggingByApplication("aws-all", "s3"), is(notNullValue())); assertThat(taggingServiceImpl.taggingByApplication("aws-all", ""), is(notNullValue())); }
@Override public List<Map<String, Long>> taggingByApplication(String assetGroup, String targetType) throws ServiceException { List<Map<String, Long>> taggsApplication = new ArrayList<>(); List<String> tagsList = new ArrayList<>(); tagsList.add(""); tagsList.add("unknown"); Map<String, Long> tagsMap = new HashMap<>(); Map<String, Object> mustTermsFilter = new HashMap<>(); String targetTypes = complainceRepository.getTargetTypeForAG(assetGroup, null); List<String> targetTypesList = new ArrayList<>(Arrays.asList(targetTypes.split(","))); Map<String, Object> mustNotFilter = new HashMap<>(); HashMultimap<String, Object> shouldFilter = HashMultimap.create(); String aggsEntityType = "_entitytype.keyword"; mustTermsFilter.put("tags.Application.keyword", tagsList); Map<String, Object> mustFilter = new HashMap<>(); mustFilter.put("latest", true); Map<String, Long> assetWithoutTag; Map<String, Long> emptyOrUnkownAssets; Long emptyOrUnkownAssetsLong; Long assetWithoutTagLong = 0l; String type = null; if (!StringUtils.isEmpty(targetType)) { type = "'" + targetType + "'"; if (targetTypesList.contains(type)) { assetWithoutTag = getAssetCountWithoutApplicationTag(assetGroup, targetType); try { emptyOrUnkownAssetsLong = elasticSearchRepository .getTotalDocumentCountForIndexAndTypeWithMustNotTermsFilter(assetGroup, targetType, mustFilter, null, null, null, mustTermsFilter, null, null); } catch (Exception e) { throw new ServiceException(e); } if (assetWithoutTag.containsKey(targetType)) { assetWithoutTagLong = assetWithoutTag.get(targetType); } tagsMap.put(targetType, emptyOrUnkownAssetsLong + assetWithoutTagLong); } } else { assetWithoutTag = getAssetCountWithoutApplicationTag(assetGroup, null); try { emptyOrUnkownAssets = elasticSearchRepository.getTotalDistributionForIndexAndType(assetGroup, null, mustFilter, mustNotFilter, shouldFilter, aggsEntityType, TEN_THOUSAND, mustTermsFilter); } catch (Exception e) { throw new ServiceException(e); } getNoTagsData(tagsMap, targetTypesList, assetWithoutTag, emptyOrUnkownAssets); } taggsApplication.add(tagsMap); return taggsApplication; }
TaggingServiceImpl implements TaggingService, Constants { @Override public List<Map<String, Long>> taggingByApplication(String assetGroup, String targetType) throws ServiceException { List<Map<String, Long>> taggsApplication = new ArrayList<>(); List<String> tagsList = new ArrayList<>(); tagsList.add(""); tagsList.add("unknown"); Map<String, Long> tagsMap = new HashMap<>(); Map<String, Object> mustTermsFilter = new HashMap<>(); String targetTypes = complainceRepository.getTargetTypeForAG(assetGroup, null); List<String> targetTypesList = new ArrayList<>(Arrays.asList(targetTypes.split(","))); Map<String, Object> mustNotFilter = new HashMap<>(); HashMultimap<String, Object> shouldFilter = HashMultimap.create(); String aggsEntityType = "_entitytype.keyword"; mustTermsFilter.put("tags.Application.keyword", tagsList); Map<String, Object> mustFilter = new HashMap<>(); mustFilter.put("latest", true); Map<String, Long> assetWithoutTag; Map<String, Long> emptyOrUnkownAssets; Long emptyOrUnkownAssetsLong; Long assetWithoutTagLong = 0l; String type = null; if (!StringUtils.isEmpty(targetType)) { type = "'" + targetType + "'"; if (targetTypesList.contains(type)) { assetWithoutTag = getAssetCountWithoutApplicationTag(assetGroup, targetType); try { emptyOrUnkownAssetsLong = elasticSearchRepository .getTotalDocumentCountForIndexAndTypeWithMustNotTermsFilter(assetGroup, targetType, mustFilter, null, null, null, mustTermsFilter, null, null); } catch (Exception e) { throw new ServiceException(e); } if (assetWithoutTag.containsKey(targetType)) { assetWithoutTagLong = assetWithoutTag.get(targetType); } tagsMap.put(targetType, emptyOrUnkownAssetsLong + assetWithoutTagLong); } } else { assetWithoutTag = getAssetCountWithoutApplicationTag(assetGroup, null); try { emptyOrUnkownAssets = elasticSearchRepository.getTotalDistributionForIndexAndType(assetGroup, null, mustFilter, mustNotFilter, shouldFilter, aggsEntityType, TEN_THOUSAND, mustTermsFilter); } catch (Exception e) { throw new ServiceException(e); } getNoTagsData(tagsMap, targetTypesList, assetWithoutTag, emptyOrUnkownAssets); } taggsApplication.add(tagsMap); return taggsApplication; } }
TaggingServiceImpl implements TaggingService, Constants { @Override public List<Map<String, Long>> taggingByApplication(String assetGroup, String targetType) throws ServiceException { List<Map<String, Long>> taggsApplication = new ArrayList<>(); List<String> tagsList = new ArrayList<>(); tagsList.add(""); tagsList.add("unknown"); Map<String, Long> tagsMap = new HashMap<>(); Map<String, Object> mustTermsFilter = new HashMap<>(); String targetTypes = complainceRepository.getTargetTypeForAG(assetGroup, null); List<String> targetTypesList = new ArrayList<>(Arrays.asList(targetTypes.split(","))); Map<String, Object> mustNotFilter = new HashMap<>(); HashMultimap<String, Object> shouldFilter = HashMultimap.create(); String aggsEntityType = "_entitytype.keyword"; mustTermsFilter.put("tags.Application.keyword", tagsList); Map<String, Object> mustFilter = new HashMap<>(); mustFilter.put("latest", true); Map<String, Long> assetWithoutTag; Map<String, Long> emptyOrUnkownAssets; Long emptyOrUnkownAssetsLong; Long assetWithoutTagLong = 0l; String type = null; if (!StringUtils.isEmpty(targetType)) { type = "'" + targetType + "'"; if (targetTypesList.contains(type)) { assetWithoutTag = getAssetCountWithoutApplicationTag(assetGroup, targetType); try { emptyOrUnkownAssetsLong = elasticSearchRepository .getTotalDocumentCountForIndexAndTypeWithMustNotTermsFilter(assetGroup, targetType, mustFilter, null, null, null, mustTermsFilter, null, null); } catch (Exception e) { throw new ServiceException(e); } if (assetWithoutTag.containsKey(targetType)) { assetWithoutTagLong = assetWithoutTag.get(targetType); } tagsMap.put(targetType, emptyOrUnkownAssetsLong + assetWithoutTagLong); } } else { assetWithoutTag = getAssetCountWithoutApplicationTag(assetGroup, null); try { emptyOrUnkownAssets = elasticSearchRepository.getTotalDistributionForIndexAndType(assetGroup, null, mustFilter, mustNotFilter, shouldFilter, aggsEntityType, TEN_THOUSAND, mustTermsFilter); } catch (Exception e) { throw new ServiceException(e); } getNoTagsData(tagsMap, targetTypesList, assetWithoutTag, emptyOrUnkownAssets); } taggsApplication.add(tagsMap); return taggsApplication; }  }
TaggingServiceImpl implements TaggingService, Constants { @Override public List<Map<String, Long>> taggingByApplication(String assetGroup, String targetType) throws ServiceException { List<Map<String, Long>> taggsApplication = new ArrayList<>(); List<String> tagsList = new ArrayList<>(); tagsList.add(""); tagsList.add("unknown"); Map<String, Long> tagsMap = new HashMap<>(); Map<String, Object> mustTermsFilter = new HashMap<>(); String targetTypes = complainceRepository.getTargetTypeForAG(assetGroup, null); List<String> targetTypesList = new ArrayList<>(Arrays.asList(targetTypes.split(","))); Map<String, Object> mustNotFilter = new HashMap<>(); HashMultimap<String, Object> shouldFilter = HashMultimap.create(); String aggsEntityType = "_entitytype.keyword"; mustTermsFilter.put("tags.Application.keyword", tagsList); Map<String, Object> mustFilter = new HashMap<>(); mustFilter.put("latest", true); Map<String, Long> assetWithoutTag; Map<String, Long> emptyOrUnkownAssets; Long emptyOrUnkownAssetsLong; Long assetWithoutTagLong = 0l; String type = null; if (!StringUtils.isEmpty(targetType)) { type = "'" + targetType + "'"; if (targetTypesList.contains(type)) { assetWithoutTag = getAssetCountWithoutApplicationTag(assetGroup, targetType); try { emptyOrUnkownAssetsLong = elasticSearchRepository .getTotalDocumentCountForIndexAndTypeWithMustNotTermsFilter(assetGroup, targetType, mustFilter, null, null, null, mustTermsFilter, null, null); } catch (Exception e) { throw new ServiceException(e); } if (assetWithoutTag.containsKey(targetType)) { assetWithoutTagLong = assetWithoutTag.get(targetType); } tagsMap.put(targetType, emptyOrUnkownAssetsLong + assetWithoutTagLong); } } else { assetWithoutTag = getAssetCountWithoutApplicationTag(assetGroup, null); try { emptyOrUnkownAssets = elasticSearchRepository.getTotalDistributionForIndexAndType(assetGroup, null, mustFilter, mustNotFilter, shouldFilter, aggsEntityType, TEN_THOUSAND, mustTermsFilter); } catch (Exception e) { throw new ServiceException(e); } getNoTagsData(tagsMap, targetTypesList, assetWithoutTag, emptyOrUnkownAssets); } taggsApplication.add(tagsMap); return taggsApplication; }  @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 { @Override public List<Map<String, Long>> taggingByApplication(String assetGroup, String targetType) throws ServiceException { List<Map<String, Long>> taggsApplication = new ArrayList<>(); List<String> tagsList = new ArrayList<>(); tagsList.add(""); tagsList.add("unknown"); Map<String, Long> tagsMap = new HashMap<>(); Map<String, Object> mustTermsFilter = new HashMap<>(); String targetTypes = complainceRepository.getTargetTypeForAG(assetGroup, null); List<String> targetTypesList = new ArrayList<>(Arrays.asList(targetTypes.split(","))); Map<String, Object> mustNotFilter = new HashMap<>(); HashMultimap<String, Object> shouldFilter = HashMultimap.create(); String aggsEntityType = "_entitytype.keyword"; mustTermsFilter.put("tags.Application.keyword", tagsList); Map<String, Object> mustFilter = new HashMap<>(); mustFilter.put("latest", true); Map<String, Long> assetWithoutTag; Map<String, Long> emptyOrUnkownAssets; Long emptyOrUnkownAssetsLong; Long assetWithoutTagLong = 0l; String type = null; if (!StringUtils.isEmpty(targetType)) { type = "'" + targetType + "'"; if (targetTypesList.contains(type)) { assetWithoutTag = getAssetCountWithoutApplicationTag(assetGroup, targetType); try { emptyOrUnkownAssetsLong = elasticSearchRepository .getTotalDocumentCountForIndexAndTypeWithMustNotTermsFilter(assetGroup, targetType, mustFilter, null, null, null, mustTermsFilter, null, null); } catch (Exception e) { throw new ServiceException(e); } if (assetWithoutTag.containsKey(targetType)) { assetWithoutTagLong = assetWithoutTag.get(targetType); } tagsMap.put(targetType, emptyOrUnkownAssetsLong + assetWithoutTagLong); } } else { assetWithoutTag = getAssetCountWithoutApplicationTag(assetGroup, null); try { emptyOrUnkownAssets = elasticSearchRepository.getTotalDistributionForIndexAndType(assetGroup, null, mustFilter, mustNotFilter, shouldFilter, aggsEntityType, TEN_THOUSAND, mustTermsFilter); } catch (Exception e) { throw new ServiceException(e); } getNoTagsData(tagsMap, targetTypesList, assetWithoutTag, emptyOrUnkownAssets); } taggsApplication.add(tagsMap); return taggsApplication; }  @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);  }