use of org.springframework.dao.DataAccessException in project opennms by OpenNMS.
the class AlarmdIT method changeFields.
@Test
public void changeFields() throws InterruptedException, SQLException {
assertEmptyAlarmTable();
String reductionKey = "testUpdateField";
MockNode node1 = m_mockNetwork.getNode(1);
// Verify we have the default alarm
sendNodeDownEvent(reductionKey, node1);
int severity = m_jdbcTemplate.queryForObject("select severity from alarms a where a.reductionKey = ?", new Object[] { reductionKey }, Integer.class).intValue();
assertEquals(OnmsSeverity.MAJOR, OnmsSeverity.get(severity));
// Store the original logmsg from the original alarm (we are about to test changing it with subsequent alarm reduction)
String defaultLogMsg = m_jdbcTemplate.query("select logmsg from alarms", new ResultSetExtractor<String>() {
@Override
public String extractData(ResultSet results) throws SQLException, DataAccessException {
results.next();
int row = results.getRow();
boolean isLast = results.isLast();
boolean isFirst = results.isFirst();
if (row != 1 && !isLast && !isFirst) {
throw new SQLException("Row count is not = 1. There should only be one row returned from the query: \n" + results.getStatement());
}
return results.getString(1);
}
});
assertTrue("The logmsg column should not be null", defaultLogMsg != null);
// Duplicate the alarm but change the severity and verify the change
sendNodeDownEventWithUpdateFieldSeverity(reductionKey, node1, OnmsSeverity.CRITICAL);
severity = m_jdbcTemplate.queryForObject("select severity from alarms", Integer.class).intValue();
assertEquals("Severity should now be Critical", OnmsSeverity.CRITICAL, OnmsSeverity.get(severity));
// Duplicate the alarm but don't force the change of severity
sendNodeDownEvent(reductionKey, node1);
severity = m_jdbcTemplate.queryForObject("select severity from alarms", Integer.class).intValue();
assertEquals("Severity should still be Critical", OnmsSeverity.CRITICAL, OnmsSeverity.get(severity));
// Duplicate the alarm and change the logmsg
sendNodeDownEventChangeLogMsg(reductionKey, node1, "new logMsg");
String newLogMsg = m_jdbcTemplate.query("select logmsg from alarms", new ResultSetExtractor<String>() {
@Override
public String extractData(ResultSet results) throws SQLException, DataAccessException {
results.next();
return results.getString(1);
}
});
assertEquals("new logMsg", newLogMsg);
assertTrue(!newLogMsg.equals(defaultLogMsg));
// Duplicate the alarm but force logmsg to not change (lggmsg field is updated by default)
sendNodeDownEventDontChangeLogMsg(reductionKey, node1, "newer logMsg");
newLogMsg = m_jdbcTemplate.query("select logmsg from alarms", new ResultSetExtractor<String>() {
@Override
public String extractData(ResultSet results) throws SQLException, DataAccessException {
results.next();
return results.getString(1);
}
});
assertTrue("The logMsg should not have changed.", !"newer logMsg".equals(newLogMsg));
assertEquals("The logMsg should still be equal to the previous update.", "new logMsg", newLogMsg);
// Duplicate the alarm with the default configuration and verify the logmsg has changed (as is the default behavior
// for this field)
sendNodeDownEvent(reductionKey, node1);
newLogMsg = m_jdbcTemplate.query("select logmsg from alarms", new ResultSetExtractor<String>() {
@Override
public String extractData(ResultSet results) throws SQLException, DataAccessException {
results.next();
return results.getString(1);
}
});
assertTrue("The logMsg should have changed.", !"new logMsg".equals(newLogMsg));
assertEquals("The logMsg should new be the default logMsg.", newLogMsg, defaultLogMsg);
}
use of org.springframework.dao.DataAccessException in project libresonic by Libresonic.
the class DBController method handleRequestInternal.
@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST })
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
String query = request.getParameter("query");
if (query != null) {
map.put("query", query);
try {
List<?> result = daoHelper.getJdbcTemplate().query(query, new ColumnMapRowMapper());
map.put("result", result);
} catch (DataAccessException x) {
map.put("error", ExceptionUtils.getRootCause(x).getMessage());
}
}
return new ModelAndView("db", "model", map);
}
use of org.springframework.dao.DataAccessException in project opennms by OpenNMS.
the class LocationMonitorIdValidator method validate.
/**
* {@inheritDoc}
*/
@Override
public void validate(Object obj, Errors errors) {
LocationMonitorIdCommand cmd = (LocationMonitorIdCommand) obj;
if (cmd.getMonitorId() == null) {
errors.rejectValue("monitorId", "monitorId.notSpecified", new Object[] { "monitorId" }, "Value required.");
} else {
try {
String monitorId = cmd.getMonitorId();
OnmsLocationMonitor monitor = m_locationMonitorDao.get(monitorId);
if (monitor == null) {
throw new ObjectRetrievalFailureException(OnmsLocationMonitor.class, monitorId, "Could not find location monitor with id " + monitorId, null);
}
} catch (DataAccessException e) {
errors.rejectValue("monitorId", "monitorId.notFound", new Object[] { "monitorId", cmd.getMonitorId() }, "Valid location monitor ID required.");
}
}
}
use of org.springframework.dao.DataAccessException in project opennms by OpenNMS.
the class StatisticsReportCommandValidator method validate.
/**
* {@inheritDoc}
*/
@Override
public void validate(Object obj, Errors errors) {
StatisticsReportCommand cmd = (StatisticsReportCommand) obj;
if (cmd.getId() == null) {
errors.rejectValue("id", "statisticsReportId.notSpecified", new Object[] { "id" }, "Value required.");
} else {
try {
int id = cmd.getId();
m_statisticsReportDao.load(id);
} catch (DataAccessException e) {
errors.rejectValue("id", "statisticsReportId.notFound", new Object[] { "id", cmd.getId() }, "Valid statistics report ID required.");
}
}
}
use of org.springframework.dao.DataAccessException in project spring-data-mongodb by spring-projects.
the class MongoExceptionTranslatorUnitTests method checkTranslatedMongoException.
private void checkTranslatedMongoException(Class<? extends Exception> clazz, int code) {
DataAccessException translated = translator.translateExceptionIfPossible(new MongoException(code, ""));
assertThat("Expected exception of type " + clazz.getName() + "!", translated, is(not(nullValue())));
Throwable cause = translated.getRootCause();
assertThat(cause, is(instanceOf(MongoException.class)));
assertThat(((MongoException) cause).getCode(), is(code));
}
Aggregations