package com.airhacks.headlands.processors.boundary;

import javax.cache.processor.EntryProcessor;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;

/**
 *
 * @author airhacks.com
 */
public class EntryProcessorExecutorTest {

    EntryProcessorExecutor cut;

    @Before
    public void init() {
        this.cut = new EntryProcessorExecutor();
        this.cut.initializeEngine();
    }

    @Test
    public void createProcessorFromEmptyScript() {
        EntryProcessor<String, String, String> invalid = this.cut.createProcessorFromScript("");
        assertNull(invalid);
    }

    @Test(expected = IllegalStateException.class)
    public void createProcessorFromInvalidScript() {
        EntryProcessor<String, String, String> invalid = this.cut.createProcessorFromScript("INVALID");
        assertNull(invalid);
    }

    @Test
    public void createValidProcessorFromScript() {
        String expected = "works";
        String processorScript = "function process(entry,args){ return \"" + expected + "\"}";
        EntryProcessor<String, String, String> processor = this.cut.createProcessorFromScript(processorScript);
        assertNotNull(processor);
        String result = processor.process(null);
        assertThat(result, is(expected));
    }

    @Test
    public void createValidProcessorWithArgument() {
        String expected = "works";
        String processorScript = "function process(entry,arg){ return arg[0]}";
        EntryProcessor<String, String, String> processor = this.cut.createProcessorFromScript(processorScript);
        assertNotNull(processor);
        String result = processor.process(null, expected);
        assertThat(result, is(expected));
    }

}

@@ -57,134 +57,246 @@
 ort 
-javax.cache.processor.EntryProcessor;%0Aimport static org.hamcrest.CoreMatchers.is;%0Aimport static org.junit.Assert.assertNotNull
+com.airhacks.headlands.cache.boundary.CacheDiscoverer;%0Aimport com.airhacks.headlands.engine.control.NashornEngine;%0Aimport java.util.HashSet;%0Aimport java.util.Map;%0Aimport javax.cache.Cache;%0Aimport javax.cache.processor.EntryProcessorResult
 ;%0Aim
@@ -331,16 +331,19 @@
 .assertN
+otN
 ull;%0Aimp
@@ -377,19 +377,19 @@
 .assertT
-hat
+rue
 ;%0Aimport
@@ -429,16 +429,96 @@
 it.Test;
+%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.when;
 %0A%0A/**%0A *
@@ -620,16 +620,77 @@
 tor cut;
+%0A    String existingCache = %22dukes%22;%0A    private Cache cache;
 %0A%0A    @B
@@ -732,45 +732,48 @@
     
-this.cut = new EntryProcessorExecutor
+NashornEngine engine = new NashornEngine
 ();%0A
@@ -780,24 +780,22 @@
         
-this.cut
+engine
 .initial
@@ -815,438 +815,260 @@
     
-%7D%0A%0A
     
-@Test%0A    public void createProcessorFromEmptyScript() %7B%0A        EntryProcessor%3CString, String, String%3E invalid = this.cut.createProcessorFromScript(%22%22);%0A        assertNull(invalid
+this.cut = new EntryProcessorExecutor();%0A        this.cut.engine = engine;%0A        this.cut.discoverer = mock(CacheDiscoverer.class
 );%0A    
-%7D%0A%0A
     
-@Test(expected = IllegalStateException.class)%0A    public void createProcessorFromInvalidScript() %7B%0A        EntryProcessor%3CString, String, String%3E invalid = this.cut.createProcessorFromScript(%22INVALID%22);%0A        assertNull(invalid
+this.cache = mock(Cache.class);%0A        when(this.cut.discoverer.getCache(existingCache)).thenReturn(cache
 );%0A 
@@ -1276,160 +1276,49 @@
     
-EntryProcessor%3CString, String, String%3E processor = this.cut.createProcessorFromScript(processorScript);%0A        assertNotNull(processor);%0A        
+Map%3CString, EntryProcessorResult%3C
 String
+%3E%3E
  res
@@ -1327,462 +1327,120 @@
 t = 
-processor.process(null);%0A        assertThat(result, is(
+this.cut.
 ex
-p
 ec
+u
 te
-d));%0A    %7D%0A%0A    @Test%0A    public void createValidProcessorWithArgument() %7B%0A        String expected = %22works%22;%0A        String processorScript = %22function process(entry,arg)%7B return arg%5B0%5D%7D%22;%0A        EntryProcessor%3CString, String, String%3E processor = this.cut.createProcessorFromScript(processorScript);%0A        assertNotNull(processor);%0A        String result = processor.process(null, expected
+(existingCache, processorScript, new HashSet%3C%3E(), new HashSet%3C%3E());%0A        assertNotNull(result
 );%0A 
@@ -1457,31 +1457,27 @@
 ertT
-hat
+rue
 (result
-, is(expected
+.isEmpty(
 ));%0A

test fixed