1# --
2# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
3# --
4# This software comes with ABSOLUTELY NO WARRANTY. For details, see
5# the enclosed file COPYING for license information (GPL). If you
6# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
7# --
8
9use strict;
10use warnings;
11use utf8;
12
13use vars (qw($Self));
14
15use Kernel::GenericInterface::Debugger;
16use Kernel::GenericInterface::Invoker;
17
18# create a Debugger instance
19my $DebuggerObject = Kernel::GenericInterface::Debugger->new(
20    DebuggerConfig => {
21        DebugThreshold => 'debug',
22        TestMode       => 1,
23    },
24    WebserviceID      => 1,
25    CommunicationType => 'Requester',
26);
27
28# create an Invoker instance
29my $InvokerObject = Kernel::GenericInterface::Invoker->new(
30    DebuggerObject => $DebuggerObject,
31    Invoker        => 1,
32    InvokerType    => 'Test::Test',
33    WebserviceID   => 1,
34);
35$Self->Is(
36    ref $InvokerObject,
37    'Kernel::GenericInterface::Invoker',
38    'InvokerObject was correctly instantiated',
39);
40
41# PrepareRequest Tests
42my @InvokerPrepareRequestTests = (
43
44    {
45        Name          => 'Test no data',
46        Data          => undef,
47        ResultData    => undef,
48        ResultSuccess => 0,
49    },
50    {
51        Name          => 'Test empty data',
52        Data          => {},
53        ResultData    => undef,
54        ResultSuccess => 0,
55    },
56    {
57        Name => 'Test wrong data',
58        Data => {
59            ASoWrongParameter => 'JustAValue',
60        },
61        ResultData    => undef,
62        ResultSuccess => 0,
63    },
64    {
65        Name => 'Test TicketNumber parameter empty value',
66        Data => {
67            TicketNumber => '',
68        },
69        ResultData    => undef,
70        ResultSuccess => 0,
71    },
72    {
73        Name => 'Test Action parameter empty value',
74        Data => {
75            Action => '',
76        },
77        ResultData    => undef,
78        ResultSuccess => 0,
79    },
80    {
81        Name => 'Test Action parameter without TicketNumber',
82        Data => {
83            Action => 'Add',
84        },
85        ResultData    => undef,
86        ResultSuccess => 0,
87    },
88    {
89        Name => 'Test correct call just TicketNumber',
90        Data => {
91            TicketNumber => '12345',
92        },
93        ResultData => {
94            TicketNumber => '12345',
95        },
96        ResultSuccess => 1,
97    },
98    {
99        Name => 'Test correct call TicketNumber and Action',
100        Data => {
101            TicketNumber => '12345',
102            Action       => 'Add',
103        },
104        ResultData => {
105            TicketNumber => '12345',
106            Action       => 'AddTest',
107        },
108        ResultSuccess => 1,
109    },
110
111);
112
113for my $Test (@InvokerPrepareRequestTests) {
114    my $InvokerResult = $InvokerObject->PrepareRequest(
115        Data => $Test->{Data},
116    );
117
118    # check if function return correct status
119    $Self->Is(
120        $InvokerResult->{Success},
121        $Test->{ResultSuccess},
122        $Test->{Name} . ' (Success).',
123    );
124
125    # check if function return correct data
126    $Self->IsDeeply(
127        $InvokerResult->{Data},
128        $Test->{ResultData},
129        $Test->{Name} . ' (Data Structure).',
130    );
131
132    if ( !$Test->{ResultSuccess} ) {
133        $Self->False(
134            $InvokerResult->{Success},
135            $Test->{Name} . ' (Error Message: ' .
136                $InvokerResult->{ErrorMessage} . ')',
137        );
138    }
139    else {
140        $Self->Is(
141            ref $InvokerObject,
142            'Kernel::GenericInterface::Invoker',
143            $Test->{Name} . ' (Not Error Message).',
144        );
145    }
146}
147
148# HandleResponse Tests
149my @InvokerHandleResponseTests = (
150
151    {
152        Name          => 'Test no data',
153        Data          => undef,
154        ResultData    => undef,
155        ResultSuccess => 0,
156    },
157    {
158        Name          => 'Test empty data',
159        Data          => {},
160        ResultData    => undef,
161        ResultSuccess => 0,
162    },
163    {
164        Name => 'Test wrong data',
165        Data => {
166            ASoWrongParameter => 'JustAValue',
167        },
168        ResultData    => undef,
169        ResultSuccess => 0,
170    },
171    {
172        Name => 'Test TicketNumber parameter empty value',
173        Data => {
174            TicketNumber => '',
175        },
176        ResultData    => undef,
177        ResultSuccess => 0,
178    },
179    {
180        Name => 'Test TicketNumber parameter wrong value',
181        Data => {
182            TicketNumber => 'asdf0987opiu',
183        },
184        ResultData    => undef,
185        ResultSuccess => 0,
186    },
187    {
188        Name => 'Test Action parameter empty value',
189        Data => {
190            Action => '',
191        },
192        ResultData    => undef,
193        ResultSuccess => 0,
194    },
195    {
196        Name => 'Test Action parameter without TicketNumber',
197        Data => {
198            Action => 'Add',
199        },
200        ResultData    => undef,
201        ResultSuccess => 0,
202    },
203    {
204        Name => 'Test correct call just TicketNumber',
205        Data => {
206            TicketNumber => '12345',
207        },
208        ResultData    => undef,
209        ResultSuccess => 0,
210    },
211    {
212        Name => 'Test correct call without ResponseSuccess',
213        Data => {
214            TicketNumber => '12345',
215            Action       => 'AddTest',
216        },
217        ResultData    => undef,
218        ResultSuccess => 0,
219    },
220    {
221        Name => 'Test just ResponseSucces param.',
222        Data => {
223            TicketNumber => '',
224        },
225        ResponseSuccess => '1',
226        ResultData      => undef,
227        ResultSuccess   => 0,
228    },
229    {
230        Name => 'Test just ResponseErrorMessage param.',
231        Data => {
232            TicketNumber => '12345',
233        },
234        ResponseErrorMessage => 'Just an error message.',
235        ResultData           => undef,
236        ResultSuccess        => 0,
237    },
238    {
239        Name => 'Test just correct params from Response',
240        Data => {
241            TicketNumber => '',
242        },
243        ResponseSuccess      => '1',
244        ResponseErrorMessage => 'Just an error message.',
245        ResultData           => undef,
246        ResultSuccess        => 0,
247    },
248    {
249        Name => 'Test correct call without Action',
250        Data => {
251            TicketNumber => '12345',
252        },
253        ResponseSuccess      => '1',
254        ResponseErrorMessage => 'Just an error message.',
255        ResultData           => {
256            TicketNumber => '12345',
257        },
258        ResultSuccess => 1,
259    },
260    {
261        Name => 'Test correct call with all params',
262        Data => {
263            TicketNumber => '12345',
264            Action       => 'AddTest',
265        },
266        ResponseSuccess      => '1',
267        ResponseErrorMessage => 'Just an error message.',
268        ResultData           => {
269            TicketNumber => '12345',
270            Action       => 'Add',
271        },
272        ResultSuccess => 1,
273    },
274
275);
276
277for my $Test (@InvokerHandleResponseTests) {
278    my %InvokerParams = {};
279    $InvokerParams{Data}                 = $Test->{Data}                 || undef;
280    $InvokerParams{ResponseSuccess}      = $Test->{ResponseSuccess}      || undef;
281    $InvokerParams{ResponseErrorMessage} = $Test->{ResponseErrorMessage} || undef;
282    my $InvokerResult = $InvokerObject->HandleResponse(
283        %InvokerParams,
284    );
285
286    # check if function return correct status
287    $Self->Is(
288        $InvokerResult->{Success},
289        $Test->{ResultSuccess},
290        $Test->{Name} . ' (Success).',
291    );
292
293    # check if function return correct data
294    $Self->IsDeeply(
295        $InvokerResult->{Data},
296        $Test->{ResultData},
297        $Test->{Name} . ' (Data Structure).',
298    );
299
300    if ( !$Test->{ResultSuccess} ) {
301        $Self->True(
302            $InvokerResult->{ErrorMessage},
303            $Test->{Name} . ' error message',
304        );
305    }
306}
307
308# complete cycle
309
310# PrepareRequest call
311my $InvokerResult = $InvokerObject->PrepareRequest(
312    Data => {
313        TicketNumber => '12345',
314        Action       => 'Add',
315    },
316);
317
318# check invoker call success
319$Self->True(
320    $InvokerResult->{Success},
321    '(Complete Cycle) Check PrepareRequest call success.',
322);
323
324# returned data should match with expected data
325$Self->IsDeeply(
326    \%{$InvokerResult},
327    {
328        'Data' => {
329            'Action'       => 'AddTest',
330            'TicketNumber' => '12345'
331        },
332        'Success' => 1
333    },
334    'Returned data should match with expected data.',
335);
336
337# HandleResponse call
338$InvokerResult = $InvokerObject->HandleResponse(
339    Data => {
340        Action       => $InvokerResult->{Data}->{Action},
341        TicketNumber => $InvokerResult->{Data}->{TicketNumber},
342    },
343    ResponseSuccess      => $InvokerResult->{Success},
344    ResponseErrorMessage => $InvokerResult->{ErrorMessage},
345);
346
347# check HandleResponse call success
348$Self->True(
349    $InvokerResult->{Success},
350    '(Complete Cycle) Check HandleResponse call success.',
351);
352
353# HandleResponse data should match the initial data
354$Self->IsDeeply(
355    \%{$InvokerResult},
356    {
357        'Data' => {
358            'Action'       => 'Add',
359            'TicketNumber' => '12345'
360        },
361        'Success' => 1
362    },
363    'Returned data should match with expected data.',
364);
365
3661;
367