#!/usr/bin/env perl

# Copyright (C) 2005  Joshua Hoblitt
#
# $Id: 02_parse.t,v 1.5 2005-10-14 23:30:48 jhoblitt Exp $

use strict;
use warnings;

use lib qw( ./lib ./t );

use Test::More tests => 33;

use PS::IPP::PSFTP::Parser;
use Test::Warn;

my $example1 =<<END;
2005-08-06|19:30:01|telescope_2005-08-06T19:30:01|metadata||8640|c462d570deb517d9bd92e5bb6a21cc86|http://flaxen.ifa.hawaii.edu/otis/data/metadata/telescope_2005-08-06T19:30:01
2005-08-06|19:40:01|telescope_2005-08-06T19:40:01|metadata||8640|c462d570deb517d9bd92e5bb6a21cc86|http://flaxen.ifa.hawaii.edu/otis/data/metadata/telescope_2005-08-06T19:40:01
2005-08-06|19:50:01|telescope_2005-08-06T19:50:01|metadata||8640|c462d570deb517d9bd92e5bb6a21cc86|http://flaxen.ifa.hawaii.edu/otis/data/metadata/telescope_2005-08-06T19:50:01
END

my $parser = PS::IPP::PSFTP::Parser->new;

{
my $results = $parser->parse($example1);
my @items = @{ $results };

is(scalar @items, 3, "correct number of item returned");

{
    my $item = pop @items;
    is($item->stamp,   "2005-08-06T19:30:01",           "correct time stamp");
    is($item->file_id, "telescope_2005-08-06T19:30:01", "correct file id");
    is($item->type,    "metadata",                      "correct type");
    is($item->size,    8640,                            "correct size");
    is($item->md5,     "c462d570deb517d9bd92e5bb6a21cc86", "correct md5");
    is($item->url,
        "http://flaxen.ifa.hawaii.edu/otis/data/metadata/telescope_2005-08-06T19:30:01", "correct url");
    is($item->attributes, undef, "no attributes");
}

{
    my $item = pop @items;
    is($item->stamp,   "2005-08-06T19:40:01",           "correct time stamp");
    is($item->file_id, "telescope_2005-08-06T19:40:01", "correct file id");
    is($item->type,    "metadata",                      "correct type");
    is($item->size,    8640,                            "correct size");
    is($item->md5,     "c462d570deb517d9bd92e5bb6a21cc86", "correct md5");
    is($item->url,     "http://flaxen.ifa.hawaii.edu/otis/data/metadata/telescope_2005-08-06T19:40:01", "correct url");
    is($item->attributes, undef, "no attributes");
}

{
    my $item = pop @items;
    is($item->stamp,   "2005-08-06T19:50:01",           "correct time stamp");
    is($item->file_id, "telescope_2005-08-06T19:50:01", "correct file id");
    is($item->type,    "metadata",                      "correct type");
    is($item->size,    8640,                            "correct size");
    is($item->md5,     "c462d570deb517d9bd92e5bb6a21cc86", "correct md5");
    is($item->url,     "http://flaxen.ifa.hawaii.edu/otis/data/metadata/telescope_2005-08-06T19:50:01", "correct url");
    is($item->attributes, undef, "no attributes");
}
}

my $example2 =<<END;
2005-08-06|19:30:01|telescope_2005-08-06T19:30:01|metadata|foo=bar:baz=baz|8640|c462d570deb517d9bd92e5bb6a21cc86|http://flaxen.ifa.hawaii.edu/otis/data/metadata/telescope_2005-08-06T19:30:01
END

{
my $results = $parser->parse($example2);
my @items = @{ $results };

is(scalar @items, 1, "correct number of item returned");

{
    my $item = pop @items;
    is($item->stamp,   "2005-08-06T19:30:01",           "correct time stamp");
    is($item->file_id, "telescope_2005-08-06T19:30:01", "correct file id");
    is($item->type,    "metadata",                      "correct type");
    is($item->size,    8640,                            "correct size");
    is($item->md5,     "c462d570deb517d9bd92e5bb6a21cc86", "correct md5");
    is($item->url,     "http://flaxen.ifa.hawaii.edu/otis/data/metadata/telescope_2005-08-06T19:30:01", "correct url");
    eq_hash($item->attributes, { foo => 'bar', baz => 'baz' }, "no attributes");
}
}

warning_like {
    my $results = $parser->parse(<<END);
2005-08-06|19:30:01|telescope_2005-08-06T19:30:01||foo=bar:baz=baz|8640|c462d570deb517d9bd92e5bb6a21cc86|http://flaxen.ifa.hawaii.edu/otis/data/metadata/telescope_2005-08-06T19:30:01
END
    is($results, undef, "field not allowed to be null");
} qr/record: .*? is missing required field/,
    "warning for null required field";

warning_like {
my $results = $parser->parse(<<END);
2005-08-06|19:30:01|telescope_2005-08-06T19:30:01|foo=bar:baz=baz|8640|c462d570deb517d9bd92e5bb6a21cc86|http://flaxen.ifa.hawaii.edu/otis/data/metadata/telescope_2005-08-06T19:30:01
END
    is($results, undef, "not enough fields");
} qr/record: .*? is missing required field/,
    "warning for missing required field";
