Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/env perl |
|
|
2 |
|
3 |
# Copyright (C) 2016 KohaSuomi |
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 File::Copy qw/copy/; |
23 |
use File::Temp; |
24 |
use File::Path qw/make_path rmtree/; |
25 |
|
26 |
use Test::More tests => 9; |
27 |
|
28 |
use C4::Context; |
29 |
use Koha::REST::V1; |
30 |
|
31 |
my $oldSwagger_title = "Swagger Sample App"; |
32 |
my $newSwagger_title = "Koha REST API"; |
33 |
|
34 |
# Construct an example swagger.json |
35 |
my $swaggerJsonTest = |
36 |
qq ({ |
37 |
"swagger": "2.0", |
38 |
"info": { |
39 |
"title": "$oldSwagger_title", |
40 |
"version": "1" |
41 |
}, |
42 |
"paths": {}, |
43 |
"definitions": {}, |
44 |
"parameters": {} |
45 |
}); |
46 |
|
47 |
my $swaggerJsonTestModified = |
48 |
qq ({ |
49 |
"swagger": "2.0", |
50 |
"info": { |
51 |
"title": "$newSwagger_title", |
52 |
"version": "1" |
53 |
}, |
54 |
"paths": {}, |
55 |
"definitions": {}, |
56 |
"parameters": {} |
57 |
}); |
58 |
|
59 |
# Path containing actual swagger specification files |
60 |
my $real_swaggerPath = C4::Context->config("intranetdir").'/api/v1/'; |
61 |
|
62 |
# Create a tmp directory where we can modify swagger.json |
63 |
my $swaggerPath = File::Temp->newdir()."/"; |
64 |
make_path($swaggerPath); |
65 |
|
66 |
# Copy minifySwagger.pl to this location |
67 |
copy($real_swaggerPath.'minifySwagger.pl', $swaggerPath); |
68 |
|
69 |
my $pathToMinifier = $swaggerPath.'minifySwagger.pl'; |
70 |
my $pathToSwaggerJson = $swaggerPath.'swagger.json'; |
71 |
my $pathToSwaggerMinJson = $swaggerPath.'swagger.min.json'; |
72 |
|
73 |
is(-e $pathToSwaggerJson, undef, "swagger.json does not yet exist"); |
74 |
is(-e $pathToSwaggerMinJson, undef, "swagger.min.json does not yet exist"); |
75 |
|
76 |
my $exists = -e $pathToSwaggerJson; |
77 |
# Create swagger.json test file |
78 |
open my $fh, '>', $pathToSwaggerJson; |
79 |
print $fh $swaggerJsonTest; |
80 |
close $fh; |
81 |
|
82 |
ok(-e $pathToSwaggerJson, "swagger.json created!"); |
83 |
|
84 |
Koha::REST::V1::minifySwagger(undef, $swaggerPath); |
85 |
ok(-e $pathToSwaggerMinJson, "swagger.min.json created!"); |
86 |
my $lastmodified = -C $pathToSwaggerMinJson; |
87 |
|
88 |
# Read swagger.min.json |
89 |
require Swagger2; |
90 |
my $swaggerMin = Swagger2->new($pathToSwaggerMinJson); |
91 |
$swaggerMin = $swaggerMin->expand; |
92 |
my $oldSwaggerTitle = $swaggerMin->{'api_spec'}->{'data'}->{'info'}->{'title'}; |
93 |
is($oldSwaggerTitle, $oldSwagger_title, "old title found"); |
94 |
|
95 |
# Modify swagger.json |
96 |
sleep(1); # make sure modification time differs from swagger.min.json |
97 |
open $fh, '>', $pathToSwaggerJson; |
98 |
print $fh $swaggerJsonTestModified; |
99 |
close $fh; |
100 |
|
101 |
Koha::REST::V1::minifySwagger(undef, $swaggerPath); |
102 |
|
103 |
ok($lastmodified > -C $pathToSwaggerMinJson, |
104 |
"swagger.min.json modified after editing swagger.json"); |
105 |
$lastmodified = -C $pathToSwaggerMinJson; |
106 |
|
107 |
# Re-read swagger.min.json |
108 |
$swaggerMin = Swagger2->new($pathToSwaggerMinJson); |
109 |
$swaggerMin = $swaggerMin->expand; |
110 |
my $newSwaggerTitle = $swaggerMin->{'api_spec'}->{'data'}->{'info'}->{'title'}; |
111 |
is($newSwaggerTitle, $newSwagger_title, "new title found"); # was minified |
112 |
|
113 |
Koha::REST::V1::minifySwagger(undef, $swaggerPath); |
114 |
ok($lastmodified == -C $pathToSwaggerMinJson, |
115 |
"swagger.min.json not modified because no modifications to swagger.json"); |
116 |
|
117 |
# Cleanup |
118 |
rmtree($swaggerPath) if $swaggerPath ne $real_swaggerPath; |
119 |
is(-e $swaggerPath, undef, "tmp files deleted"); |