Search in sources :

Example 1 with GenericDbmsSupport

use of nl.nn.adapterframework.jdbc.dbms.GenericDbmsSupport 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));
}
Also used : MessageKeeperLevel(nl.nn.adapterframework.util.MessageKeeper.MessageKeeperLevel) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) GenericDbmsSupport(nl.nn.adapterframework.jdbc.dbms.GenericDbmsSupport) URL(java.net.URL) ClassLoaderException(nl.nn.adapterframework.configuration.ClassLoaderException) Answer(org.mockito.stubbing.Answer) ApplicationContext(org.springframework.context.ApplicationContext) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ResultSet(java.sql.ResultSet) ApplicationMessageEvent(nl.nn.adapterframework.lifecycle.ApplicationMessageEvent) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender)

Example 2 with GenericDbmsSupport

use of nl.nn.adapterframework.jdbc.dbms.GenericDbmsSupport 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);
}
Also used : JmsRealm(nl.nn.adapterframework.jms.JmsRealm) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) GenericDbmsSupport(nl.nn.adapterframework.jdbc.dbms.GenericDbmsSupport) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender) URL(java.net.URL)

Example 3 with GenericDbmsSupport

use of nl.nn.adapterframework.jdbc.dbms.GenericDbmsSupport in project iaf by ibissource.

the class ConfigurationUtilsTest method mockDatabase.

private void mockDatabase() 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();
    // Override prepareStatement(String query) and return a mock to validate the parameters
    doAnswer(new Answer<PreparedStatementMock>() {

        @Override
        public PreparedStatementMock answer(InvocationOnMock invocation) throws Throwable {
            String query = (String) invocation.getArguments()[0];
            stmt = PreparedStatementMock.newInstance(query);
            return stmt;
        }
    }).when(conn).prepareStatement(anyString());
    doReturn(fq).when(ibisContext).createBeanAutowireByName(FixedQuerySender.class);
    // STUB a TransactionManager
    PlatformTransactionManager ptm = new PlatformTransactionManager() {

        @Override
        public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
            return mock(TransactionStatus.class);
        }

        @Override
        public void commit(TransactionStatus status) throws TransactionException {
        // STUB
        }

        @Override
        public void rollback(TransactionStatus status) throws TransactionException {
        // STUB
        }
    };
    doReturn(ptm).when(ibisContext).getBean("txManager", PlatformTransactionManager.class);
}
Also used : TransactionDefinition(org.springframework.transaction.TransactionDefinition) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Connection(java.sql.Connection) TransactionStatus(org.springframework.transaction.TransactionStatus) GenericDbmsSupport(nl.nn.adapterframework.jdbc.dbms.GenericDbmsSupport) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager)

Example 4 with GenericDbmsSupport

use of nl.nn.adapterframework.jdbc.dbms.GenericDbmsSupport in project iaf by ibissource.

the class MessageStoreListenerTest method createListener.

@Override
public MessageStoreListener<M> createListener() throws Exception {
    MessageStoreListener listener = spy(new MessageStoreListener() {

        @Override
        protected Object getRawMessage(Connection conn, Map threadContext) throws ListenerException {
            // super class JdbcListener always wraps this in a MessageWrapper
            MessageWrapper<Object> mw = new MessageWrapper<>();
            mw.setMessage(Message.asMessage(threadContext.get(STUB_RESULT_KEY)));
            mw.setId("" + threadContext.get(PipeLineSession.originalMessageIdKey));
            return mw;
        }
    });
    DatabaseMetaData md = mock(DatabaseMetaData.class);
    doReturn("product").when(md).getDatabaseProductName();
    doReturn("version").when(md).getDatabaseProductVersion();
    Connection conn = mock(Connection.class);
    doReturn(md).when(conn).getMetaData();
    JndiDataSourceFactory factory = new JndiDataSourceFactory();
    DataSource dataSource = mock(DataSource.class);
    String dataSourceName = "myDummyDataSource";
    factory.add(dataSource, dataSourceName);
    listener.setDataSourceFactory(factory);
    doReturn(conn).when(dataSource).getConnection();
    listener.setConnectionsArePooled(false);
    listener.setDatasourceName(dataSourceName);
    doReturn(new GenericDbmsSupport()).when(listener).getDbmsSupport();
    return listener;
}
Also used : Connection(java.sql.Connection) GenericDbmsSupport(nl.nn.adapterframework.jdbc.dbms.GenericDbmsSupport) DatabaseMetaData(java.sql.DatabaseMetaData) DataSource(javax.sql.DataSource) ListenerException(nl.nn.adapterframework.core.ListenerException) MessageStoreListener(nl.nn.adapterframework.jdbc.MessageStoreListener) JndiDataSourceFactory(nl.nn.adapterframework.jndi.JndiDataSourceFactory) Map(java.util.Map)

Aggregations

Connection (java.sql.Connection)4 GenericDbmsSupport (nl.nn.adapterframework.jdbc.dbms.GenericDbmsSupport)4 FixedQuerySender (nl.nn.adapterframework.jdbc.FixedQuerySender)3 URL (java.net.URL)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 DatabaseMetaData (java.sql.DatabaseMetaData)1 Map (java.util.Map)1 DataSource (javax.sql.DataSource)1 ClassLoaderException (nl.nn.adapterframework.configuration.ClassLoaderException)1 ListenerException (nl.nn.adapterframework.core.ListenerException)1 MessageStoreListener (nl.nn.adapterframework.jdbc.MessageStoreListener)1 JmsRealm (nl.nn.adapterframework.jms.JmsRealm)1 JndiDataSourceFactory (nl.nn.adapterframework.jndi.JndiDataSourceFactory)1 ApplicationMessageEvent (nl.nn.adapterframework.lifecycle.ApplicationMessageEvent)1 MessageKeeperLevel (nl.nn.adapterframework.util.MessageKeeper.MessageKeeperLevel)1 Answer (org.mockito.stubbing.Answer)1 ApplicationContext (org.springframework.context.ApplicationContext)1 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)1