QpidQmf { public static <T> T extractValue(Object object, String ... mapKeys) throws JMSException { log.debug("Extracting [{}] from {}", Arrays.toString(mapKeys), object); if (object == null) { throw new IllegalArgumentException("Map Names is null"); } for (String key : mapKeys) { log.debug("Extracting key {} from the object", key); if (!(object instanceof Map)) { String msg = String.format("The object under key %s is not a map! Object: %s", key, object); throw new RuntimeException(msg); } object = ((Map<String, Object>) object).get(key); log.debug("Extracted {} under key {}", object, key); if (object == null) { throw new RuntimeException("The extracted value at key " + key + " was null!"); } } if (object instanceof byte[]) { log.debug("Found byte array that will be Stringified to {}", new String((byte[]) object)); return (T) new String((byte[]) object); } if (object instanceof Boolean) { log.debug("Found boolean"); return (T) object; } if (object instanceof Long) { log.debug("Found Long"); return (T) object; } else { throw new RuntimeException("Expected the value to be byte[] but found: " + object.getClass()); } } @Inject  QpidQmf(QpidConnection qpidConnection, Configuration config); QpidStatus getStatus(); String getLastFlowStoppedQueue(); static T extractValue(Object object, String  ... mapKeys);  }
@Test(expected = IllegalArgumentException.class) public void extractStringNull() throws JMSException { QpidQmf.extractValue(null, "_values"); }
@Test public void extractStringWrongValueTypeTest() throws JMSException { Map<String, Object> map = new HashMap<>(); map.put("key", "value"); try { QpidQmf.extractValue(map, "key"); } catch (Exception e) { Assert.assertTrue(e.toString(), e.getMessage().contains("Expected the value to be byte[]")); return; } Assert.fail("Expected exception about value type"); }
@Test public void extractStringTooDeepTest() throws JMSException { Map<String, Object> map = new HashMap<>(); map.put("key", "value"); try { QpidQmf.extractValue(map, "key", "key2"); } catch (Exception e) { Assert.assertTrue(e.toString(), e.getMessage().contains("object under key key2 is not a map!")); return; } Assert.fail("Expected exception about value type"); }
@Test public void extractStringWrongKeyTest() throws JMSException { Map<String, Object> map = new HashMap<>(); map.put("key", "value"); try { QpidQmf.extractValue(map, "key123"); } catch (Exception e) { Assert.assertTrue(e.toString(), e.getMessage() .contains("The extracted value at key key123 was null")); return; } Assert.fail("Expected exception about value type"); }
@Test public void extractStringTest() throws JMSException { Map<String, Object> map = new HashMap<>(); map.put("key", "value".getBytes()); Assert.assertEquals("value", QpidQmf.extractValue(map, "key")); }
@Test public void extractStringTwoLevelsTest() throws JMSException { Map<String, Object> l2 = new HashMap<>(); l2.put("key", "value".getBytes()); Map<String, Object> map = new HashMap<>(); map.put("keyl1", l2); Assert.assertEquals("value", QpidQmf.extractValue(map, "keyl1", "key")); }