Search in sources :

Example 1 with UnableToExecuteStatementException

use of org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException in project killbill by killbill.

the class TestDefaultRolledUpUsageDao method testDuplicateRecords.

@Test(groups = "slow")
public void testDuplicateRecords() {
    final UUID subscriptionId = UUID.randomUUID();
    final String unitType1 = "foo";
    final String unitType2 = "bar";
    final LocalDate startDate = new LocalDate(2013, 1, 1);
    final LocalDate endDate = new LocalDate(2013, 2, 1);
    final Long amount1 = 10L;
    final Long amount2 = 5L;
    final Long amount3 = 13L;
    RolledUpUsageModelDao usage1 = new RolledUpUsageModelDao(subscriptionId, unitType1, startDate, amount1, UUID.randomUUID().toString());
    RolledUpUsageModelDao usage2 = new RolledUpUsageModelDao(subscriptionId, unitType1, startDate.plusDays(1), amount2, UUID.randomUUID().toString());
    RolledUpUsageModelDao usage3 = new RolledUpUsageModelDao(subscriptionId, unitType2, endDate.minusDays(1), amount3, UUID.randomUUID().toString());
    List<RolledUpUsageModelDao> usages = new ArrayList<RolledUpUsageModelDao>();
    usages.add(usage1);
    usages.add(usage2);
    usages.add(usage3);
    rolledUpUsageDao.record(usages, internalCallContext);
    final List<RolledUpUsageModelDao> result = rolledUpUsageDao.getAllUsageForSubscription(subscriptionId, startDate, endDate, internalCallContext);
    assertEquals(result.size(), 3);
    try {
        rolledUpUsageDao.record(usages, internalCallContext);
        fail("duplicate records accepted");
    } catch (UnableToExecuteStatementException e) {
        assertEquals(result.size(), 3);
    }
}
Also used : ArrayList(java.util.ArrayList) UnableToExecuteStatementException(org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) Test(org.testng.annotations.Test)

Example 2 with UnableToExecuteStatementException

use of org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException in project SONG by overture-stack.

the class UploadService method upload.

@SneakyThrows
public ResponseEntity<String> upload(@NonNull String studyId, @NonNull String payload, boolean isAsyncValidation) {
    studyService.checkStudyExist(studyId);
    String analysisType;
    String uploadId;
    val status = JsonUtils.ObjectNode();
    status.put("status", "ok");
    try {
        val analysisId = JsonUtils.readTree(payload).at("/analysisId").asText();
        List<String> ids;
        if (isNullOrEmpty(analysisId)) {
            // Our business rules say that we always want to create a new record if no analysisId is set,
            // even if the rest of the content is duplicated.
            ids = Collections.emptyList();
        } else {
            ids = uploadRepository.findByBusinessKey(studyId, analysisId);
        }
        if (isNull(ids) || ids.isEmpty()) {
            uploadId = id.generate(UPLOAD_PREFIX);
            create(studyId, analysisId, uploadId, payload);
        } else if (ids.size() == 1) {
            uploadId = ids.get(0);
            val previousUpload = uploadRepository.get(uploadId);
            status.put("status", format("WARNING: replaced content for analysisId '%s'", analysisId));
            status.put("replaced", previousUpload.getPayload());
            update(uploadId, payload);
        } else {
            throw buildServerException(getClass(), UPLOAD_ID_NOT_FOUND, "Multiple upload ids found for analysisId='%s', study='%s'", analysisId, studyId);
        }
        analysisType = JsonUtils.readTree(payload).at("/analysisType").asText("");
    } catch (UnableToExecuteStatementException jdbie) {
        log.error(jdbie.getCause().getMessage());
        throw buildServerException(getClass(), UPLOAD_REPOSITORY_CREATE_RECORD, "Unable to create record in upload repository");
    } catch (JsonProcessingException jpe) {
        log.error(jpe.getCause().getMessage());
        throw buildServerException(getClass(), PAYLOAD_PARSING, "Unable parse the input payload: %s ", payload);
    }
    if (isAsyncValidation) {
        // Asynchronous operation.
        validator.asyncValidate(uploadId, payload, analysisType);
    } else {
        // Synchronous operation
        validator.syncValidate(uploadId, payload, analysisType);
    }
    status.put("uploadId", uploadId);
    return ok(status.toString());
}
Also used : lombok.val(lombok.val) UnableToExecuteStatementException(org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) SneakyThrows(lombok.SneakyThrows)

Aggregations

UnableToExecuteStatementException (org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 SneakyThrows (lombok.SneakyThrows)1 lombok.val (lombok.val)1 LocalDate (org.joda.time.LocalDate)1 Test (org.testng.annotations.Test)1