use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class SnmpValueTest method testSnmpObjId.
@Test
public void testSnmpObjId() {
for (final SnmpValueFactory factory : m_factories) {
final String oid = ".1.3.6.1.4.1.2925.4.5.2.1.1";
final SnmpObjId id = SnmpObjId.get(oid);
final SnmpValue value = factory.getObjectId(id);
final String className = factory.getClass().getName();
assertTrue(className + ": getInetAddress isDisplayable should be true", value.isDisplayable());
assertEquals(className + ": getObjectId to SnmpObjId should return " + oid, id, value.toSnmpObjId());
assertEquals(className + ": getObjectId to String should return " + oid, oid, value.toString());
assertEquals(className + ": getObjectId to DisplayString should return " + oid, oid, value.toDisplayString());
try {
value.toInt();
fail(className + ": getObjectId to int should throw an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
/* expected */
}
try {
value.toLong();
fail(className + ": getObjectId to long should throw an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
/* expected */
}
try {
value.toBigInteger();
fail(className + ": getObjectId to BigInteger should throw an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
/* expected */
}
try {
value.toHexString();
fail(className + ": getObjectId to HexString should throw an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
/* expected */
}
try {
value.toInetAddress();
fail(className + ": getObjectId to InetAddress should throw an IllegalArgumentException");
} catch (final IllegalArgumentException e) {
/* expected */
}
}
}
use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class EventConstants method toString.
//
// For Trapd
//
/**
* Converts the value of the instance to a string representation in the
* correct encoding system.
*
* @param encoding a {@link java.lang.String} object.
* @param value a {@link java.lang.Object} object.
* @return a {@link java.lang.String} object.
*/
public static String toString(String encoding, Object value) {
if (encoding == null || value == null) {
throw new IllegalArgumentException("Cannot take null parameters.");
}
String result = null;
if (XML_ENCODING_TEXT.equals(encoding)) {
if (value instanceof String)
result = (String) value;
else if (value instanceof Number)
result = value.toString();
else if (value instanceof SnmpValue)
result = ((SnmpValue) value).toString();
} else if (XML_ENCODING_BASE64.equals(encoding)) {
if (value instanceof String)
result = new String(Base64.encodeBase64(((String) value).getBytes()));
else if (value instanceof Number) {
byte[] ibuf = null;
if (value instanceof BigInteger)
ibuf = ((BigInteger) value).toByteArray();
else
ibuf = BigInteger.valueOf(((Number) value).longValue()).toByteArray();
result = new String(Base64.encodeBase64(ibuf));
} else if (value instanceof SnmpValue) {
SnmpValue snmpValue = (SnmpValue) value;
result = new String(Base64.encodeBase64(snmpValue.getBytes()));
}
} else if (XML_ENCODING_MAC_ADDRESS.equals(encoding)) {
if (value instanceof SnmpValue) {
SnmpValue snmpValue = (SnmpValue) value;
StringBuffer macAddress = new StringBuffer();
byte[] bytes = snmpValue.getBytes();
for (int i = 0; i < bytes.length; i++) {
if (i > 0)
macAddress.append(":");
macAddress.append(String.format("%02X", bytes[i]));
}
result = macAddress.toString();
}
}
if (result == null)
throw new IllegalArgumentException("unable to encode " + value + " of type " + value.getClass());
return result;
}
use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class HibernateEventWriterIT method testWriteEventWithNull.
/**
* Tests writing nulls to postgres db and the db encoding.
* @throws SQLException
*/
@Test
public void testWriteEventWithNull() throws Exception {
EventBuilder bldr = new EventBuilder("testUei", "testSource");
bldr.setLogDest(HibernateEventWriter.LOG_MSG_DEST_LOG_AND_DISPLAY);
bldr.addParam("test", "testVal");
final String testVal2 = "valWith Null ";
bldr.addParam("test2", testVal2);
byte[] bytes = new byte[] { 0x07, (byte) 0xD7, 0x04, 0x0A, 0x01, 0x17, 0x06, 0x00, 0x2B, 0x00, 0x00 };
SnmpValue snmpVal = SnmpUtils.getValueFactory().getOctetString(bytes);
assertFalse(snmpVal.isDisplayable());
bldr.addParam("test3", snmpVal.toString());
String b64 = EventConstants.toString(EventConstants.XML_ENCODING_BASE64, snmpVal);
bldr.addParam("test", b64);
Event event = bldr.getEvent();
assertEquals(new Integer(0), event.getDbid());
m_eventWriter.process(bldr.getLog());
assertTrue(event.getDbid() > 0);
final String parms = jdbcTemplate.queryForObject("SELECT eventParms FROM events LIMIT 1", String.class);
assertEquals("test=testVal(string,text);test2=valWith%0Null%0(string,text);test3=" + snmpVal.toString() + "(string,text);test=B9cECgEXBgArAAA%61(string,text)", parms);
}
use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class SnmpMonitorStrategyTest method testErrorConditions.
@Test
public void testErrorConditions() {
SnmpValue result = int32Value(1);
ThrowableAnticipator ta = new ThrowableAnticipator();
ta.anticipate(new IllegalArgumentException("operator X is unknown"));
try {
monitor.meetsCriteria(result, "X", "123");
} catch (Throwable t) {
ta.throwableReceived(t);
}
ta.verifyAnticipated();
}
use of org.opennms.netmgt.snmp.SnmpValue in project opennms by OpenNMS.
the class SnmpMonitorStrategyTest method testMeetsCriteriaWithObjectID.
@Test
public void testMeetsCriteriaWithObjectID() {
SnmpValue result = oid(".1.2.3.4.5.6.7.8.9");
testSyntaxEquals(result, ".1.2.3.4.5.6.7.8.9", "..1.2.3.4.5.6.7.8.9");
testSyntaxMatches(result, "\\.7\\.", "\\.11\\.");
}
Aggregations