#!/usr/bin/perl

use strict;
use warnings;

use Apache::Test qw( -withtestmore );

plan tests => 14;

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;

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

    ok( $idata->unlock( "foo", "read" ), "read unlock" );
}

Test::IData->setup;

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

    ok( $idata->unlock( "foo", "read" ), "read unlock" );
    ok( $idata->unlock( "foo", "read" ), "read unlock" );
}

Test::IData->setup;

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

    ok( $idata->unlock( "foo", "write" ), "write unlock" );
}

Test::IData->setup;

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

    is( $idata->unlock( "foo", "read" ), undef, "storage object does not exist" );
}

Test::IData->setup;

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

    is( $idata->unlock( "foo", "write" ), undef, "storage object does not exist" );
}

Test::IData->setup;

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

    is( $idata->unlock( "foo", "read" ), undef, "no lock set" );
}

Test::IData->setup;

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

    is( $idata->unlock( "foo", "write" ), undef, "no lock set" );
}

Test::IData->setup;

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

    is( $idata->unlock( "foo", "read" ), undef, "read unlock under write lock" );
}

Test::IData->setup;

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

    is( $idata->unlock( "foo", "write" ), undef, "write unlock under read lock" );
}

Test::IData->setup;

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

    $idata->unlock();
};
like( $@, qr/2 were expected/, "no params" );

Test::IData->setup;

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

    $idata->unlock( "foo" );
};
like( $@, qr/2 were expected/, "not enough params" );

Test::IData->setup;

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

    $idata->unlock( "foo", "both" );
};
like( $@, qr/is read or write/, "not read or write" );

Test::IData->setup;

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

    $idata->unlock( "foo", 'read', 3 );
};
like( $@, qr/2 were expected/, "too many params" );

Test::IData->cleanup;
