#!/usr/bin/perl

# Copryight (C) 2004-2005  Joshua Hoblitt
#
# $Id: 62_client_delete_instance.t,v 1.1 2005-12-03 02:52:31 jhoblitt Exp $

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

use Apache::Test qw( -withtestmore );

plan tests => 9;

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

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

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

Test::Nebulous->setup;

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

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

    my $uri = $neb->delete_instance( @$locations[0] );

    is( $uri, @$locations[0], "delete instance" );

    ok( ! -e _get_file_path( @$locations[0] ), "deleted file" );
}

Test::Nebulous->setup;

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

    my $uri1 = $neb->find_instances( "foo" )->[0];

    ok( $neb->delete_instance( $uri1 ), "delete instance" );

    my $uri2 = $neb->find_instances( "foo" )->[0];

    isnt( $uri1, $uri2, "other instance remains" );

    ok( $neb->delete_instance( $uri2 ), "delete instance" );

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

    is( $locations, undef, "no remaning instances" );
}

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

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

Test::Nebulous->setup;

eval {
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );
    $neb->delete_instance();
};
like( $@, qr/1 was expected/, "no params" );

Test::Nebulous->setup;

eval {
    my $neb = Nebulous::Client->new(
        proxy => "http://$hostport/nebulous",
    );
    $neb->delete_instance( "foo", 2 );
};
like( $@, qr/1 was expected/, "too many params" );

Test::Nebulous->cleanup;
