@Test public void getRelevantFAQSFromEs() throws Exception { List<Map<String, Object>> releventFaqsList = new ArrayList<>(); List<String> faqsList = new ArrayList<>(); List<String> tags = new ArrayList<>(); String response = "{\"took\":2,\"timed_out\":false,\"_shards\":{\"total\":5,\"successful\":5,\"failed\":0},\"hits\":{\"total\":17,\"max_score\":1,\"hits\":[{\"_index\":\"faqs\",\"_type\":\"faqinfo\",\"_id\":\"w1q2\",\"_score\":1,\"_source\":{\"faqid\":\"q2\",\"faqname\":\"How is patching compliance % calculated ?\",\"answer\":\"Total patched resources divided by total running resources\",\"widgetid\":\"w1\",\"tag\":\"patching\"}}]}}"; Map<String, Object> faq = new HashMap<>(); faq.put("faqid", "w1q1"); faq.put("widgetid", "w1"); faq.put("faq", "how is overallComplaince % calculated"); releventFaqsList.add(faq); faqsList.add("w1q4"); faqsList.add("w1q5"); tags.add("patching"); mockStatic(PacHttpUtils.class); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenReturn(response); ReflectionTestUtils.setField(faqRepositoryImpl, "esUrl", "dummyEsURL"); List<Map<String, Object>> releventFaqs = faqRepositoryImpl.getRelevantFAQSFromEs("w1", "1", tags, faqsList, releventFaqsList); assertTrue(releventFaqs.size() > 0 || releventFaqs.size() == 0); when(PacHttpUtils.doHttpPost(anyString(), anyString())).thenThrow(new DataException()); assertThatThrownBy(() -> faqRepositoryImpl.getRelevantFAQSFromEs("w1", "1", tags, faqsList, releventFaqsList)).isInstanceOf(DataException.class); }
@Override public List<Map<String, Object>> getRelevantFAQSFromEs(String widgetId, String domainId, List<String> tag, List<String> faqid, List<Map<String, Object>> releventfaqs) throws DataException { JsonParser jsonParser = new JsonParser(); StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") .append("faqs/faqinfo").append("/").append("_search"); StringBuilder requestFaqBody = new StringBuilder( "{\"_source\":[\"faqname\",\"answer\"],\"query\":{\"bool\":{\"must\":[{\"terms\":{\"tag.keyword\":" + tag + "}},{\"match\":{\"domainid.keyword\":\"" + domainId + "\"}}],\"must_not\":[{\"terms\":{\"faqid.keyword\":" + faqid + "}}]}}}"); String responseFaqJson ; try{ responseFaqJson= PacHttpUtils.doHttpPost( urlToQueryBuffer.toString(), requestFaqBody.toString()); }catch(Exception e){ throw new DataException(e); } JsonObject resultFaqJson = (JsonObject) jsonParser .parse(responseFaqJson); JsonObject hitsFaq = (JsonObject) resultFaqJson.get("hits"); JsonArray jsonArrayFaq = hitsFaq.get("hits").getAsJsonArray(); for (int j = 0; j < jsonArrayFaq.size(); j++) { JsonObject sourceFaq = (JsonObject) jsonArrayFaq.get(j) .getAsJsonObject().get("_source"); ConcurrentHashMap<String, Object> relectfaqDetails = new ConcurrentHashMap<>(); relectfaqDetails.put("faqName", sourceFaq.get("faqname") .getAsString()); relectfaqDetails.put("faqAnswer", sourceFaq.get("answer") .getAsString()); releventfaqs.add(relectfaqDetails); } return releventfaqs; }
FAQRespositoryImpl implements FAQRepository, Constants { @Override public List<Map<String, Object>> getRelevantFAQSFromEs(String widgetId, String domainId, List<String> tag, List<String> faqid, List<Map<String, Object>> releventfaqs) throws DataException { JsonParser jsonParser = new JsonParser(); StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") .append("faqs/faqinfo").append("/").append("_search"); StringBuilder requestFaqBody = new StringBuilder( "{\"_source\":[\"faqname\",\"answer\"],\"query\":{\"bool\":{\"must\":[{\"terms\":{\"tag.keyword\":" + tag + "}},{\"match\":{\"domainid.keyword\":\"" + domainId + "\"}}],\"must_not\":[{\"terms\":{\"faqid.keyword\":" + faqid + "}}]}}}"); String responseFaqJson ; try{ responseFaqJson= PacHttpUtils.doHttpPost( urlToQueryBuffer.toString(), requestFaqBody.toString()); }catch(Exception e){ throw new DataException(e); } JsonObject resultFaqJson = (JsonObject) jsonParser .parse(responseFaqJson); JsonObject hitsFaq = (JsonObject) resultFaqJson.get("hits"); JsonArray jsonArrayFaq = hitsFaq.get("hits").getAsJsonArray(); for (int j = 0; j < jsonArrayFaq.size(); j++) { JsonObject sourceFaq = (JsonObject) jsonArrayFaq.get(j) .getAsJsonObject().get("_source"); ConcurrentHashMap<String, Object> relectfaqDetails = new ConcurrentHashMap<>(); relectfaqDetails.put("faqName", sourceFaq.get("faqname") .getAsString()); relectfaqDetails.put("faqAnswer", sourceFaq.get("answer") .getAsString()); releventfaqs.add(relectfaqDetails); } return releventfaqs; } }
FAQRespositoryImpl implements FAQRepository, Constants { @Override public List<Map<String, Object>> getRelevantFAQSFromEs(String widgetId, String domainId, List<String> tag, List<String> faqid, List<Map<String, Object>> releventfaqs) throws DataException { JsonParser jsonParser = new JsonParser(); StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") .append("faqs/faqinfo").append("/").append("_search"); StringBuilder requestFaqBody = new StringBuilder( "{\"_source\":[\"faqname\",\"answer\"],\"query\":{\"bool\":{\"must\":[{\"terms\":{\"tag.keyword\":" + tag + "}},{\"match\":{\"domainid.keyword\":\"" + domainId + "\"}}],\"must_not\":[{\"terms\":{\"faqid.keyword\":" + faqid + "}}]}}}"); String responseFaqJson ; try{ responseFaqJson= PacHttpUtils.doHttpPost( urlToQueryBuffer.toString(), requestFaqBody.toString()); }catch(Exception e){ throw new DataException(e); } JsonObject resultFaqJson = (JsonObject) jsonParser .parse(responseFaqJson); JsonObject hitsFaq = (JsonObject) resultFaqJson.get("hits"); JsonArray jsonArrayFaq = hitsFaq.get("hits").getAsJsonArray(); for (int j = 0; j < jsonArrayFaq.size(); j++) { JsonObject sourceFaq = (JsonObject) jsonArrayFaq.get(j) .getAsJsonObject().get("_source"); ConcurrentHashMap<String, Object> relectfaqDetails = new ConcurrentHashMap<>(); relectfaqDetails.put("faqName", sourceFaq.get("faqname") .getAsString()); relectfaqDetails.put("faqAnswer", sourceFaq.get("answer") .getAsString()); releventfaqs.add(relectfaqDetails); } return releventfaqs; }  }
FAQRespositoryImpl implements FAQRepository, Constants { @Override public List<Map<String, Object>> getRelevantFAQSFromEs(String widgetId, String domainId, List<String> tag, List<String> faqid, List<Map<String, Object>> releventfaqs) throws DataException { JsonParser jsonParser = new JsonParser(); StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") .append("faqs/faqinfo").append("/").append("_search"); StringBuilder requestFaqBody = new StringBuilder( "{\"_source\":[\"faqname\",\"answer\"],\"query\":{\"bool\":{\"must\":[{\"terms\":{\"tag.keyword\":" + tag + "}},{\"match\":{\"domainid.keyword\":\"" + domainId + "\"}}],\"must_not\":[{\"terms\":{\"faqid.keyword\":" + faqid + "}}]}}}"); String responseFaqJson ; try{ responseFaqJson= PacHttpUtils.doHttpPost( urlToQueryBuffer.toString(), requestFaqBody.toString()); }catch(Exception e){ throw new DataException(e); } JsonObject resultFaqJson = (JsonObject) jsonParser .parse(responseFaqJson); JsonObject hitsFaq = (JsonObject) resultFaqJson.get("hits"); JsonArray jsonArrayFaq = hitsFaq.get("hits").getAsJsonArray(); for (int j = 0; j < jsonArrayFaq.size(); j++) { JsonObject sourceFaq = (JsonObject) jsonArrayFaq.get(j) .getAsJsonObject().get("_source"); ConcurrentHashMap<String, Object> relectfaqDetails = new ConcurrentHashMap<>(); relectfaqDetails.put("faqName", sourceFaq.get("faqname") .getAsString()); relectfaqDetails.put("faqAnswer", sourceFaq.get("answer") .getAsString()); releventfaqs.add(relectfaqDetails); } return releventfaqs; }  @Override List<Map<String, Object>> getFAQSFromEs(String widgetId,
            String domainId); @Override List<Map<String, Object>> getRelevantFAQSFromEs(String widgetId,
            String domainId, List<String> tag, List<String> faqid,
            List<Map<String, Object>> releventfaqs); }
FAQRespositoryImpl implements FAQRepository, Constants { @Override public List<Map<String, Object>> getRelevantFAQSFromEs(String widgetId, String domainId, List<String> tag, List<String> faqid, List<Map<String, Object>> releventfaqs) throws DataException { JsonParser jsonParser = new JsonParser(); StringBuilder urlToQueryBuffer = new StringBuilder(esUrl).append("/") .append("faqs/faqinfo").append("/").append("_search"); StringBuilder requestFaqBody = new StringBuilder( "{\"_source\":[\"faqname\",\"answer\"],\"query\":{\"bool\":{\"must\":[{\"terms\":{\"tag.keyword\":" + tag + "}},{\"match\":{\"domainid.keyword\":\"" + domainId + "\"}}],\"must_not\":[{\"terms\":{\"faqid.keyword\":" + faqid + "}}]}}}"); String responseFaqJson ; try{ responseFaqJson= PacHttpUtils.doHttpPost( urlToQueryBuffer.toString(), requestFaqBody.toString()); }catch(Exception e){ throw new DataException(e); } JsonObject resultFaqJson = (JsonObject) jsonParser .parse(responseFaqJson); JsonObject hitsFaq = (JsonObject) resultFaqJson.get("hits"); JsonArray jsonArrayFaq = hitsFaq.get("hits").getAsJsonArray(); for (int j = 0; j < jsonArrayFaq.size(); j++) { JsonObject sourceFaq = (JsonObject) jsonArrayFaq.get(j) .getAsJsonObject().get("_source"); ConcurrentHashMap<String, Object> relectfaqDetails = new ConcurrentHashMap<>(); relectfaqDetails.put("faqName", sourceFaq.get("faqname") .getAsString()); relectfaqDetails.put("faqAnswer", sourceFaq.get("answer") .getAsString()); releventfaqs.add(relectfaqDetails); } return releventfaqs; }  @Override List<Map<String, Object>> getFAQSFromEs(String widgetId,
            String domainId); @Override List<Map<String, Object>> getRelevantFAQSFromEs(String widgetId,
            String domainId, List<String> tag, List<String> faqid,
            List<Map<String, Object>> releventfaqs);  }