Bug 17032: Make sure Swagger object definition is up-to-date with Koha-object
authorLari Taskula <larit@student.uef.fi>
Wed, 3 Aug 2016 11:55:08 +0000 (14:55 +0300)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 9 Sep 2016 12:57:52 +0000 (12:57 +0000)
If a definition in api/v1/definitions/*.json has a corresponding Koha-object by
filename, compare properties of the definition and columns of the Koha-object
and ensure that all columns are defined inside properties of the definition.

To test:
1. Run t/db_dependent/api/v1/swagger/definitions.t
2. See that all of the tests pass. (If not, file a bug report to fix missing
   properties in the Swagger definition.)
3. Remove a property from api/v1/swagger/definitions/patron.json
4. Repeat step 1
5. See that test fails and lets you know that the property you just removed
   is missing from the definition.

Signed-off-by: Benjamin Rokseth <benjamin.rokseth@kul.oslo.kommune.no>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
t/db_dependent/api/v1/swagger/definitions.t [new file with mode: 0644]

diff --git a/t/db_dependent/api/v1/swagger/definitions.t b/t/db_dependent/api/v1/swagger/definitions.t
new file mode 100644 (file)
index 0000000..fa53146
--- /dev/null
@@ -0,0 +1,88 @@
+#!/usr/bin/env perl
+
+# Copyright 2016 Koha-Suomi
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Test::More tests => 1;
+use Test::Mojo;
+
+use Module::Load::Conditional;
+use Swagger2;
+
+use C4::Context;
+
+my $swaggerPath = C4::Context->config('intranetdir')."/api/v1";
+my $swagger = Swagger2->new($swaggerPath."/swagger.json")->expand;
+my $api_spec = $swagger->api_spec->data;
+
+# The basic idea of this test:
+# 1. Find all definitions in Swagger under api/v1/definitions
+# 2. Iterating over each definition, check 'type' of the definition
+#    * If type is not 'object', definition is ok. We only want objects.
+#    * If type is an 'object', attempt to load the corresponding Koha-object.
+#        -> If corresponding Koha-object is not found, definition is ok.
+#        -> If corresponding Koha-object is found and loaded, compare its
+#           columns to properties of the object defined in Swagger.
+#             --> If columns match properties, definition is ok.
+#             --> If columns do not match properties, definition is not ok.
+my @definition_names = keys $api_spec->{definitions};
+
+subtest 'api/v1/definitions/*.json up-to-date with corresponding Koha-object' => sub {
+    plan tests => scalar(@definition_names);
+
+    foreach my $name (@definition_names) {
+        my $definition = $api_spec->{definitions}->{$name};
+
+        if ($definition->{type} eq "object") {
+            my $kohaObject = _koha_object($name);
+
+            unless ($kohaObject && $kohaObject->can("_columns")) {
+                ok(1, "$name is an object, but not a Koha-object!");
+                next;
+            }
+
+            my $columns = $kohaObject->_columns;
+            my $properties = $definition->{properties};
+            is(_is_up_to_date($properties, $columns), "",
+               "$name matches ".ref($kohaObject)." and is not missing any properties.");
+        } else {
+            ok(1, "$name type is not an object. It is ".$definition->{type}.".");
+        }
+    }
+};
+
+sub _koha_object {
+    my ($name) = @_;
+
+    $name = "Koha::" . ucfirst $name;
+
+    if (Module::Load::Conditional::can_load(modules => {$name => undef})) {
+        return bless {}, $name ;
+    }
+}
+
+sub _is_up_to_date {
+    my ($properties, $columns) = @_;
+
+    my @missing;
+    foreach my $column (@$columns) {
+        push @missing, $column unless $properties->{$column};
+    }
+    return join(', ' , @missing);
+}