@Test public void provideData(ComponentManager componentManager) throws Exception { InstanceId id = new InstanceId(UUID.randomUUID().toString()); InstanceIdManager idManager = componentManager.getInstance(InstanceIdManager.class); when(idManager.getInstanceId()).thenReturn(id); JestClient client = mock(JestClient.class); SearchResult searchResult = new SearchResult(new Gson()); String resultString = "{\n" + " \"took\": 4,\n" + " \"timed_out\": false,\n" + " \"_shards\": {\n" + " \"total\": 5,\n" + " \"successful\": 5,\n" + " \"failed\": 0\n" + " },\n" + " \"hits\": {\n" + " \"total\": 2,\n" + " \"max_score\": 0,\n" + " \"hits\": []\n" + " },\n" + " \"aggregations\": {\n" + " \"firstPingDate\": {\n" + " \"value\": 1392854400000\n" + " },\n" + " \"serverTime\": {\n" + " \"value\": 1393200000000\n" + " }\n" + " }\n" + "}"; searchResult.setJsonString(resultString); searchResult.setJsonObject(new JsonParser().parse(resultString).getAsJsonObject()); searchResult.setSucceeded(true); when(client.execute(any(Search.class))).thenReturn(searchResult); JestClientManager jestManager = componentManager.getInstance(JestClientManager.class); when(jestManager.getClient()).thenReturn(client); Map<String, Object> data = provider.provideData(); assertEquals(2, data.size()); assertEquals(4L, data.get("sinceDays")); assertEquals(1392854400000L, data.get("firstPingDate")); }
@Override public Map<String, Object> provideData() { Map<String, Object> jsonMap = new HashMap<>(); try { String instanceId = this.instanceIdManager.getInstanceId().toString(); Search search = new Search.Builder(constructSearchJSON(instanceId)) .addIndex(JestClientManager.INDEX) .addType(JestClientManager.TYPE) .setSearchType(SearchType.COUNT) .build(); JestResult result = this.jestClientManager.getClient().execute(search); if (!result.isSucceeded()) { this.logger.warn(ERROR_MESSAGE, result.getErrorMessage()); return jsonMap; } @SuppressWarnings("unchecked") Map<String, Object> aggregationsMap = (Map<String, Object>) result.getValue("aggregations"); @SuppressWarnings("unchecked") Map<String, Object> serverTimeMap = (Map<String, Object>) aggregationsMap.get(PROPERTY_SERVER_TIME); Object serverTimeObject = serverTimeMap.get(PROPERTY_VALUE); @SuppressWarnings("unchecked") Map<String, Object> firstPingDateMap = (Map<String, Object>) aggregationsMap.get(PROPERTY_FIRST_PING_DATE); Object firstPingDateObject = firstPingDateMap.get(PROPERTY_VALUE); if (serverTimeObject != null && firstPingDateObject != null) { long sinceDays = Math.round(((double) serverTimeObject - (double) firstPingDateObject) / 86400000D); jsonMap.put(PROPERTY_SINCE_DAYS, sinceDays); long firstPingDate = Math.round((double) firstPingDateObject); jsonMap.put(PROPERTY_FIRST_PING_DATE, firstPingDate); } else { jsonMap.put(PROPERTY_SINCE_DAYS, 0); } } catch (Exception e) { this.logger.warn(ERROR_MESSAGE, ExceptionUtils.getRootCauseMessage(e)); } return jsonMap; }
DatePingDataProvider implements PingDataProvider { @Override public Map<String, Object> provideData() { Map<String, Object> jsonMap = new HashMap<>(); try { String instanceId = this.instanceIdManager.getInstanceId().toString(); Search search = new Search.Builder(constructSearchJSON(instanceId)) .addIndex(JestClientManager.INDEX) .addType(JestClientManager.TYPE) .setSearchType(SearchType.COUNT) .build(); JestResult result = this.jestClientManager.getClient().execute(search); if (!result.isSucceeded()) { this.logger.warn(ERROR_MESSAGE, result.getErrorMessage()); return jsonMap; } @SuppressWarnings("unchecked") Map<String, Object> aggregationsMap = (Map<String, Object>) result.getValue("aggregations"); @SuppressWarnings("unchecked") Map<String, Object> serverTimeMap = (Map<String, Object>) aggregationsMap.get(PROPERTY_SERVER_TIME); Object serverTimeObject = serverTimeMap.get(PROPERTY_VALUE); @SuppressWarnings("unchecked") Map<String, Object> firstPingDateMap = (Map<String, Object>) aggregationsMap.get(PROPERTY_FIRST_PING_DATE); Object firstPingDateObject = firstPingDateMap.get(PROPERTY_VALUE); if (serverTimeObject != null && firstPingDateObject != null) { long sinceDays = Math.round(((double) serverTimeObject - (double) firstPingDateObject) / 86400000D); jsonMap.put(PROPERTY_SINCE_DAYS, sinceDays); long firstPingDate = Math.round((double) firstPingDateObject); jsonMap.put(PROPERTY_FIRST_PING_DATE, firstPingDate); } else { jsonMap.put(PROPERTY_SINCE_DAYS, 0); } } catch (Exception e) { this.logger.warn(ERROR_MESSAGE, ExceptionUtils.getRootCauseMessage(e)); } return jsonMap; } }
DatePingDataProvider implements PingDataProvider { @Override public Map<String, Object> provideData() { Map<String, Object> jsonMap = new HashMap<>(); try { String instanceId = this.instanceIdManager.getInstanceId().toString(); Search search = new Search.Builder(constructSearchJSON(instanceId)) .addIndex(JestClientManager.INDEX) .addType(JestClientManager.TYPE) .setSearchType(SearchType.COUNT) .build(); JestResult result = this.jestClientManager.getClient().execute(search); if (!result.isSucceeded()) { this.logger.warn(ERROR_MESSAGE, result.getErrorMessage()); return jsonMap; } @SuppressWarnings("unchecked") Map<String, Object> aggregationsMap = (Map<String, Object>) result.getValue("aggregations"); @SuppressWarnings("unchecked") Map<String, Object> serverTimeMap = (Map<String, Object>) aggregationsMap.get(PROPERTY_SERVER_TIME); Object serverTimeObject = serverTimeMap.get(PROPERTY_VALUE); @SuppressWarnings("unchecked") Map<String, Object> firstPingDateMap = (Map<String, Object>) aggregationsMap.get(PROPERTY_FIRST_PING_DATE); Object firstPingDateObject = firstPingDateMap.get(PROPERTY_VALUE); if (serverTimeObject != null && firstPingDateObject != null) { long sinceDays = Math.round(((double) serverTimeObject - (double) firstPingDateObject) / 86400000D); jsonMap.put(PROPERTY_SINCE_DAYS, sinceDays); long firstPingDate = Math.round((double) firstPingDateObject); jsonMap.put(PROPERTY_FIRST_PING_DATE, firstPingDate); } else { jsonMap.put(PROPERTY_SINCE_DAYS, 0); } } catch (Exception e) { this.logger.warn(ERROR_MESSAGE, ExceptionUtils.getRootCauseMessage(e)); } return jsonMap; }  }
DatePingDataProvider implements PingDataProvider { @Override public Map<String, Object> provideData() { Map<String, Object> jsonMap = new HashMap<>(); try { String instanceId = this.instanceIdManager.getInstanceId().toString(); Search search = new Search.Builder(constructSearchJSON(instanceId)) .addIndex(JestClientManager.INDEX) .addType(JestClientManager.TYPE) .setSearchType(SearchType.COUNT) .build(); JestResult result = this.jestClientManager.getClient().execute(search); if (!result.isSucceeded()) { this.logger.warn(ERROR_MESSAGE, result.getErrorMessage()); return jsonMap; } @SuppressWarnings("unchecked") Map<String, Object> aggregationsMap = (Map<String, Object>) result.getValue("aggregations"); @SuppressWarnings("unchecked") Map<String, Object> serverTimeMap = (Map<String, Object>) aggregationsMap.get(PROPERTY_SERVER_TIME); Object serverTimeObject = serverTimeMap.get(PROPERTY_VALUE); @SuppressWarnings("unchecked") Map<String, Object> firstPingDateMap = (Map<String, Object>) aggregationsMap.get(PROPERTY_FIRST_PING_DATE); Object firstPingDateObject = firstPingDateMap.get(PROPERTY_VALUE); if (serverTimeObject != null && firstPingDateObject != null) { long sinceDays = Math.round(((double) serverTimeObject - (double) firstPingDateObject) / 86400000D); jsonMap.put(PROPERTY_SINCE_DAYS, sinceDays); long firstPingDate = Math.round((double) firstPingDateObject); jsonMap.put(PROPERTY_FIRST_PING_DATE, firstPingDate); } else { jsonMap.put(PROPERTY_SINCE_DAYS, 0); } } catch (Exception e) { this.logger.warn(ERROR_MESSAGE, ExceptionUtils.getRootCauseMessage(e)); } return jsonMap; }  @Override Map<String, Object> provideMapping(); @Override Map<String, Object> provideData(); }
DatePingDataProvider implements PingDataProvider { @Override public Map<String, Object> provideData() { Map<String, Object> jsonMap = new HashMap<>(); try { String instanceId = this.instanceIdManager.getInstanceId().toString(); Search search = new Search.Builder(constructSearchJSON(instanceId)) .addIndex(JestClientManager.INDEX) .addType(JestClientManager.TYPE) .setSearchType(SearchType.COUNT) .build(); JestResult result = this.jestClientManager.getClient().execute(search); if (!result.isSucceeded()) { this.logger.warn(ERROR_MESSAGE, result.getErrorMessage()); return jsonMap; } @SuppressWarnings("unchecked") Map<String, Object> aggregationsMap = (Map<String, Object>) result.getValue("aggregations"); @SuppressWarnings("unchecked") Map<String, Object> serverTimeMap = (Map<String, Object>) aggregationsMap.get(PROPERTY_SERVER_TIME); Object serverTimeObject = serverTimeMap.get(PROPERTY_VALUE); @SuppressWarnings("unchecked") Map<String, Object> firstPingDateMap = (Map<String, Object>) aggregationsMap.get(PROPERTY_FIRST_PING_DATE); Object firstPingDateObject = firstPingDateMap.get(PROPERTY_VALUE); if (serverTimeObject != null && firstPingDateObject != null) { long sinceDays = Math.round(((double) serverTimeObject - (double) firstPingDateObject) / 86400000D); jsonMap.put(PROPERTY_SINCE_DAYS, sinceDays); long firstPingDate = Math.round((double) firstPingDateObject); jsonMap.put(PROPERTY_FIRST_PING_DATE, firstPingDate); } else { jsonMap.put(PROPERTY_SINCE_DAYS, 0); } } catch (Exception e) { this.logger.warn(ERROR_MESSAGE, ExceptionUtils.getRootCauseMessage(e)); } return jsonMap; }  @Override Map<String, Object> provideMapping(); @Override Map<String, Object> provideData();  }