@Test public void getRecommendationDetailTest() throws Exception { String response = "{\"_scroll_id\":\"id\",\"hits\":{\"hits\":[{\"_source\":{\"_resourceid\":\"123\",\"monthlysavings\":2," + "\"recommendation\":\"Amazon EBS Volumes\",\"recommendationId\":\"DAvU99Dc4C\",\"resourceinfo\":{\"Volume Type\":\"General purpose(SSD)\"," + "\"Volume Size\":\"20\",\"Snapshot Name\":\"null\",\"Monthly Storage Cost\":\"$2.00\"},\"category\":\"cost_optimizing\"," + "\"type\":\"volume\",\"_loaddate\":\"2019-06-12\",\"status\":\"warning\",\"latest\":true}}]}}"; String countresponse = "{\"count\":43}"; when(rdsRepository.queryForString(anyString())).thenReturn("parent"); mockStatic(PacHttpUtils.class); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(countresponse,response); when(elasticSearchRepository.buildScrollRequest(anyString(), anyString())).thenReturn("request"); assertTrue(recommendationsRepository.getRecommendationDetail("ag", "id","app").size() == 1); }
public Map<String,Object> getRecommendationDetail(String assetGroup, String recommendationId, String application) throws DataException { Map<String,Object> recommendationDetail = new HashMap<>(); List<Map<String,Object>> resources = new ArrayList<>(); String parentType = ""; if(StringUtils.isNotBlank(application)) { String query = "SELECT type FROM Recommendation_Mappings WHERE checkId =\""+recommendationId+"\""; try { parentType = rdsRepository.queryForString(query); } catch (Exception exception) { LOGGER.error("Error in getRecommendationDetail for getting parent type " , exception); } } StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/").append(assetGroup).append("/") .append("recommendation").append("/").append(Constants.SEARCH).append("?scroll=") .append(Constants.ES_PAGE_SCROLL_TTL); String urlToQuery = urlToQueryBuffer.toString(); String urlToScroll = new StringBuilder(esUrl).append("/").append(Constants.SEARCH).append("/scroll") .toString(); StringBuilder requestBody = new StringBuilder("{\"size\":10000,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"match\":{\"recommendationId.keyword\":\""); requestBody.append(recommendationId).append("\"}}"); if(StringUtils.isNotBlank(application)) { requestBody.append(",{\"has_parent\":{\"parent_type\":\""); requestBody.append(parentType); requestBody.append("\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}},{\"match\":{\"_entity.keyword\":true}},{\"match\":{\"tags.Application.keyword\":\""); requestBody.append(application); requestBody.append("\"}}]}}}}"); } requestBody.append("]}}}"); long totalDocs = getTotalDocCount(assetGroup, "recommendation", "{" + requestBody.toString().substring(14)); String request = requestBody.toString(); String scrollId = null; if(totalDocs > 0){ for (int index = 0; index <= (totalDocs / Constants.ES_PAGE_SIZE); index++) { String responseDetails = null; try { if (StringUtils.isNotBlank(scrollId)) { request = elasticSearchRepository.buildScrollRequest(scrollId, Constants.ES_PAGE_SCROLL_TTL); urlToQuery = urlToScroll; } responseDetails = PacHttpUtils.doHttpPost(urlToQuery, request); scrollId = processResponseAndSendTheScrollBack(responseDetails, resources); } catch (Exception e) { } } } recommendationDetail.put("resources", resources); return recommendationDetail; }
RecommendationsRepository { public Map<String,Object> getRecommendationDetail(String assetGroup, String recommendationId, String application) throws DataException { Map<String,Object> recommendationDetail = new HashMap<>(); List<Map<String,Object>> resources = new ArrayList<>(); String parentType = ""; if(StringUtils.isNotBlank(application)) { String query = "SELECT type FROM Recommendation_Mappings WHERE checkId =\""+recommendationId+"\""; try { parentType = rdsRepository.queryForString(query); } catch (Exception exception) { LOGGER.error("Error in getRecommendationDetail for getting parent type " , exception); } } StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/").append(assetGroup).append("/") .append("recommendation").append("/").append(Constants.SEARCH).append("?scroll=") .append(Constants.ES_PAGE_SCROLL_TTL); String urlToQuery = urlToQueryBuffer.toString(); String urlToScroll = new StringBuilder(esUrl).append("/").append(Constants.SEARCH).append("/scroll") .toString(); StringBuilder requestBody = new StringBuilder("{\"size\":10000,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"match\":{\"recommendationId.keyword\":\""); requestBody.append(recommendationId).append("\"}}"); if(StringUtils.isNotBlank(application)) { requestBody.append(",{\"has_parent\":{\"parent_type\":\""); requestBody.append(parentType); requestBody.append("\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}},{\"match\":{\"_entity.keyword\":true}},{\"match\":{\"tags.Application.keyword\":\""); requestBody.append(application); requestBody.append("\"}}]}}}}"); } requestBody.append("]}}}"); long totalDocs = getTotalDocCount(assetGroup, "recommendation", "{" + requestBody.toString().substring(14)); String request = requestBody.toString(); String scrollId = null; if(totalDocs > 0){ for (int index = 0; index <= (totalDocs / Constants.ES_PAGE_SIZE); index++) { String responseDetails = null; try { if (StringUtils.isNotBlank(scrollId)) { request = elasticSearchRepository.buildScrollRequest(scrollId, Constants.ES_PAGE_SCROLL_TTL); urlToQuery = urlToScroll; } responseDetails = PacHttpUtils.doHttpPost(urlToQuery, request); scrollId = processResponseAndSendTheScrollBack(responseDetails, resources); } catch (Exception e) { } } } recommendationDetail.put("resources", resources); return recommendationDetail; } }
RecommendationsRepository { public Map<String,Object> getRecommendationDetail(String assetGroup, String recommendationId, String application) throws DataException { Map<String,Object> recommendationDetail = new HashMap<>(); List<Map<String,Object>> resources = new ArrayList<>(); String parentType = ""; if(StringUtils.isNotBlank(application)) { String query = "SELECT type FROM Recommendation_Mappings WHERE checkId =\""+recommendationId+"\""; try { parentType = rdsRepository.queryForString(query); } catch (Exception exception) { LOGGER.error("Error in getRecommendationDetail for getting parent type " , exception); } } StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/").append(assetGroup).append("/") .append("recommendation").append("/").append(Constants.SEARCH).append("?scroll=") .append(Constants.ES_PAGE_SCROLL_TTL); String urlToQuery = urlToQueryBuffer.toString(); String urlToScroll = new StringBuilder(esUrl).append("/").append(Constants.SEARCH).append("/scroll") .toString(); StringBuilder requestBody = new StringBuilder("{\"size\":10000,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"match\":{\"recommendationId.keyword\":\""); requestBody.append(recommendationId).append("\"}}"); if(StringUtils.isNotBlank(application)) { requestBody.append(",{\"has_parent\":{\"parent_type\":\""); requestBody.append(parentType); requestBody.append("\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}},{\"match\":{\"_entity.keyword\":true}},{\"match\":{\"tags.Application.keyword\":\""); requestBody.append(application); requestBody.append("\"}}]}}}}"); } requestBody.append("]}}}"); long totalDocs = getTotalDocCount(assetGroup, "recommendation", "{" + requestBody.toString().substring(14)); String request = requestBody.toString(); String scrollId = null; if(totalDocs > 0){ for (int index = 0; index <= (totalDocs / Constants.ES_PAGE_SIZE); index++) { String responseDetails = null; try { if (StringUtils.isNotBlank(scrollId)) { request = elasticSearchRepository.buildScrollRequest(scrollId, Constants.ES_PAGE_SCROLL_TTL); urlToQuery = urlToScroll; } responseDetails = PacHttpUtils.doHttpPost(urlToQuery, request); scrollId = processResponseAndSendTheScrollBack(responseDetails, resources); } catch (Exception e) { } } } recommendationDetail.put("resources", resources); return recommendationDetail; }  }
RecommendationsRepository { public Map<String,Object> getRecommendationDetail(String assetGroup, String recommendationId, String application) throws DataException { Map<String,Object> recommendationDetail = new HashMap<>(); List<Map<String,Object>> resources = new ArrayList<>(); String parentType = ""; if(StringUtils.isNotBlank(application)) { String query = "SELECT type FROM Recommendation_Mappings WHERE checkId =\""+recommendationId+"\""; try { parentType = rdsRepository.queryForString(query); } catch (Exception exception) { LOGGER.error("Error in getRecommendationDetail for getting parent type " , exception); } } StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/").append(assetGroup).append("/") .append("recommendation").append("/").append(Constants.SEARCH).append("?scroll=") .append(Constants.ES_PAGE_SCROLL_TTL); String urlToQuery = urlToQueryBuffer.toString(); String urlToScroll = new StringBuilder(esUrl).append("/").append(Constants.SEARCH).append("/scroll") .toString(); StringBuilder requestBody = new StringBuilder("{\"size\":10000,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"match\":{\"recommendationId.keyword\":\""); requestBody.append(recommendationId).append("\"}}"); if(StringUtils.isNotBlank(application)) { requestBody.append(",{\"has_parent\":{\"parent_type\":\""); requestBody.append(parentType); requestBody.append("\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}},{\"match\":{\"_entity.keyword\":true}},{\"match\":{\"tags.Application.keyword\":\""); requestBody.append(application); requestBody.append("\"}}]}}}}"); } requestBody.append("]}}}"); long totalDocs = getTotalDocCount(assetGroup, "recommendation", "{" + requestBody.toString().substring(14)); String request = requestBody.toString(); String scrollId = null; if(totalDocs > 0){ for (int index = 0; index <= (totalDocs / Constants.ES_PAGE_SIZE); index++) { String responseDetails = null; try { if (StringUtils.isNotBlank(scrollId)) { request = elasticSearchRepository.buildScrollRequest(scrollId, Constants.ES_PAGE_SCROLL_TTL); urlToQuery = urlToScroll; } responseDetails = PacHttpUtils.doHttpPost(urlToQuery, request); scrollId = processResponseAndSendTheScrollBack(responseDetails, resources); } catch (Exception e) { } } } recommendationDetail.put("resources", resources); return recommendationDetail; }  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> getRecommendationDetail(String assetGroup, String recommendationId, String application) throws DataException { Map<String,Object> recommendationDetail = new HashMap<>(); List<Map<String,Object>> resources = new ArrayList<>(); String parentType = ""; if(StringUtils.isNotBlank(application)) { String query = "SELECT type FROM Recommendation_Mappings WHERE checkId =\""+recommendationId+"\""; try { parentType = rdsRepository.queryForString(query); } catch (Exception exception) { LOGGER.error("Error in getRecommendationDetail for getting parent type " , exception); } } StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/").append(assetGroup).append("/") .append("recommendation").append("/").append(Constants.SEARCH).append("?scroll=") .append(Constants.ES_PAGE_SCROLL_TTL); String urlToQuery = urlToQueryBuffer.toString(); String urlToScroll = new StringBuilder(esUrl).append("/").append(Constants.SEARCH).append("/scroll") .toString(); StringBuilder requestBody = new StringBuilder("{\"size\":10000,\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":\"true\"}},{\"match\":{\"recommendationId.keyword\":\""); requestBody.append(recommendationId).append("\"}}"); if(StringUtils.isNotBlank(application)) { requestBody.append(",{\"has_parent\":{\"parent_type\":\""); requestBody.append(parentType); requestBody.append("\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"latest\":true}},{\"match\":{\"_entity.keyword\":true}},{\"match\":{\"tags.Application.keyword\":\""); requestBody.append(application); requestBody.append("\"}}]}}}}"); } requestBody.append("]}}}"); long totalDocs = getTotalDocCount(assetGroup, "recommendation", "{" + requestBody.toString().substring(14)); String request = requestBody.toString(); String scrollId = null; if(totalDocs > 0){ for (int index = 0; index <= (totalDocs / Constants.ES_PAGE_SIZE); index++) { String responseDetails = null; try { if (StringUtils.isNotBlank(scrollId)) { request = elasticSearchRepository.buildScrollRequest(scrollId, Constants.ES_PAGE_SCROLL_TTL); urlToQuery = urlToScroll; } responseDetails = PacHttpUtils.doHttpPost(urlToQuery, request); scrollId = processResponseAndSendTheScrollBack(responseDetails, resources); } catch (Exception e) { } } } recommendationDetail.put("resources", resources); return recommendationDetail; }  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);  }