GeofenceUpdateTask implements CTGeofenceTask { @WorkerThread @Override public void execute() { if (ctGeofenceAdapter == null) { return; } CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Executing GeofenceUpdateTask..."); CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Reading previously registered geofences from file..."); String oldFenceListString = FileUtils.readFromFile(context, FileUtils.getCachedFullPath(context, CTGeofenceConstants.CACHED_FILE_NAME)); if (!oldFenceListString.trim().equals("")) { List<String> ctOldGeofenceIdList = null; JSONObject ctOldGeofenceObject = null; try { ctOldGeofenceObject = new JSONObject(oldFenceListString); ctOldGeofenceIdList = Utils.jsonToGeoFenceList(ctOldGeofenceObject); } catch (Exception e) { CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Failed to read previously registered geofences from file"); e.printStackTrace(); } if (fenceList != null) { ctGeofenceAdapter.removeAllGeofence(ctOldGeofenceIdList, new OnSuccessListener() { @Override public void onSuccess(Object o) { addGeofences(fenceList); } }); } else { addGeofences(ctOldGeofenceObject); } } else { addGeofences(fenceList); } CTGeofenceAPI.getLogger().debug(CTGeofenceAPI.GEOFENCE_LOG_TAG, "Finished executing GeofenceUpdateTask"); } GeofenceUpdateTask(Context context, @Nullable JSONObject fenceList); @WorkerThread @Override void execute(); @Override void setOnCompleteListener(@NonNull OnCompleteListener onCompleteListener);  }
@Test public void executeTestTC1() throws Exception { when(FileUtils.getCachedDirName(application)).thenReturn(""); when(FileUtils.getCachedFullPath(any(Context.class), anyString())).thenReturn(""); when(FileUtils.readFromFile(any(Context.class), anyString())).thenReturn(""); CTGeofenceSettings currentGeofenceSettings = new CTGeofenceSettings.Builder() .setGeofenceMonitoringCount(1) .build(); when(ctGeofenceAPI.getGeofenceSettings()).thenReturn(currentGeofenceSettings); GeofenceUpdateTask updateTask = new GeofenceUpdateTask(application, GeofenceJSON.getGeofence()); updateTask.execute(); ArgumentCaptor<JSONObject> argumentCaptorJson = ArgumentCaptor.forClass(JSONObject.class); verifyStatic(FileUtils.class); FileUtils.writeJsonToFile(any(Context.class), anyString(), anyString(), argumentCaptorJson.capture()); JSONAssert.assertEquals(GeofenceJSON.getFirst(), argumentCaptorJson.getValue(), true); ArgumentCaptor<List<CTGeofence>> argumentCaptor = ArgumentCaptor.forClass(List.class); verify(ctGeofenceAdapter).addAllGeofence(argumentCaptor.capture(), any(OnSuccessListener.class)); assertEquals(argumentCaptor.getValue().size(), 1); }
@Test public void executeTestTC2() throws Exception { when(FileUtils.getCachedDirName(application)).thenReturn(""); when(FileUtils.getCachedFullPath(any(Context.class), anyString())).thenReturn(""); when(FileUtils.readFromFile(any(Context.class), anyString())).thenReturn(""); CTGeofenceSettings currentGeofenceSettings = new CTGeofenceSettings.Builder() .setGeofenceMonitoringCount(1) .build(); when(ctGeofenceAPI.getGeofenceSettings()).thenReturn(currentGeofenceSettings); GeofenceUpdateTask updateTask = new GeofenceUpdateTask(application, GeofenceJSON.getEmptyGeofence()); updateTask.execute(); ArgumentCaptor<JSONObject> argumentCaptorJson = ArgumentCaptor.forClass(JSONObject.class); verifyStatic(FileUtils.class); FileUtils.writeJsonToFile(any(Context.class), anyString(), anyString(), argumentCaptorJson.capture()); JSONAssert.assertEquals(GeofenceJSON.getEmptyGeofence(), argumentCaptorJson.getValue(), true); ArgumentCaptor<List<CTGeofence>> argumentCaptor = ArgumentCaptor.forClass(List.class); verify(ctGeofenceAdapter).addAllGeofence(argumentCaptor.capture(), any(OnSuccessListener.class)); assertEquals(argumentCaptor.getValue().size(), 0); }
@Test public void executeTestTC3() throws Exception { when(FileUtils.getCachedDirName(application)).thenReturn(""); when(FileUtils.getCachedFullPath(any(Context.class), anyString())).thenReturn(""); when(FileUtils.readFromFile(any(Context.class), anyString())).thenReturn(""); CTGeofenceSettings currentGeofenceSettings = new CTGeofenceSettings.Builder() .setGeofenceMonitoringCount(1) .build(); when(ctGeofenceAPI.getGeofenceSettings()).thenReturn(currentGeofenceSettings); GeofenceUpdateTask updateTask = new GeofenceUpdateTask(application, GeofenceJSON.getEmptyJson()); updateTask.execute(); ArgumentCaptor<JSONObject> argumentCaptorJson = ArgumentCaptor.forClass(JSONObject.class); verifyStatic(FileUtils.class); FileUtils.writeJsonToFile(any(Context.class), anyString(), anyString(), argumentCaptorJson.capture()); JSONAssert.assertEquals(GeofenceJSON.getEmptyJson(), argumentCaptorJson.getValue(), true); ArgumentCaptor<List<CTGeofence>> argumentCaptor = ArgumentCaptor.forClass(List.class); verify(ctGeofenceAdapter).addAllGeofence(argumentCaptor.capture(), any(OnSuccessListener.class)); assertEquals(argumentCaptor.getValue().size(), 0); }
@Test public void executeTestTC4() throws Exception { when(FileUtils.getCachedDirName(application)).thenReturn(""); when(FileUtils.getCachedFullPath(any(Context.class), anyString())).thenReturn(""); when(FileUtils.readFromFile(any(Context.class), anyString())).thenReturn(GeofenceJSON.getFirst().toString()); CTGeofenceSettings currentGeofenceSettings = new CTGeofenceSettings.Builder() .setGeofenceMonitoringCount(1) .build(); when(ctGeofenceAPI.getGeofenceSettings()).thenReturn(currentGeofenceSettings); GeofenceUpdateTask updateTask = new GeofenceUpdateTask(application, GeofenceJSON.getGeofence()); updateTask.execute(); ArgumentCaptor<List<String>> argumentCaptorOldGeofence = ArgumentCaptor.forClass(List.class); verify(ctGeofenceAdapter) .removeAllGeofence(argumentCaptorOldGeofence.capture(), any(OnSuccessListener.class)); assertThat(argumentCaptorOldGeofence.getValue(), is(Arrays.asList(new String[]{"310001"}))); }
@Test public void executeTestTC5() throws Exception { when(FileUtils.getCachedDirName(application)).thenReturn(""); when(FileUtils.getCachedFullPath(any(Context.class), anyString())).thenReturn(""); when(FileUtils.readFromFile(any(Context.class), anyString())).thenReturn(GeofenceJSON.getGeofenceString()); CTGeofenceSettings currentGeofenceSettings = new CTGeofenceSettings.Builder() .setGeofenceMonitoringCount(2) .build(); when(ctGeofenceAPI.getGeofenceSettings()).thenReturn(currentGeofenceSettings); GeofenceUpdateTask updateTask = new GeofenceUpdateTask(application, null); updateTask.execute(); ArgumentCaptor<JSONObject> argumentCaptorJson = ArgumentCaptor.forClass(JSONObject.class); verifyStatic(FileUtils.class); FileUtils.writeJsonToFile(any(Context.class), anyString(), anyString(), argumentCaptorJson.capture()); JSONAssert.assertEquals(GeofenceJSON.getGeofence(), argumentCaptorJson.getValue(), true); ArgumentCaptor<List<CTGeofence>> argumentCaptor = ArgumentCaptor.forClass(List.class); verify(ctGeofenceAdapter).addAllGeofence(argumentCaptor.capture(), any(OnSuccessListener.class)); assertEquals(argumentCaptor.getValue().size(), 2); }