#!/usr/bin/perl

use strict;
use warnings;

use Apache::Test qw( -withtestmore );

plan tests => 15;

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

use PS::IPP::IData::Client;
use PS::IPP::IData::Util qw( :standard );
use Test::IData;

my $hostport = Apache::Test->config->{ 'hostport' };

Test::IData->setup;

{
    # key
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );
    $idata->replicate( "foo" );

    my $uri = $idata->cull( "foo" );

    ok( $uri, "good cull" );
    ok( ! -e _get_file_path( $uri ), "file doesn't exist" );
}

Test::IData->setup;

SKIP: {
    skip "unsure if this should be part of the API", 2;

    # key, $volume
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );
    $idata->replicate( "foo", "node01" );

    my $uri = $idata->cull( "foo", "node01" );

    ok( $uri, "good cull" );
    ok( ! -e _get_file_path( $uri ), "file exists" );
}

Test::IData->setup;

{
    # key
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );

    $idata->replicate( "foo", "node01" );
    $idata->replicate( "foo", "node02" );

    my $uri1 = $idata->cull( "foo" );
    my $uri2 = $idata->cull( "foo" );

    ok( $uri1, "good cull" );
    ok( $uri2, "good cull" );
    ok( ! -e _get_file_path( $uri1 ), "file exists" );
    ok( ! -e _get_file_path( $uri2 ), "file exists" );
}

Test::IData->setup;

SKIP: {
    skip "unsure if this should be part of the API", 4;

    # key, $volume
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->create( "foo" );

    $idata->replicate( "foo", "node01" );
    $idata->replicate( "foo", "node02" );

    my $uri1 = $idata->cull( "foo", "node01" );
    my $uri2 = $idata->cull( "foo", "node02" );

    ok( $uri1, "good cull" );
    ok( $uri2, "good cull" );
    ok( ! -e _get_file_path( $uri1 ), "file exists" );
    ok( ! -e _get_file_path( $uri2 ), "file exists" );
}

Test::IData->setup;

{
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    my $uri = $idata->cull( "foo" );

    is( $uri, undef, "storage object does not exist" );
}

Test::IData->setup;

eval {
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->cull();
};
like( $@, qr/1 was expected/, "no params" );

Test::IData->setup;

eval {
    my $idata = PS::IPP::IData::Client->new(
        proxy => "http://$hostport/idata",
    );
    $idata->cull( 1, 2 );
};
like( $@, qr/1 was expected/, "too many params" );

Test::IData->cleanup;
