RBACDecorator implements RBACDecoratorMBean { @SuppressWarnings("unchecked") private void decorateCanInvoke(Map<String, Object> mBeanInfo, String method, boolean canInvoke) { LOG.trace("decorateCanInvoke: {} - {}", method, canInvoke); String[] methodNameAndArgs = method.split("[()]"); Object op = ((Map<String, Object>) mBeanInfo.get("op")).get(methodNameAndArgs[0]); if (op instanceof List) { List<Map<String, Object>> overloaded = (List<Map<String, Object>>) op; for (Map<String, Object> m : overloaded) { String args = argsToString((List<Map<String, String>>) m.get("args")); if ((methodNameAndArgs.length == 1 && args.equals("")) || (methodNameAndArgs.length > 1 && args.equals(methodNameAndArgs[1]))) { m.put("canInvoke", canInvoke); LOG.trace(" op: {}({}) - {}", methodNameAndArgs[0], args, m.get("canInvoke")); break; } } } else { ((Map<String, Object>) op).put("canInvoke", canInvoke); LOG.trace(" op: {} - {}", method, ((Map<String, Object>) op).get("canInvoke")); } Map<String, Object> opByString = (Map<String, Object>) mBeanInfo.get("opByString"); Map<String, Object> opByStringMethod = (Map<String, Object>) opByString.get(method); opByStringMethod.put("canInvoke", canInvoke); LOG.trace(" opByString: {} - {}", method, opByStringMethod.get("canInvoke")); } RBACDecorator(BundleContext bundleContext); @Override @SuppressWarnings("unchecked") void decorate(Map<String, Object> result); @Override boolean getVerify(); @Override void setVerify(boolean verify); static Logger LOG; }
@Test @SuppressWarnings("unchecked") public void decorateCanInvoke() throws Exception { BundleContext bc = setUpMocksForDecorate(); RBACDecorator decorator = new RBACDecorator(bc); decorator.setVerify(true); Map<String, Object> result = readInput(); LOG.info("In: {}", result); decorator.decorate(result); LOG.info("Out: {}", result); Map<String, Map<String, Object>> domains = (Map<String, Map<String, Object>>) result.get("domains"); Map<String, Object> domain = domains.get("org.apache.activemq"); Map<String, Object> mbean = (Map<String, Object>) domain.get("type=Broker,brokerName=amq"); assertThat(mbean.get("canInvoke"), equalTo(false)); Map<String, Object> op = (Map<String, Object>) mbean.get("op"); LOG.info("op = {}", op); assertThat(((Map<String, Object>) op.get("removeQueue")).get("canInvoke"), equalTo(false)); assertThat(((Map<String, Object>) op.get("addQueue")).get("canInvoke"), equalTo(false)); assertThat(((Map<String, Object>) op.get("stop")).get("canInvoke"), equalTo(true)); assertThat(((Map<String, Object>) op.get("start")).get("canInvoke"), equalTo(true)); assertThat(((List<Map<String, Object>>) op.get("overloadedMethod")).get(0).get("canInvoke"), equalTo(true)); assertThat(((List<Map<String, Object>>) op.get("overloadedMethod")).get(1).get("canInvoke"), equalTo(false)); assertThat(((List<Map<String, Object>>) op.get("overloadedMethod")).get(2).get("canInvoke"), equalTo(true)); Map<String, Map<String, Boolean>> opByString = (Map<String, Map<String, Boolean>>) mbean.get("opByString"); assertThat(opByString, notNullValue()); LOG.info("opByString = {}", opByString); assertThat(opByString.keySet(), containsInAnyOrder( "removeQueue(java.lang.String)", "addQueue(java.lang.String)", "stop()", "start()", "overloadedMethod(java.lang.String)", "overloadedMethod(java.lang.String,java.lang.Object)", "overloadedMethod()")); assertThat(opByString.get("removeQueue(java.lang.String)").get("canInvoke"), equalTo(false)); assertThat(opByString.get("addQueue(java.lang.String)").get("canInvoke"), equalTo(false)); assertThat(opByString.get("stop()").get("canInvoke"), equalTo(true)); assertThat(opByString.get("start()").get("canInvoke"), equalTo(true)); assertThat(opByString.get("overloadedMethod(java.lang.String)").get("canInvoke"), equalTo(true)); assertThat(opByString.get("overloadedMethod(java.lang.String,java.lang.Object)").get("canInvoke"), equalTo(false)); assertThat(opByString.get("overloadedMethod()").get("canInvoke"), equalTo(true)); }