### Question:
SnmpInfo { public static SnmpInfo load(Configuration configuration) { String host = configuration.getSimpleValue(PARAM_HOST, null); String port = configuration.getSimpleValue(PARAM_PORT, DEFAULT_PORT); String oid = configuration.getSimpleValue(PARAM_VARIABLE_BINDING_PREFIX, null); String trapOid = configuration.getSimpleValue(PARAM_TRAP_OID, null); return new SnmpInfo(host, port, oid, trapOid); } private  SnmpInfo(String host, String port, String oid, String trapOid); static SnmpInfo load(Configuration configuration); @Override String toString(); final public String host; final public String port; final public String oid; final public String trapOid; final public String error; }### Answer:
@Test public void shouldDetectMissingParams() throws Exception { Configuration configuration = new Configuration(); SnmpInfo snmpInfo = SnmpInfo.load(configuration); assertNotNull(snmpInfo.error, "SnmpInfo#load should have detected an error"); assertEquals(snmpInfo.error, "Missing: OID, host"); configuration.setSimpleValue(PARAM_HOST, "pipo"); snmpInfo = SnmpInfo.load(configuration); assertNotNull(snmpInfo.error, "SnmpInfo#load should have detected an error"); assertEquals(snmpInfo.error, "Missing: OID"); configuration.remove(PARAM_HOST); configuration.setSimpleValue(PARAM_VARIABLE_BINDING_PREFIX, "molo"); snmpInfo = SnmpInfo.load(configuration); assertNotNull(snmpInfo.error, "SnmpInfo#load should have detected an error"); assertEquals(snmpInfo.error, "Missing: host"); }
@Test public void shouldAssumeDefaultValues() throws Exception { Configuration configuration = new Configuration(); configuration.setSimpleValue(PARAM_HOST, "pipo"); configuration.setSimpleValue(PARAM_VARIABLE_BINDING_PREFIX, "molo"); SnmpInfo snmpInfo = SnmpInfo.load(configuration); assertNull(snmpInfo.error, "SnmpInfo#load should not have detected an error"); assertEquals(snmpInfo.host, "pipo"); assertEquals(snmpInfo.oid, "molo"); assertEquals(snmpInfo.port, SnmpInfo.DEFAULT_PORT); }
@Test public void fallBackToGlobalValues() throws Exception { Configuration configuration = new Configuration(); configuration.setSimpleValue(PARAM_VARIABLE_BINDING_PREFIX, "molo"); Configuration preferences = new Configuration(); preferences.put(new PropertySimple("defaultTargetHost","hugo")); SnmpInfo snmpInfo = SnmpInfo.load(configuration,preferences); assertNull(snmpInfo.error, "SnmpInfo#load should not have detected an error"); assertEquals(snmpInfo.host, "hugo"); assertEquals(snmpInfo.oid, "molo"); assertEquals(snmpInfo.port, SnmpInfo.DEFAULT_PORT); configuration.setSimpleValue(PARAM_HOST, "pipo"); snmpInfo = SnmpInfo.load(configuration,preferences); assertNull(snmpInfo.error, "SnmpInfo#load should not have detected an error"); assertEquals(snmpInfo.host, "pipo"); assertEquals(snmpInfo.oid, "molo"); assertEquals(snmpInfo.port, SnmpInfo.DEFAULT_PORT); }
@Test public void shouldExposeAllParams() throws Exception { Configuration configuration = new Configuration(); configuration.setSimpleValue(PARAM_HOST, "pipo"); configuration.setSimpleValue(PARAM_VARIABLE_BINDING_PREFIX, "molo"); configuration.setSimpleValue(PARAM_PORT, "123"); configuration.setSimpleValue(PARAM_TRAP_OID, "logo"); SnmpInfo snmpInfo = SnmpInfo.load(configuration); assertNull(snmpInfo.error, "SnmpInfo#load should not have detected an error"); assertEquals(snmpInfo.host, "pipo"); assertEquals(snmpInfo.port, "123"); assertEquals(snmpInfo.oid, "molo"); assertEquals(snmpInfo.trapOid, "logo"); }