#!/usr/bin/perl

# Copryight (C) 2007-2008  Joshua Hoblitt
#
# $Id: 66_client_xattr.t,v 1.1 2008-05-20 00:54:30 jhoblitt Exp $

use strict;
use warnings FATAL => qw( all );

use Apache::Test qw( -withtestmore );
plan tests => 32;


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

use Nebulous::Client;
use Nebulous::Util qw( :standard );
use Test::Nebulous;

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

# 1 key / xattr

Test::Nebulous->setup;

{
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );

    my $uri = $neb->create('foo');

    ok($neb->setxattr('foo', 'bar', 'baz', 'create'), 'set object xattr');
    {
        my $xattrs = $neb->listxattr('foo');
        is(scalar @$xattrs, 1, 'number of xattrs');
        is(@$xattrs[0], 'bar', 'xattr name');
    }

    my $value = $neb->getxattr('foo', 'bar');
    is($value, 'baz', 'xattr value');

    ok($neb->removexattr('foo', 'bar'), "remove object xattr");
    {
        my $xattrs = $neb->listxattr('foo');
        is(scalar @$xattrs, 0, 'number of xattrs');
    }
}

# multiple xattrs

Test::Nebulous->setup;

{
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );

    my $uri = $neb->create('foo');

    ok($neb->setxattr('foo', 'bar', 'baz', 'create'), 'set object xattr');
    ok($neb->setxattr('foo', 'bonk', 'quix', 'create'), 'set object xattr');
    
    {
        my $xattrs = $neb->listxattr('foo');
        is(scalar @$xattrs, 2, 'number of xattrs');
        is(@$xattrs[0], 'bar', 'xattr name');
        is(@$xattrs[1], 'bonk', 'xattr name');
    }

    my $value = $neb->getxattr('foo', 'bar');
    is($value, 'baz', 'xattr value');
    $value = $neb->getxattr('foo', 'bonk');
    is($value, 'quix', 'xattr value');

    ok($neb->removexattr('foo', 'bar'), "remove object xattr");
    ok($neb->removexattr('foo', 'bonk'), "remove object xattr");
    {
        my $xattrs = $neb->listxattr('foo');
        is(scalar @$xattrs, 0, 'number of xattrs');
    }
}

# replace xattrs

Test::Nebulous->setup;

{
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );

    my $uri = $neb->create('foo');

    ok($neb->setxattr('foo', 'bar', 'baz', 'create'), 'set object xattr');
    ok($neb->setxattr('foo', 'bar', 'quix', 'replace'), 're-set object xattr');
    
    {
        my $xattrs = $neb->listxattr('foo');
        is(scalar @$xattrs, 1, 'number of xattrs');
        is(@$xattrs[0], 'bar', 'xattr name');
    }

    my $value = $neb->getxattr('foo', 'bar');
    is($value, 'quix', 'xattr value');

    ok($neb->removexattr('foo', 'bar'), "remove object xattr");
    {
        my $xattrs = $neb->listxattr('foo');
        is(scalar @$xattrs, 0, 'number of xattrs');
    }
}

# setxattr

Test::Nebulous->setup;

eval {
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );

    $neb->setxattr();
};
like($@, qr/4 were expected/, "no params");

Test::Nebulous->setup;

eval {
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );

    $neb->create('foo');

    $neb->setxattr('foo', 'bar');
};
like($@, qr/4 were expected/, "too few params");

Test::Nebulous->setup;

eval {
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );

    $neb->create('foo');

    $neb->setxattr('foo', 'bar', 'baz');
};
like($@, qr/4 were expected/, "too few params");

Test::Nebulous->setup;

eval {
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );

    $neb->create('foo');

    $neb->setxattr('foo', 'bar', 'baz', 'create', 'quix');
};
like($@, qr/4 were expected/, "too many params");

# listxattr

Test::Nebulous->setup;

eval {
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );

    $neb->create('foo');

    $neb->listxattr();
};
like($@, qr/1 was expected/, "no params");

Test::Nebulous->setup;

eval {
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );

    $neb->create('foo');

    $neb->listxattr('foo', 'bar');
};
like($@, qr/1 was expected/, "too many params");

# getxattr

Test::Nebulous->setup;

eval {
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );

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

Test::Nebulous->setup;

eval {
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );

    $neb->create('foo');

    $neb->getxattr('foo');
};
like($@, qr/2 were expected/, "too few params");

Test::Nebulous->setup;

eval {
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );

    $neb->create('foo');

    $neb->getxattr('foo', 'bar', 'baz');
};
like($@, qr/2 were expected/, "too many params");

Test::Nebulous->cleanup;
