@Test(expected = IllegalArgumentException.class) public void generateAndSaveShortUrlPostfixShouldThowExceptionWithWrongUrl() throws Exception { urlShorterManager.generateAndSaveShortUrlPostfix(INCORRECT, null); }
@Transactional(propagation = Propagation.REQUIRED) public String generateAndSaveShortUrlPostfix(String url, String name) { if (!validator.isValid(url)) { throw new IllegalArgumentException("Invalid url format: " + url); } Date expiredDate = new Date(System.currentTimeMillis() - expiredPeriodParam * MILLISECONDS_PER_DAY); urlShorterDao.deleteExpiredUrls(expiredDate); String id; if (name != null && !urlShorterDao.loadUrlById(name).isPresent()) { id = name; } else { id = Hashing.murmur3_32() .hashString(url, StandardCharsets.UTF_8).toString(); } urlShorterDao.storeUrl(id, url); return id; }
UrlShorterManager { @Transactional(propagation = Propagation.REQUIRED) public String generateAndSaveShortUrlPostfix(String url, String name) { if (!validator.isValid(url)) { throw new IllegalArgumentException("Invalid url format: " + url); } Date expiredDate = new Date(System.currentTimeMillis() - expiredPeriodParam * MILLISECONDS_PER_DAY); urlShorterDao.deleteExpiredUrls(expiredDate); String id; if (name != null && !urlShorterDao.loadUrlById(name).isPresent()) { id = name; } else { id = Hashing.murmur3_32() .hashString(url, StandardCharsets.UTF_8).toString(); } urlShorterDao.storeUrl(id, url); return id; } }
UrlShorterManager { @Transactional(propagation = Propagation.REQUIRED) public String generateAndSaveShortUrlPostfix(String url, String name) { if (!validator.isValid(url)) { throw new IllegalArgumentException("Invalid url format: " + url); } Date expiredDate = new Date(System.currentTimeMillis() - expiredPeriodParam * MILLISECONDS_PER_DAY); urlShorterDao.deleteExpiredUrls(expiredDate); String id; if (name != null && !urlShorterDao.loadUrlById(name).isPresent()) { id = name; } else { id = Hashing.murmur3_32() .hashString(url, StandardCharsets.UTF_8).toString(); } urlShorterDao.storeUrl(id, url); return id; }  }
UrlShorterManager { @Transactional(propagation = Propagation.REQUIRED) public String generateAndSaveShortUrlPostfix(String url, String name) { if (!validator.isValid(url)) { throw new IllegalArgumentException("Invalid url format: " + url); } Date expiredDate = new Date(System.currentTimeMillis() - expiredPeriodParam * MILLISECONDS_PER_DAY); urlShorterDao.deleteExpiredUrls(expiredDate); String id; if (name != null && !urlShorterDao.loadUrlById(name).isPresent()) { id = name; } else { id = Hashing.murmur3_32() .hashString(url, StandardCharsets.UTF_8).toString(); } urlShorterDao.storeUrl(id, url); return id; }  @Transactional(propagation = Propagation.REQUIRED) String generateAndSaveShortUrlPostfix(String url, String name); @Transactional(propagation = Propagation.SUPPORTS) Optional<String> getOriginalUrl(String id); }
UrlShorterManager { @Transactional(propagation = Propagation.REQUIRED) public String generateAndSaveShortUrlPostfix(String url, String name) { if (!validator.isValid(url)) { throw new IllegalArgumentException("Invalid url format: " + url); } Date expiredDate = new Date(System.currentTimeMillis() - expiredPeriodParam * MILLISECONDS_PER_DAY); urlShorterDao.deleteExpiredUrls(expiredDate); String id; if (name != null && !urlShorterDao.loadUrlById(name).isPresent()) { id = name; } else { id = Hashing.murmur3_32() .hashString(url, StandardCharsets.UTF_8).toString(); } urlShorterDao.storeUrl(id, url); return id; }  @Transactional(propagation = Propagation.REQUIRED) String generateAndSaveShortUrlPostfix(String url, String name); @Transactional(propagation = Propagation.SUPPORTS) Optional<String> getOriginalUrl(String id); static final int MILLISECONDS_PER_DAY; }