/**
 * Copyright 2010 OpenEngSB Division, Vienna University of Technology
 *
 * 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.openengsb.core.workflow;

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

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openengsb.core.workflow.model.RuleBaseElementId;
import org.openengsb.core.workflow.model.RuleBaseElementType;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

@RunWith(Parameterized.class)
public abstract class AbstractRuleManagerCrudTest<SourceType extends RuleManager> extends
        AbstractRuleManagerTest<SourceType> {

    @Parameters
    public static List<Object[]> data() {
        List<Object[]> data = new ArrayList<Object[]>();
        // functions:
        data.add(new Object[] {new RuleBaseElementId(RuleBaseElementType.Function, "org.openengsb", "test42"),
            "function void test42(){ System.out.println(\"sample-code\");}",
            new RuleBaseElementId(RuleBaseElementType.Function, "at.ac.tuwien", "test"),
            "function void test(){ System.out.println(\"bla42\");}", });

        // rules:
        data.add(new Object[] {new RuleBaseElementId(RuleBaseElementType.Rule, "org.openengsb", "test42"),
            "when\nthen\nSystem.out.println(\"sample-code\");",
            new RuleBaseElementId(RuleBaseElementType.Rule, "at.ac.tuwien", "test"),
            "when\nthen\nSystem.out.println(\"\");", });

        String sampleFlow = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                + "<process xmlns=\"http://drools.org/drools-5.0/process\""
                + "         xmlns:xs=\"http://www.w3.org/2001/XMLSchema-instance\""
                + "         xs:schemaLocation=\"http://drools.org/drools-5.0/process drools-processes-5.0.xsd\""
                + "         type=\"RuleFlow\" name=\"flowname\" id=\"flowId\" package-name=\"org.openengsb\" >" + ""
                + "  <header>" + "  </header>" + "  <nodes>"
                + "    <start id=\"1\" name=\"Start\" x=\"100\" y=\"100\" width=\"48\" height=\"48\" />"
                + "    <end id=\"2\" name=\"End\" x=\"245\" y=\"105\" width=\"48\" height=\"48\" />" + "  </nodes>"
                + "  <connections>" + "    <connection from=\"1\" to=\"2\" />" + "  </connections>" + "</process>";

        data.add(new Object[] { new RuleBaseElementId(RuleBaseElementType.Process, "org.openengsb", "flowId"),
                sampleFlow, new RuleBaseElementId(RuleBaseElementType.Process, "at.ac.tuwien", "flowId2"),
                sampleFlow.replace("org.openengsb", "at.ac.tuwien"), });

        // TODO: imports & globals
        // data.add(new Object[] { new
        // RuleBaseElementId(RuleBaseElementType.Import, "ignored",
        // "java.util.Currency"),
        // "java.util.Currency", new
        // RuleBaseElementId(RuleBaseElementType.Import, "ignored",
        // "java.util.Random"),
        // "java.util.Random", });
        // globals:

        return data;
    }

    protected String code1;
    protected String code2;
    protected RuleBaseElementId id1;
    protected RuleBaseElementId id2;

    public AbstractRuleManagerCrudTest(RuleBaseElementId id1, String code1, RuleBaseElementId id2, String code2) {
        this.code1 = code1;
        this.code2 = code2;
        this.id1 = id1;
        this.id2 = id2;
    }

    @Before
    @Override
    public void setUp() throws Exception {
        super.setUp();
    }

    @Test
    public void testCreate() throws RuleBaseException {
        source.add(id1, code1);
        assertEquals(code1, source.get(id1));
    }

    @Test
    public void testUpdate() throws RuleBaseException {
        source.add(id1, code1);
        source.update(id1, code2);
        assertEquals(code2, source.get(id1));
    }

    @Test
    public void testList() throws RuleBaseException {
        source.add(id1, code1);
        source.add(id2, code2);
        Collection<RuleBaseElementId> result = source.list(id1.getType(), id1.getPackageName());
        assertTrue(result.contains(id1));
        assertFalse(result.contains(id2));
    }

    @Test
    public void testListAll() throws RuleBaseException {
        source.add(id1, code1);
        source.add(id2, code2);
        Collection<RuleBaseElementId> result = source.list(id1.getType());
        assertTrue(result.contains(id1));
        assertTrue(result.contains(id2));
    }

    @Test
    public void testDelete() throws RuleBaseException {
        source.add(id1, code1);
        source.delete(id1);
    }

}

@@ -1042,16 +1042,111 @@
 tType;%0A%0A
+import static org.hamcrest.CoreMatchers.equalTo;%0Aimport static org.hamcrest.CoreMatchers.not;%0A%0A
 import s
@@ -1223,14 +1223,14 @@
 sert
-False;
+That;%0A
 %0Aimp
@@ -1254,25 +1254,38 @@
 nit.
-Assert.assertTrue
+matchers.JUnitMatchers.hasItem
 ;%0A%0A@
@@ -4579,36 +4579,43 @@
 sert
-Equals(code2, source.get(id1
+That(source.get(id1), equalTo(code2
 ));%0A
@@ -4861,35 +4861,35 @@
  assertT
-rue
+hat
 (result
-.contains
+, hasItem
 (id1));%0A
@@ -4906,35 +4906,39 @@
 sert
-False
+That
 (result
-.contains
+, not(hasItem
 (id2))
+)
 ;%0A  
@@ -5163,35 +5163,35 @@
  assertT
-rue
+hat
 (result
-.contains
+, hasItem
 (id1));%0A
@@ -5209,27 +5209,27 @@
 ertT
-rue
+hat
 (result
-.contains
+, hasItem
 (id2

use hamcrest-notation in workflow-tests