@SuppressWarnings({ "unchecked", "static-access" }) @Test public void fetchCurrentCountStatsForAssetGroupsTest() throws Exception{ HttpEntity jsonEntity = new StringEntity("{\"hits\":{\"hits\":[{\"_source\":{\"ag\":\"ag\",\"type\":\"type\"}}]}}", ContentType.APPLICATION_JSON); when(response.getEntity()).thenReturn(jsonEntity); when(restClient.performRequest(anyString(), anyString(), anyMap(), any(HttpEntity.class), Matchers.<Header>anyVararg())).thenReturn(response); ReflectionTestUtils.setField(esManager, "restClient", restClient); when(sl.getStatusCode()).thenReturn(100); when(response.getStatusLine()).thenReturn(sl); assertThat(esManager.fetchCurrentCountStatsForAssetGroups("date").size(),is(1)); }
@SuppressWarnings("unchecked") public static Map<String, Map<String, Map<String, Object>>> fetchCurrentCountStatsForAssetGroups(String date) { Map<String, Map<String, Map<String, Object>>> asgInfoList = new HashMap<>(); try { ObjectMapper objMapper = new ObjectMapper(); String payLoad = "{\"query\": { \"match\": { \"date\": \"" + date + "\"} }}"; Response response = invokeAPI("GET", "assetgroup_stats/count_type/_search?size=10000", payLoad); String responseJson = EntityUtils.toString(response.getEntity()); Map<String, Object> docMap = objMapper.readValue(responseJson, new TypeReference<Map<String, Object>>() { }); List<Map<String, Object>> docs = (List<Map<String, Object>>) ((Map<String, Object>) docMap.get("hits")) .get("hits"); for (Map<String, Object> doc : docs) { Map<String, Object> _doc = (Map<String, Object>) doc.get("_source"); Map<String, Map<String, Object>> typeInfo = asgInfoList.get(_doc.get("ag").toString()); if (typeInfo == null) { typeInfo = new HashMap<>(); asgInfoList.put(_doc.get("ag").toString(), typeInfo); } typeInfo.put(_doc.get("type").toString(), _doc); _doc.remove("ag"); _doc.remove("type"); } } catch (ParseException | IOException e) { LOGGER.error("Error in fetchCurrentCountStatsForAssetGroups" ,e ); } return asgInfoList; }
ESManager implements Constants { @SuppressWarnings("unchecked") public static Map<String, Map<String, Map<String, Object>>> fetchCurrentCountStatsForAssetGroups(String date) { Map<String, Map<String, Map<String, Object>>> asgInfoList = new HashMap<>(); try { ObjectMapper objMapper = new ObjectMapper(); String payLoad = "{\"query\": { \"match\": { \"date\": \"" + date + "\"} }}"; Response response = invokeAPI("GET", "assetgroup_stats/count_type/_search?size=10000", payLoad); String responseJson = EntityUtils.toString(response.getEntity()); Map<String, Object> docMap = objMapper.readValue(responseJson, new TypeReference<Map<String, Object>>() { }); List<Map<String, Object>> docs = (List<Map<String, Object>>) ((Map<String, Object>) docMap.get("hits")) .get("hits"); for (Map<String, Object> doc : docs) { Map<String, Object> _doc = (Map<String, Object>) doc.get("_source"); Map<String, Map<String, Object>> typeInfo = asgInfoList.get(_doc.get("ag").toString()); if (typeInfo == null) { typeInfo = new HashMap<>(); asgInfoList.put(_doc.get("ag").toString(), typeInfo); } typeInfo.put(_doc.get("type").toString(), _doc); _doc.remove("ag"); _doc.remove("type"); } } catch (ParseException | IOException e) { LOGGER.error("Error in fetchCurrentCountStatsForAssetGroups" ,e ); } return asgInfoList; } }
ESManager implements Constants { @SuppressWarnings("unchecked") public static Map<String, Map<String, Map<String, Object>>> fetchCurrentCountStatsForAssetGroups(String date) { Map<String, Map<String, Map<String, Object>>> asgInfoList = new HashMap<>(); try { ObjectMapper objMapper = new ObjectMapper(); String payLoad = "{\"query\": { \"match\": { \"date\": \"" + date + "\"} }}"; Response response = invokeAPI("GET", "assetgroup_stats/count_type/_search?size=10000", payLoad); String responseJson = EntityUtils.toString(response.getEntity()); Map<String, Object> docMap = objMapper.readValue(responseJson, new TypeReference<Map<String, Object>>() { }); List<Map<String, Object>> docs = (List<Map<String, Object>>) ((Map<String, Object>) docMap.get("hits")) .get("hits"); for (Map<String, Object> doc : docs) { Map<String, Object> _doc = (Map<String, Object>) doc.get("_source"); Map<String, Map<String, Object>> typeInfo = asgInfoList.get(_doc.get("ag").toString()); if (typeInfo == null) { typeInfo = new HashMap<>(); asgInfoList.put(_doc.get("ag").toString(), typeInfo); } typeInfo.put(_doc.get("type").toString(), _doc); _doc.remove("ag"); _doc.remove("type"); } } catch (ParseException | IOException e) { LOGGER.error("Error in fetchCurrentCountStatsForAssetGroups" ,e ); } return asgInfoList; }  }
ESManager implements Constants { @SuppressWarnings("unchecked") public static Map<String, Map<String, Map<String, Object>>> fetchCurrentCountStatsForAssetGroups(String date) { Map<String, Map<String, Map<String, Object>>> asgInfoList = new HashMap<>(); try { ObjectMapper objMapper = new ObjectMapper(); String payLoad = "{\"query\": { \"match\": { \"date\": \"" + date + "\"} }}"; Response response = invokeAPI("GET", "assetgroup_stats/count_type/_search?size=10000", payLoad); String responseJson = EntityUtils.toString(response.getEntity()); Map<String, Object> docMap = objMapper.readValue(responseJson, new TypeReference<Map<String, Object>>() { }); List<Map<String, Object>> docs = (List<Map<String, Object>>) ((Map<String, Object>) docMap.get("hits")) .get("hits"); for (Map<String, Object> doc : docs) { Map<String, Object> _doc = (Map<String, Object>) doc.get("_source"); Map<String, Map<String, Object>> typeInfo = asgInfoList.get(_doc.get("ag").toString()); if (typeInfo == null) { typeInfo = new HashMap<>(); asgInfoList.put(_doc.get("ag").toString(), typeInfo); } typeInfo.put(_doc.get("type").toString(), _doc); _doc.remove("ag"); _doc.remove("type"); } } catch (ParseException | IOException e) { LOGGER.error("Error in fetchCurrentCountStatsForAssetGroups" ,e ); } return asgInfoList; }  static Map<String, Object> uploadData(String index, String type, List<Map<String, Object>> docs, String loaddate); static void refresh(String index); static void uploadData(String index, String type, List<Map<String, Object>> docs, String idKey,
            boolean refresh); static String createESDoc(Map<String, ?> doc); static Response invokeAPI(String method, String endpoint, String payLoad); static void configureIndexAndTypes(String ds, List<Map<String, String>> errorList); static Map<String, Map<String, String>> getExistingInfo(String indexName, String type,
            List<String> filters); @SuppressWarnings("unchecked") static Map<String, Map<String, Map<String, Object>>> fetchCurrentCountStatsForAssetGroups(String date); static void createIndex(String indexName, List<Map<String, String>> errorList); static void createType(String indexName, String typename, List<Map<String, String>> errorList); static void createType(String index, String type, String parent); static void uploadData(String index, String type, List<Map<String, Object>> docs, String[] parentKey); static void deleteOldDocuments(String index, String type, String field, String value); static long updateLoadDate(String index, String type, String accountId, String region, String loaddate,boolean checkLatest); }
ESManager implements Constants { @SuppressWarnings("unchecked") public static Map<String, Map<String, Map<String, Object>>> fetchCurrentCountStatsForAssetGroups(String date) { Map<String, Map<String, Map<String, Object>>> asgInfoList = new HashMap<>(); try { ObjectMapper objMapper = new ObjectMapper(); String payLoad = "{\"query\": { \"match\": { \"date\": \"" + date + "\"} }}"; Response response = invokeAPI("GET", "assetgroup_stats/count_type/_search?size=10000", payLoad); String responseJson = EntityUtils.toString(response.getEntity()); Map<String, Object> docMap = objMapper.readValue(responseJson, new TypeReference<Map<String, Object>>() { }); List<Map<String, Object>> docs = (List<Map<String, Object>>) ((Map<String, Object>) docMap.get("hits")) .get("hits"); for (Map<String, Object> doc : docs) { Map<String, Object> _doc = (Map<String, Object>) doc.get("_source"); Map<String, Map<String, Object>> typeInfo = asgInfoList.get(_doc.get("ag").toString()); if (typeInfo == null) { typeInfo = new HashMap<>(); asgInfoList.put(_doc.get("ag").toString(), typeInfo); } typeInfo.put(_doc.get("type").toString(), _doc); _doc.remove("ag"); _doc.remove("type"); } } catch (ParseException | IOException e) { LOGGER.error("Error in fetchCurrentCountStatsForAssetGroups" ,e ); } return asgInfoList; }  static Map<String, Object> uploadData(String index, String type, List<Map<String, Object>> docs, String loaddate); static void refresh(String index); static void uploadData(String index, String type, List<Map<String, Object>> docs, String idKey,
            boolean refresh); static String createESDoc(Map<String, ?> doc); static Response invokeAPI(String method, String endpoint, String payLoad); static void configureIndexAndTypes(String ds, List<Map<String, String>> errorList); static Map<String, Map<String, String>> getExistingInfo(String indexName, String type,
            List<String> filters); @SuppressWarnings("unchecked") static Map<String, Map<String, Map<String, Object>>> fetchCurrentCountStatsForAssetGroups(String date); static void createIndex(String indexName, List<Map<String, String>> errorList); static void createType(String indexName, String typename, List<Map<String, String>> errorList); static void createType(String index, String type, String parent); static void uploadData(String index, String type, List<Map<String, Object>> docs, String[] parentKey); static void deleteOldDocuments(String index, String type, String field, String value); static long updateLoadDate(String index, String type, String accountId, String region, String loaddate,boolean checkLatest);  }