Search in sources :

Example 31 with FixedQuerySender

use of nl.nn.adapterframework.jdbc.FixedQuerySender 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 32 with FixedQuerySender

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

the class ShowConfiguration method getConfigsFromDatabase.

private List<Map<String, Object>> getConfigsFromDatabase(String configurationName, String dataSourceName) {
    List<Map<String, Object>> returnMap = new ArrayList<Map<String, Object>>();
    if (StringUtils.isEmpty(dataSourceName)) {
        dataSourceName = JndiDataSourceFactory.GLOBAL_DEFAULT_DATASOURCE_NAME;
        if (StringUtils.isEmpty(dataSourceName)) {
            return null;
        }
    }
    FixedQuerySender qs = getIbisContext().createBeanAutowireByName(FixedQuerySender.class);
    qs.setDatasourceName(dataSourceName);
    qs.setQuery("SELECT COUNT(*) FROM IBISCONFIG");
    try {
        qs.configure();
        qs.open();
        try (Connection conn = qs.getConnection()) {
            String query = "SELECT NAME, VERSION, FILENAME, RUSER, ACTIVECONFIG, AUTORELOAD, CRE_TYDST FROM IBISCONFIG WHERE NAME=?";
            try (PreparedStatement stmt = conn.prepareStatement(query)) {
                stmt.setString(1, configurationName);
                try (ResultSet rs = stmt.executeQuery()) {
                    while (rs.next()) {
                        Map<String, Object> config = new HashMap<>();
                        config.put("name", rs.getString(1));
                        config.put("version", rs.getString(2));
                        config.put("filename", rs.getString(3));
                        config.put("user", rs.getString(4));
                        config.put("active", rs.getBoolean(5));
                        config.put("autoreload", rs.getBoolean(6));
                        Date creationDate = rs.getDate(7);
                        config.put("created", DateUtils.format(creationDate, DateUtils.FORMAT_GENERICDATETIME));
                        returnMap.add(config);
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new ApiException(e);
    } finally {
        qs.close();
    }
    returnMap.sort(new NameComparatorBase<Map<String, Object>>() {

        @Override
        public int compare(Map<String, Object> obj1, Map<String, Object> obj2) {
            String filename1 = (String) obj1.get(orderBy);
            String filename2 = (String) obj2.get(orderBy);
            // invert the results as we want the latest version first
            return -compareNames(filename1, filename2);
        }
    });
    return returnMap;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Date(java.util.Date) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) ResultSet(java.sql.ResultSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender)

Aggregations

FixedQuerySender (nl.nn.adapterframework.jdbc.FixedQuerySender)32 Connection (java.sql.Connection)17 PreparedStatement (java.sql.PreparedStatement)16 ResultSet (java.sql.ResultSet)14 SenderException (nl.nn.adapterframework.core.SenderException)13 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)11 Parameter (nl.nn.adapterframework.parameters.Parameter)10 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 SQLException (java.sql.SQLException)8 LinkedHashMap (java.util.LinkedHashMap)8 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)8 Map (java.util.Map)7 PipeRunException (nl.nn.adapterframework.core.PipeRunException)7 JdbcException (nl.nn.adapterframework.jdbc.JdbcException)7 ArrayList (java.util.ArrayList)6 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)5 ListenerException (nl.nn.adapterframework.core.ListenerException)4 IbisWebServiceSender (nl.nn.adapterframework.http.IbisWebServiceSender)4 WebServiceListener (nl.nn.adapterframework.http.WebServiceListener)4