@Test public void testOverrideReserve() { long expiryTime = System.currentTimeMillis()+ TestConstants.ONE_DAY_IN_MILLIS; MongoTemplate mockMongoTemplate = EasyMock.createMock(MongoTemplate.class); PersistableDevice persistableDev = EasyMock.createMock(PersistableDevice.class); DawgPoundMongoService dawgPoundService = new DawgPoundMongoService(); ReflectionTestUtils.setField(dawgPoundService, "mongoTemplate", mockMongoTemplate); EasyMock.expect(mockMongoTemplate.findById(TestConstants.DEVICE_ID, PersistableDevice.class, TestConstants.COLLECTION_NAME )).andReturn(persistableDev); EasyMock.expect(persistableDev.unreserve()).andReturn("previous_token"); EasyMock.expect(persistableDev.reserve(TestConstants.TOKEN)).andReturn(TestConstants.TOKEN); persistableDev.setExpiration(expiryTime); mockMongoTemplate.save(persistableDev, TestConstants.COLLECTION_NAME); EasyMock.replay(mockMongoTemplate,persistableDev); Assert.assertTrue(dawgPoundService.overrideReserve(TestConstants.DEVICE_ID, TestConstants.TOKEN, expiryTime)); EasyMock.verify(mockMongoTemplate,persistableDev); }
@Override public synchronized boolean overrideReserve(String id, String token, long expiration) { long currentTime = System.currentTimeMillis(); if (expiration < currentTime) { LOGGER.warn("Reservation time invalid. reserver="+token+", deviceId="+id+", expiration="+expiration); return false; } PersistableDevice device = getById(id); if (null == device) { LOGGER.warn("reserver="+token+" attempted to reserve a non-existing device. deviceId="+id); return false; } else { device.unreserve(); device.reserve(token); device.setExpiration(expiration); mongoTemplate.save(device, COLLECTION_NAME); LOGGER.info("Device reserved: deviceId="+id+", reserver="+token); return true; } }
DawgPoundMongoService implements IDawgPoundService { @Override public synchronized boolean overrideReserve(String id, String token, long expiration) { long currentTime = System.currentTimeMillis(); if (expiration < currentTime) { LOGGER.warn("Reservation time invalid. reserver="+token+", deviceId="+id+", expiration="+expiration); return false; } PersistableDevice device = getById(id); if (null == device) { LOGGER.warn("reserver="+token+" attempted to reserve a non-existing device. deviceId="+id); return false; } else { device.unreserve(); device.reserve(token); device.setExpiration(expiration); mongoTemplate.save(device, COLLECTION_NAME); LOGGER.info("Device reserved: deviceId="+id+", reserver="+token); return true; } } }
DawgPoundMongoService implements IDawgPoundService { @Override public synchronized boolean overrideReserve(String id, String token, long expiration) { long currentTime = System.currentTimeMillis(); if (expiration < currentTime) { LOGGER.warn("Reservation time invalid. reserver="+token+", deviceId="+id+", expiration="+expiration); return false; } PersistableDevice device = getById(id); if (null == device) { LOGGER.warn("reserver="+token+" attempted to reserve a non-existing device. deviceId="+id); return false; } else { device.unreserve(); device.reserve(token); device.setExpiration(expiration); mongoTemplate.save(device, COLLECTION_NAME); LOGGER.info("Device reserved: deviceId="+id+", reserver="+token); return true; } }  }
DawgPoundMongoService implements IDawgPoundService { @Override public synchronized boolean overrideReserve(String id, String token, long expiration) { long currentTime = System.currentTimeMillis(); if (expiration < currentTime) { LOGGER.warn("Reservation time invalid. reserver="+token+", deviceId="+id+", expiration="+expiration); return false; } PersistableDevice device = getById(id); if (null == device) { LOGGER.warn("reserver="+token+" attempted to reserve a non-existing device. deviceId="+id); return false; } else { device.unreserve(); device.reserve(token); device.setExpiration(expiration); mongoTemplate.save(device, COLLECTION_NAME); LOGGER.info("Device reserved: deviceId="+id+", reserver="+token); return true; } }  @PostConstruct void init(); @Override synchronized String unreserve(String id); @Override synchronized boolean overrideReserve(String id, String token, long expiration); @SuppressWarnings("unchecked") @Override Map<String,Object>[] getByReserver(String token); @Override synchronized String reserve(String id, String token, long expiration); @Override PersistableDevice getById(String id); @Override Map<String, String> isReserved(String id); @SuppressWarnings("unchecked") Map<String,Object>[] list(); }
DawgPoundMongoService implements IDawgPoundService { @Override public synchronized boolean overrideReserve(String id, String token, long expiration) { long currentTime = System.currentTimeMillis(); if (expiration < currentTime) { LOGGER.warn("Reservation time invalid. reserver="+token+", deviceId="+id+", expiration="+expiration); return false; } PersistableDevice device = getById(id); if (null == device) { LOGGER.warn("reserver="+token+" attempted to reserve a non-existing device. deviceId="+id); return false; } else { device.unreserve(); device.reserve(token); device.setExpiration(expiration); mongoTemplate.save(device, COLLECTION_NAME); LOGGER.info("Device reserved: deviceId="+id+", reserver="+token); return true; } }  @PostConstruct void init(); @Override synchronized String unreserve(String id); @Override synchronized boolean overrideReserve(String id, String token, long expiration); @SuppressWarnings("unchecked") @Override Map<String,Object>[] getByReserver(String token); @Override synchronized String reserve(String id, String token, long expiration); @Override PersistableDevice getById(String id); @Override Map<String, String> isReserved(String id); @SuppressWarnings("unchecked") Map<String,Object>[] list();  }