/**
 * Licensed to the Austrian Association for Software Tool Integration (AASTI)
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. The AASTI licenses this file to you 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.itests.exam;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openengsb.core.api.AliveState;
import org.openengsb.core.common.AbstractOpenEngSBService;
import org.openengsb.domain.example.ExampleDomain;
import org.openengsb.domain.example.event.LogEvent;
import org.openengsb.itests.util.AbstractPreConfiguredExamTestHelper;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.context.SecurityContextHolder;

@RunWith(JUnit4TestRunner.class)
// This one will run each test in it's own container (slower speed)
// @ExamReactorStrategy(AllConfinedStagedReactorFactory.class)
public class WorkflowIT extends AbstractPreConfiguredExamTestHelper {

    public static class DummyLogDomain extends AbstractOpenEngSBService implements ExampleDomain {
        private boolean wasCalled = false;

        @Override
        public String doSomething(String message) {
            wasCalled = true;
            return "something";
        }

        @Override
        public AliveState getAliveState() {
            return AliveState.OFFLINE;
        }

        @Override
        public String doSomething(ExampleEnum exampleEnum) {
            wasCalled = true;
            return "something";
        }

        @Override
        public String doSomethingWithLogEvent(LogEvent event) {
            wasCalled = true;
            return "something";
        }

        public boolean isWasCalled() {
            return wasCalled;
        }
    }

    @Test
    public void testCorrectContextHolderStrategy() throws Exception {
        assertThat(SecurityContextHolder.getContextHolderStrategy().getClass().getSimpleName(),
            is("InheritableThreadLocalSecurityContextHolderStrategy"));
    }

    /**
     * Ignored because security manager is commented in the moment.
     */
    @Ignore
    @Test(expected = AccessDeniedException.class)
    public void testUserAccessToRuleManager_shouldThrowException() throws Exception {
        authenticate("user", "password");
    }

}

@@ -947,16 +947,74 @@
 tThat;%0A%0A
+import java.util.Dictionary;%0Aimport java.util.Hashtable;%0A%0A
 import o
@@ -1030,16 +1030,16 @@
 Ignore;%0A
-
 import o
@@ -1128,16 +1128,307 @@
 eState;%0A
+import org.openengsb.core.api.context.ContextHolder;%0Aimport org.openengsb.core.api.workflow.RuleManager;%0Aimport org.openengsb.core.api.workflow.WorkflowService;%0Aimport org.openengsb.core.api.workflow.model.RuleBaseElementId;%0Aimport org.openengsb.core.api.workflow.model.RuleBaseElementType;%0A
 import o
@@ -3042,16 +3042,16 @@
 Name(),%0A
-
         
@@ -3117,24 +3117,1311 @@
 %22));%0A    %7D%0A%0A
+    @Test%0A    public void testCreateRuleAndTriggerDomain() throws Exception %7B%0A        DummyLogDomain exampleMock = new DummyLogDomain();%0A        Dictionary%3CString, Object%3E properties = new Hashtable%3CString, Object%3E();%0A        properties.put(%22domain%22, %22example%22);%0A        properties.put(%22connector%22, %22example%22);%0A        properties.put(%22location.foo%22, %22example2%22);%0A        getBundleContext().registerService(ExampleDomain.class.getName(), exampleMock, properties);%0A%0A        RuleManager ruleManager = getOsgiService(RuleManager.class);%0A%0A        ruleManager.addImport(ExampleDomain.class.getName());%0A        ruleManager.addImport(LogEvent.class.getName());%0A%0A        ruleManager.addGlobal(ExampleDomain.class.getName(), %22example2%22);%0A%0A        ruleManager.add(new RuleBaseElementId(RuleBaseElementType.Rule, %22example-trigger%22), %22%22 +%0A                %22when%5Cn%22 +%0A                %22    l : LogEvent()%5Cn%22 +%0A                %22then%5Cn%22 +%0A                %22    example2.doSomething(%5C%2242%5C%22);%5Cn%22%0A            );%0A%0A        ContextHolder.get().setCurrentContextId(%22foo%22);%0A        WorkflowService workflowService = getOsgiService(WorkflowService.class);%0A%0A        authenticate(%22admin%22, %22password%22);%0A        workflowService.processEvent(new LogEvent());%0A%0A        assertThat(exampleMock.wasCalled, is(true));%0A    %7D%0A%0A
     /**%0A    

create additional itest for workflow-service