<commit_before>#!perl

############
# Copyright 2011-2013 Proofpoint, Inc.
#
# 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.
############

use strict;
use Text::CSV_XS;
use JSON;
use LWP::UserAgent;
use Getopt::Std;
my %opt;
getopts('f:dprH',\%opt);
my $DEBUG = $opt{'d'} ? 1 : 0;
my $FILE = $opt{'f'} ? $opt{'f'} : "";
my $PARSEONLY = $opt{'p'} ? 1 : 0;
my $RUNTESTS = $opt{'r'} ? 1 : 0;
my $HALT = $opt{'H'} ? 1 : 0;

if(length($FILE) == 0)
{
print "usage: inv_test.pl -f <testcase file>
    -f: csv file with testcases (see cmdb_tests.csv)
    -p: parse testcase file only 
    -r: run tests
    -d: debug
";
exit;
}

my $ua = LWP::UserAgent->new;
$ua->timeout(320);


my @rows;
my $csv = Text::CSV_XS->new ({binary => 1 }) or die "Cannot use CSV: ".Text::CSV->error_diag ();
open my $fh,  $FILE or die "$FILE: $!";
my @fields;
my @tests;
while (my $row = $csv->getline($fh)) 
{
    if(scalar @fields)
    {
        my $t={};
        if($row->[0])
        {
            for(my $f=0;$f<scalar(@fields);$f++)
            {
                $t->{$fields[$f]} = $row->[$f];
            }
            push @tests, $t;
        }
    }
    else
    {
        @fields=@$row;
    }
}


if ($PARSEONLY)
{
    print to_json(\@tests,{pretty=>1});
    print "\n";
}

if($RUNTESTS)
{
    my $testResults={
        pass => 0,
        fail => 0
    };
    foreach my $test (@tests)
    {
        my $result=&execTest($test);
        print "$result\n";
        if($result=~'FAIL')
        {
            $$testResults{'fail'}++;
        }
        else
        {
            $$testResults{'pass'}++;
        }
        if($result=~'FAIL' && $HALT)
        {
            last;
        }
        print "\n";
    }    
    print "Complete: Total: " . ($$testResults{'pass'} + $$testResults{'fail'}) . " Pass: $$testResults{'pass'}  Fail: $$testResults{'fail'}\n";
}

sub makeUrlQueryStr()
{
    my $datastr=shift;
    my $data=from_json($datastr,{allow_nonref=>1});
    my $querystr="";
    foreach my $key (keys(%{$data}))
    {
        $querystr.="&" if($querystr);
        $querystr.=$key . "=" . $data->{$key};
    }
    return $querystr;
}

sub execTest()
{
    my $test = shift;
    my $response;
    my @expected_result;

### assemble request from test data
    my $testurl=$test->{'host:port'} . $test->{'baseurl'} . $test->{'entity'} . '/';
    if ($test->{'method'} ne 'POST')
    {
        $testurl.=$test->{'entity_key'};
    }
    $test->{'user/pass'}=~m|(.+)/(.+)|;
    my $user=$1;
    my $pass=$2;
    my $result='';
    $test->{'host:port'}=~m|//(.*)|;
    my $hostport=$1;
    $ua->credentials($hostport,'Operations Only',$user,$pass);
    if($test->{'method'} eq 'GET')
    {
        if($test->{'data'})
        {
            $testurl.="?" . &makeUrlQueryStr( $test->{'data'} );
        }
        $response = $ua->get($testurl);
    }
    else<commit_msg>adjusting test script to take arguments to test aginst non-localhost<commit_after>#!perl

############
# Copyright 2011-2013 Proofpoint, Inc.
#
# 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.
############

use strict;
use Text::CSV_XS;
use JSON;
use LWP::UserAgent;
use Getopt::Std;
my %opt;
getopts('f:dprHh:R:',\%opt);
my $DEBUG = $opt{'d'} ? 1 : 0;
my $FILE = $opt{'f'} ? $opt{'f'} : "";
my $PARSEONLY = $opt{'p'} ? 1 : 0;
my $RUNTESTS = $opt{'r'} ? 1 : 0;
my $HALT = $opt{'H'} ? 1 : 0;
my $HOSTPORT= $opt{'h'} ? $opt{'h'} : undef;
my $REALM =  $opt{'R'} ? $opt{'R'} : 'Operations Only';

if(length($FILE) == 0)
{
print "usage: inv_test.pl -f <testcase file>
    -f: csv file with testcases (see cmdb_tests.csv)
    -p: parse testcase file only 
    -r: run tests
    -d: debug
    -h: alternate host/port (ex:  https://localhost)
";
exit;
}

my $ua = LWP::UserAgent->new;
$ua->timeout(320);


my @rows;
my $csv = Text::CSV_XS->new ({binary => 1 }) or die "Cannot use CSV: ".Text::CSV->error_diag ();
open my $fh,  $FILE or die "$FILE: $!";
my @fields;
my @tests;
while (my $row = $csv->getline($fh)) 
{
    if(scalar @fields)
    {
        my $t={};
        if($row->[0])
        {
            for(my $f=0;$f<scalar(@fields);$f++)
            {
                $t->{$fields[$f]} = $row->[$f];
            }
            push @tests, $t;
        }
    }
    else
    {
        @fields=@$row;
    }
}


if ($PARSEONLY)
{
    print to_json(\@tests,{pretty=>1});
    print "\n";
}

if($RUNTESTS)
{
    my $testResults={
        pass => 0,
        fail => 0
    };
    foreach my $test (@tests)
    {
        my $result=&execTest($test);
        print "$result\n";
        if($result=~'FAIL')
        {
            $$testResults{'fail'}++;
        }
        else
        {
            $$testResults{'pass'}++;
        }
        if($result=~'FAIL' && $HALT)
        {
            last;
        }
        print "\n";
    }    
    print "Complete: Total: " . ($$testResults{'pass'} + $$testResults{'fail'}) . " Pass: $$testResults{'pass'}  Fail: $$testResults{'fail'}\n";
}

sub makeUrlQueryStr()
{
    my $datastr=shift;
    my $data=from_json($datastr,{allow_nonref=>1});
    my $querystr="";
    foreach my $key (keys(%{$data}))
    {
        $querystr.="&" if($querystr);
        $querystr.=$key . "=" . $data->{$key};
    }
    return $querystr;
}

sub execTest()
{
    my $test = shift;
    my $response;
    my @expected_result;

### assemble request from test data
    my $hostport_config= $HOSTPORT || $test->{'host:port'};
    my $testurl=$hostport_config . $test->{'baseurl'} . $test->{'entity'} . '/';
    if ($test->{'method'} ne 'POST')
    {
        $testurl.=$test->{'entity_key'};
    }
    $test->{'user/pass'}=~m|(.+)/(.+)|;
    my $user=$1;
    my $pass=$2;
    my $result='';
    $hostport_config=~m|//(.*)|;
    my $hostport=$1;
    $ua->credentials($hostport,$REALM,$user,$pass);
    if($test->{'method'} eq 'GET')
    {
        if($test->{'data'})
        {
            $testurl.="?" . &makeUrlQueryStr( $test->{'data'} );
        }
        $response = $ua->get($testurl);
    }
    else
{'fraction_non_alphanumeric': 0.20601785200115175, 'fraction_numerical': 0.009789807083213361, 'mean_word_length': 2.4856999498243852, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 56, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2037688', 'n_tokens_mistral': 2558, 'n_tokens_neox': 2464, 'n_words': 617}