@Test public void getCerticatesSummaryTest_NoCertificates() throws Exception { String response = "{\"aggregations\":{\"certs\":{\"doc_count\":269,\"openfilter\":{\"doc_count\":23,\"rules\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":0,\"buckets\":[]}}}}}"; Map<String, Long> certificates = new HashMap<>(); certificates.put("certificates", 0L); certificates.put("certificates_expiring", 0L); when(complianceRepository.getCertificates(anyString())).thenReturn(certificates); ReflectionTestUtils.setField(certificateRepository, "complianceRepository", complianceRepository); mockStatic(PacHttpUtils.class); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(response); ReflectionTestUtils.setField(certificateRepository, "esUrl", "dummyEsURL"); Map<String, Object> certificateSummary = certificateRepository.getCertificatesSummary("ag"); assertTrue(Integer.valueOf(certificateSummary.get("totalCertificates").toString()).equals(0)); assertTrue(Float.valueOf(certificateSummary.get("compliantPercent").toString()) == 100.0); }
public Map<String, Object> getCertificatesSummary(String assetGroup) throws DataException { Map<String, Object> certificateSummary = new HashMap<>(); Map<String, Long> certificates = complianceRepository .getCertificates(assetGroup); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append( assetGroup); urlToQuery.append(CERT_SEARCH); String requestBody = "{\"size\":0,\"aggs\":{\"certs\":{\"children\":{\"type\":\"issue_cert\"},\"aggs\":{\"openfilter\":{\"filter\":{\"term\":{\"issueStatus\":\"open\"}}," + "\"aggs\":{\"rules\":{\"terms\":{\"field\":\"ruleId.keyword\",\"size\":10}}}}}}}}"; String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody); } catch (Exception e) { throw new DataException(e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( "aggregations").toString()); certificateSummary.put(EXP_IN_45_DAYS, 0); certificateSummary.put(EXP_IN_30_DAYS, 0); try { JsonArray rules = aggsJson.getAsJsonObject() .getAsJsonObject("certs").getAsJsonObject("openfilter") .getAsJsonObject("rules").getAsJsonArray(BUCKETS); if (rules.size() > 0) { for (int j = 0; j < rules.size(); j++) { if (SSL_CERT_45_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { certificateSummary.put( EXP_IN_45_DAYS, Integer.valueOf(rules.get(j).getAsJsonObject() .get(DOC_COUNT).toString())); } if (SSL_CERT_30_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { certificateSummary.put( EXP_IN_30_DAYS, Integer.valueOf(rules.get(j).getAsJsonObject() .get(DOC_COUNT).toString())); } } } } catch (Exception e) { LOGGER.error("Error in getCerticatesSummary", e); } float compliantCount = Float.valueOf(certificates.get(CERTIFICATES).toString()) - Float.valueOf(certificates.get("certificates_expiring").toString()); DecimalFormat df = new DecimalFormat("#.00"); if (certificates.get(CERTIFICATES) > 0) { certificateSummary.put("compliantPercent", Math.floor(Float .valueOf(df.format((compliantCount / certificates .get(CERTIFICATES)) * HUNDRED)))); } else { certificateSummary.put("compliantPercent", HUNDRED); } certificateSummary.put("totalCertificates", certificates.get(CERTIFICATES)); return certificateSummary; }
CertificateRepository implements Constants { public Map<String, Object> getCertificatesSummary(String assetGroup) throws DataException { Map<String, Object> certificateSummary = new HashMap<>(); Map<String, Long> certificates = complianceRepository .getCertificates(assetGroup); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append( assetGroup); urlToQuery.append(CERT_SEARCH); String requestBody = "{\"size\":0,\"aggs\":{\"certs\":{\"children\":{\"type\":\"issue_cert\"},\"aggs\":{\"openfilter\":{\"filter\":{\"term\":{\"issueStatus\":\"open\"}}," + "\"aggs\":{\"rules\":{\"terms\":{\"field\":\"ruleId.keyword\",\"size\":10}}}}}}}}"; String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody); } catch (Exception e) { throw new DataException(e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( "aggregations").toString()); certificateSummary.put(EXP_IN_45_DAYS, 0); certificateSummary.put(EXP_IN_30_DAYS, 0); try { JsonArray rules = aggsJson.getAsJsonObject() .getAsJsonObject("certs").getAsJsonObject("openfilter") .getAsJsonObject("rules").getAsJsonArray(BUCKETS); if (rules.size() > 0) { for (int j = 0; j < rules.size(); j++) { if (SSL_CERT_45_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { certificateSummary.put( EXP_IN_45_DAYS, Integer.valueOf(rules.get(j).getAsJsonObject() .get(DOC_COUNT).toString())); } if (SSL_CERT_30_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { certificateSummary.put( EXP_IN_30_DAYS, Integer.valueOf(rules.get(j).getAsJsonObject() .get(DOC_COUNT).toString())); } } } } catch (Exception e) { LOGGER.error("Error in getCerticatesSummary", e); } float compliantCount = Float.valueOf(certificates.get(CERTIFICATES).toString()) - Float.valueOf(certificates.get("certificates_expiring").toString()); DecimalFormat df = new DecimalFormat("#.00"); if (certificates.get(CERTIFICATES) > 0) { certificateSummary.put("compliantPercent", Math.floor(Float .valueOf(df.format((compliantCount / certificates .get(CERTIFICATES)) * HUNDRED)))); } else { certificateSummary.put("compliantPercent", HUNDRED); } certificateSummary.put("totalCertificates", certificates.get(CERTIFICATES)); return certificateSummary; } }
CertificateRepository implements Constants { public Map<String, Object> getCertificatesSummary(String assetGroup) throws DataException { Map<String, Object> certificateSummary = new HashMap<>(); Map<String, Long> certificates = complianceRepository .getCertificates(assetGroup); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append( assetGroup); urlToQuery.append(CERT_SEARCH); String requestBody = "{\"size\":0,\"aggs\":{\"certs\":{\"children\":{\"type\":\"issue_cert\"},\"aggs\":{\"openfilter\":{\"filter\":{\"term\":{\"issueStatus\":\"open\"}}," + "\"aggs\":{\"rules\":{\"terms\":{\"field\":\"ruleId.keyword\",\"size\":10}}}}}}}}"; String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody); } catch (Exception e) { throw new DataException(e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( "aggregations").toString()); certificateSummary.put(EXP_IN_45_DAYS, 0); certificateSummary.put(EXP_IN_30_DAYS, 0); try { JsonArray rules = aggsJson.getAsJsonObject() .getAsJsonObject("certs").getAsJsonObject("openfilter") .getAsJsonObject("rules").getAsJsonArray(BUCKETS); if (rules.size() > 0) { for (int j = 0; j < rules.size(); j++) { if (SSL_CERT_45_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { certificateSummary.put( EXP_IN_45_DAYS, Integer.valueOf(rules.get(j).getAsJsonObject() .get(DOC_COUNT).toString())); } if (SSL_CERT_30_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { certificateSummary.put( EXP_IN_30_DAYS, Integer.valueOf(rules.get(j).getAsJsonObject() .get(DOC_COUNT).toString())); } } } } catch (Exception e) { LOGGER.error("Error in getCerticatesSummary", e); } float compliantCount = Float.valueOf(certificates.get(CERTIFICATES).toString()) - Float.valueOf(certificates.get("certificates_expiring").toString()); DecimalFormat df = new DecimalFormat("#.00"); if (certificates.get(CERTIFICATES) > 0) { certificateSummary.put("compliantPercent", Math.floor(Float .valueOf(df.format((compliantCount / certificates .get(CERTIFICATES)) * HUNDRED)))); } else { certificateSummary.put("compliantPercent", HUNDRED); } certificateSummary.put("totalCertificates", certificates.get(CERTIFICATES)); return certificateSummary; }  }
CertificateRepository implements Constants { public Map<String, Object> getCertificatesSummary(String assetGroup) throws DataException { Map<String, Object> certificateSummary = new HashMap<>(); Map<String, Long> certificates = complianceRepository .getCertificates(assetGroup); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append( assetGroup); urlToQuery.append(CERT_SEARCH); String requestBody = "{\"size\":0,\"aggs\":{\"certs\":{\"children\":{\"type\":\"issue_cert\"},\"aggs\":{\"openfilter\":{\"filter\":{\"term\":{\"issueStatus\":\"open\"}}," + "\"aggs\":{\"rules\":{\"terms\":{\"field\":\"ruleId.keyword\",\"size\":10}}}}}}}}"; String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody); } catch (Exception e) { throw new DataException(e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( "aggregations").toString()); certificateSummary.put(EXP_IN_45_DAYS, 0); certificateSummary.put(EXP_IN_30_DAYS, 0); try { JsonArray rules = aggsJson.getAsJsonObject() .getAsJsonObject("certs").getAsJsonObject("openfilter") .getAsJsonObject("rules").getAsJsonArray(BUCKETS); if (rules.size() > 0) { for (int j = 0; j < rules.size(); j++) { if (SSL_CERT_45_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { certificateSummary.put( EXP_IN_45_DAYS, Integer.valueOf(rules.get(j).getAsJsonObject() .get(DOC_COUNT).toString())); } if (SSL_CERT_30_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { certificateSummary.put( EXP_IN_30_DAYS, Integer.valueOf(rules.get(j).getAsJsonObject() .get(DOC_COUNT).toString())); } } } } catch (Exception e) { LOGGER.error("Error in getCerticatesSummary", e); } float compliantCount = Float.valueOf(certificates.get(CERTIFICATES).toString()) - Float.valueOf(certificates.get("certificates_expiring").toString()); DecimalFormat df = new DecimalFormat("#.00"); if (certificates.get(CERTIFICATES) > 0) { certificateSummary.put("compliantPercent", Math.floor(Float .valueOf(df.format((compliantCount / certificates .get(CERTIFICATES)) * HUNDRED)))); } else { certificateSummary.put("compliantPercent", HUNDRED); } certificateSummary.put("totalCertificates", certificates.get(CERTIFICATES)); return certificateSummary; }  Map<String, Object> getCertificatesExpiryByApplication(
            String assetGroup); @SuppressWarnings("unchecked") List<Map<String, Object>> getCertificatesDetails(String assetGroup,
            String searchText, Map<String, String> filter); Map<String, Object> getCertificatesSummary(String assetGroup); }
CertificateRepository implements Constants { public Map<String, Object> getCertificatesSummary(String assetGroup) throws DataException { Map<String, Object> certificateSummary = new HashMap<>(); Map<String, Long> certificates = complianceRepository .getCertificates(assetGroup); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append( assetGroup); urlToQuery.append(CERT_SEARCH); String requestBody = "{\"size\":0,\"aggs\":{\"certs\":{\"children\":{\"type\":\"issue_cert\"},\"aggs\":{\"openfilter\":{\"filter\":{\"term\":{\"issueStatus\":\"open\"}}," + "\"aggs\":{\"rules\":{\"terms\":{\"field\":\"ruleId.keyword\",\"size\":10}}}}}}}}"; String responseJson = ""; try { responseJson = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody); } catch (Exception e) { throw new DataException(e); } JsonParser jsonParser = new JsonParser(); JsonObject resultJson = (JsonObject) jsonParser.parse(responseJson); JsonObject aggsJson = (JsonObject) jsonParser.parse(resultJson.get( "aggregations").toString()); certificateSummary.put(EXP_IN_45_DAYS, 0); certificateSummary.put(EXP_IN_30_DAYS, 0); try { JsonArray rules = aggsJson.getAsJsonObject() .getAsJsonObject("certs").getAsJsonObject("openfilter") .getAsJsonObject("rules").getAsJsonArray(BUCKETS); if (rules.size() > 0) { for (int j = 0; j < rules.size(); j++) { if (SSL_CERT_45_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { certificateSummary.put( EXP_IN_45_DAYS, Integer.valueOf(rules.get(j).getAsJsonObject() .get(DOC_COUNT).toString())); } if (SSL_CERT_30_DAYS_EXP_RULE.equals(rules.get(j) .getAsJsonObject().get("key").getAsString())) { certificateSummary.put( EXP_IN_30_DAYS, Integer.valueOf(rules.get(j).getAsJsonObject() .get(DOC_COUNT).toString())); } } } } catch (Exception e) { LOGGER.error("Error in getCerticatesSummary", e); } float compliantCount = Float.valueOf(certificates.get(CERTIFICATES).toString()) - Float.valueOf(certificates.get("certificates_expiring").toString()); DecimalFormat df = new DecimalFormat("#.00"); if (certificates.get(CERTIFICATES) > 0) { certificateSummary.put("compliantPercent", Math.floor(Float .valueOf(df.format((compliantCount / certificates .get(CERTIFICATES)) * HUNDRED)))); } else { certificateSummary.put("compliantPercent", HUNDRED); } certificateSummary.put("totalCertificates", certificates.get(CERTIFICATES)); return certificateSummary; }  Map<String, Object> getCertificatesExpiryByApplication(
            String assetGroup); @SuppressWarnings("unchecked") List<Map<String, Object>> getCertificatesDetails(String assetGroup,
            String searchText, Map<String, String> filter); Map<String, Object> getCertificatesSummary(String assetGroup);  }