@Test public void testReserveFeatureWithNoDevice() { MongoTemplate mockMongoTemplate = EasyMock.createMock(MongoTemplate.class); DawgPoundMongoService dawgPoundService = new DawgPoundMongoService(); ReflectionTestUtils.setField(dawgPoundService, "mongoTemplate", mockMongoTemplate); EasyMock.expect(mockMongoTemplate.findById(TestConstants.DEVICE_ID, PersistableDevice.class, TestConstants.COLLECTION_NAME )).andReturn(null); EasyMock.replay(mockMongoTemplate); Assert.assertNull(dawgPoundService.reserve(TestConstants.DEVICE_ID, TestConstants.TOKEN, (System.currentTimeMillis() + TestConstants.ONE_DAY_IN_MILLIS))); EasyMock.verify(mockMongoTemplate); }
@Override public synchronized String reserve(String id, String token, long expiration) { PersistableDevice device = getById(id); if (null == device) { return null; } else { if (device.isReserved()) { String reservedToken = device.getReserver(); LOGGER.info("Reservation attempted, but device already reserved. Current: reserved: deviceId="+id+", reserver="+reservedToken); return reservedToken; } else { device.unreserve(); device.reserve(token); device.setExpiration(expiration); String[] keys = device.getKeys(); List<String> newKeys = new ArrayList<String>(Arrays.asList(keys)); newKeys.add(token); device.setKeys(newKeys.toArray(new String[newKeys.size()])); mongoTemplate.save(device, COLLECTION_NAME); LOGGER.info("Device reserved: deviceId="+id+", reserver="+token); return token; } } }
DawgPoundMongoService implements IDawgPoundService { @Override public synchronized String reserve(String id, String token, long expiration) { PersistableDevice device = getById(id); if (null == device) { return null; } else { if (device.isReserved()) { String reservedToken = device.getReserver(); LOGGER.info("Reservation attempted, but device already reserved. Current: reserved: deviceId="+id+", reserver="+reservedToken); return reservedToken; } else { device.unreserve(); device.reserve(token); device.setExpiration(expiration); String[] keys = device.getKeys(); List<String> newKeys = new ArrayList<String>(Arrays.asList(keys)); newKeys.add(token); device.setKeys(newKeys.toArray(new String[newKeys.size()])); mongoTemplate.save(device, COLLECTION_NAME); LOGGER.info("Device reserved: deviceId="+id+", reserver="+token); return token; } } } }
DawgPoundMongoService implements IDawgPoundService { @Override public synchronized String reserve(String id, String token, long expiration) { PersistableDevice device = getById(id); if (null == device) { return null; } else { if (device.isReserved()) { String reservedToken = device.getReserver(); LOGGER.info("Reservation attempted, but device already reserved. Current: reserved: deviceId="+id+", reserver="+reservedToken); return reservedToken; } else { device.unreserve(); device.reserve(token); device.setExpiration(expiration); String[] keys = device.getKeys(); List<String> newKeys = new ArrayList<String>(Arrays.asList(keys)); newKeys.add(token); device.setKeys(newKeys.toArray(new String[newKeys.size()])); mongoTemplate.save(device, COLLECTION_NAME); LOGGER.info("Device reserved: deviceId="+id+", reserver="+token); return token; } } }  }
DawgPoundMongoService implements IDawgPoundService { @Override public synchronized String reserve(String id, String token, long expiration) { PersistableDevice device = getById(id); if (null == device) { return null; } else { if (device.isReserved()) { String reservedToken = device.getReserver(); LOGGER.info("Reservation attempted, but device already reserved. Current: reserved: deviceId="+id+", reserver="+reservedToken); return reservedToken; } else { device.unreserve(); device.reserve(token); device.setExpiration(expiration); String[] keys = device.getKeys(); List<String> newKeys = new ArrayList<String>(Arrays.asList(keys)); newKeys.add(token); device.setKeys(newKeys.toArray(new String[newKeys.size()])); mongoTemplate.save(device, COLLECTION_NAME); LOGGER.info("Device reserved: deviceId="+id+", reserver="+token); return token; } } }  @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 String reserve(String id, String token, long expiration) { PersistableDevice device = getById(id); if (null == device) { return null; } else { if (device.isReserved()) { String reservedToken = device.getReserver(); LOGGER.info("Reservation attempted, but device already reserved. Current: reserved: deviceId="+id+", reserver="+reservedToken); return reservedToken; } else { device.unreserve(); device.reserve(token); device.setExpiration(expiration); String[] keys = device.getKeys(); List<String> newKeys = new ArrayList<String>(Arrays.asList(keys)); newKeys.add(token); device.setKeys(newKeys.toArray(new String[newKeys.size()])); mongoTemplate.save(device, COLLECTION_NAME); LOGGER.info("Device reserved: deviceId="+id+", reserver="+token); return token; } } }  @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();  }