Search in sources :

Example 46 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project dhis2-core by dhis2.

the class JdbcEventAnalyticsManagerTest method testHandlingWhenExceptionIsNull.

@Test
void testHandlingWhenExceptionIsNull() {
    // Given
    final DataIntegrityViolationException aNullException = null;
    // When
    assertThrows(QueryRuntimeException.class, () -> handle(aNullException), E7133.getMessage());
}
Also used : DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.jupiter.api.Test)

Example 47 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project dhis2-core by dhis2.

the class JdbcEventAnalyticsManagerTest method testHandlingWhenExceptionCauseIsNotPSQLException.

@Test
void testHandlingWhenExceptionCauseIsNotPSQLException() {
    // Given
    final ArrayIndexOutOfBoundsException aRandomCause = new ArrayIndexOutOfBoundsException();
    final DataIntegrityViolationException aNonPSQLExceptionCause = new DataIntegrityViolationException("not caused by PSQLException", aRandomCause);
    // When
    assertThrows(QueryRuntimeException.class, () -> handle(aNonPSQLExceptionCause), E7133.getMessage());
}
Also used : DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.jupiter.api.Test)

Example 48 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project dhis2-core by dhis2.

the class MinMaxOutlierDetectionManager method getOutlierValues.

/**
 * Returns a list of outlier data values based on min-max values for the
 * given request.
 *
 * @param request the {@link OutlierDetectionRequest}.
 * @return a list of {@link OutlierValue}.
 */
public List<OutlierValue> getOutlierValues(OutlierDetectionRequest request) {
    final String ouPathClause = getOrgUnitPathClause(request.getOrgUnits());
    // @formatter:off
    final String sql = "select de.uid as de_uid, ou.uid as ou_uid, coc.uid as coc_uid, aoc.uid as aoc_uid, " + "de.name as de_name, ou.name as ou_name, coc.name as coc_name, aoc.name as aoc_name, " + "pe.startdate as pe_start_date, pt.name as pt_name, " + "dv.value::double precision as value, dv.followup as follow_up, " + "least(abs(dv.value::double precision - mm.minimumvalue), " + "abs(dv.value::double precision - mm.maximumvalue)) as bound_abs_dev, " + "mm.minimumvalue as lower_bound, " + "mm.maximumvalue as upper_bound " + "from datavalue dv " + "inner join dataelement de on dv.dataelementid = de.dataelementid " + "inner join categoryoptioncombo coc on dv.categoryoptioncomboid = coc.categoryoptioncomboid " + "inner join categoryoptioncombo aoc on dv.attributeoptioncomboid = aoc.categoryoptioncomboid " + "inner join period pe on dv.periodid = pe.periodid " + "inner join periodtype pt on pe.periodtypeid = pt.periodtypeid " + "inner join organisationunit ou on dv.sourceid = ou.organisationunitid " + // Min-max value join
    "inner join minmaxdataelement mm on (dv.dataelementid = mm.dataelementid " + "and dv.sourceid = mm.sourceid and dv.categoryoptioncomboid = mm.categoryoptioncomboid) " + "where dv.dataelementid in (:data_element_ids) " + "and pe.startdate >= :start_date " + "and pe.enddate <= :end_date " + "and " + ouPathClause + " " + "and dv.deleted is false " + // Filter for values outside the min-max range
    "and (dv.value::double precision < mm.minimumvalue or dv.value::double precision > mm.maximumvalue) " + // Order and limit
    "order by bound_abs_dev desc " + "limit :max_results;";
    // @formatter:on
    final SqlParameterSource params = new MapSqlParameterSource().addValue("data_element_ids", request.getDataElementIds()).addValue("start_date", request.getStartDate()).addValue("end_date", request.getEndDate()).addValue("max_results", request.getMaxResults());
    final Calendar calendar = PeriodType.getCalendar();
    try {
        return jdbcTemplate.query(sql, params, getRowMapper(calendar));
    } catch (DataIntegrityViolationException ex) {
        // Casting non-numeric data to double, catching exception is faster
        // than filtering
        log.error(ErrorCode.E2208.getMessage(), ex);
        throw new IllegalQueryException(ErrorCode.E2208);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) SqlParameterSource(org.springframework.jdbc.core.namedparam.SqlParameterSource) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Calendar(org.hisp.dhis.calendar.Calendar) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 49 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project spring-framework by spring-projects.

the class SQLErrorCodeSQLExceptionTranslatorTests method customExceptionTranslation.

@Test
public void customExceptionTranslation() {
    final String TASK = "TASK";
    final String SQL = "SQL SELECT *";
    final SQLErrorCodes customErrorCodes = new SQLErrorCodes();
    final CustomSQLErrorCodesTranslation customTranslation = new CustomSQLErrorCodesTranslation();
    customErrorCodes.setBadSqlGrammarCodes("1", "2");
    customErrorCodes.setDataIntegrityViolationCodes("3", "4");
    customTranslation.setErrorCodes("1");
    customTranslation.setExceptionClass(CustomErrorCodeException.class);
    customErrorCodes.setCustomTranslations(customTranslation);
    SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(customErrorCodes);
    // Should custom translate this
    SQLException badSqlEx = new SQLException("", "", 1);
    assertThat(sext.translate(TASK, SQL, badSqlEx).getClass()).isEqualTo(CustomErrorCodeException.class);
    assertThat(sext.translate(TASK, SQL, badSqlEx).getCause()).isEqualTo(badSqlEx);
    // Shouldn't custom translate this
    SQLException invResEx = new SQLException("", "", 3);
    DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, invResEx);
    assertThat(diex.getCause()).isEqualTo(invResEx);
    // Shouldn't custom translate this - invalid class
    assertThatIllegalArgumentException().isThrownBy(() -> customTranslation.setExceptionClass(String.class));
}
Also used : SQLException(java.sql.SQLException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.jupiter.api.Test)

Example 50 with DataIntegrityViolationException

use of org.springframework.dao.DataIntegrityViolationException in project spring-framework by spring-projects.

the class SQLErrorCodeSQLExceptionTranslatorTests method customTranslateMethodTranslation.

@SuppressWarnings("serial")
@Test
public void customTranslateMethodTranslation() {
    final String TASK = "TASK";
    final String SQL = "SQL SELECT *";
    final DataAccessException customDex = new DataAccessException("") {
    };
    final SQLException badSqlEx = new SQLException("", "", 1);
    SQLException intVioEx = new SQLException("", "", 6);
    SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator() {

        @Override
        @Nullable
        protected DataAccessException customTranslate(String task, @Nullable String sql, SQLException sqlex) {
            assertThat(task).isEqualTo(TASK);
            assertThat(sql).isEqualTo(SQL);
            return (sqlex == badSqlEx) ? customDex : null;
        }
    };
    sext.setSqlErrorCodes(ERROR_CODES);
    // Shouldn't custom translate this
    assertThat(sext.translate(TASK, SQL, badSqlEx)).isEqualTo(customDex);
    DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, intVioEx);
    assertThat(diex.getCause()).isEqualTo(intVioEx);
}
Also used : SQLException(java.sql.SQLException) DataAccessException(org.springframework.dao.DataAccessException) DeadlockLoserDataAccessException(org.springframework.dao.DeadlockLoserDataAccessException) Nullable(org.springframework.lang.Nullable) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.jupiter.api.Test)

Aggregations

DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)117 Test (org.junit.Test)26 HashMap (java.util.HashMap)12 BadRequestException (com.sequenceiq.cloudbreak.controller.BadRequestException)11 Transactional (org.springframework.transaction.annotation.Transactional)11 Test (org.junit.jupiter.api.Test)10 Transactional (javax.transaction.Transactional)9 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)9 User (ca.corefacility.bioinformatics.irida.model.user.User)8 SQLException (java.sql.SQLException)8 ConstraintViolation (javax.validation.ConstraintViolation)8 HashSet (java.util.HashSet)7 Locale (java.util.Locale)6 ConstraintViolationException (javax.validation.ConstraintViolationException)6 Date (java.util.Date)5 List (java.util.List)5 Set (java.util.Set)5 EntityExistsException (ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)4 ConnectorException (com.netflix.metacat.common.server.connectors.exception.ConnectorException)4 InvalidMetaException (com.netflix.metacat.common.server.connectors.exception.InvalidMetaException)4