@Override protected GenericModel<HistoryDto> newInstance(Long id) { return new History(id); }
protected List<History> generateHistory(Application app, int count) { LOG.debug("Generate history..."); List<History> histories = new ArrayList<>(); if (app == null) { app = generateApplication(null); } for (int i = 0; i < count; i++) { History history = new History(); history.setApplication(app); history.setLastModifyTime(System.currentTimeMillis()); history.setSequenceNumber(i + 1); history.setChange(generateChange()); history = historyDao.save(history); histories.add(history); LOG.debug("Generated history {}", history); } return histories; }
@Override public HistoryDto saveHistory(HistoryDto historyDto) { HistoryDto savedDto = null; if (isValidSqlObject(historyDto)) { LOG.debug("History dto object is valid. Saving history..."); String applicationId = historyDto.getApplicationId(); if (isValidSqlId(applicationId)) { Application application = applicationDao.getNextSeqNumber(applicationId); if (application != null) { int sequenceNumber = application.getSequenceNumber(); historyDto.setSequenceNumber(sequenceNumber); historyDto.setLastModifyTime(System.currentTimeMillis()); History savedHistory = historyDao.persist(new History(historyDto)); savedDto = savedHistory != null ? savedHistory.toDto() : null; } else { LOG.debug("Can't get sequence number for application id [{}] .", applicationId); } } else { LOG.debug("Incorrect application id, can't save history."); } } else { LOG.info("Invalid HistoryDto object. Can't save object."); } return savedDto; }
@Override public HistoryDto toDto() { HistoryDto dto = createDto(); dto.setId(getStringId()); if (application != null) { dto.setApplicationId(application.getStringId()); } dto.setChange(change != null ? change.toDto() : null); dto.setSequenceNumber(sequenceNumber); dto.setLastModifyTime(lastModifyTime); return dto; }
@Test public void findBySeqNumberStartTest() { List<History> histories = generateHistory(null, 3); Assert.assertNotNull(histories); History third = histories.get(2); List<History> found = historyDao.findBySeqNumberStart(third.getApplication().getId().toString(), 2); Assert.assertEquals(1, found.size()); Assert.assertEquals(third, found.get(0)); }
@Test public void findBySeqNumberRangeTest() { List<History> histories = generateHistory(null, 3); Assert.assertNotNull(histories); History second = histories.get(1); List<History> found = historyDao.findBySeqNumberRange(second.getApplication().getId().toString(), 1, 2); Assert.assertEquals(1, found.size()); Assert.assertEquals(second, found.get(0)); } }
@Test public void findBySeqNumberTest() { List<History> histories = generateHistory(null, 3); Assert.assertNotNull(histories); History third = histories.get(2); History found = historyDao.findBySeqNumber(third.getApplication().getId().toString(), 3); Assert.assertEquals(third, found); }
@Test public void findByAppIdTest() { List<History> histories = generateHistory(null, 3); Assert.assertNotNull(histories); History first = histories.get(0); List<History> found = historyDao.findByAppId(first.getApplication().getId().toString()); Assert.assertEquals(histories, found); }