Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
3 |
# Copyright 2016 Koha-Suomi |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 3 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Test::More tests => 1; |
23 |
use Test::Mojo; |
24 |
|
25 |
use Module::Load::Conditional; |
26 |
use Swagger2; |
27 |
|
28 |
use C4::Context; |
29 |
|
30 |
my $swaggerPath = C4::Context->config('intranetdir')."/api/v1"; |
31 |
my $swagger = Swagger2->new($swaggerPath."/swagger.json")->expand; |
32 |
my $api_spec = $swagger->api_spec->data; |
33 |
|
34 |
# The basic idea of this test: |
35 |
# 1. Find all definitions in Swagger under api/v1/definitions |
36 |
# 2. Iterating over each definition, check 'type' of the definition |
37 |
# * If type is not 'object', definition is ok. We only want objects. |
38 |
# * If type is an 'object', attempt to load the corresponding Koha-object. |
39 |
# -> If corresponding Koha-object is not found, definition is ok. |
40 |
# -> If corresponding Koha-object is found and loaded, compare its |
41 |
# columns to properties of the object defined in Swagger. |
42 |
# --> If columns match properties, definition is ok. |
43 |
# --> If columns do not match properties, definition is not ok. |
44 |
my @definition_names = keys $api_spec->{definitions}; |
45 |
|
46 |
subtest 'api/v1/definitions/*.json up-to-date with corresponding Koha-object' => sub { |
47 |
plan tests => scalar(@definition_names); |
48 |
|
49 |
foreach my $name (@definition_names) { |
50 |
my $definition = $api_spec->{definitions}->{$name}; |
51 |
|
52 |
if ($definition->{type} eq "object") { |
53 |
my $kohaObject = _koha_object($name); |
54 |
|
55 |
unless ($kohaObject && $kohaObject->can("_columns")) { |
56 |
ok(1, "$name is an object, but not a Koha-object!"); |
57 |
next; |
58 |
} |
59 |
|
60 |
my $columns = $kohaObject->_columns; |
61 |
my $properties = $definition->{properties}; |
62 |
is(_is_up_to_date($properties, $columns), "", |
63 |
"$name matches ".ref($kohaObject)." and is not missing any properties."); |
64 |
} else { |
65 |
ok(1, "$name type is not an object. It is ".$definition->{type}."."); |
66 |
} |
67 |
} |
68 |
}; |
69 |
|
70 |
sub _koha_object { |
71 |
my ($name) = @_; |
72 |
|
73 |
$name = "Koha::" . ucfirst $name; |
74 |
|
75 |
if (Module::Load::Conditional::can_load(modules => {$name => undef})) { |
76 |
return bless {}, $name ; |
77 |
} |
78 |
} |
79 |
|
80 |
sub _is_up_to_date { |
81 |
my ($properties, $columns) = @_; |
82 |
|
83 |
my @missing; |
84 |
foreach my $column (@$columns) { |
85 |
push @missing, $column unless $properties->{$column}; |
86 |
} |
87 |
return join(', ' , @missing); |
88 |
} |