use of nl.nn.adapterframework.jdbc.FixedQuerySender in project iaf by ibissource.
the class ClassLoaderManagerTest method mockDatabase.
private static void mockDatabase() throws Exception {
// Mock a FixedQuerySender
JmsRealm jmsRealm = spy(new JmsRealm());
jmsRealm.setDatasourceName("fake");
jmsRealm.setRealmName("myRealm");
JmsRealmFactory.getInstance().registerJmsRealm(jmsRealm);
FixedQuerySender fq = mock(FixedQuerySender.class);
doReturn(new GenericDbmsSupport()).when(fq).getDbmsSupport();
Connection conn = mock(Connection.class);
doReturn(conn).when(fq).getConnection();
PreparedStatement stmt = mock(PreparedStatement.class);
doReturn(stmt).when(conn).prepareStatement(anyString());
ResultSet rs = mock(ResultSet.class);
doReturn(true).when(rs).next();
doReturn("dummy").when(rs).getString(anyInt());
URL file = ClassLoaderManager.class.getResource(JAR_FILE);
doReturn(Misc.streamToBytes(file.openStream())).when(rs).getBytes(anyInt());
doReturn(rs).when(stmt).executeQuery();
doReturn(fq).when(ibisContext).createBeanAutowireByName(FixedQuerySender.class);
}
use of nl.nn.adapterframework.jdbc.FixedQuerySender in project iaf by ibissource.
the class DatabaseClassLoaderTest method mockDatabase.
private void mockDatabase(boolean throwException) throws Exception {
// Mock a FixedQuerySender
FixedQuerySender fq = mock(FixedQuerySender.class);
doReturn(new GenericDbmsSupport()).when(fq).getDbmsSupport();
Connection conn = mock(Connection.class);
doReturn(conn).when(fq).getConnection();
PreparedStatement stmt = mock(PreparedStatement.class);
doReturn(stmt).when(conn).prepareStatement(anyString());
ResultSet rs = mock(ResultSet.class);
doReturn(!throwException).when(rs).next();
doReturn("dummy").when(rs).getString(anyInt());
URL file = this.getClass().getResource(JAR_FILE);
doReturn(Misc.streamToBytes(file.openStream())).when(rs).getBytes(anyInt());
doReturn(rs).when(stmt).executeQuery();
doReturn(fq).when(ibisContext).createBeanAutowireByName(FixedQuerySender.class);
// IbisContext.log is a void method
@SuppressWarnings("rawtypes") Answer answer = new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
String message = invocation.getArgument(0);
MessageKeeperLevel level = invocation.getArgument(1);
Exception exception = invocation.getArgument(2);
new ApplicationMessageEvent(spy(ApplicationContext.class), message, level, exception);
return null;
}
};
// Mock the IbisContext's log method which uses getApplicationContext which in turn creates a
// new ApplicationContext if non exists. This functionality should be removed sometime in the future.
// During testing, the IbisContext never initialises and thus there is no ApplicationContext. The
// creation of the ApplicationContext during the test phase causes IllegalStateExceptions
// In turn this causes the actual thing we want to test to never be 'hit', aka the log message.
doAnswer(answer).when(ibisContext).log(anyString(), any(MessageKeeperLevel.class), any(Exception.class));
}
use of nl.nn.adapterframework.jdbc.FixedQuerySender in project iaf by ibissource.
the class TestQuerySenderMocks method testIfMockedResultSetWorks.
@Test
public void testIfMockedResultSetWorks() throws Exception {
configuration = new TestConfiguration();
ResultSet mockedResultSet = FixedQuerySenderMock.ResultSetBuilder.create().build();
configuration.mockQuery("SELECT COUNT(*) FROM FAKE_DATABASE_TABLE", mockedResultSet);
FixedQuerySender qs = SpringUtils.createBean(configuration, FixedQuerySender.class);
qs.setQuery("SELECT COUNT(*) FROM FAKE_DATABASE_TABLE");
qs.configure();
qs.open();
try (Connection conn = qs.getConnection()) {
try (PreparedStatement stmt = conn.prepareStatement("SELECT JOBNAME,JOBGROUP,ADAPTER,RECEIVER,CRON,EXECUTIONINTERVAL,MESSAGE,LOCKER,LOCK_KEY FROM IBISSCHEDULES")) {
try (ResultSet rs = stmt.executeQuery()) {
assertTrue(rs == mockedResultSet);
}
}
}
}
use of nl.nn.adapterframework.jdbc.FixedQuerySender in project iaf by ibissource.
the class CleanupDatabaseJob method execute.
@Override
public void execute(IbisManager ibisManager) {
Date date = new Date();
int maxRows = AppConstants.getInstance().getInt("cleanup.database.maxrows", 25000);
List<MessageLogObject> messageLogs = getAllMessageLogs(ibisManager);
for (MessageLogObject mlo : messageLogs) {
FixedQuerySender qs = null;
try {
qs = SpringUtils.createBean(getApplicationContext(), FixedQuerySender.class);
qs.setDatasourceName(mlo.getDatasourceName());
qs.setName("cleanupDatabase-" + mlo.getTableName());
qs.setQueryType("other");
qs.setTimeout(getQueryTimeout());
qs.setScalar(true);
Parameter param = new Parameter("now", DateUtils.format(date));
param.setType(ParameterType.TIMESTAMP);
qs.addParameter(param);
String query = qs.getDbmsSupport().getCleanUpIbisstoreQuery(mlo.getTableName(), mlo.getKeyField(), mlo.getTypeField(), mlo.getExpiryDateField(), maxRows);
qs.setQuery(query);
qs.configure();
qs.open();
boolean deletedAllRecords = false;
while (!deletedAllRecords) {
Message result = qs.sendMessage(Message.nullMessage(), null);
String resultString = result.asString();
log.info("deleted [" + resultString + "] rows");
int numberOfRowsAffected = Integer.valueOf(resultString);
if (maxRows <= 0 || numberOfRowsAffected < maxRows) {
deletedAllRecords = true;
} else {
log.info("executing the query again for job [cleanupDatabase]!");
}
}
} catch (Exception e) {
String msg = "error while deleting expired records from table [" + mlo.getTableName() + "] (as part of scheduled job execution): " + e.getMessage();
getMessageKeeper().add(msg, MessageKeeperLevel.ERROR);
log.error(getLogPrefix() + msg);
} finally {
if (qs != null) {
qs.close();
}
}
}
}
use of nl.nn.adapterframework.jdbc.FixedQuerySender in project iaf by ibissource.
the class CheckReloadJob method execute.
@Override
public void execute(IbisManager ibisManager) {
if (ibisManager.getIbisContext().isLoadingConfigs()) {
String msg = "skipping checkReload because one or more configurations are currently loading";
getMessageKeeper().add(msg, MessageKeeperLevel.INFO);
log.info(getLogPrefix() + msg);
return;
}
List<String> configNames = new ArrayList<>();
List<String> configsToReload = new ArrayList<>();
FixedQuerySender qs = SpringUtils.createBean(getApplicationContext(), FixedQuerySender.class);
qs.setDatasourceName(getDataSource());
qs.setQuery("SELECT COUNT(*) FROM IBISCONFIG");
String booleanValueTrue = qs.getDbmsSupport().getBooleanValue(true);
String selectQuery = "SELECT VERSION FROM IBISCONFIG WHERE NAME=? AND ACTIVECONFIG = " + booleanValueTrue + " and AUTORELOAD = " + booleanValueTrue;
try {
qs.configure();
qs.open();
try (Connection conn = qs.getConnection();
PreparedStatement stmt = conn.prepareStatement(selectQuery)) {
for (Configuration configuration : ibisManager.getConfigurations()) {
String configName = configuration.getName();
configNames.add(configName);
if (DATABASE_CLASSLOADER.equals(configuration.getClassLoaderType())) {
stmt.setString(1, configName);
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
String ibisConfigVersion = rs.getString(1);
// DatabaseClassLoader configurations always have a version
String configVersion = configuration.getVersion();
if (StringUtils.isEmpty(configVersion) && configuration.getClassLoader() != null) {
// If config hasn't loaded yet, don't skip it!
log.warn(getLogPrefix() + "skipping autoreload for configuration [" + configName + "] unable to determine [configuration.version]");
} else if (!StringUtils.equalsIgnoreCase(ibisConfigVersion, configVersion)) {
log.info(getLogPrefix() + "configuration [" + configName + "] with version [" + configVersion + "] will be reloaded with new version [" + ibisConfigVersion + "]");
configsToReload.add(configName);
}
}
}
}
}
}
} catch (Exception e) {
getMessageKeeper().add("error while executing query [" + selectQuery + "] (as part of scheduled job execution)", e);
} finally {
qs.close();
}
if (!configsToReload.isEmpty()) {
for (String configToReload : configsToReload) {
ibisManager.getIbisContext().reload(configToReload);
}
}
if (CONFIG_AUTO_DB_CLASSLOADER) {
// load new (activated) configs
List<String> dbConfigNames = null;
try {
dbConfigNames = ConfigurationUtils.retrieveConfigNamesFromDatabase(ibisManager.getIbisContext());
} catch (ConfigurationException e) {
getMessageKeeper().add("error while retrieving configuration names from database", e);
}
if (dbConfigNames != null && !dbConfigNames.isEmpty()) {
for (String currentDbConfigurationName : dbConfigNames) {
if (!configNames.contains(currentDbConfigurationName)) {
ibisManager.getIbisContext().load(currentDbConfigurationName);
}
}
// unload old (deactivated) configurations
for (String currentConfigurationName : configNames) {
if (!dbConfigNames.contains(currentConfigurationName) && DATABASE_CLASSLOADER.equals(ibisManager.getConfiguration(currentConfigurationName).getClassLoaderType())) {
ibisManager.getIbisContext().unload(currentConfigurationName);
}
}
}
}
}
Aggregations