/*
 * Copyright 2012 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.drools.simulation.impl;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import org.drools.core.command.runtime.BatchExecutionCommandImpl;
import org.drools.simulation.fluent.batch.FluentBatchExecution;
import org.drools.simulation.fluent.batch.impl.FluentBatchExecutionImpl;
import org.drools.core.io.impl.ByteArrayResource;
import org.junit.Test;
import org.kie.internal.KnowledgeBase;
import org.kie.internal.KnowledgeBaseFactory;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.api.command.BatchExecutionCommand;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.ExecutionResults;
import org.kie.internal.runtime.StatelessKnowledgeSession;

public class BatchFluentTest {

    @Test
    public void testBatchSimple() {
        FluentBatchExecution f = new FluentBatchExecutionImpl();

        List list = new ArrayList();

        // @formatter:off          
        BatchExecutionCommand cmd = f.newBatchExecution()
                .setGlobal("list", list)
                .insert(new Person("yoda", 150)).set("y")
                .insert(new Person("salaboy", 28)).set("x")
                .fireAllRules()
                .getBatchExecution();
        // @formatter:on

        assertEquals(4, ((BatchExecutionCommandImpl) cmd).getCommands().size());

        ExecutionResults results = createStatelessSession().execute(cmd);

        assertEquals(2, results.getIdentifiers().size());
        assertEquals("y", (String) results.getIdentifiers().toArray()[0]);
        assertEquals("x", (String) results.getIdentifiers().toArray()[1]);
    }

    @Test
    public void testBatchSimpleWithSetInGlobal() {
        FluentBatchExecution f = new FluentBatchExecutionImpl();

        List list = new ArrayList();

        // @formatter:off          
        BatchExecutionCommand cmd = f.newBatchExecution()
                .setGlobal("list", list).set("myGlobal")
                .insert(new Person("yoda", 150)).set("y")
                .insert(new Person("salaboy", 28)).set("x")
                .fireAllRules()
                .getBatchExecution();
        // @formatter:on

        assertEquals(4, ((BatchExecutionCommandImpl) cmd).getCommands().size());

        ExecutionResults results = createStatelessSession().execute(cmd);

        assertEquals(3, results.getIdentifiers().size());
        assertEquals("myGlobal", (String) results.getIdentifiers().toArray()[0]);
        assertEquals("y", (String) results.getIdentifiers().toArray()[1]);
        assertEquals("x", (String) results.getIdentifiers().toArray()[2]);
    }

    private StatelessKnowledgeSession createStatelessSession() {
        String str = "package org.drools.simulation.test\n"
                + "import " + Person.class.getName() + "\n"
                + "global java.util.List list\n"
                + "rule updateAge no-loop\n"
                + "  when\n"
                + "    $p : Person()\n"
                + "  then\n"
                + "    modify( $p ) { setAge( $p.getAge() + 10 ) };\n"
                + "end\n";
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(new ByteArrayResource(str.getBytes()), ResourceType.DRL);
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
        return kbase.newStatelessKnowledgeSession();
    }

}

@@ -628,106 +628,8 @@
 l;%0A%0A
-import static org.junit.Assert.assertEquals;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 impo
@@ -686,24 +686,74 @@
 ommandImpl;%0A
+import org.drools.core.io.impl.ByteArrayResource;%0A
 import org.d
@@ -892,45 +892,100 @@
 org.
-drools.core.io.impl.ByteArray
+junit.Test;%0Aimport org.kie.api.command.BatchExecutionCommand;%0Aimport org.kie.api.io.
 Resource
 ;%0Aim
@@ -972,32 +972,36 @@
 .api.io.Resource
+Type
 ;%0Aimport org.jun
@@ -993,34 +993,56 @@
 %0Aimport org.
-junit.Test
+kie.api.runtime.ExecutionResults
 ;%0Aimport org
@@ -1242,181 +1242,230 @@
 kie.
-api.command.BatchExecutionCommand;%0Aimport org.kie.api.io.ResourceType;%0Aimport org.kie.api.runtime.ExecutionResults;%0Aimport org.kie.internal.runtime.StatelessKnowledgeSession
+internal.runtime.StatelessKnowledgeSession;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport static java.util.Arrays.asList;%0Aimport static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertTrue
 ;%0A%0Ap
@@ -2223,37 +2223,21 @@
   assert
-Equals(%22y%22, (String) 
+True(
 results.
@@ -2257,95 +2257,37 @@
 s().
-toArray()%5B0%5D);%0A        assertEquals(%22x%22, (String) results.getIdentifiers().toArray()%5B1%5D
+containsAll(asList(%22x%22, %22y%22))
 );%0A 
@@ -3055,223 +3055,79 @@
 sert
-Equals(%22myGlobal%22, (String) results.getIdentifiers().toArray()%5B0%5D);%0A        assertEquals(%22y%22, (String) results.getIdentifiers().toArray()%5B1%5D);%0A        assertEquals(%22x%22, (String) results.getIdentifiers().toArray()%5B2%5D
+True(results.getIdentifiers().containsAll(asList(%22myGlobal%22, %22x%22, %22y%22))
 );%0A 

fix BatchFluentTest on Java 8