#!/usr/bin/env perl
# PROJECT: Pipeline
# MODULE:  $RCSfile: casava-test-framework.pl,v $
# AUTHOR:  Stephen Allan
#
# Copyright (c) 2008 Illumina
# This source file is covered by the "Illumina Public Source License"
# agreement and bound by the terms therein.
#
# The script is wrapper around tests defined in the test xml files
#

use warnings FATAL => 'all';
use strict;
use POSIX;
use constant DEBUG => 0;    # set to 1 to get debug info

use lib '@CASAVA_FULL_PERL_LIBDIR@';
use Data::Dumper;
use CasavaTest::Testing;
use CasavaTest::TestingTools;
use Getopt::Long;

my $testId            = "CasavaTests_BF00151";
my $testSetId         = "";
my $type              = "CASAVA";
my $GA_TEST_DATA_PATH = "";                

my $usage =
    "Usage: $0 [options]\n"
  . "OPTIONAL\n"
  . "\t-t, --testId                             - test id from $pipelineTestXmlName\n"
   . "\t-s, --testSetId                          - test set id from $pipelineTestXmlName\n"
  . "\t--type                                   - Type of test [IPAR_1.01 | GA]\n"
  . "\t-g, --$ENV_NAME_GA_TEST_DATA=PATH        - Path to Test dataset "
  . " - PATH to $ENV_NAME_GA_TEST_DATA (if not set then loaded from enviroment)\n\n";

my $r = GetOptions(
    "$ENV_NAME_GA_TEST_DATA|g=s" => \$GA_TEST_DATA_PATH,
    "testId|t=s"                 => \$testId,
    "testSetId|s=s"              => \$testSetId,
    "type=s"                     => \$type
);

if ( $GA_TEST_DATA_PATH eq "" && !defined $ENV{$ENV_NAME_GA_TEST_DATA} ) {
    die "$ENV_NAME_GA_TEST_DATA not defined\n" . $usage;
}


my $projectTestFile;
($projectTestFile, $GA_TEST_DATA_PATH) = getTestXmlPath($GA_TEST_DATA_PATH, $testId.".xml");

#print "The project test file is $projectTestFile\n";

# process the projectTests XML file to get a list of campaigns to include
my $campaignReference;
my $campaignList;
my $testParameterData;
my $testRef;
my $testSetRef;
($campaignList, $testParameterData) = processProjectTestXml($campaignReference, 
                                                            $projectTestFile, 
                                                            $testSetId, 
                                                            $testParameterData);

my $testSpecificationData;
foreach my $campaign(@$campaignList)
{
    %$testSpecificationData = ();
    my $suiteList = processCampaign($testSpecificationData, $campaign, $GA_TEST_DATA_PATH);

#    print "The testSpecificationData after processing the campaign file - \n";
#    print Dumper($testSpecificationData);
    
    foreach my $suite(@$suiteList)
    {
#        print "\nSuite name is - $suite\n";
        
        my $dataFolder;
        my $testSpecificationList = processSuite($testSpecificationData, $suite, $dataFolder, $GA_TEST_DATA_PATH);        
        
#        print "The testSpecificationData after processing the suite file - \n";
#        print Dumper($testSpecificationData);
    
        foreach my $testSpecification(@$testSpecificationList)
        {
#            print "The current test specification ID is - $testSpecification\n";
            %$testParameterData = ();
            %$testRef = ();
            %$testSetRef = ();
            my $specificationParameters = processTestSpecification($testParameterData, 
                                                                   $testSpecification, 
                                                                   $dataFolder, 
                                                                   $GA_TEST_DATA_PATH);
#            print "testParameterData for campaign $campaign -\n";
#            print Dumper($testParameterData);

            my $campaignSpecificationPrefix = "Campaign-".$campaign."-";
            my $suiteSpecificationPrefix = "Suite-".$suite."-";
            initialiseTestSetRefStructure($testSetRef, 
                                          $campaignSpecificationPrefix, 
                                          $suiteSpecificationPrefix, 
                                          $testSpecificationData);

            my $testSpecificationPrefix = "TestSpecification-".$testSpecification."-";
            initialiseTestRefStructure($testRef, $testSpecificationPrefix, $specificationParameters, $testParameterData);
            
            my $status = runTest($GA_TEST_DATA_PATH, $testId, $testSetId, $type, $testRef, $testSetRef);
            if ( $status != 0 ) {
                die "Test $testId:$testSetId failed\n";
            }
            
        }
    }

}
    print "Well we got through to the end of the wrapper!\n";


