3 # Copyright 2016 Koha-Suomi
5 # This file is part of Koha.
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
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.
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.
22 use Test::More tests => 1;
25 use Module::Load::Conditional;
26 use JSON::Validator::OpenAPI;
31 my $swaggerPath = C4::Context->config('intranetdir') . "/api/v1/swagger";
32 my $swagger = JSON::Validator::OpenAPI->new->load_and_validate_schema(
33 $swaggerPath . "/swagger.json",
35 allow_invalid_ref => 1
38 my $api_spec = $swagger->schema->data;
39 my $schema = Koha::Database->new->schema;
41 # The basic idea of this test:
42 # 1. Find all definitions in Swagger under api/v1/definitions
43 # 2. Iterating over each definition, check 'type' of the definition
44 # * If type is not 'object', definition is ok. We only want objects.
45 # * If type is an 'object', attempt to load the corresponding Koha-object.
46 # -> If corresponding Koha-object is not found, definition is ok.
47 # -> If corresponding Koha-object is found and loaded, compare its
48 # columns to properties of the object defined in Swagger.
49 # --> If columns match properties, definition is ok.
50 # --> If columns do not match properties, definition is not ok.
51 my @definition_names = keys %{ $api_spec->{definitions} };
53 subtest 'api/v1/definitions/*.json up-to-date with corresponding Koha-object' => sub {
54 plan tests => 2*(scalar(@definition_names) - 1);
56 foreach my $name (@definition_names) {
57 my $definition = $api_spec->{definitions}->{$name};
59 if ($definition->{type} eq "object") {
60 my $kohaObject = _koha_object($name);
62 unless ($kohaObject && $kohaObject->can("_columns")) {
63 ok(1, "$name is an object, but not a Koha-object!");
67 my $columns_info = $schema->resultset( $kohaObject->_type )->result_source->columns_info;
68 my $properties = $definition->{properties};
69 my @missing = check_columns_exist($properties, $columns_info);
71 fail( "Columns are missing for $name: " . join(", ", @missing ) );
73 pass( "No columns are missing for $name" );
75 my @nullable= check_is_nullable($properties, $columns_info);
77 fail( "Columns is nullable in DB, not in swagger file for $name: " . join(", ", @nullable ) );
79 pass( "No null are missing for $name" );
82 ok(1, "$name type is not an object. It is ".$definition->{type}.".");
90 $name = "Koha::" . ucfirst $name;
92 if (Module::Load::Conditional::can_load(modules => {$name => undef})) {
93 return bless {}, $name ;
97 sub check_columns_exist {
98 my ($properties, $columns_info) = @_;
100 for my $column_name ( keys %$columns_info ) {
101 my $c_info = $columns_info->{$column_name};
102 unless ( exists $properties->{$column_name} ) {
103 push @missing_column, $column_name;
107 return @missing_column;
110 sub check_is_nullable {
111 my ($properties, $columns_info) = @_;
112 my @missing_nullable;
113 for my $column_name ( keys %$columns_info ) {
114 my $c_info = $columns_info->{$column_name};
115 if ( $c_info->{is_nullable} or $c_info->{datetime_undef_if_invalid} ) {
116 my $type = $properties->{$column_name}{type};
117 next unless $type; # FIXME Is it ok not to have type defined?
118 unless ( ref($type) ) {
119 push @missing_nullable, $column_name;
122 my $null_exists = grep {/^null$/} @$type;
123 unless ( $null_exists ) {
124 push @missing_nullable, $column_name;
129 return @missing_nullable;