@SuppressWarnings("deprecation") @Test public void getGeneralRecommendationsTest() throws Exception { String recommendationJson = "{\"aggregations\":{\"recommendations\":{\"buckets\":[{\"key\":\"abcd\",\"doc_count\":29}]}}}"; mockStatic(PacHttpUtils.class); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(recommendationJson); List<Map<String,Object>> recommendations = new ArrayList<>(); Map<String,Object> recommendation = new HashMap<>(); recommendation.put("checkname","name"); recommendation.put("checkdescription","description"); recommendations.add(recommendation); when(elasticSearchRepository.getDataFromES(anyString(), anyString(), anyObject(), anyObject(), anyObject(), anyObject(), anyObject())).thenReturn(recommendations); assertTrue(recommendationsRepository.getGeneralRecommendations("category", new ArrayList<String>()).size() == 2); }
public Map<String,Object> getGeneralRecommendations(String category, List<String> providers) throws DataException { Map<String,Object> result = new HashMap<>(); List<Map<String,Object>> recommendations = new ArrayList<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append("global_recommendations").append("/") .append("recommendation").append("/").append(Constants.SEARCH); StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}},{\"match\":{\"category.keyword\":\""); requestBody.append(category); requestBody.append("\"}}],\"filter\":[{\"terms\":{\"_cloudType\":[\""); requestBody.append(String.join("\",\" ", providers.stream().collect(Collectors.toList()))); requestBody.append("\"]}}]}},\"aggs\":{\"recommendations\":{\"terms\":{\"field\":\"recommendationId.keyword\",\"size\":10000}}}}"); String responseDetails; try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error("Error in getGeneralRecommendations "+e); throw new DataException(e); } JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject(); JsonArray recommendationBuckets = aggregations.get("recommendations").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); if (recommendationBuckets.size() > 0) { for (int i=0; i<recommendationBuckets.size();i++) { JsonObject recommendationObj = (JsonObject) recommendationBuckets.get(i); if (recommendationObj != null) { String recommendationId = recommendationObj.get(Constants.KEY).getAsString(); Map<String,Object> recommendationInfo = getRecommendation(recommendationId); if(!recommendationInfo.isEmpty()) { Map<String,Object> recommendation = new HashMap<>(); recommendation.put("recommendationId", recommendationId); recommendation.put("recommendation", recommendationInfo.get("checkname")); recommendation.put("description", recommendationInfo.get("checkdescription").toString().replace("[NL]", "<br />")); recommendation.put("recommended", recommendationObj.get(Constants.DOC_COUNT).getAsLong()); recommendations.add(recommendation); } } } } result.put("category", category); result.put("response", recommendations); return result; }
RecommendationsRepository { public Map<String,Object> getGeneralRecommendations(String category, List<String> providers) throws DataException { Map<String,Object> result = new HashMap<>(); List<Map<String,Object>> recommendations = new ArrayList<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append("global_recommendations").append("/") .append("recommendation").append("/").append(Constants.SEARCH); StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}},{\"match\":{\"category.keyword\":\""); requestBody.append(category); requestBody.append("\"}}],\"filter\":[{\"terms\":{\"_cloudType\":[\""); requestBody.append(String.join("\",\" ", providers.stream().collect(Collectors.toList()))); requestBody.append("\"]}}]}},\"aggs\":{\"recommendations\":{\"terms\":{\"field\":\"recommendationId.keyword\",\"size\":10000}}}}"); String responseDetails; try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error("Error in getGeneralRecommendations "+e); throw new DataException(e); } JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject(); JsonArray recommendationBuckets = aggregations.get("recommendations").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); if (recommendationBuckets.size() > 0) { for (int i=0; i<recommendationBuckets.size();i++) { JsonObject recommendationObj = (JsonObject) recommendationBuckets.get(i); if (recommendationObj != null) { String recommendationId = recommendationObj.get(Constants.KEY).getAsString(); Map<String,Object> recommendationInfo = getRecommendation(recommendationId); if(!recommendationInfo.isEmpty()) { Map<String,Object> recommendation = new HashMap<>(); recommendation.put("recommendationId", recommendationId); recommendation.put("recommendation", recommendationInfo.get("checkname")); recommendation.put("description", recommendationInfo.get("checkdescription").toString().replace("[NL]", "<br />")); recommendation.put("recommended", recommendationObj.get(Constants.DOC_COUNT).getAsLong()); recommendations.add(recommendation); } } } } result.put("category", category); result.put("response", recommendations); return result; } }
RecommendationsRepository { public Map<String,Object> getGeneralRecommendations(String category, List<String> providers) throws DataException { Map<String,Object> result = new HashMap<>(); List<Map<String,Object>> recommendations = new ArrayList<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append("global_recommendations").append("/") .append("recommendation").append("/").append(Constants.SEARCH); StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}},{\"match\":{\"category.keyword\":\""); requestBody.append(category); requestBody.append("\"}}],\"filter\":[{\"terms\":{\"_cloudType\":[\""); requestBody.append(String.join("\",\" ", providers.stream().collect(Collectors.toList()))); requestBody.append("\"]}}]}},\"aggs\":{\"recommendations\":{\"terms\":{\"field\":\"recommendationId.keyword\",\"size\":10000}}}}"); String responseDetails; try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error("Error in getGeneralRecommendations "+e); throw new DataException(e); } JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject(); JsonArray recommendationBuckets = aggregations.get("recommendations").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); if (recommendationBuckets.size() > 0) { for (int i=0; i<recommendationBuckets.size();i++) { JsonObject recommendationObj = (JsonObject) recommendationBuckets.get(i); if (recommendationObj != null) { String recommendationId = recommendationObj.get(Constants.KEY).getAsString(); Map<String,Object> recommendationInfo = getRecommendation(recommendationId); if(!recommendationInfo.isEmpty()) { Map<String,Object> recommendation = new HashMap<>(); recommendation.put("recommendationId", recommendationId); recommendation.put("recommendation", recommendationInfo.get("checkname")); recommendation.put("description", recommendationInfo.get("checkdescription").toString().replace("[NL]", "<br />")); recommendation.put("recommended", recommendationObj.get(Constants.DOC_COUNT).getAsLong()); recommendations.add(recommendation); } } } } result.put("category", category); result.put("response", recommendations); return result; }  }
RecommendationsRepository { public Map<String,Object> getGeneralRecommendations(String category, List<String> providers) throws DataException { Map<String,Object> result = new HashMap<>(); List<Map<String,Object>> recommendations = new ArrayList<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append("global_recommendations").append("/") .append("recommendation").append("/").append(Constants.SEARCH); StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}},{\"match\":{\"category.keyword\":\""); requestBody.append(category); requestBody.append("\"}}],\"filter\":[{\"terms\":{\"_cloudType\":[\""); requestBody.append(String.join("\",\" ", providers.stream().collect(Collectors.toList()))); requestBody.append("\"]}}]}},\"aggs\":{\"recommendations\":{\"terms\":{\"field\":\"recommendationId.keyword\",\"size\":10000}}}}"); String responseDetails; try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error("Error in getGeneralRecommendations "+e); throw new DataException(e); } JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject(); JsonArray recommendationBuckets = aggregations.get("recommendations").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); if (recommendationBuckets.size() > 0) { for (int i=0; i<recommendationBuckets.size();i++) { JsonObject recommendationObj = (JsonObject) recommendationBuckets.get(i); if (recommendationObj != null) { String recommendationId = recommendationObj.get(Constants.KEY).getAsString(); Map<String,Object> recommendationInfo = getRecommendation(recommendationId); if(!recommendationInfo.isEmpty()) { Map<String,Object> recommendation = new HashMap<>(); recommendation.put("recommendationId", recommendationId); recommendation.put("recommendation", recommendationInfo.get("checkname")); recommendation.put("description", recommendationInfo.get("checkdescription").toString().replace("[NL]", "<br />")); recommendation.put("recommended", recommendationObj.get(Constants.DOC_COUNT).getAsLong()); recommendations.add(recommendation); } } } } result.put("category", category); result.put("response", recommendations); return result; }  List<Map<String,Object>> getRecommendationSummary(String assetGroup, String application); Map<String,Object> getSummaryByApplication(String assetGroup, String category); Map<String, Object> getSummaryByApplication(String assetGroup); Map<String,Object> getRecommendations(String assetGroup, String category, String application); Map<String,Object> getRecommendationDetail(String assetGroup, String recommendationId, String application); @SuppressWarnings("deprecation") Map<String,Object> getRecommendation(String recommendationId); List<Map<String,Object>> getGeneralRecommendationSummary(List<String> providers); Map<String,Object> getGeneralRecommendations(String category, List<String> providers); Map<String,Object> getGeneralRecommendationDetail(String recommendationId); }
RecommendationsRepository { public Map<String,Object> getGeneralRecommendations(String category, List<String> providers) throws DataException { Map<String,Object> result = new HashMap<>(); List<Map<String,Object>> recommendations = new ArrayList<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append("global_recommendations").append("/") .append("recommendation").append("/").append(Constants.SEARCH); StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}},{\"match\":{\"category.keyword\":\""); requestBody.append(category); requestBody.append("\"}}],\"filter\":[{\"terms\":{\"_cloudType\":[\""); requestBody.append(String.join("\",\" ", providers.stream().collect(Collectors.toList()))); requestBody.append("\"]}}]}},\"aggs\":{\"recommendations\":{\"terms\":{\"field\":\"recommendationId.keyword\",\"size\":10000}}}}"); String responseDetails; try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error("Error in getGeneralRecommendations "+e); throw new DataException(e); } JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject(); JsonArray recommendationBuckets = aggregations.get("recommendations").getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); if (recommendationBuckets.size() > 0) { for (int i=0; i<recommendationBuckets.size();i++) { JsonObject recommendationObj = (JsonObject) recommendationBuckets.get(i); if (recommendationObj != null) { String recommendationId = recommendationObj.get(Constants.KEY).getAsString(); Map<String,Object> recommendationInfo = getRecommendation(recommendationId); if(!recommendationInfo.isEmpty()) { Map<String,Object> recommendation = new HashMap<>(); recommendation.put("recommendationId", recommendationId); recommendation.put("recommendation", recommendationInfo.get("checkname")); recommendation.put("description", recommendationInfo.get("checkdescription").toString().replace("[NL]", "<br />")); recommendation.put("recommended", recommendationObj.get(Constants.DOC_COUNT).getAsLong()); recommendations.add(recommendation); } } } } result.put("category", category); result.put("response", recommendations); return result; }  List<Map<String,Object>> getRecommendationSummary(String assetGroup, String application); Map<String,Object> getSummaryByApplication(String assetGroup, String category); Map<String, Object> getSummaryByApplication(String assetGroup); Map<String,Object> getRecommendations(String assetGroup, String category, String application); Map<String,Object> getRecommendationDetail(String assetGroup, String recommendationId, String application); @SuppressWarnings("deprecation") Map<String,Object> getRecommendation(String recommendationId); List<Map<String,Object>> getGeneralRecommendationSummary(List<String> providers); Map<String,Object> getGeneralRecommendations(String category, List<String> providers); Map<String,Object> getGeneralRecommendationDetail(String recommendationId);  }