#!/usr/bin/perl

# Simulation test rules for object S1529AW.

use strict;

use Env;
use Benchmark ':hireswallclock';
use DBI;
use Test::More tests=>1;

# Global variables:
our $dbh;
our $g_st="Verify DO generation for S1529AW"; # Test name.

# Perform tests.

connect_database();
ok(verify_do_S1529AW(),$g_st);

# Verify that the DO corresponds to the correct SSM object.
sub verify_do_S1529AW {
    my $success=0;
    my $ssm_id=get_ssm_id("S1529AW");
    my $sql="SELECT ssm_id FROM derivedobjects where ssm_id=?";
    my $sth=$dbh->prepare($sql);
    my $result=$sth->execute($ssm_id);
    my $row;
    my $row_cnt=0;

    while($row=$sth->fetchrow_arrayref()){
        if($row->[0]){
            $row_cnt++;
        }
    }

    if(!$row_cnt){
        $success=0;
    } else { $success=1; }
    return $success;   
}

# Get the SSM ID given an object's name.
sub get_ssm_id {
    
    my($obj_name)=@_;
    my $sql="select ssm_id from ssm where object_name=?";
    my $sth=$dbh->prepare($sql);
    my $result=$sth->execute($obj_name);
    my $row;
    while($row=$sth->fetchrow_arrayref()){
        if($row->[0]){
            return $row->[0]; # only one return value should ever be returned.
        }
        return 0; # fail case
    }       
}

# Connect database
sub connect_database {
    my $dbname="psmops_drchang_standard_st";
    my $dbhost="mops00.ifa.hawaii.edu";
    my $dbuser="mops";
    my $dbpass="mops";
    my $dbport="3306";
    my $dbconnect="DBI:mysql:$dbname:$dbhost:$dbport";

    $dbh=DBI->connect($dbconnect,$dbuser,$dbpass) || die "ERROR: Cannot connect to database $dbname.";
}
