@Test public void getRecommendationSummaryTest_Exception() throws Exception { mockStatic(PacHttpUtils.class); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenThrow(new DataException()); assertThatThrownBy(() -> recommendationsRepository.getRecommendationSummary("ag", null)).isInstanceOf(DataException.class); }
public List<Map<String,Object>> getRecommendationSummary(String assetGroup, String application) throws DataException { List<Map<String,Object>> recommendationSummary = new ArrayList<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup).append("/").append(Constants.SEARCH); StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}}"); if(StringUtils.isNotBlank(application)) { requestBody.append(",{\"match\":{\"tags.Application.keyword\":\""); requestBody.append(application); requestBody.append("\"}}"); } requestBody.append("]}},\"aggs\":{\"recommendations\":{\"children\":{\"type\":\"recommendation\"},\"aggs\":{\"latest\":{\"filter\":{\"term\":{\"latest\":\"true\"}}," + "\"aggs\":{\"category\":{\"terms\":{\"field\":\"category.keyword\",\"size\":1000},\"aggs\":{\"savings\":{\"sum\":{\"field\":\"monthlysavings\"}}}}}}}}}}"); String responseDetails; try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error("Error while fetching recommendation summary from ES ", e); throw new DataException(e); } JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject(); boolean dataAvailable = false; if(aggregations != null) { JsonObject recommendations = aggregations.get("recommendations").getAsJsonObject(); if(recommendations.has("latest") && recommendations.get("latest").getAsJsonObject().has("category")) { JsonArray categoryBuckets = recommendations.get("latest").getAsJsonObject().get("category") .getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); if (categoryBuckets.size() > 0) { dataAvailable = true; for (int i=0; i<categoryBuckets.size();i++) { JsonObject categoryObj = (JsonObject) categoryBuckets.get(i); if (categoryObj != null) { Map<String,Object> category = new HashMap<>(); category.put("category", categoryObj.get("key").getAsString()); category.put("recommendations", categoryObj.get("doc_count").getAsLong()); JsonObject savingsObj = categoryObj.get("savings").getAsJsonObject(); if(savingsObj.size() != 0) { long potentialMonthlySavings = Math.round(savingsObj.get("value").getAsDouble()); if(potentialMonthlySavings > 0) { category.put("potentialMonthlySavings", potentialMonthlySavings); } } recommendationSummary.add(category); } } } } } if(!dataAvailable) { String[] categories = recommendationCategories.split(","); for(int i=0; i < categories.length; i++) { Map<String,Object> category = new HashMap<>(); category.put("category", categories[i]); category.put("recommendations", 0L); recommendationSummary.add(category); } } return recommendationSummary; }
RecommendationsRepository { public List<Map<String,Object>> getRecommendationSummary(String assetGroup, String application) throws DataException { List<Map<String,Object>> recommendationSummary = new ArrayList<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup).append("/").append(Constants.SEARCH); StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}}"); if(StringUtils.isNotBlank(application)) { requestBody.append(",{\"match\":{\"tags.Application.keyword\":\""); requestBody.append(application); requestBody.append("\"}}"); } requestBody.append("]}},\"aggs\":{\"recommendations\":{\"children\":{\"type\":\"recommendation\"},\"aggs\":{\"latest\":{\"filter\":{\"term\":{\"latest\":\"true\"}}," + "\"aggs\":{\"category\":{\"terms\":{\"field\":\"category.keyword\",\"size\":1000},\"aggs\":{\"savings\":{\"sum\":{\"field\":\"monthlysavings\"}}}}}}}}}}"); String responseDetails; try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error("Error while fetching recommendation summary from ES ", e); throw new DataException(e); } JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject(); boolean dataAvailable = false; if(aggregations != null) { JsonObject recommendations = aggregations.get("recommendations").getAsJsonObject(); if(recommendations.has("latest") && recommendations.get("latest").getAsJsonObject().has("category")) { JsonArray categoryBuckets = recommendations.get("latest").getAsJsonObject().get("category") .getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); if (categoryBuckets.size() > 0) { dataAvailable = true; for (int i=0; i<categoryBuckets.size();i++) { JsonObject categoryObj = (JsonObject) categoryBuckets.get(i); if (categoryObj != null) { Map<String,Object> category = new HashMap<>(); category.put("category", categoryObj.get("key").getAsString()); category.put("recommendations", categoryObj.get("doc_count").getAsLong()); JsonObject savingsObj = categoryObj.get("savings").getAsJsonObject(); if(savingsObj.size() != 0) { long potentialMonthlySavings = Math.round(savingsObj.get("value").getAsDouble()); if(potentialMonthlySavings > 0) { category.put("potentialMonthlySavings", potentialMonthlySavings); } } recommendationSummary.add(category); } } } } } if(!dataAvailable) { String[] categories = recommendationCategories.split(","); for(int i=0; i < categories.length; i++) { Map<String,Object> category = new HashMap<>(); category.put("category", categories[i]); category.put("recommendations", 0L); recommendationSummary.add(category); } } return recommendationSummary; } }
RecommendationsRepository { public List<Map<String,Object>> getRecommendationSummary(String assetGroup, String application) throws DataException { List<Map<String,Object>> recommendationSummary = new ArrayList<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup).append("/").append(Constants.SEARCH); StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}}"); if(StringUtils.isNotBlank(application)) { requestBody.append(",{\"match\":{\"tags.Application.keyword\":\""); requestBody.append(application); requestBody.append("\"}}"); } requestBody.append("]}},\"aggs\":{\"recommendations\":{\"children\":{\"type\":\"recommendation\"},\"aggs\":{\"latest\":{\"filter\":{\"term\":{\"latest\":\"true\"}}," + "\"aggs\":{\"category\":{\"terms\":{\"field\":\"category.keyword\",\"size\":1000},\"aggs\":{\"savings\":{\"sum\":{\"field\":\"monthlysavings\"}}}}}}}}}}"); String responseDetails; try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error("Error while fetching recommendation summary from ES ", e); throw new DataException(e); } JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject(); boolean dataAvailable = false; if(aggregations != null) { JsonObject recommendations = aggregations.get("recommendations").getAsJsonObject(); if(recommendations.has("latest") && recommendations.get("latest").getAsJsonObject().has("category")) { JsonArray categoryBuckets = recommendations.get("latest").getAsJsonObject().get("category") .getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); if (categoryBuckets.size() > 0) { dataAvailable = true; for (int i=0; i<categoryBuckets.size();i++) { JsonObject categoryObj = (JsonObject) categoryBuckets.get(i); if (categoryObj != null) { Map<String,Object> category = new HashMap<>(); category.put("category", categoryObj.get("key").getAsString()); category.put("recommendations", categoryObj.get("doc_count").getAsLong()); JsonObject savingsObj = categoryObj.get("savings").getAsJsonObject(); if(savingsObj.size() != 0) { long potentialMonthlySavings = Math.round(savingsObj.get("value").getAsDouble()); if(potentialMonthlySavings > 0) { category.put("potentialMonthlySavings", potentialMonthlySavings); } } recommendationSummary.add(category); } } } } } if(!dataAvailable) { String[] categories = recommendationCategories.split(","); for(int i=0; i < categories.length; i++) { Map<String,Object> category = new HashMap<>(); category.put("category", categories[i]); category.put("recommendations", 0L); recommendationSummary.add(category); } } return recommendationSummary; }  }
RecommendationsRepository { public List<Map<String,Object>> getRecommendationSummary(String assetGroup, String application) throws DataException { List<Map<String,Object>> recommendationSummary = new ArrayList<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup).append("/").append(Constants.SEARCH); StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}}"); if(StringUtils.isNotBlank(application)) { requestBody.append(",{\"match\":{\"tags.Application.keyword\":\""); requestBody.append(application); requestBody.append("\"}}"); } requestBody.append("]}},\"aggs\":{\"recommendations\":{\"children\":{\"type\":\"recommendation\"},\"aggs\":{\"latest\":{\"filter\":{\"term\":{\"latest\":\"true\"}}," + "\"aggs\":{\"category\":{\"terms\":{\"field\":\"category.keyword\",\"size\":1000},\"aggs\":{\"savings\":{\"sum\":{\"field\":\"monthlysavings\"}}}}}}}}}}"); String responseDetails; try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error("Error while fetching recommendation summary from ES ", e); throw new DataException(e); } JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject(); boolean dataAvailable = false; if(aggregations != null) { JsonObject recommendations = aggregations.get("recommendations").getAsJsonObject(); if(recommendations.has("latest") && recommendations.get("latest").getAsJsonObject().has("category")) { JsonArray categoryBuckets = recommendations.get("latest").getAsJsonObject().get("category") .getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); if (categoryBuckets.size() > 0) { dataAvailable = true; for (int i=0; i<categoryBuckets.size();i++) { JsonObject categoryObj = (JsonObject) categoryBuckets.get(i); if (categoryObj != null) { Map<String,Object> category = new HashMap<>(); category.put("category", categoryObj.get("key").getAsString()); category.put("recommendations", categoryObj.get("doc_count").getAsLong()); JsonObject savingsObj = categoryObj.get("savings").getAsJsonObject(); if(savingsObj.size() != 0) { long potentialMonthlySavings = Math.round(savingsObj.get("value").getAsDouble()); if(potentialMonthlySavings > 0) { category.put("potentialMonthlySavings", potentialMonthlySavings); } } recommendationSummary.add(category); } } } } } if(!dataAvailable) { String[] categories = recommendationCategories.split(","); for(int i=0; i < categories.length; i++) { Map<String,Object> category = new HashMap<>(); category.put("category", categories[i]); category.put("recommendations", 0L); recommendationSummary.add(category); } } return recommendationSummary; }  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 List<Map<String,Object>> getRecommendationSummary(String assetGroup, String application) throws DataException { List<Map<String,Object>> recommendationSummary = new ArrayList<>(); StringBuilder urlToQuery = new StringBuilder(esUrl).append("/").append(assetGroup).append("/").append(Constants.SEARCH); StringBuilder requestBody = new StringBuilder("{\"size\":0,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}}"); if(StringUtils.isNotBlank(application)) { requestBody.append(",{\"match\":{\"tags.Application.keyword\":\""); requestBody.append(application); requestBody.append("\"}}"); } requestBody.append("]}},\"aggs\":{\"recommendations\":{\"children\":{\"type\":\"recommendation\"},\"aggs\":{\"latest\":{\"filter\":{\"term\":{\"latest\":\"true\"}}," + "\"aggs\":{\"category\":{\"terms\":{\"field\":\"category.keyword\",\"size\":1000},\"aggs\":{\"savings\":{\"sum\":{\"field\":\"monthlysavings\"}}}}}}}}}}"); String responseDetails; try { responseDetails = PacHttpUtils.doHttpPost(urlToQuery.toString(), requestBody.toString()); } catch (Exception e) { LOGGER.error("Error while fetching recommendation summary from ES ", e); throw new DataException(e); } JsonParser parser = new JsonParser(); JsonObject responseDetailsjson = parser.parse(responseDetails).getAsJsonObject(); JsonObject aggregations = responseDetailsjson.get(Constants.AGGREGATIONS).getAsJsonObject(); boolean dataAvailable = false; if(aggregations != null) { JsonObject recommendations = aggregations.get("recommendations").getAsJsonObject(); if(recommendations.has("latest") && recommendations.get("latest").getAsJsonObject().has("category")) { JsonArray categoryBuckets = recommendations.get("latest").getAsJsonObject().get("category") .getAsJsonObject().get(Constants.BUCKETS).getAsJsonArray(); if (categoryBuckets.size() > 0) { dataAvailable = true; for (int i=0; i<categoryBuckets.size();i++) { JsonObject categoryObj = (JsonObject) categoryBuckets.get(i); if (categoryObj != null) { Map<String,Object> category = new HashMap<>(); category.put("category", categoryObj.get("key").getAsString()); category.put("recommendations", categoryObj.get("doc_count").getAsLong()); JsonObject savingsObj = categoryObj.get("savings").getAsJsonObject(); if(savingsObj.size() != 0) { long potentialMonthlySavings = Math.round(savingsObj.get("value").getAsDouble()); if(potentialMonthlySavings > 0) { category.put("potentialMonthlySavings", potentialMonthlySavings); } } recommendationSummary.add(category); } } } } } if(!dataAvailable) { String[] categories = recommendationCategories.split(","); for(int i=0; i < categories.length; i++) { Map<String,Object> category = new HashMap<>(); category.put("category", categories[i]); category.put("recommendations", 0L); recommendationSummary.add(category); } } return recommendationSummary; }  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);  }