#!/usr/bin/perl 

use COETest;

my $unitname="COE_deassign_app_from_profile";
my $date = `date`; 

my @arg;
my $index=0;

$passindex=0;
$otherindex=0;
$failindex=0;
my $flagmanualcheck="false";
my $ret;

my @TEST;
# TEST[0]=Name
# TEST[1]=Desc
# TEST[2]=Reference
# TEST[3]=ExpectedResult
# TEST[4]=ExpectedStatus
# TEST[5]=ActualResult
# TEST[6]=ActualStatus
# TEST[7]=Pass/Other/Fail

sub printTestHeader;
sub runTest;
sub checkStatus;
sub countStatus;
sub printTestSummary;
sub printTestData;


# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
#- create local test account group/profile
@arg=();
$arg[0]="/h/COE/bin/COE_add_profile_data";
$arg[1]="Local Prof";
$arg[2]="Test Acct L";
$arg[3]="Local_Prof";
$arg[4]="l";
system(@arg);

#- create global test account group/profile
@arg=();
$arg[0]="/h/COE/bin/COE_add_profile_data";
$arg[1]="Global Prof";
$arg[2]="Test Acct G";
$arg[3]="Global_Prof";
$arg[4]="g";
system(@arg);

#- create local test app data
@arg=();
$arg[0]="/h/COE/bin/COE_add_app_data";
$arg[1]="Test App L";
$arg[2]="TestName";
$arg[3]="Test Acct L";
$arg[4]="/h/TESTAPP/data/Icons/Icon.pm";
$arg[5]="/h/TESTAPP/bin/testapp";
$arg[6]="l";
$arg[7]="-1 one -2 two";
system(@arg);

#- create global test app data
@arg=();
$arg[0]="/h/COE/bin/COE_add_app_data";
$arg[1]="Test App G";
$arg[2]="TestName";
$arg[3]="Test Acct G";
$arg[4]="/h/TESTAPP/data/Icons/Icon.pm";
$arg[5]="/h/TESTAPP/bin/testapp";
$arg[6]="g";
$arg[7]="-1 one -2 two";
system(@arg);

#- assign local test app to local test profile
@arg=();
$arg[0]="/h/COE/bin/COE_assign_app_to_profile";
$arg[1]="Local Prof";
$arg[2]="l";
$arg[3]="Test App L";
system(@arg);

#- assign global test app to global test profile
@arg=();
$arg[0]="/h/COE/bin/COE_assign_app_to_profile";
$arg[1]="Global Prof";
$arg[2]="g";
$arg[3]="Test App G";
system(@arg);

print "==========================\n";
print " Unit Test for $unitname\n";
print " $date\n";
print "==========================\n";

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$index++; @arg=();
$TEST[0][$index] = "$unitname exists";
$TEST[1][$index] = "Does API/Tool exist.";
$TEST[2][$index] = "kernapi-3.3.0.0-199712";
$TEST[3][$index] = "File exists";
$TEST[4][$index] = 0;
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$flagmanualcheck="false";
$arg[0]="/bin/ls";
$arg[1]="/h/COE/bin/$unitname";

printTestHeader $index, @TEST;
$ret=(runTest @arg)/256;
$TEST[6][$index] = $ret;
$TEST[7][$index] = checkStatus $flagmanualcheck, $ret, $TEST[4][$index];
countStatus $TEST[7][$index], $passindex, $otherindex, $failindex;

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$index++; @arg=();
$TEST[0][$index] = "$unitname";
$TEST[1][$index] = "Useage message given when no message provided.";
$TEST[2][$index] = "kernapi-3.3.0.0-199712";
$TEST[3][$index] = "Useage message";
$TEST[4][$index] = 202;
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$flagmanualcheck="true";
$arg[0]="/h/COE/bin/$unitname";

printTestHeader $index, @TEST;
$ret=(runTest @arg)/256;
$TEST[6][$index] = $ret;
$TEST[7][$index] = checkStatus $flagmanualcheck, $ret, $TEST[4][$index];
countStatus $TEST[7][$index], $passindex, $otherindex, $failindex;

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$index++; @arg=();
$TEST[0][$index] = "$unitname -h";
$TEST[1][$index] = "Help Display.";
$TEST[2][$index] = "kernapi-3.3.0.0-199712";
$TEST[3][$index] = "displays Useage message";
$TEST[4][$index] = 0;
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$flagmanualcheck="true";
$arg[0]="/h/COE/bin/$unitname";
$arg[1]="-h";

printTestHeader $index, @TEST;
$ret=(runTest @arg)/256;
print "ret = $ret\n";
$TEST[6][$index] = $ret;
$TEST[7][$index] = checkStatus $flagmanualcheck, $ret, $TEST[4][$index];
countStatus $TEST[7][$index], $passindex, $otherindex, $failindex;

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$index++; @arg=();
$TEST[0][$index] = "$unitname -H";
$TEST[1][$index] = "Help Display.";
$TEST[2][$index] = "kernapi-3.3.0.0-199712";
$TEST[3][$index] = "displays COE Man Page";
$TEST[4][$index] = 0;
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$flagmanualcheck="true";
$arg[0]="/h/COE/bin/$unitname";
$arg[1]="-H";

printTestHeader $index, @TEST;
$ret=(runTest @arg)/256;
print "ret = $ret\n";
$TEST[6][$index] = $ret;
$TEST[7][$index] = checkStatus $flagmanualcheck, $ret, $TEST[4][$index];
countStatus $TEST[7][$index], $passindex, $otherindex, $failindex;

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$index++; @arg=();
$TEST[0][$index] = "$unitname -V";
$TEST[1][$index] = "Version Display.";
$TEST[2][$index] = "kernapi-3.3.0.0-199712";
$TEST[3][$index] = "displays version number";
$TEST[4][$index] = 0;
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$flagmanualcheck="true";
$arg[0]="/h/COE/bin/$unitname";
$arg[1]="-V";

printTestHeader $index, @TEST;
$ret=(runTest @arg)/256;
print "ret = $ret\n";
$TEST[6][$index] = $ret;
$TEST[7][$index] = checkStatus $flagmanualcheck, $ret, $TEST[4][$index];
countStatus $TEST[7][$index], $passindex, $otherindex, $failindex;

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$index++; @arg=();
$TEST[0][$index] = "$unitname \"Local Prof\" l \"Test App L\"";
$TEST[1][$index] = "deassign a local app from a local profile";
$TEST[2][$index] = "kernapi-3.3.0.0-199712";
$TEST[3][$index] = "";
$TEST[4][$index] = 0;
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$flagmanualcheck="true";
$arg[0]="/h/COE/bin/$unitname";
$arg[1]="Local Prof";
$arg[2]="l";
$arg[3]="Test App L";

printTestHeader $index, @TEST;
$ret=(runTest @arg)/256;

#- get app/prof data
@arg=();
$arg[0]="/h/COE/bin/COE_get_profileapp_data";
$arg[1]="Local Prof";
$arg[2]="l";
system(@arg);

print "ret = $ret\n";
$TEST[6][$index] = $ret;
$TEST[7][$index] = checkStatus $flagmanualcheck, $ret, $TEST[4][$index];
countStatus $TEST[7][$index], $passindex, $otherindex, $failindex;

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$index++; @arg=();
$TEST[0][$index] = "$unitname \"No Such Prof\" l \"Test App L\"";
$TEST[1][$index] = "deassign a local app from an undefined profile";
$TEST[2][$index] = "kernapi-3.3.0.0-199712";
$TEST[3][$index] = "COE_NO_SUCH_PROFILE_APP_DATA";
$TEST[4][$index] = 209;
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$flagmanualcheck="true";
$arg[0]="/h/COE/bin/$unitname";
$arg[1]="No Such Prof";
$arg[2]="l";
$arg[3]="Test App L";

printTestHeader $index, @TEST;
$ret=(runTest @arg)/256;

#- get error string
@arg=();
$arg[0]="/h/COE/bin/COE_get_error_string";
$arg[1]=$ret;
system(@arg);

print "ret = $ret\n";
$TEST[6][$index] = $ret;
$TEST[7][$index] = checkStatus $flagmanualcheck, $ret, $TEST[4][$index];
countStatus $TEST[7][$index], $passindex, $otherindex, $failindex;

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$index++; @arg=();
$TEST[0][$index] = "$unitname \"Local Prof\" l \"Test App X\"";
$TEST[1][$index] = "Deassign an undefined app from a local profile";
$TEST[2][$index] = "kernapi-3.3.0.0-199712";
$TEST[3][$index] = "COE_NO_SUCH_PROFILE_APP_DATA";
$TEST[4][$index] = 209;
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$flagmanualcheck="true";
$arg[0]="/h/COE/bin/$unitname";
$arg[1]="Local Prof";
$arg[2]="l";
$arg[3]="Test App X";

printTestHeader $index, @TEST;
$ret=(runTest @arg)/256;

#- get error string
@arg=();
$arg[0]="/h/COE/bin/COE_get_error_string";
$arg[1]=$ret;
system(@arg);

print "ret = $ret\n";
$TEST[6][$index] = $ret;
$TEST[7][$index] = checkStatus $flagmanualcheck, $ret, $TEST[4][$index];
countStatus $TEST[7][$index], $passindex, $otherindex, $failindex;

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$index++; @arg=();
$TEST[0][$index] = "$unitname \"Global Prof\" g \"Test App G\"";
$TEST[1][$index] = "Assign a global app to a global profile";
$TEST[2][$index] = "kernapi-3.3.0.0-199712";
$TEST[3][$index] = "";
$TEST[4][$index] = 0;
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$flagmanualcheck="true";
$arg[0]="/h/COE/bin/$unitname";
$arg[1]="Global Prof";
$arg[2]="g";
$arg[3]="Test App G";

printTestHeader $index, @TEST;
$ret=(runTest @arg)/256;

#- get app/prof data
@arg=();
$arg[0]="/h/COE/bin/COE_get_profileapp_data";
$arg[1]="Global Prof";
$arg[2]="g";
system(@arg);

print "ret = $ret\n";
$TEST[6][$index] = $ret;
$TEST[7][$index] = checkStatus $flagmanualcheck, $ret, $TEST[4][$index];
countStatus $TEST[7][$index], $passindex, $otherindex, $failindex;

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$index++; @arg=();
$TEST[0][$index] = "$unitname \"No Such Prof\" g \"Test App G\"";
$TEST[1][$index] = "deassign a global app from an undefined profile";
$TEST[2][$index] = "kernapi-3.3.0.0-199712";
$TEST[3][$index] = "COE_NO_SUCH_PROFILE_APP_DATA";
$TEST[4][$index] = 209;
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$flagmanualcheck="true";
$arg[0]="/h/COE/bin/$unitname";
$arg[1]="No Such Prof";
$arg[2]="g";
$arg[3]="Test App G";

printTestHeader $index, @TEST;
$ret=(runTest @arg)/256;

#- get error string
@arg=();
$arg[0]="/h/COE/bin/COE_get_error_string";
$arg[1]=$ret;
system(@arg);

print "ret = $ret\n";
$TEST[6][$index] = $ret;
$TEST[7][$index] = checkStatus $flagmanualcheck, $ret, $TEST[4][$index];
countStatus $TEST[7][$index], $passindex, $otherindex, $failindex;

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$index++; @arg=();
$TEST[0][$index] = "$unitname \"Global Prof\" g \"Test App X\"";
$TEST[1][$index] = "Deassign an undefined app from a global profile";
$TEST[2][$index] = "kernapi-3.3.0.0-199712";
$TEST[3][$index] = "COE_NO_SUCH_PROFILE_APP_DATA";
$TEST[4][$index] = 209;
# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
$flagmanualcheck="true";
$arg[0]="/h/COE/bin/$unitname";
$arg[1]="Global Prof";
$arg[2]="g";
$arg[3]="Test App X";

printTestHeader $index, @TEST;
$ret=(runTest @arg)/256;

#- get error string
@arg=();
$arg[0]="/h/COE/bin/COE_get_error_string";
$arg[1]=$ret;
system(@arg);

print "ret = $ret\n";
$TEST[6][$index] = $ret;
$TEST[7][$index] = checkStatus $flagmanualcheck, $ret, $TEST[4][$index];
countStatus $TEST[7][$index], $passindex, $otherindex, $failindex;

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
#- remove the new local app for this test
@arg=();
$arg[0]="/h/COE/bin/COE_delete_app_data";
$arg[1]="-a";
$arg[2]="Test App L";
$arg[3]="l";
system(@arg);

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
#- remove the new local profile for this test
@arg=();
$arg[0]="/h/COE/bin/COE_delete_profile_data";
$arg[1]="Local Prof";
$arg[2]="l";
system(@arg);

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
#- remove the new global app for this test
@arg=();
$arg[0]="/h/COE/bin/COE_delete_app_data";
$arg[1]="-a";
$arg[2]="Test App G";
$arg[3]="g";
system(@arg);

# --+----|----+----|----+----|----+----|----+----|----+----|----+----|----+----|
#- remove the new global profile for this test
@arg=();
$arg[0]="/h/COE/bin/COE_delete_profile_data";
$arg[1]="Global Prof";
$arg[2]="g";
system(@arg);

# ------------------------------------------------------------------------------
printTestSummary $unitname, $passindex, $otherindex, $failindex, @TEST;
printTestData $unitname, $passindex, $otherindex, $failindex;
exit;

# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
sub countStatus {
        my ($status)=@_;

        if ("$status" eq "P") {$passindex++;}
        if ("$status" eq "X") {$otherindex++;}
        if ("$status" eq "F") {$failindex++;}
}

