RestoreServiceImpl implements RestoreService { @Override @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = ImportException.class) public void importDatabase(ParseSession session) throws ImportException { try { if (!ehourConfig.isInDemoMode()) { session.start(); databaseTruncater.truncateDatabase(); XMLEventReader xmlEventReader = BackupFileUtil.createXmlReaderFromFile(session.getFilename()); XmlParser parser = new XmlParserBuilder() .setConfigurationDao(configurationDao) .setConfigurationParserDao(configurationParserDao) .setEntityParserDao(entityParserDao) .setXmlReader(xmlEventReader) .setSkipValidation(true) .setBackupConfig(backupConfig) .build(); parser.parseXml(session, xmlEventReader); } } catch (Exception e) { session.setGlobalError(true); session.setGlobalErrorMessage(e.getMessage()); LOG.error(e.getMessage(), e); throw new ImportException(e.getMessage(), e); } finally { session.deleteFile(); session.setImported(true); session.finish(); } } @Autowired  RestoreServiceImpl(ConfigurationDao configurationDao,
                              ConfigurationParserDao configurationParserDao,
                              EntityParserDao entityParserDao,
                              DatabaseTruncater databaseTruncater,
                              EhourConfig ehourConfig,
                              BackupConfig backupConfig,
                              TaskExecutor taskExecutor); @Override @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = ImportException.class) void importDatabase(ParseSession session); void setConfigurationDao(ConfigurationDao configurationDao); void setDatabaseTruncater(DatabaseTruncater databaseTruncater);  }
@Test public void shouldImport() throws IOException, ImportException { Configuration version = new Configuration("version", "0.8.3"); when(configurationDao.findById("version")).thenReturn(version); File file = new File("src/test/resources/import/import_data_full.xml"); String tempPath = FileUtils.getTempDirectoryPath(); File destFile = new File(tempPath + "/tmp.xml"); FileUtils.copyFile(file, destFile); ParseSession session = new ParseSession(); session.setFilename(destFile.getAbsolutePath()); restoreService.importDatabase(session); assertFalse(session.isImportable()); assertFalse(destFile.exists()); verify(entityParserDao, times(10)).persist(any(DomainObject.class)); }
@Test public void shouldNotImportInDemoMode() throws IOException, ImportException { Configuration version = new Configuration("version", "0.8.3"); when(configurationDao.findById("version")).thenReturn(version); File file = new File("src/test/resources/import/import_data_full.xml"); String tempPath = FileUtils.getTempDirectoryPath(); File destFile = new File(tempPath + "/tmp.xml"); FileUtils.copyFile(file, destFile); ParseSession session = new ParseSession(); session.setFilename(destFile.getAbsolutePath()); configStub.setDemoMode(true); restoreService.importDatabase(session); assertFalse(session.isImportable()); assertFalse(destFile.exists()); assertEquals(0, userRoleParserDao.getFindUserCount()); }