Revert "Bug 20073: Move Elasticsearch configs to yaml files and improve the default...
[koha-equinox.git] / t / Koha / SearchEngine / Elasticsearch.t
1 #!/usr/bin/perl
2 #
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 1;
21 use Test::Exception;
22
23 use t::lib::Mocks;
24
25 use Koha::SearchEngine::Elasticsearch;
26
27 subtest '_read_configuration() tests' => sub {
28
29     plan tests => 10;
30
31     my $configuration;
32     t::lib::Mocks::mock_config( 'elasticsearch', undef );
33
34     # 'elasticsearch' missing in configuration
35     throws_ok {
36         $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
37     }
38     'Koha::Exceptions::Config::MissingEntry',
39       'Configuration problem, exception thrown';
40     is(
41         $@->message,
42         "Missing 'elasticsearch' block in config file",
43         'Exception message is correct'
44     );
45
46     # 'elasticsearch' present but no 'server' entry
47     t::lib::Mocks::mock_config( 'elasticsearch', {} );
48     throws_ok {
49         $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
50     }
51     'Koha::Exceptions::Config::MissingEntry',
52       'Configuration problem, exception thrown';
53     is(
54         $@->message,
55         "Missing 'server' entry in config file for elasticsearch",
56         'Exception message is correct'
57     );
58
59     # 'elasticsearch' and 'server' entries present, but no 'index_name'
60     t::lib::Mocks::mock_config( 'elasticsearch', { server => 'a_server' } );
61     throws_ok {
62         $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
63     }
64     'Koha::Exceptions::Config::MissingEntry',
65       'Configuration problem, exception thrown';
66     is(
67         $@->message,
68         "Missing 'index_name' entry in config file for elasticsearch",
69         'Exception message is correct'
70     );
71
72     # Correct configuration, only one server
73     t::lib::Mocks::mock_config( 'elasticsearch',  { server => 'a_server', index_name => 'index' } );
74
75     $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
76     is( $configuration->{index_name}, 'index', 'Index configuration parsed correctly' );
77     is_deeply( $configuration->{nodes}, ['a_server'], 'Server configuration parsed correctly' );
78
79     # Correct configuration, two servers
80     my @servers = ('a_server', 'another_server');
81     t::lib::Mocks::mock_config( 'elasticsearch', { server => \@servers, index_name => 'index' } );
82
83     $configuration = Koha::SearchEngine::Elasticsearch::_read_configuration;
84     is( $configuration->{index_name}, 'index', 'Index configuration parsed correctly' );
85     is_deeply( $configuration->{nodes}, \@servers , 'Server configuration parsed correctly' );
86 };