#!/usr/bin/perl

# Copryight (C) 2007  Joshua Hoblitt
#
# $Id: 14_server_xattr.t,v 1.2 2007-05-03 22:10:14 jhoblitt Exp $

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

use Test::More tests => 32;

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

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

my $neb = Nebulous::Server->new(
    dsn         => "DBI:mysql:database=test:host=localhost",
    dbuser      => "test",
    dbpasswd    => "",
);

# 1 key / xattr

Test::Nebulous->setup;

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

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

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

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

# multiple xattrs

Test::Nebulous->setup;

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

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

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

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

# replace xattrs

Test::Nebulous->setup;

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

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

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

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

# setxattr_object

Test::Nebulous->setup;

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

Test::Nebulous->setup;

eval {
    $neb->create_object('foo');

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

Test::Nebulous->setup;

eval {
    $neb->create_object('foo');

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

Test::Nebulous->setup;

eval {
    $neb->create_object('foo');

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

# listxattr_object

Test::Nebulous->setup;

eval {
    $neb->create_object('foo');

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

Test::Nebulous->setup;

eval {
    $neb->create_object('foo');

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

# getxattr_object

Test::Nebulous->setup;

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

Test::Nebulous->setup;

eval {
    $neb->create_object('foo');

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

Test::Nebulous->setup;

eval {
    $neb->create_object('foo');

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

Test::Nebulous->cleanup;
