diff --git a/t/api-rest/api-callforwards.t b/t/api-rest/api-callforwards.t index 54aba38b79..41fc13e7e8 100644 --- a/t/api-rest/api-callforwards.t +++ b/t/api-rest/api-callforwards.t @@ -87,7 +87,7 @@ SKIP:{ } $test_machine->check_bundle(); - my($cf1_id) = $test_machine->get_id_from_hal($cf1->{content_collection}); #($cf1,'callforwards'); + my $cf1_id = $test_machine->get_id_from_hal($cf1->{content_collection}); #($cf1,'callforwards'); cmp_ok ($cf1_id, '>', 0, "should be positive integer"); my $cf1single_uri = "/api/callforwards/$cf1_id"; my $cf1single; diff --git a/t/api-rest/api-calllists.t b/t/api-rest/api-calllists.t index 109d8c149d..e5234c8796 100644 --- a/t/api-rest/api-calllists.t +++ b/t/api-rest/api-calllists.t @@ -4,6 +4,7 @@ use warnings; use Test::More; use Test::Collection; use Data::Dumper; +use Test::ForceArray qw/:all/; my $test_machine = Test::Collection->new( name => 'calllists', @@ -23,7 +24,7 @@ SKIP: if ($sub1->{total_count} < 1) { skip("Precondition not met: need a subscriber",1); } - ($sub1_id) = $sub1->{_embedded}->{'ngcp:subscribers'}->[0]->{_links}->{self}{href} =~ m!subscribers/([0-9]*)$!; + $sub1_id = $test_machine->get_id_from_hal($sub1,'subscribers'); } cmp_ok ($sub1_id, '>', 0, "should be positive integer"); #---- @@ -71,7 +72,7 @@ SKIP: if ($cust1->{total_count} < 1) { skip("Precondition not met: need a customer",1); } - ($cust1_id) = $cust1->{_embedded}->{'ngcp:customers'}->[0]->{_links}{self}{href} =~ m!customers/([0-9]*)$!; + $cust1_id = $test_machine->get_id_from_hal($cust1,'customers');; } cmp_ok ($cust1_id, '>', 0, "should be positive integer"); @@ -95,9 +96,9 @@ SKIP: if ($sub1->{total_count} < 1) { skip("Precondition not met: need a subscriber",1); } - ($sub1_id) = $sub1->{_embedded}->{'ngcp:subscribers'}->[0]->{_links}{self}{href} =~ m!subscribers/([0-9]*)$!; - #$sub1_user = $sub1->{_embedded}->{'ngcp:subscribers'}->{'username'}; - #$sub1_pass = $sub1->{_embedded}->{'ngcp:subscribers'}->{'webpassword'} // ''; + $sub1_id = $test_machine->get_id_from_hal($sub1,'subscribers'); + #$sub1_user = $test_machine->get_embedded_item($sub1,'subscribers')->{'username'}; + #$sub1_pass = $test_machine->get_embedded_item($sub1,'subscribers')->{'webpassword'} // ''; cmp_ok ($sub1_id, '>', 0, "should be positive integer"); diff --git a/t/api-rest/api-calls.t b/t/api-rest/api-calls.t index b39d17797a..0ca03065a4 100644 --- a/t/api-rest/api-calls.t +++ b/t/api-rest/api-calls.t @@ -18,7 +18,7 @@ SKIP: if ($sub1->{total_count} < 1) { skip("Precondition not met: need a subscriber",1); } - ($sub1_id) = $sub1->{_embedded}->{'ngcp:subscribers'}->[0]->{_links}{self}{href} =~ m!subscribers/([0-9]*)$!; + $sub1_id = $test_machine->get_id_from_hal($sub1,'subscribers'); cmp_ok ($sub1_id, '>', 0, "should be positive integer"); diff --git a/t/api-rest/api-cfdestinationsets.t b/t/api-rest/api-cfdestinationsets.t index 756da5aabd..1f910ac9a7 100644 --- a/t/api-rest/api-cfdestinationsets.t +++ b/t/api-rest/api-cfdestinationsets.t @@ -74,7 +74,7 @@ SKIP: if ($sub1->{total_count} < 1) { skip("Precondition not met: need a subscriber",1); } - my ($sub1_id) = $sub1->{_embedded}->{'ngcp:subscribers'}->[0]->{_links}{self}{href} =~ m!subscribers/([0-9]*)$!; + my $sub1_id = $test_machine->get_id_from_hal($sub1,'subscribers'); cmp_ok ($sub1_id, '>', 0, "should be positive integer"); diff --git a/t/api-rest/api-contracts.t b/t/api-rest/api-contracts.t index b711502c76..37b5dbfae3 100644 --- a/t/api-rest/api-contracts.t +++ b/t/api-rest/api-contracts.t @@ -4,6 +4,7 @@ use Sipwise::Base; use Net::Domain qw(hostfqdn); use JSON qw(); use Test::More; +use Test::ForceArray qw/:all/; use DateTime qw(); use DateTime::Format::Strptime qw(); @@ -206,12 +207,13 @@ my @allcontracts = (); # remove any contact we find in the collection for later check if(ref $collection->{_links}->{'ngcp:contracts'} eq "HASH") { + my $item = get_embedded_item($collection,'contracts'); # TODO: handle hashref - ok($collection->{_embedded}->{'ngcp:contracts'}->[0]->{status} ne "terminated", "check if we don't have terminated contracts in response"); - ok($collection->{_embedded}->{'ngcp:contracts'}->[0]->{type} eq "sippeering" || $collection->{_embedded}->{'ngcp:contracts'}->[0]->{type} eq "reseller", "check for correct system contract type"); - ok(exists $collection->{_embedded}->{'ngcp:contracts'}->[0]->{_links}->{'ngcp:systemcontacts'}, "check presence of ngcp:systemcontacts relation"); - ok(exists $collection->{_embedded}->{'ngcp:contracts'}->[0]->{_links}->{'ngcp:billingprofiles'}, "check presence of ngcp:billingprofiles relation"); delete $contracts{$collection->{_links}->{'ngcp:contracts'}->{href}}; + ok($item->{status} ne "terminated", "check if we don't have terminated contracts in response"); + ok($item->{type} eq "sippeering" || $item->{type} eq "reseller", "check for correct system contract type"); + ok(exists $item->{_links}->{'ngcp:systemcontacts'}, "check presence of ngcp:systemcontacts relation"); + ok(exists $item->{_links}->{'ngcp:billingprofiles'}, "check presence of ngcp:billingprofiles relation"); } else { foreach my $c(@{ $collection->{_links}->{'ngcp:contracts'} }) { delete $contracts{$c->{href}}; diff --git a/t/api-rest/api-customers.t b/t/api-rest/api-customers.t index 2316f24e0c..14712c3873 100644 --- a/t/api-rest/api-customers.t +++ b/t/api-rest/api-customers.t @@ -6,6 +6,7 @@ use warnings; use Net::Domain qw(hostfqdn); use JSON qw(); use Test::More; +use Test::ForceArray qw/:all/; use DateTime qw(); use DateTime::Format::Strptime qw(); @@ -54,7 +55,7 @@ $req = HTTP::Request->new('GET', $uri.'/api/systemcontacts/?page=1&rows=1'); $res = $ua->request($req); is($res->code, 200, "fetch system contacts"); my $sysct = JSON::from_json($res->decoded_content); -my $system_contact_id = $sysct->{_embedded}->{'ngcp:systemcontacts'}->[0]->{id}; +my $system_contact_id = get_embedded_item($sysct, 'systemcontacts')->{id}; # first, create a contact $req = HTTP::Request->new('POST', $uri.'/api/customercontacts/'); @@ -225,11 +226,12 @@ my @allcustomers = (); # remove any contact we find in the collection for later check if(ref $collection->{_links}->{'ngcp:customers'} eq "HASH") { - ok($collection->{_embedded}->{'ngcp:customers'}->[0]->{type} eq "sipaccount" || $collection->{_embedded}->{'ngcp:customers'}->[0]->{type} eq "pbxaccount", "check for correct customer contract type"); - ok($collection->{_embedded}->{'ngcp:customers'}->[0]->{status} ne "terminated", "check if we don't have terminated customers in response"); - ok(exists $collection->{_embedded}->{'ngcp:customers'}->[0]->{_links}->{'ngcp:customercontacts'}, "check presence of ngcp:customercontacts relation"); - ok(exists $collection->{_embedded}->{'ngcp:customers'}->[0]->{_links}->{'ngcp:billingprofiles'}, "check presence of ngcp:billingprofiles relation"); - ok(exists $collection->{_embedded}->{'ngcp:customers'}->[0]->{_links}->{'ngcp:customerbalances'}, "check presence of ngcp:customerbalances relation"); + my $item = get_embedded_item($collection,'customers'); + ok( $item->{type} eq "sipaccount" || $item->{type} eq "pbxaccount", "check for correct customer contract type"); + ok($item->{status} ne "terminated", "check if we don't have terminated customers in response"); + ok(exists $item->{_links}->{'ngcp:customercontacts'}, "check presence of ngcp:customercontacts relation"); + ok(exists $item->{_links}->{'ngcp:billingprofiles'}, "check presence of ngcp:billingprofiles relation"); + ok(exists $item->{_links}->{'ngcp:customerbalances'}, "check presence of ngcp:customerbalances relation"); delete $customers{$collection->{_links}->{'ngcp:customers'}->{href}}; } else { foreach my $c(@{ $collection->{_links}->{'ngcp:customers'} }) { diff --git a/t/api-rest/api-rewriterulesets.t b/t/api-rest/api-rewriterulesets.t index 26f335d579..0c872ae754 100644 --- a/t/api-rest/api-rewriterulesets.t +++ b/t/api-rest/api-rewriterulesets.t @@ -4,6 +4,7 @@ use strict; use Net::Domain qw(hostfqdn); use JSON qw(); use Test::More; +use Test::ForceArray qw/:all/; my $uri = $ENV{CATALYST_SERVER} || ('https://'.hostfqdn.':4443'); @@ -228,8 +229,9 @@ my @allrules = (); # remove any entry we find in the collection for later check if(ref $collection->{_links}->{'ngcp:rewriterules'} eq "HASH") { - ok(exists $collection->{_embedded}->{'ngcp:rewriterules'}->{_links}->{'ngcp:rewriterules'}, "check presence of ngcp:rewriterules relation"); - ok(exists $collection->{_embedded}->{'ngcp:rewriterules'}->{_links}->{'ngcp:rewriterulesets'}, "check presence of ngcp:rewriterulesets relation"); + my $item = get_embedded_item($collection,'rewriterules'); + ok(exists $item->{_links}->{'ngcp:rewriterules'}, "check presence of ngcp:rewriterules relation"); + ok(exists $item->{_links}->{'ngcp:rewriterulesets'}, "check presence of ngcp:rewriterulesets relation"); delete $rules{$collection->{_links}->{'ngcp:rewriterules'}->{href}}; } else { foreach my $c(@{ $collection->{_links}->{'ngcp:rewriterules'} }) { diff --git a/t/lib/Test/Collection.pm b/t/lib/Test/Collection.pm index aa416c4498..805299d3d7 100644 --- a/t/lib/Test/Collection.pm +++ b/t/lib/Test/Collection.pm @@ -1308,12 +1308,29 @@ sub resource_clear_file{ print "cmd=$cmd;\n"; `$cmd`; } + sub get_id_from_hal{ my($self,$hal,$name) = @_; - $name //= $self->name; - (my ($id)) = $hal->{_embedded}->{'ngcp:'.$name}->{_links}{self}{href} =~ m!${name}/([0-9]*)$!; + my $embedded = $self->get_embedded_item($hal,$name); + (my ($id)) = $embedded->{_links}{self}{href}=~ m!${name}/([0-9]*)$! if $embedded; return $id; } + +sub get_embedded_item{ + my($self,$hal,$name) = @_; + $name //= $self->name; + my $embedded = $hal->{_embedded}->{'ngcp:'.$name} ; + return 'ARRAY' eq ref $embedded ? $embedded->[0] : $embedded ; +} + +sub get_embedded_forcearray{ + my($self,$hal,$name) = @_; + $name //= $self->name; + my $embedded = $hal->{_embedded}->{'ngcp:'.$name} ; + return 'ARRAY' eq ref $embedded ? $embedded : [ $embedded ]; +} + + sub uri2location{ my($self,$uri) = @_; $uri=~s/^.*?(\/api\/.*$)/$1/; diff --git a/t/lib/Test/ForceArray.pm b/t/lib/Test/ForceArray.pm new file mode 100644 index 0000000000..aefcf2f7f1 --- /dev/null +++ b/t/lib/Test/ForceArray.pm @@ -0,0 +1,32 @@ +package Test::ForceArray; + +use strict; +use Exporter qw'import'; + +our @EXPORT = qw//; +our @EXPORT_OK = qw/&get_embedded_item &get_embedded_forcearray &get_id_from_hal/; +our %EXPORT_TAGS = ( + DEFAULT => [qw/&get_embedded_item &get_embedded_forcearray &get_id_from_hal/], + all => [qw/&get_embedded_item &get_embedded_forcearray &get_id_from_hal/] +); + +sub get_embedded_item{ + my($hal,$name) = @_; + my $embedded = $hal->{_embedded}->{'ngcp:'.$name} ; + return 'ARRAY' eq ref $embedded ? $embedded->[0] : $embedded ; +} + +sub get_embedded_forcearray{ + my($hal,$name) = @_; + my $embedded = $hal->{_embedded}->{'ngcp:'.$name} ; + return 'ARRAY' eq ref $embedded ? $embedded : [ $embedded ]; +} + +sub get_id_from_hal{ + my($hal,$name) = @_; + my $embedded = get_embedded_item($hal,$name); + (my ($id)) = $embedded->{_links}{self}{href} =~ m!${name}/([0-9]*)$! if $embedded; + return $id; +} + +1; \ No newline at end of file