File Coverage

blib/lib/Paws/Net/FileMockCaller.pm
Criterion Covered Total %
statement 28 31 90.3
branch 1 2 50.0
condition n/a
subroutine 8 8 100.0
pod 0 2 0.0
total 37 43 86.0


line stmt bran cond sub pod time code
1             package Paws::Net::FileMockCaller;
2 4     4   7064 use Moose;
  4         9  
  4         26  
3              
4             with 'Paws::Net::RetryCallerRole', 'Paws::Net::CallerRole';
5              
6 4     4   22885 use File::Slurper qw(read_text write_text);
  4         9535  
  4         182  
7 4     4   20 use JSON::MaybeXS;
  4         7  
  4         163  
8 4     4   21 use Moose::Util::TypeConstraints;
  4         7  
  4         30  
9 4     4   8231 use Path::Tiny;
  4         7972  
  4         1753  
10              
11             has file => (is => 'rw', isa => 'Str', trigger => \&_set_file);
12              
13             has real_caller => (
14             is => 'ro',
15             does => 'Paws::Net::CallerRole',
16             default => sub {
17             require Paws::Net::Caller;
18             Paws::Net::Caller->new;
19             }
20             );
21              
22             sub _set_file {
23 90     90   30797 my $self = shift;
24 90         2621 $self->_clear_file_contents;
25 90         2427 $self->_clear_method;
26 90         2367 $self->_clear_service;
27 90         2290 $self->_clear_params;
28             }
29              
30             has file_contents => (
31             is => 'ro',
32             isa => 'HashRef',
33             lazy => 1,
34             clearer => '_clear_file_contents',
35             default => sub {
36             my $self = shift;
37             my $content = read_text($self->file);
38             my $hash = $self->_encoder->decode($content);
39             return $hash;
40             }
41             );
42              
43             has service => (
44             is => 'ro',
45             isa => 'Str',
46             clearer => '_clear_service',
47             lazy => 1,
48             default => sub { shift->file_contents->{ request }->{ service } }
49             );
50             has method => (
51             is => 'ro',
52             isa => 'Str',
53             clearer => '_clear_method',
54             lazy => 1,
55             default => sub { shift->file_contents->{ request }->{ call } }
56             );
57             has params => (
58             is => 'ro',
59             isa => 'HashRef',
60             clearer => '_clear_params',
61             lazy => 1,
62             default => sub { shift->file_contents->{ request }->{ params } }
63             );
64              
65             has _encoder => (is => 'ro', default => sub { JSON::MaybeXS->new(canonical => 1) });
66              
67             sub send_request {
68 90     90 0 234 my ($self, $service, $call_object) = @_;
69              
70 90         2154 my $actual_call = $self->_encoder->encode($service->to_hash($call_object));
71 90         2223 my $recorded_call = $self->_encoder->encode($self->params);
72              
73 90 50       306 if ($actual_call ne $recorded_call) {
74 0         0 warn "CALL: $actual_call";
75 0         0 warn "RECORDED: $recorded_call";
76              
77 0         0 Paws::Exception->throw(
78             request_id => '',
79             code => 'ReplayInvalid',
80             message => 'The calling parameters and the parameters used to generate the call are not equal'
81             )
82             }
83            
84 90         2144 my $response = $self->file_contents;
85 90         646 return ($response->{response}{status}, $response->{response}{content}, $response->{response}{headers});
86             };
87              
88             sub caller_to_response {
89 29     29 0 95 my ($self, $service, $call_object, $status, $content, $headers) = @_;
90            
91 29         672 return $self->real_caller->caller_to_response($service, $call_object, $status, $content, $headers);
92             };
93              
94             1;