GroovyConditionScript implements ConditionScript { @Override public boolean execute(final Map<String, Object> boundVariables) { if (LOGGER.isDebugEnabled()) { StringBuilder msgBuilder = new StringBuilder(); msgBuilder.append("The script is bound to the following variables:\n"); for (Entry<String, Object> entry : boundVariables.entrySet()) { msgBuilder.append("* ").append(entry.getKey()).append(":").append(entry.getValue()).append("\n"); } LOGGER.debug(msgBuilder.toString()); } Binding binding = new Binding(boundVariables); this.script.setBinding(binding); return (boolean) this.script.run(); } GroovyConditionScript(final Script script); @Override boolean execute(final Map<String, Object> boundVariables);  }
@Test public void testScriptExecutionPositiveEqualsNoBinding() throws ConditionParsingException { String script = "\"a\".equals(\"a\")"; ConditionScript parsedScript = this.shell.parse(script); Assert.assertEquals(parsedScript.execute(new HashMap<String, Object>()), true); }
@Test public void testScriptExecutionNotEqualsNoBinding() throws ConditionParsingException { String script = "\"a\".equals(\"b\")"; ConditionScript parsedScript = this.shell.parse(script); Assert.assertEquals(parsedScript.execute(new HashMap<String, Object>()), false); }
@Test(expectedExceptions = ClassCastException.class) public void testScriptExecutionWithNonBooleanReturnNoBinding() throws ConditionParsingException { String script = "\"a\".concat(\"b\")"; ConditionScript parsedScript = this.shell.parse(script); Assert.assertEquals(parsedScript.execute(new HashMap<String, Object>()), false); }
@Test public void testScriptExecutionPositiveEqualsSingleBinding() throws ConditionParsingException { String script = "resource == \"b\""; ConditionScript parsedScript = this.shell.parse(script); Map<String, Object> parameter = new HashMap<>(); parameter.put("resource", new ResourceHandler(new HashSet<Attribute>(), "", "")); Assert.assertEquals(parsedScript.execute(parameter), false); }
@Test public void testScriptExecutionPositiveEqualsMultipleBindings() throws ConditionParsingException { String script = "resource != subject"; ConditionScript parsedScript = this.shell.parse(script); Map<String, Object> parameter = new HashMap<>(); parameter.put("resource", new ResourceHandler(new HashSet<Attribute>(), "", "")); parameter.put("subject", new SubjectHandler(new HashSet<Attribute>())); Assert.assertEquals(parsedScript.execute(parameter), true); }
@Test public void testScriptExecutionPositiveEqualsMultipleBindingsWithMatcher() throws ConditionParsingException { String script = "match.any(resource.attributes(\"\",\"\"), subject.attributes(\"\",\"\"))"; ConditionScript parsedScript = this.shell.parse(script); Map<String, Object> parameter = new HashMap<>(); parameter.put("resource", new ResourceHandler(new HashSet<Attribute>(), "", "")); parameter.put("subject", new SubjectHandler(new HashSet<Attribute>())); parameter.put("match", new AttributeMatcher()); Assert.assertEquals(parsedScript.execute(parameter), false); }
@Test(expectedExceptions = ConditionParsingException.class) public void testScriptExecutionUsingReflection() throws ConditionParsingException { String script = "new ResourceHandler().getClass().getClassLoader().loadClass(System.class.getName())." + "getMethod(\"exit\");"; ConditionScript parsedScript = this.shell.parse(script); Map<String, Object> parameter = new HashMap<>(); Assert.assertNotNull(parsedScript.execute(parameter)); }
@Test public void testScriptExecutionUsingAssignment() throws ConditionParsingException { String script = "resource = null; resource == null;"; ConditionScript parsedScript = this.shell.parse(script); Map<String, Object> parameter = new HashMap<>(); final ResourceHandler resourceHandler = new ResourceHandler(new HashSet<Attribute>(), "", ""); parameter.put("resource", resourceHandler); Assert.assertEquals(parsedScript.execute(parameter), true); Assert.assertNotNull(resourceHandler); }