@Test public void getTrendAnnotationsTest() throws Exception { String response = "{\"hits\":{\"total\":68,\"hits\":[{\"_index\":\"assetgroup_annotations\",\"_type\":\"annotations\",\"_id\":\"20180722\"," + "\"_source\":{\"date\":\"2018-07-22\",\"note\":\"Test global\",\"ag\":\"\",\"noteId\":\"20180722\"}}]}}"; mockStatic(PacHttpUtils.class); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(response); ReflectionTestUtils.setField(vulnerabilityRepository, "esUrl", "dummyEsURL"); assertTrue(vulnerabilityRepository.getTrendAnnotations("ag", new Date()).size() == 1); }
public List<Map<String, Object>> getTrendAnnotations(String ag, Date from) { List<Map<String, Object>> notes = new ArrayList<>(); SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/") .append("assetgroup_annotations/annotations/_search"); StringBuilder requestBody = new StringBuilder( "{\"size\":10000,\"query\":{\"bool\":{\"must\":[{\"range\":{\"date\":{\"gte\":\""); requestBody.append(dateformat.format(from)); requestBody.append("\",\"lte\":\""); requestBody.append(dateformat.format(new Date())); requestBody.append("\",\"format\":\"yyyy-MM-dd\"}}}"); requestBody.append(",{\"terms\":{\"ag.keyword\":[\"\",\""); requestBody.append(ag).append("\"]}}]}}}"); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error("Error in getTrendAnnotations from ES", e); } JsonParser jsonParser = new JsonParser(); if (StringUtils.isNotEmpty(responseJson)) { JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonArray hits = resultJson.get("hits").getAsJsonObject().get("hits").getAsJsonArray(); Map<String, Object> note; if (hits.size() > 0) { for (int i = 0; i < hits.size(); i++) { JsonObject obj = (JsonObject) hits.get(i); JsonObject sourceJson = (JsonObject) obj.get("_source"); if (sourceJson != null) { note = new Gson().fromJson(sourceJson, new TypeToken<Map<String, Object>>() { }.getType()); notes.add(note); } } } } return notes; }
VulnerabilityRepository implements Constants { public List<Map<String, Object>> getTrendAnnotations(String ag, Date from) { List<Map<String, Object>> notes = new ArrayList<>(); SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/") .append("assetgroup_annotations/annotations/_search"); StringBuilder requestBody = new StringBuilder( "{\"size\":10000,\"query\":{\"bool\":{\"must\":[{\"range\":{\"date\":{\"gte\":\""); requestBody.append(dateformat.format(from)); requestBody.append("\",\"lte\":\""); requestBody.append(dateformat.format(new Date())); requestBody.append("\",\"format\":\"yyyy-MM-dd\"}}}"); requestBody.append(",{\"terms\":{\"ag.keyword\":[\"\",\""); requestBody.append(ag).append("\"]}}]}}}"); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error("Error in getTrendAnnotations from ES", e); } JsonParser jsonParser = new JsonParser(); if (StringUtils.isNotEmpty(responseJson)) { JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonArray hits = resultJson.get("hits").getAsJsonObject().get("hits").getAsJsonArray(); Map<String, Object> note; if (hits.size() > 0) { for (int i = 0; i < hits.size(); i++) { JsonObject obj = (JsonObject) hits.get(i); JsonObject sourceJson = (JsonObject) obj.get("_source"); if (sourceJson != null) { note = new Gson().fromJson(sourceJson, new TypeToken<Map<String, Object>>() { }.getType()); notes.add(note); } } } } return notes; } }
VulnerabilityRepository implements Constants { public List<Map<String, Object>> getTrendAnnotations(String ag, Date from) { List<Map<String, Object>> notes = new ArrayList<>(); SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/") .append("assetgroup_annotations/annotations/_search"); StringBuilder requestBody = new StringBuilder( "{\"size\":10000,\"query\":{\"bool\":{\"must\":[{\"range\":{\"date\":{\"gte\":\""); requestBody.append(dateformat.format(from)); requestBody.append("\",\"lte\":\""); requestBody.append(dateformat.format(new Date())); requestBody.append("\",\"format\":\"yyyy-MM-dd\"}}}"); requestBody.append(",{\"terms\":{\"ag.keyword\":[\"\",\""); requestBody.append(ag).append("\"]}}]}}}"); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error("Error in getTrendAnnotations from ES", e); } JsonParser jsonParser = new JsonParser(); if (StringUtils.isNotEmpty(responseJson)) { JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonArray hits = resultJson.get("hits").getAsJsonObject().get("hits").getAsJsonArray(); Map<String, Object> note; if (hits.size() > 0) { for (int i = 0; i < hits.size(); i++) { JsonObject obj = (JsonObject) hits.get(i); JsonObject sourceJson = (JsonObject) obj.get("_source"); if (sourceJson != null) { note = new Gson().fromJson(sourceJson, new TypeToken<Map<String, Object>>() { }.getType()); notes.add(note); } } } } return notes; }  }
VulnerabilityRepository implements Constants { public List<Map<String, Object>> getTrendAnnotations(String ag, Date from) { List<Map<String, Object>> notes = new ArrayList<>(); SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/") .append("assetgroup_annotations/annotations/_search"); StringBuilder requestBody = new StringBuilder( "{\"size\":10000,\"query\":{\"bool\":{\"must\":[{\"range\":{\"date\":{\"gte\":\""); requestBody.append(dateformat.format(from)); requestBody.append("\",\"lte\":\""); requestBody.append(dateformat.format(new Date())); requestBody.append("\",\"format\":\"yyyy-MM-dd\"}}}"); requestBody.append(",{\"terms\":{\"ag.keyword\":[\"\",\""); requestBody.append(ag).append("\"]}}]}}}"); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error("Error in getTrendAnnotations from ES", e); } JsonParser jsonParser = new JsonParser(); if (StringUtils.isNotEmpty(responseJson)) { JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonArray hits = resultJson.get("hits").getAsJsonObject().get("hits").getAsJsonArray(); Map<String, Object> note; if (hits.size() > 0) { for (int i = 0; i < hits.size(); i++) { JsonObject obj = (JsonObject) hits.get(i); JsonObject sourceJson = (JsonObject) obj.get("_source"); if (sourceJson != null) { note = new Gson().fromJson(sourceJson, new TypeToken<Map<String, Object>>() { }.getType()); notes.add(note); } } } } return notes; }  List<Map<String, Object>> getAllVulnerabilities(List<String> vulnAssetsAffectedQids); List<Map<String, Object>> getAllVulnerabilitiesByAssetGroup(String assetGroup, String targetType,
			Map<String, Object> mustFilter, List<String> occurrenceFieldList, int from, int size,
			Map<String, Object> mustTermsFilter); List<Map<String, Object>> getDetailsByResourceId(String assetGroup, Map<String, Object> mustFilter,
			List<String> resourceFieldList, Map<String, Object> mustTermFilter); Map<String, Long> getAssetsAffectedCount(String assetGroup, Map<String, String> filter, String parentType); List<Map<String, Object>> getVulnerabilyAcrossAppAndEnv(String assetGroup, String filter, String application,
			String parentType, String severity); List<Map<String, Object>> getVulnerabilityTrend(String assetGroup, Map<String, String> filter, Date from,
			Date to); List<Map<String, Object>> getVulnerabilitiesDistribution(String assetGroup, String parentType); Map<String, Object> getVulnerabilitysummaryByResourceId(String resourceId); List<Map<String, Object>> getVulnerabilityDetailsByResourceId(String resourceId); @SuppressWarnings("deprecation") List<Map<String, Object>> fetchExecDirectorApps(); Map<String, Object> getUniqueHost(String assetGroup, String severity); Map<String, Object> getVulnInfo(String assetGroup, String severity); Map<String, Object> getUniqueApp(String assetGroup); List<Map<String, Object>> getAgingSummary(String assetGroup); long getTotalQualysHostCount(String index, String vulnType); Map<String, Object> getVulnerabilityByQid(String qid); Map<String, Object> getDistributionSummaryByInfraType(String assetGroup, String severitylevel,
			String parentType); Map<String, Long> getProdInfoByEnv(String assetGroup, String severitylevel); Map<String, Long> getNonProdInfoByEnv(String assetGroup, String severitylevel); List<Map<String, Object>> getDistributionSummaryByVulnType(String assetGroup, String severity); Map<String, Object> getAllQidByAG(String assetGroup, String severity); Map<String, Long> getAppsBySeverity(String assetGroup, String parentType, String severity); boolean createTrendAnnotation(TrendNote request); void createIndex(String indexName); List<Map<String, Object>> getTrendAnnotations(String ag, Date from); boolean deleteTrendAnnotation(String noteId); List<Map<String, Object>> getDataFromPacmanRDS(String query); long getRunningInstancesCount(String index, String vulnType); long getExemptedByRuleCount(String index, String vulnType); Map<String, Object> getUniqueHostBySeverity(String index, String severity); Map<String, Object> getCompliantHostsBySeverity(String assetGroup); List<String> getNonCompliantResourceIds(String index); @SuppressWarnings("deprecation") List<Map<String, Object>> fetchOrgInfoForApps(); List<Map<String, Object>> getTrendProgress(String assetGroup, String ruleId, LocalDate startDate,
			LocalDate endDate, String trendCategory); int vulnerabilityAssetsCount(String assetGroup, String targetType, Map<String, Object> mustFilter, int from,
			int size, Map<String, Object> mustTermsFilter); }
VulnerabilityRepository implements Constants { public List<Map<String, Object>> getTrendAnnotations(String ag, Date from) { List<Map<String, Object>> notes = new ArrayList<>(); SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/") .append("assetgroup_annotations/annotations/_search"); StringBuilder requestBody = new StringBuilder( "{\"size\":10000,\"query\":{\"bool\":{\"must\":[{\"range\":{\"date\":{\"gte\":\""); requestBody.append(dateformat.format(from)); requestBody.append("\",\"lte\":\""); requestBody.append(dateformat.format(new Date())); requestBody.append("\",\"format\":\"yyyy-MM-dd\"}}}"); requestBody.append(",{\"terms\":{\"ag.keyword\":[\"\",\""); requestBody.append(ag).append("\"]}}]}}}"); String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error("Error in getTrendAnnotations from ES", e); } JsonParser jsonParser = new JsonParser(); if (StringUtils.isNotEmpty(responseJson)) { JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonArray hits = resultJson.get("hits").getAsJsonObject().get("hits").getAsJsonArray(); Map<String, Object> note; if (hits.size() > 0) { for (int i = 0; i < hits.size(); i++) { JsonObject obj = (JsonObject) hits.get(i); JsonObject sourceJson = (JsonObject) obj.get("_source"); if (sourceJson != null) { note = new Gson().fromJson(sourceJson, new TypeToken<Map<String, Object>>() { }.getType()); notes.add(note); } } } } return notes; }  List<Map<String, Object>> getAllVulnerabilities(List<String> vulnAssetsAffectedQids); List<Map<String, Object>> getAllVulnerabilitiesByAssetGroup(String assetGroup, String targetType,
			Map<String, Object> mustFilter, List<String> occurrenceFieldList, int from, int size,
			Map<String, Object> mustTermsFilter); List<Map<String, Object>> getDetailsByResourceId(String assetGroup, Map<String, Object> mustFilter,
			List<String> resourceFieldList, Map<String, Object> mustTermFilter); Map<String, Long> getAssetsAffectedCount(String assetGroup, Map<String, String> filter, String parentType); List<Map<String, Object>> getVulnerabilyAcrossAppAndEnv(String assetGroup, String filter, String application,
			String parentType, String severity); List<Map<String, Object>> getVulnerabilityTrend(String assetGroup, Map<String, String> filter, Date from,
			Date to); List<Map<String, Object>> getVulnerabilitiesDistribution(String assetGroup, String parentType); Map<String, Object> getVulnerabilitysummaryByResourceId(String resourceId); List<Map<String, Object>> getVulnerabilityDetailsByResourceId(String resourceId); @SuppressWarnings("deprecation") List<Map<String, Object>> fetchExecDirectorApps(); Map<String, Object> getUniqueHost(String assetGroup, String severity); Map<String, Object> getVulnInfo(String assetGroup, String severity); Map<String, Object> getUniqueApp(String assetGroup); List<Map<String, Object>> getAgingSummary(String assetGroup); long getTotalQualysHostCount(String index, String vulnType); Map<String, Object> getVulnerabilityByQid(String qid); Map<String, Object> getDistributionSummaryByInfraType(String assetGroup, String severitylevel,
			String parentType); Map<String, Long> getProdInfoByEnv(String assetGroup, String severitylevel); Map<String, Long> getNonProdInfoByEnv(String assetGroup, String severitylevel); List<Map<String, Object>> getDistributionSummaryByVulnType(String assetGroup, String severity); Map<String, Object> getAllQidByAG(String assetGroup, String severity); Map<String, Long> getAppsBySeverity(String assetGroup, String parentType, String severity); boolean createTrendAnnotation(TrendNote request); void createIndex(String indexName); List<Map<String, Object>> getTrendAnnotations(String ag, Date from); boolean deleteTrendAnnotation(String noteId); List<Map<String, Object>> getDataFromPacmanRDS(String query); long getRunningInstancesCount(String index, String vulnType); long getExemptedByRuleCount(String index, String vulnType); Map<String, Object> getUniqueHostBySeverity(String index, String severity); Map<String, Object> getCompliantHostsBySeverity(String assetGroup); List<String> getNonCompliantResourceIds(String index); @SuppressWarnings("deprecation") List<Map<String, Object>> fetchOrgInfoForApps(); List<Map<String, Object>> getTrendProgress(String assetGroup, String ruleId, LocalDate startDate,
			LocalDate endDate, String trendCategory); int vulnerabilityAssetsCount(String assetGroup, String targetType, Map<String, Object> mustFilter, int from,
			int size, Map<String, Object> mustTermsFilter);  }