BackGroundServiceActor extends BaseActor { private void updateUserCount(Request actorMessage) { ProjectLogger.log("In BackgroundService actor in updateUserCount method."); Util.DbInfo locDbInfo = Util.dbInfoMap.get(JsonKey.GEO_LOCATION_DB); List<Object> locationIds = (List<Object>) actorMessage.getRequest().get(JsonKey.LOCATION_IDS); String operation = (String) actorMessage.getRequest().get(JsonKey.OPERATION); ProjectLogger.log("operation for updating UserCount" + operation); Response response = cassandraOperation.getRecordsByProperty( locDbInfo.getKeySpace(), locDbInfo.getTableName(), JsonKey.ID, locationIds); List<Map<String, Object>> list = (List<Map<String, Object>>) response.get(JsonKey.RESPONSE); if (null != list && !list.isEmpty()) { for (Map<String, Object> map : list) { String locationId = (String) map.get(JsonKey.ID); ProjectLogger.log("Processing start for LocationId " + locationId); Long userCountTTL = 0L; int userCount = (map.get(JsonKey.USER_COUNT) == null) ? 0 : (int) (map.get(JsonKey.USER_COUNT)); ProjectLogger.log("userCount is " + userCount + "for location id " + locationId); if (userCount == 0 && !StringUtils.isBlank(operation) && operation.equalsIgnoreCase("UpdateUserCountScheduler")) { ProjectLogger.log("Processing start for LocationId for Scheduler " + locationId); int count = getUserCount(locationId); Map<String, Object> reqMap = new HashMap<>(); reqMap.put(JsonKey.ID, locationId); reqMap.put(JsonKey.USER_COUNT, count); reqMap.put(JsonKey.USER_COUNT_TTL, String.valueOf(System.currentTimeMillis())); cassandraOperation.updateRecord( locDbInfo.getKeySpace(), locDbInfo.getTableName(), reqMap); } else if (!StringUtils.isBlank(operation) && operation.equalsIgnoreCase("GeoLocationManagementActor")) { ProjectLogger.log( "Processing start for LocationId for GeoLocationManagementActor " + locationId); try { if (!StringUtils.isBlank((String) map.get(JsonKey.USER_COUNT_TTL))) { userCountTTL = Long.valueOf((String) map.get(JsonKey.USER_COUNT_TTL)); } } catch (Exception ex) { ProjectLogger.log( "Exception occurred while converting string to long " + (String) map.get(JsonKey.USER_COUNT_TTL)); userCountTTL = 0L; } ProjectLogger.log("userCountTTL == " + userCountTTL); Long currentTime = System.currentTimeMillis(); Long diff = currentTime - userCountTTL; int hours = (int) (diff / (1000 * 60 * 60)); if (hours >= 24) { ProjectLogger.log("Updating user count for LocnId " + locationId); int usrCount = getUserCount(locationId); Map<String, Object> reqMap = new HashMap<>(); reqMap.put(JsonKey.ID, locationId); reqMap.put(JsonKey.USER_COUNT, usrCount); reqMap.put(JsonKey.USER_COUNT_TTL, String.valueOf(System.currentTimeMillis())); cassandraOperation.updateRecord( locDbInfo.getKeySpace(), locDbInfo.getTableName(), reqMap); } } } ProjectLogger.log("Processing end user count update "); } }  @Override void onReceive(Request request);  }
@Test @Ignore public void updateUserCountTest() { Map<String, Object> locnMap = new HashMap<String, Object>(); locnMap.put(JsonKey.ID, locnId); locnMap.put(JsonKey.USER_COUNT, 0); cassandraOperation.updateRecord( geoLocationDbInfo.getKeySpace(), geoLocationDbInfo.getTableName(), locnMap); List<Object> locnIdList = new ArrayList<>(); locnIdList.add(locnId); TestKit probe = new TestKit(system); ActorRef subject = system.actorOf(props); Request actorMessage = new Request(); actorMessage.getRequest().put(JsonKey.LOCATION_IDS, locnIdList); actorMessage.getRequest().put(JsonKey.OPERATION, "GeoLocationManagementActor"); actorMessage.setOperation(BackgroundOperations.updateUserCountToLocationID.name()); subject.tell(actorMessage, probe.getRef()); try { Thread.sleep(20000); } catch (InterruptedException e) { ProjectLogger.log(e.getMessage(), e); } Response response = cassandraOperation.getRecordById( geoLocationDbInfo.getKeySpace(), geoLocationDbInfo.getTableName(), locnId); List<Map<String, Object>> reslist = (List<Map<String, Object>>) response.get(JsonKey.RESPONSE); Map<String, Object> map = reslist.get(0); int count = (int) map.get(JsonKey.USER_COUNT); boolean bool = (count >= 0) ? true : false; assertTrue(bool); }