/*
 * ################################################################
 *
 * ProActive Parallel Suite(TM): The Java(TM) library for
 *    Parallel, Distributed, Multi-Core Computing for
 *    Enterprise Grids & Clouds
 *
 * Copyright (C) 1997-2011 INRIA/University of
 *                 Nice-Sophia Antipolis/ActiveEon
 * Contact: proactive@ow2.org or contact@activeeon.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation; version 3 of
 * the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 *
 *  Initial developer(s):               The ActiveEon Team
 *                        http://www.activeeon.com/
 *  Contributor(s):
 *
 * ################################################################
 * $$ACTIVEEON_INITIAL_DEV$$
 */
package functionaltests.workflow;

import org.junit.Assert;
import org.ow2.proactive.scheduler.common.Scheduler;
import org.ow2.proactive.scheduler.common.job.JobId;
import org.ow2.proactive.scheduler.common.job.factories.JobFactory;

import functionalTests.FunctionalTest;
import functionaltests.SchedulerTHelper;


/**
 * Tests that valid workflow-enabled jobs are accepted by the scheduler,
 * and invalid workflows-enabled jobs rejected
 *
 * @author The ProActive Team
 * @since ProActive Scheduling 2.2
 */
public class TestWorkflowSubmission extends FunctionalTest {

    private static final String jobs_path = TestWorkflowSubmission.class.getResource(
            "/functionaltests/workflow/descriptors/").getPath();

    private static final int jobs_valid = 17;

    private static final int jobs_fail = 44;

    @org.junit.Test
    public void run() throws Throwable {
        testFail();
        testValid();
    }

    /**
     * Submits a batch of illegal workflow jobs,
     * checks for failure at the job creation
     * 
     * @throws Throwable
     */
    private static void testFail() throws Throwable {
        Scheduler userInt = SchedulerTHelper.getSchedulerInterface();

        for (int i = 0; i < jobs_fail; i++) {
            String job_path = jobs_path + "flow_fail_" + (i + 1) + ".xml";

            Exception exc = null;
            JobId job = null;
            try {
                job = userInt.submit(JobFactory.getFactory().createJob(job_path));
            } catch (Exception e) {
                exc = e;
            }
            Assert
                    .assertTrue("Job " + job_path + " was supposed to be rejected but was created",
                            job == null);
            Assert.assertTrue(exc != null);
        }
        SchedulerTHelper.log(jobs_fail + " invalid jobs successfully rejected");
    }

    /**
     * Submits a batch of valid workflow jobs,
     * checks for success at the job creation
     * 
     * @throws Throwable
     */
    private static void testValid() throws Throwable {
        Scheduler userInt = SchedulerTHelper.getSchedulerInterface();

        for (int i = 0; i < jobs_valid; i++) {
            String job_path = jobs_path + "flow_valid_" + (i + 1) + ".xml";

            Exception exc = null;
            JobId job = null;
            try {
                job = userInt.submit(JobFactory.getFactory().createJob(job_path));
            } catch (Exception e) {
                exc = e;
            } finally {
                userInt.removeJob(job);
            }
            Assert.assertTrue("JobFactory returned null for valid job " + job_path, job != null);
            Assert.assertTrue("JobFactory threw an exception for valid job " + job_path, exc == null);
        }
        SchedulerTHelper.log(jobs_valid + " valid jobs successfully created");
    }
}

@@ -1447,16 +1447,59 @@
 kflow;%0A%0A
+import java.io.File;%0Aimport java.net.URL;%0A%0A
 import o
@@ -1515,16 +1515,16 @@
 Assert;%0A
-
 import o
@@ -2055,22 +2055,19 @@
 c final 
-String
+URL
  jobs_pa
@@ -2103,21 +2103,8 @@
 lass
-.getResource(
 %0A   
@@ -2112,16 +2112,29 @@
         
+.getResource(
 %22/functi
@@ -2170,18 +2170,8 @@
 s/%22)
-.getPath()
 ;%0A%0A 
@@ -2712,37 +2712,74 @@
  job_path = 
-jobs_path
+new File(jobs_path.toURI()).getAbsolutePath()
  + %22
+/
 flow_fail_%22 
@@ -2781,32 +2781,48 @@
 il_%22 + (i + 1) +
+%0A               
  %22.xml%22;%0A%0A      
@@ -3707,21 +3707,58 @@
 h = 
-jobs_path
+new File(jobs_path.toURI()).getAbsolutePath()
  + %22
+/
 flow
@@ -3777,16 +3777,32 @@
 i + 1) +
+%0A               
  %22.xml%22;

Fix bad URI API usage and add a "/" in xml files path