#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 7;

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

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

my $idata = "PS::IPP::IData::Server";

$idata->setup(
    "DBI:mysql:database=test:host=localhost",
    "test",
    "",
);

Test::IData->setup;

{
    # key
    my $uri = $idata->create_object( "foo" );

    my $locations = $idata->find_instances( "foo" );

    is( $uri, $locations->[0], "URIs match" );
}

Test::IData->setup;

{
    # key
    my $uri1 = $idata->create_object( "foo" );
    my $uri2 = $idata->replicate_object( "foo" );

    my $locations = $idata->find_instances( "foo" );

    ok( eq_set( [$uri1, $uri2], $locations ), "URIs match" );
}

Test::IData->setup;

{
    # key, volume
    my $uri = $idata->create_object( "foo" );

    my $locations = $idata->find_instances( "foo", "node01" );

    is( $uri, $locations->[0], "URIs match" );
}

Test::IData->setup;

{
    # key
    my $uri1 = $idata->create_object( "foo" );
    my $uri2 = $idata->replicate_object( "foo" );

    my $locations = $idata->find_instances( "foo", "node01" );

    ok( eq_set( [$uri1, $uri2], $locations ), "URIs match" );
}


Test::IData->setup;

eval {
    $idata->find_instances( "foo" );
};
like( $@, qr/no instances found/, "storage object does not exist" );

Test::IData->setup;

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

Test::IData->setup;

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

Test::IData->cleanup;
