ImportRepositoryCustomImpl implements ImportRepositoryCustom { public boolean isImportProcessLocked(String appIdentifier) throws QueryException{ boolean ret = false; BasicQuery basicquery; try{ basicquery = new BasicQuery("{ '_id':'" + appIdentifier + "'}"); }catch(Exception e) { throw new QueryException(); } List<LockImport> lockImportList = mongoTemplate.find(basicquery, LockImport.class, collectionLock); if(!lockImportList.isEmpty()){ ret = true; LockImport lockImport = lockImportList.get(0); DateTime dateLockImport = new DateTime(lockImport.getTimeStamp()); DateTime dateNow = new DateTime(); Interval interval = new Interval(dateLockImport, dateNow); if (interval.toDurationMillis() > 3600000){ lockImportProcess(appIdentifier); ret = false; } } return ret; }  @Override String saveGeoJson(String appIdentifier, Collection<Feature> featureCollection); void lockImportProcess(String appIdentifier); boolean isImportProcessLocked(String appIdentifier); void deleteLockImportProcess(String appIdentifier);  }
@Test public void isImportProcessLocked() throws Exception{ String idApplication = "1"; String strQuery = "{ '_id':'" + idApplication + "'}"; Query query=Mockito.mock(Query.class); PowerMockito.whenNew(BasicQuery.class).withArguments(strQuery).thenReturn(basicQuery); Mockito.when(mongoTemplate.find(query, LockImport.class, collectionLock)).thenReturn(lockImportList); Mockito.when(lockImportList.get(0)).thenReturn(lockImport); importRepositoryCustom.isImportProcessLocked(idApplication); }
@Test public void isImportProcessLockedSpentTheTime() throws Exception{ String idApplication = "1"; String strQuery = "{ '_id':'" + idApplication + "'}"; String strNow = DateTime.now().toString(); Query query=Mockito.mock(Query.class); PowerMockito.whenNew(BasicQuery.class).withArguments(strQuery).thenReturn(basicQuery); Mockito.when(mongoTemplate.find(query, LockImport.class, collectionLock)).thenReturn(lockImportList); Mockito.when(mongoTemplate.collectionExists(collectionLock)).thenReturn(true); Mockito.when(mongoTemplate.getCollection(collectionLock)).thenReturn(dbCollection); PowerMockito.whenNew(LockImport.class).withNoArguments().thenReturn(lockImport); PowerMockito.mockStatic(DateTime.class); Mockito.when(DateTime.now()).thenReturn(now); Mockito.when(now.toString()).thenReturn(strNow); importRepositoryCustom.isImportProcessLocked(idApplication); }