/*
 * Copyright  1999-2004 The Apache Software Foundation
 *
 *  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.
 *
 */

/* $Id$  */

package org.apache.lenya.cms.workflow;

import org.apache.lenya.ac.AccessControlException;
import org.apache.lenya.ac.Identity;
import org.apache.lenya.ac.User;
import org.apache.lenya.ac.impl.AbstractAccessControlTest;
import org.apache.lenya.cms.publication.Document;
import org.apache.lenya.cms.publication.DocumentFactory;
import org.apache.lenya.cms.publication.Publication;
import org.apache.lenya.cms.repository.RepositoryException;
import org.apache.lenya.cms.repository.RepositoryUtil;
import org.apache.lenya.cms.repository.Session;
import org.apache.lenya.workflow.WorkflowException;
import org.apache.lenya.workflow.Workflowable;

/**
 * To change the template for this generated type comment go to Window>Preferences>Java>Code
 * Generation>Code and Comments
 */
public class WorkflowTest extends AbstractAccessControlTest {

    private static final String variableName = "is_live";
    protected static final String URL = "/authoring/index.html";

    protected String getWebappUrl() {
        return "/test" + URL;
    }

    /**
     * Tests the workflow.
     * @throws Exception when something went wrong.
     */
    public void testWorkflow() throws Exception {
        Publication publication = getPublication("test");
        String url = "/" + publication.getId() + URL;
        DocumentFactory map = getFactory();
        Document document = map.getFromURL(url);

        document.getRepositoryNode().lock();

        Session session = getSession(submitSituation);
        Workflowable workflowable = WorkflowUtil.getWorkflowable(getManager(),
                session,
                getLogger(),
                document);
        if (workflowable.getVersions().length > 0) {
            if (workflowable.getLatestVersion().getState().equals("review")) {
                invoke(document, rejectSituation);
            } else if (workflowable.getLatestVersion().getState().equals("live")) {
                invoke(document, deactivateSituation);
            }
        }

        for (int situationIndex = 0; situationIndex < situations.length; situationIndex++) {
            TestSituation situation = situations[situationIndex];
            invoke(document, situation);
        }

        document.getRepositoryNode().unlock();

        getLogger().info("Test completed.");
    }

    protected void invoke(Document document, TestSituation situation)
            throws AccessControlException, RepositoryException, WorkflowException {
        Session session = getSession(situation);
        Workflowable instance = new DocumentWorkflowable(getManager(),
                session,
                document,
                getLogger());
        assertNotNull(instance);

        String event = situation.getEvent();

        getLogger().info("Event: " + event);

        WorkflowUtil.invoke(getManager(), session, getLogger(), document, event);

        boolean value = instance.getLatestVersion().getValue(variableName);

        getLogger().info("Variable: " + variableName + " = " + value);
        getLogger().info("------------------------------------------------------");

        assertEquals(value, situation.getValue());
    }

    protected Session getSession(TestSituation situation) throws AccessControlException,
            RepositoryException {
        login(situation.getUser());

        Identity identity = (Identity) getRequest().getSession()
                .getAttribute(Identity.class.getName());
        User user = identity.getUser();
        getLogger().info("User: [" + user + "]");

        Session session = RepositoryUtil.createSession(getManager(), identity);
        return session;
    }

    private static final TestSituation submitSituation = new TestSituation("lenya", "submit", false);
    private static final TestSituation rejectSituation = new TestSituation("alice", "reject", false);
    private static final TestSituation deactivateSituation = new TestSituation("alice",
            "deactivate",
            false);
    private static final TestSituation publishSituation = new TestSituation("alice",
            "publish",
            true);

    private static final TestSituation[] situations = { submitSituation, rejectSituation,
            submitSituation, publishSituation, deactivateSituation };

    /**
     * A test situation.
     */
    public static class TestSituation {
        private String user;
        private String event;
        private boolean value;

        /**
         * Creates a new test situation.
         * @param _user The user.
         * @param _event The event.
         * @param _value The variable value.
         */
        public TestSituation(String _user, String _event, boolean _value) {
            this.user = _user;
            this.event = _event;
            this.value = _value;
        }

        /**
         * Returns the event.
         * @return An event.
         */
        public String getEvent() {
            return this.event;
        }

        /**
         * Returns the user.
         * @return A string.
         */
        public String getUser() {
            return this.user;
        }

        /**
         * Returns the value.
         * @return A value.
         */
        public boolean getValue() {
            return this.value;
        }
    }

}
@@ -1178,24 +1178,66 @@
 ry.Session;%0A
+import org.apache.lenya.workflow.Version;%0A
 import org.a
@@ -2388,36 +2388,50 @@
 ) %7B%0A            
-if (
+Version version = 
 workflowable.get
@@ -2449,36 +2449,64 @@
 on()
-.getState().equals(%22review%22)
+;%0A            if (version.getValue(variableName) == true
 ) %7B%0A
@@ -2538,22 +2538,26 @@
 cument, 
-reject
+deactivate
 Situatio
@@ -2587,39 +2587,15 @@
 if (
-workflowable.getLatestV
+v
 ersion
-()
 .get
@@ -2610,20 +2610,22 @@
 equals(%22
-live
+review
 %22)) %7B%0A  
@@ -2651,34 +2651,30 @@
 e(document, 
-deactivate
+reject
 Situation);%0A

Allow WorkflowTest to run for authoring/review + is_live = true