Search in sources :

Example 6 with JdbcExecutionRequest

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

the class JdbcServiceTest method testExecuteJdbcStatementError.

/**
 * Use case where 3 statements are requested to be executed, but the 2nd is erroneous. The first statement should result in SUCCESS. The second should
 * result in ERROR, with appropriate result message. The third should result in SKIPPED with no result.
 */
@Test
public void testExecuteJdbcStatementError() {
    // 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, null, null, null, null, null));
    jdbcExecutionRequest.getStatements().add(new JdbcStatement(JdbcStatementType.UPDATE, MockJdbcOperations.CASE_1_SQL, null, null, null, null, null));
    // Execute
    JdbcExecutionResponse jdbcExecutionResponse = jdbcService.executeJdbc(jdbcExecutionRequest);
    // Assert results
    Assert.assertNull("JDBC connection is 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 actualJdbcStatement = jdbcExecutionResponse.getStatements().get(1);
        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 actualJdbcStatement = jdbcExecutionResponse.getStatements().get(2);
        Assert.assertEquals("JDBC statement [2] status", JdbcStatementStatus.SKIPPED, actualJdbcStatement.getStatus());
        Assert.assertNull("JDBC statement [2] result is not null", actualJdbcStatement.getResult());
    }
}
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 7 with JdbcExecutionRequest

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

the class JdbcServiceTest method testExecuteJdbcParamValidationStatementsMaximumStatementsSpecified.

/**
 * Parameter validation, request statement list has less statements than configured maximum.
 */
@Test
public void testExecuteJdbcParamValidationStatementsMaximumStatementsSpecified() {
    /*
         * 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();
    try {
        // Execute
        JdbcExecutionResponse jdbcExecutionResponse = jdbcService.executeJdbc(jdbcExecutionRequest);
        Assert.assertNotNull("jdbcExecutionResponse", jdbcExecutionResponse);
    } 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)

Example 8 with JdbcExecutionRequest

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

the class JdbcServiceTest method testExecuteJdbcWithS3PropertiesParamUrlBlankAfterReplace.

/**
 * If the result of replacing URL using S3 properties is blank, throws a validation error.
 */
@Test
public void testExecuteJdbcWithS3PropertiesParamUrlBlankAfterReplace() {
    String s3BucketName = "test_bucket";
    String s3ObjectKey = "test_key";
    String content = "foo=";
    putS3Object(s3BucketName, s3ObjectKey, content);
    JdbcExecutionRequest jdbcExecutionRequest = jdbcServiceTestHelper.createDefaultUpdateJdbcExecutionRequest();
    jdbcExecutionRequest.getConnection().setUrl("${foo}");
    jdbcExecutionRequest.setS3PropertiesLocation(new S3PropertiesLocation(s3BucketName, s3ObjectKey));
    try {
        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", "JDBC connection URL is required", e.getMessage());
    }
}
Also used : S3PropertiesLocation(org.finra.herd.model.api.xml.S3PropertiesLocation) JdbcExecutionRequest(org.finra.herd.model.api.xml.JdbcExecutionRequest) Test(org.junit.Test)

Example 9 with JdbcExecutionRequest

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

the class JdbcServiceTestHelper method createJdbcExecutionRequest.

/**
 * Creates a JDBC request with the specified values.
 *
 * @param jdbcConnection JDBC connection
 * @param jdbcStatements JDBC statements
 *
 * @return an execution request.
 */
public JdbcExecutionRequest createJdbcExecutionRequest(JdbcConnection jdbcConnection, List<JdbcStatement> jdbcStatements) {
    JdbcExecutionRequest jdbcExecutionRequest = new JdbcExecutionRequest();
    jdbcExecutionRequest.setConnection(jdbcConnection);
    jdbcExecutionRequest.setStatements(jdbcStatements);
    return jdbcExecutionRequest;
}
Also used : JdbcExecutionRequest(org.finra.herd.model.api.xml.JdbcExecutionRequest)

Example 10 with JdbcExecutionRequest

use of org.finra.herd.model.api.xml.JdbcExecutionRequest 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)

Aggregations

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