Search in sources :

Example 11 with JdbcStatement

use of org.finra.herd.model.api.xml.JdbcStatement in project herd by FINRAOS.

the class JdbcServiceTest method testExecuteJdbcStatementTypeQuerySuccess.

/**
 * Test case where user specifies a QUERY statement type. A proper result set should be created.
 */
@Test
public void testExecuteJdbcStatementTypeQuerySuccess() {
    // Get test request
    JdbcExecutionRequest jdbcExecutionRequest = jdbcServiceTestHelper.createDefaultQueryJdbcExecutionRequest();
    JdbcStatement expectedJdbcStatement = jdbcExecutionRequest.getStatements().get(0);
    JdbcExecutionResponse jdbcExecutionResponse = jdbcService.executeJdbc(jdbcExecutionRequest);
    Assert.assertEquals("JDBC statements size", 1, jdbcExecutionResponse.getStatements().size());
    JdbcStatement actualJdbcStatement = jdbcExecutionResponse.getStatements().get(0);
    Assert.assertNull("JDBC statement error message is not null", actualJdbcStatement.getErrorMessage());
    Assert.assertNull("JDBC statement result not is null", actualJdbcStatement.getResult());
    Assert.assertEquals("JDBC statement SQL", expectedJdbcStatement.getSql(), actualJdbcStatement.getSql());
    Assert.assertEquals("JDBC statement status", JdbcStatementStatus.SUCCESS, actualJdbcStatement.getStatus());
    Assert.assertEquals("JDBC statement type", expectedJdbcStatement.getType(), actualJdbcStatement.getType());
    Assert.assertNotNull("JDBC statement result set is null", actualJdbcStatement.getResultSet());
    Assert.assertNotNull("JDBC statement result set column names is null", actualJdbcStatement.getResultSet().getColumnNames());
    Assert.assertEquals("JDBC statement result set column names", Arrays.asList("COL1", "COL2", "COL3"), actualJdbcStatement.getResultSet().getColumnNames());
    Assert.assertNotNull("JDBC statement result set rows is null", actualJdbcStatement.getResultSet().getRows());
    Assert.assertEquals("JDBC statement result set rows size", 2, actualJdbcStatement.getResultSet().getRows().size());
    {
        JdbcStatementResultSetRow row = actualJdbcStatement.getResultSet().getRows().get(0);
        Assert.assertNotNull("JDBC statement row [0] columns is null", row.getColumns());
        Assert.assertEquals("JDBC statement row [0] columns", Arrays.asList("A", "B", "C"), row.getColumns());
    }
    {
        JdbcStatementResultSetRow row = actualJdbcStatement.getResultSet().getRows().get(1);
        Assert.assertNotNull("JDBC statement row [1] columns is null", row.getColumns());
        Assert.assertEquals("JDBC statement row [1] columns", Arrays.asList("D", "E", "F"), row.getColumns());
    }
}
Also used : JdbcStatementResultSetRow(org.finra.herd.model.api.xml.JdbcStatementResultSetRow) JdbcStatement(org.finra.herd.model.api.xml.JdbcStatement) JdbcExecutionRequest(org.finra.herd.model.api.xml.JdbcExecutionRequest) JdbcExecutionResponse(org.finra.herd.model.api.xml.JdbcExecutionResponse) Test(org.junit.Test)

Example 12 with JdbcStatement

use of org.finra.herd.model.api.xml.JdbcStatement in project herd by FINRAOS.

the class JdbcServiceTest method testExecuteJdbcParamValidationStatementsExceedMaximum.

/**
 * Parameter validation, request statement list has more statements than configured maximum.
 */
@Test
public void testExecuteJdbcParamValidationStatementsExceedMaximum() {
    /*
         * Update environment variable to use a maximum statement
         */
    int maxStatements = 1;
    try {
        HashMap<String, Object> overrideMap = new HashMap<>();
        overrideMap.put(ConfigurationValue.JDBC_MAX_STATEMENTS.getKey(), maxStatements);
        modifyPropertySourceInEnvironment(overrideMap);
    } catch (Exception e) {
        throw new RuntimeException("Error modifying environment variable.", e);
    }
    /*
         * Add 1 more statement to the JDBC request.
         * The default request should already have 1 statement.
         */
    JdbcExecutionRequest jdbcExecutionRequest = jdbcServiceTestHelper.createDefaultUpdateJdbcExecutionRequest();
    jdbcExecutionRequest.getStatements().add(new JdbcStatement());
    try {
        // Execute
        jdbcService.executeJdbc(jdbcExecutionRequest);
        Assert.fail("expected an IllegalArgumentException, but no exception was thrown");
    } catch (Exception e) {
        Assert.assertEquals("thrown exception type", IllegalArgumentException.class, e.getClass());
        Assert.assertEquals("thrown exception message", "The number of JDBC statements exceeded the maximum allowed " + maxStatements + ".", e.getMessage());
    } finally {
        /*
             * 
             */
        try {
            restorePropertySourceInEnvironment();
        } catch (Exception e) {
            throw new RuntimeException("Error restoring environment variables. Subsequent tests may be affected.", e);
        }
    }
}
Also used : JdbcStatement(org.finra.herd.model.api.xml.JdbcStatement) HashMap(java.util.HashMap) JdbcExecutionRequest(org.finra.herd.model.api.xml.JdbcExecutionRequest) Test(org.junit.Test)

Example 13 with JdbcStatement

use of org.finra.herd.model.api.xml.JdbcStatement in project herd by FINRAOS.

the class JdbcServiceTestHelper method createDefaultUpdateJdbcExecutionRequest.

/**
 * Creates a default JDBC execution request which is guaranteed to work. The request contains a single statement of UPDATE type.
 *
 * @return a valid JDBC request
 */
public JdbcExecutionRequest createDefaultUpdateJdbcExecutionRequest() {
    JdbcConnection jdbcConnection = createDefaultJdbcConnection();
    List<JdbcStatement> jdbcStatements = createDefaultUpdateJdbcStatements();
    JdbcExecutionRequest jdbcExecutionRequest = createJdbcExecutionRequest(jdbcConnection, jdbcStatements);
    return jdbcExecutionRequest;
}
Also used : JdbcStatement(org.finra.herd.model.api.xml.JdbcStatement) JdbcConnection(org.finra.herd.model.api.xml.JdbcConnection) JdbcExecutionRequest(org.finra.herd.model.api.xml.JdbcExecutionRequest)

Example 14 with JdbcStatement

use of org.finra.herd.model.api.xml.JdbcStatement in project herd by FINRAOS.

the class JdbcServiceTestHelper method createDefaultQueryJdbcExecutionRequest.

/**
 * Creates a default JDBC execution request which is guaranteed to work. The request contains a single statement of QUERY type.
 *
 * @return a valid JDBC request
 */
public JdbcExecutionRequest createDefaultQueryJdbcExecutionRequest() {
    JdbcConnection jdbcConnection = createDefaultJdbcConnection();
    List<JdbcStatement> jdbcStatements = createDefaultQueryJdbcStatements();
    JdbcExecutionRequest jdbcExecutionRequest = createJdbcExecutionRequest(jdbcConnection, jdbcStatements);
    return jdbcExecutionRequest;
}
Also used : JdbcStatement(org.finra.herd.model.api.xml.JdbcStatement) JdbcConnection(org.finra.herd.model.api.xml.JdbcConnection) JdbcExecutionRequest(org.finra.herd.model.api.xml.JdbcExecutionRequest)

Example 15 with JdbcStatement

use of org.finra.herd.model.api.xml.JdbcStatement in project herd by FINRAOS.

the class JdbcServiceImpl method validateJdbcStatements.

/**
 * Validates parameters specified in the given statements. The statements must not be null, and must not be empty.
 *
 * @param jdbcStatements the list of statements to validate
 */
private void validateJdbcStatements(List<JdbcStatement> jdbcStatements) {
    Assert.notNull(jdbcStatements, "JDBC statements are required");
    Assert.isTrue(!jdbcStatements.isEmpty(), "JDBC statements are required");
    Integer jdbcMaxStatements = configurationHelper.getProperty(ConfigurationValue.JDBC_MAX_STATEMENTS, Integer.class);
    if (jdbcMaxStatements != null) {
        Assert.isTrue(jdbcStatements.size() <= jdbcMaxStatements, "The number of JDBC statements exceeded the maximum allowed " + jdbcMaxStatements + ".");
    }
    for (int i = 0; i < jdbcStatements.size(); i++) {
        JdbcStatement jdbcStatement = jdbcStatements.get(i);
        validateJdbcStatement(jdbcStatement, i);
    }
}
Also used : JdbcStatement(org.finra.herd.model.api.xml.JdbcStatement)

Aggregations

JdbcStatement (org.finra.herd.model.api.xml.JdbcStatement)15 JdbcExecutionRequest (org.finra.herd.model.api.xml.JdbcExecutionRequest)10 JdbcExecutionResponse (org.finra.herd.model.api.xml.JdbcExecutionResponse)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)2 JdbcConnection (org.finra.herd.model.api.xml.JdbcConnection)2 S3PropertiesLocation (org.finra.herd.model.api.xml.S3PropertiesLocation)2 HashMap (java.util.HashMap)1 DataSource (javax.sql.DataSource)1 JdbcStatementResultSetRow (org.finra.herd.model.api.xml.JdbcStatementResultSetRow)1 Job (org.finra.herd.model.api.xml.Job)1 JobCreateRequest (org.finra.herd.model.api.xml.JobCreateRequest)1 Parameter (org.finra.herd.model.api.xml.Parameter)1 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)1 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)1 Transactional (org.springframework.transaction.annotation.Transactional)1