#!perl
use Cassandane::Tiny;

sub test_bulk_import_export
{
    my ($self) = @_;

    my $CardDAV = $self->{carddav};
    my $Id = $CardDAV->NewAddressBook('foo');

    my $uid1 = "3b678b69-ca41-461e-b2c7-f96b9fe48d68";
    my $uid2 = "addr1\@example.com";
    my $uid3 = "addr2\@example.com";

    my $single = <<EOF;
BEGIN:VCARD
VERSION:3.0
UID:$uid3
N:Gump;Forrest;;Mr.
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
REV:2008-04-24T19:52:43Z
END:VCARD
EOF

    my $multiple = <<EOF;
BEGIN:VCARD
VERSION:4.0
NICKNAME:me
UID;VALUE=TEXT:$uid2
FN:Cyrus Daboo
EMAIL:cdaboo\@example.com
END:VCARD
BEGIN:VCARD
VERSION:4.0
NICKNAME:eric
UID:urn:uuid:$uid1
FN:Eric York
END:VCARD
EOF

    my %Headers = (
      'Content-Type' => 'text/vcard',
      'Authorization' => $CardDAV->auth_header(),
    );

    xlog $self, "Import a single vCard";
    my $res = $CardDAV->{ua}->request('POST', $CardDAV->request_url($Id), {
        content => $single,
        headers => \%Headers,
    });
    $self->assert_num_equals(207, $res->{status});

    my $xml = XMLin($res->{content});
    $self->assert_str_equals($uid3, $xml->{'D:response'}{'D:propstat'}{'D:prop'}{'CS:uid'});

    xlog $self, "Import multiple vCards";
    $res = $CardDAV->{ua}->request('POST', $CardDAV->request_url($Id), {
        content => $multiple,
        headers => \%Headers,
    });
    $self->assert_num_equals(207, $res->{status});

    $xml = XMLin($res->{content});
    $self->assert_str_equals($uid2, $xml->{'D:response'}[0]{'D:propstat'}{'D:prop'}{'CS:uid'});
    $self->assert_str_equals("urn:uuid:$uid1", $xml->{'D:response'}[1]{'D:propstat'}{'D:prop'}{'CS:uid'});

    xlog $self, "Export the vCards";
    $res = $CardDAV->{ua}->request('GET', $CardDAV->request_url($Id), {
        headers => \%Headers,
    });
    $self->assert_num_equals(200, $res->{status});
    $self->assert_matches(qr/UID:$uid3\r\nN:Gump/, $res->{content});
    $self->assert_matches(qr/UID:$uid2\r\nFN:Cyrus Daboo/, $res->{content});
    $self->assert_matches(qr/UID:$uid1\r\nFN:Eric York/, $res->{content});
}
