Search in sources :

Example 6 with JdbcExecutionResponse

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

the class ExecuteJdbcTest method testExecuteJdbcWithReceiveTask.

/**
 * Asserts that the task executes asynchronously when receiveTaskId is specified.
 * <p/>
 * This is a very special test case which involves multithreading and transactions, therefore we cannot use the standard test methods we have. The
 * transaction MUST BE DISABLED for this test to work correctly - since we have 2 threads which both access the database, if we run transactionally, the
 * threads cannot share information.
 * <p/>
 * TODO this test could be made generic once we have async support for other tasks.
 */
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void testExecuteJdbcWithReceiveTask() throws Exception {
    // Create and persist a test job definition.
    executeJdbcTestHelper.prepareHerdDatabaseForExecuteJdbcWithReceiveTaskTest(TEST_ACTIVITI_NAMESPACE_CD, TEST_ACTIVITI_JOB_NAME, "classpath:org/finra/herd/service/testActivitiWorkflowExecuteJdbcTaskWithReceiveTask.bpmn20.xml");
    try {
        // Create a JDBC execution request.
        JdbcExecutionRequest jdbcExecutionRequest = jdbcServiceTestHelper.createDefaultUpdateJdbcExecutionRequest();
        // Create and initialize a list of parameters.
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(new Parameter("contentType", "xml"));
        parameters.add(new Parameter("jdbcExecutionRequest", xmlHelper.objectToXml(jdbcExecutionRequest)));
        // Get a job create request.
        JobCreateRequest jobCreateRequest = jobServiceTestHelper.createJobCreateRequest(TEST_ACTIVITI_NAMESPACE_CD, TEST_ACTIVITI_JOB_NAME);
        jobCreateRequest.setParameters(parameters);
        // Start the job.
        Job jobStartResponse = jobService.createAndStartJob(jobCreateRequest);
        // Wait for the process to finish
        waitUntilAllProcessCompleted();
        // Validate that the job is completed.
        Job jobGetResponse = jobService.getJob(jobStartResponse.getId(), true);
        assertEquals(JobStatusEnum.COMPLETED, jobGetResponse.getStatus());
        // Validate the task status.
        assertTrue(jobGetResponse.getParameters().contains(new Parameter("service_taskStatus", "SUCCESS")));
        // Validate the JDBC execution response.
        JdbcExecutionResponse expectedJdbcExecutionResponse = new JdbcExecutionResponse();
        JdbcStatement originalJdbcStatement = jdbcExecutionRequest.getStatements().get(0);
        JdbcStatement expectedJdbcStatement = new JdbcStatement();
        expectedJdbcStatement.setType(originalJdbcStatement.getType());
        expectedJdbcStatement.setSql(originalJdbcStatement.getSql());
        expectedJdbcStatement.setStatus(JdbcStatementStatus.SUCCESS);
        expectedJdbcStatement.setResult("1");
        expectedJdbcExecutionResponse.setStatements(Arrays.asList(expectedJdbcStatement));
        Parameter expectedJdbcExecutionResponseParameter = new Parameter("service_jsonResponse", jsonHelper.objectToJson(expectedJdbcExecutionResponse));
        assertTrue(jobGetResponse.getParameters().contains(expectedJdbcExecutionResponseParameter));
    } finally {
        // Clean up the Herd database.
        executeJdbcTestHelper.cleanUpHerdDatabaseAfterExecuteJdbcWithReceiveTaskTest(TEST_ACTIVITI_NAMESPACE_CD, TEST_ACTIVITI_JOB_NAME);
        // Clean up the Activiti.
        deleteActivitiDeployments();
    }
}
Also used : JdbcStatement(org.finra.herd.model.api.xml.JdbcStatement) ArrayList(java.util.ArrayList) Parameter(org.finra.herd.model.api.xml.Parameter) Job(org.finra.herd.model.api.xml.Job) JdbcExecutionRequest(org.finra.herd.model.api.xml.JdbcExecutionRequest) JdbcExecutionResponse(org.finra.herd.model.api.xml.JdbcExecutionResponse) JobCreateRequest(org.finra.herd.model.api.xml.JobCreateRequest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with JdbcExecutionResponse

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

the class JdbcServiceTest method testExecuteJdbcWithS3PropertiesSuccess.

/**
 * Execute JDBC using S3 properties file. Unfortunately, not many assertions that can be done through the service layer. Asserts that no errors are thrown,
 * and that the response SQL does not expose the secrets.
 */
@Test
public void testExecuteJdbcWithS3PropertiesSuccess() {
    String s3BucketName = "test_bucket";
    String s3ObjectKey = "test_key";
    String content = "foo=bar";
    putS3Object(s3BucketName, s3ObjectKey, content);
    JdbcExecutionRequest jdbcExecutionRequest = jdbcServiceTestHelper.createDefaultUpdateJdbcExecutionRequest();
    jdbcExecutionRequest.getConnection().setUrl("test_url_${foo}");
    jdbcExecutionRequest.getConnection().setUsername("test_username_${foo}");
    jdbcExecutionRequest.getConnection().setPassword("test_password_${foo}");
    JdbcStatement jdbcStatement = jdbcExecutionRequest.getStatements().get(0);
    jdbcStatement.setSql("test_sql_${foo}");
    jdbcExecutionRequest.setS3PropertiesLocation(new S3PropertiesLocation(s3BucketName, s3ObjectKey));
    try {
        JdbcExecutionResponse jdbcExecutionResponse = jdbcService.executeJdbc(jdbcExecutionRequest);
        Assert.assertEquals("jdbc execution response statement [0] sql", "test_sql_${foo}", jdbcExecutionResponse.getStatements().get(0).getSql());
    } catch (Exception e) {
        Assert.fail("unexpected exception was thrown. " + e);
    }
}
Also used : S3PropertiesLocation(org.finra.herd.model.api.xml.S3PropertiesLocation) 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 8 with JdbcExecutionResponse

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

the class JdbcServiceTest method testExecuteJdbcStatementTypeQueryError.

/**
 * Test case where user specifies a QUERY statement type, but there are SQL errors. The status should be ERROR and no result set should exist in the
 * result.
 */
@Test
public void testExecuteJdbcStatementTypeQueryError() {
    // Get test request
    JdbcExecutionRequest jdbcExecutionRequest = jdbcServiceTestHelper.createDefaultQueryJdbcExecutionRequest();
    JdbcStatement expectedJdbcStatement = jdbcExecutionRequest.getStatements().get(0);
    expectedJdbcStatement.setSql(MockJdbcOperations.CASE_2_SQL);
    JdbcExecutionResponse jdbcExecutionResponse = jdbcService.executeJdbc(jdbcExecutionRequest);
    Assert.assertEquals("JDBC statements size", 1, jdbcExecutionResponse.getStatements().size());
    JdbcStatement actualJdbcStatement = jdbcExecutionResponse.getStatements().get(0);
    Assert.assertNotNull("JDBC statement error message", actualJdbcStatement.getErrorMessage());
    Assert.assertEquals("JDBC statement error message", "java.sql.SQLException: test DataIntegrityViolationException cause", actualJdbcStatement.getErrorMessage());
    Assert.assertNull("JDBC statement result", actualJdbcStatement.getResult());
    Assert.assertEquals("JDBC statement status", JdbcStatementStatus.ERROR, actualJdbcStatement.getStatus());
    Assert.assertEquals("JDBC statement type", expectedJdbcStatement.getType(), actualJdbcStatement.getType());
    Assert.assertNull("JDBC statement result set", actualJdbcStatement.getResultSet());
}
Also used : 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 9 with JdbcExecutionResponse

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

the class JdbcServiceTest method testExecuteJdbcStatementErrorContinueOnError.

/**
 * Test case where statements result in errors, but continue on error flag is set to true for those statements. The subsequent statements should continue
 * executing.
 */
@Test
public void testExecuteJdbcStatementErrorContinueOnError() {
    // Create test request
    JdbcExecutionRequest jdbcExecutionRequest = jdbcServiceTestHelper.createDefaultUpdateJdbcExecutionRequest();
    // First statement already included
    // Second statement uses case 2 which throws an error
    jdbcExecutionRequest.getStatements().add(new JdbcStatement(JdbcStatementType.UPDATE, MockJdbcOperations.CASE_2_SQL, true, null, null, null, null));
    jdbcExecutionRequest.getStatements().add(new JdbcStatement(JdbcStatementType.UPDATE, MockJdbcOperations.CASE_1_SQL, false, null, null, null, null));
    // Execute
    JdbcExecutionResponse jdbcExecutionResponse = jdbcService.executeJdbc(jdbcExecutionRequest);
    // Assert results
    Assert.assertNull("JDBC connection is not null", jdbcExecutionResponse.getConnection());
    Assert.assertEquals("JDBC statements size", jdbcExecutionRequest.getStatements().size(), jdbcExecutionResponse.getStatements().size());
    {
        JdbcStatement actualJdbcStatement = jdbcExecutionResponse.getStatements().get(0);
        Assert.assertEquals("JDBC statement [0] status", JdbcStatementStatus.SUCCESS, actualJdbcStatement.getStatus());
    }
    {
        JdbcStatement expectedJdbcStatement = jdbcExecutionResponse.getStatements().get(1);
        JdbcStatement actualJdbcStatement = jdbcExecutionResponse.getStatements().get(1);
        Assert.assertEquals("JDBC statement [1] continue on error", expectedJdbcStatement.isContinueOnError(), actualJdbcStatement.isContinueOnError());
        Assert.assertEquals("JDBC statement [1] status", JdbcStatementStatus.ERROR, actualJdbcStatement.getStatus());
        Assert.assertNull("JDBC statement [1] result is not null", actualJdbcStatement.getResult());
        Assert.assertEquals("JDBC statement [1] error message", "java.sql.SQLException: test DataIntegrityViolationException cause", actualJdbcStatement.getErrorMessage());
    }
    {
        JdbcStatement expectedJdbcStatement = jdbcExecutionResponse.getStatements().get(2);
        JdbcStatement actualJdbcStatement = jdbcExecutionResponse.getStatements().get(2);
        Assert.assertEquals("JDBC statement [2] status", expectedJdbcStatement.isContinueOnError(), actualJdbcStatement.isContinueOnError());
        Assert.assertEquals("JDBC statement [2] status", JdbcStatementStatus.SUCCESS, actualJdbcStatement.getStatus());
    }
}
Also used : 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 10 with JdbcExecutionResponse

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

the class JdbcServiceTest method testExecuteJdbcStatementTypeQueryMaximumRows.

/**
 * Test case where user specifies a QUERY statement type and a maximum number of rows is specified in the environment.
 */
@Test
public void testExecuteJdbcStatementTypeQueryMaximumRows() {
    int expectedRowSize = 1;
    try {
        Map<String, Object> overrideMap = new HashMap<>();
        overrideMap.put(ConfigurationValue.JDBC_RESULT_MAX_ROWS.getKey(), expectedRowSize);
        modifyPropertySourceInEnvironment(overrideMap);
    } catch (Exception e) {
        throw new RuntimeException("Error modifying environment variables", e);
    }
    try {
        // Get test request
        JdbcExecutionRequest jdbcExecutionRequest = jdbcServiceTestHelper.createDefaultQueryJdbcExecutionRequest();
        JdbcExecutionResponse jdbcExecutionResponse = jdbcService.executeJdbc(jdbcExecutionRequest);
        Assert.assertEquals("result set row size", expectedRowSize, jdbcExecutionResponse.getStatements().get(0).getResultSet().getRows().size());
    } finally {
        try {
            restorePropertySourceInEnvironment();
        } catch (Exception e) {
            throw new RuntimeException("Error restoring environment variables. Subsequent tests may be affected.", e);
        }
    }
}
Also used : HashMap(java.util.HashMap) JdbcExecutionRequest(org.finra.herd.model.api.xml.JdbcExecutionRequest) JdbcExecutionResponse(org.finra.herd.model.api.xml.JdbcExecutionResponse) Test(org.junit.Test)

Aggregations

JdbcExecutionResponse (org.finra.herd.model.api.xml.JdbcExecutionResponse)14 JdbcExecutionRequest (org.finra.herd.model.api.xml.JdbcExecutionRequest)12 Test (org.junit.Test)12 JdbcStatement (org.finra.herd.model.api.xml.JdbcStatement)9 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 Parameter (org.finra.herd.model.api.xml.Parameter)3 S3PropertiesLocation (org.finra.herd.model.api.xml.S3PropertiesLocation)3 FieldExtension (org.activiti.bpmn.model.FieldExtension)2 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 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)1 Transactional (org.springframework.transaction.annotation.Transactional)1