package de.unioninvestment.eai.portal.support.scripting;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertThat;
import groovy.lang.Closure;
import groovy.lang.Script;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import de.unioninvestment.eai.portal.portlet.crud.config.CustomFilterConfig;
import de.unioninvestment.eai.portal.portlet.crud.config.GroovyScript;
import de.unioninvestment.eai.portal.portlet.crud.domain.model.filter.CustomFilter;
import de.unioninvestment.eai.portal.portlet.crud.scripting.model.ScriptFormAction;
import de.unioninvestment.eai.portal.portlet.crud.scripting.model.ScriptRow;

public class ScriptCustomFilterFactoryTest {

	@Mock
	private ScriptFormAction formActionMock;

	@Mock
	private ScriptBuilder scriptBuilderMock;

	@Before
	public void setUp() {
		MockitoAnnotations.initMocks(this);
	}

	@Test
	public void shouldCreateCustomFilterFromConfig() {
		ScriptCustomFilterFactory factory = new ScriptCustomFilterFactory(
				scriptBuilderMock, formActionMock);

		CustomFilterConfig config = new CustomFilterConfig();
		config.setFilter(new GroovyScript("abcde"));
		Script script = new Script() {
			@Override
			public Object run() {
				return new Closure<Boolean>(ScriptCustomFilterFactoryTest.this) {
					public Boolean doCall(ScriptRow row) {
						return true;
					}
				};
			}
		};
		config.getFilter().setClazz(script.getClass());

		CustomFilter customFilter = factory.createCustomFilter(config);

		assertThat(customFilter.getMatcher(),
				instanceOf(ScriptCustomFilterMatcher.class));
	}
}

@@ -157,27 +157,39 @@
 ort 
-groovy.lang.Closure
+static org.mockito.Mockito.when
 ;%0Aim
@@ -205,22 +205,23 @@
 vy.lang.
-Script
+Closure
 ;%0A%0Aimpor
@@ -288,16 +288,16 @@
 o.Mock;%0A
-
 import o
@@ -326,16 +326,100 @@
 tations;
+%0Aimport org.mockito.invocation.InvocationOnMock;%0Aimport org.mockito.stubbing.Answer;
 %0A%0Aimport
@@ -1304,87 +1304,40 @@
 ;%0A%09%09
-Script script = new Script() %7B%0A%09%09%09@Override%0A%09%09%09public Object run() %7B%0A%09%09%09%09return
+final Closure%3CBoolean%3E closure =
  new
@@ -1354,16 +1354,21 @@
 oolean%3E(
+%0A%09%09%09%09
 ScriptCu
@@ -1400,18 +1400,16 @@
 s) %7B%0A%09%09%09
-%09
-%09
 public B
@@ -1443,18 +1443,16 @@
 ) %7B%0A%09%09%09%09
-%09%09
 return t
@@ -1460,79 +1460,247 @@
 ue;%0A
-%09%09
 %09%09%09%7D%0A
-%09%09
 %09%09%7D;%0A%09%09
-%09%7D%0A%09%09%7D;%0A%09%09config.getFilter().setClazz(script.getClass()
+when(scriptBuilderMock.buildClosure(config.getFilter())).thenAnswer(%0A%09%09%09%09new Answer%3CObject%3E() %7B%0A%0A%09%09%09%09%09@Override%0A%09%09%09%09%09public Object answer(InvocationOnMock invocation)%0A%09%09%09%09%09%09%09throws Throwable %7B%0A%09%09%09%09%09%09return closure;%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D
 );%0A%0A

Test correction